diff --git a/README.md b/README.md index 0af28c85..483e8647 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ type Comment struct { Votes map[string]bool `json:"votes"` // comment votes, read only Timestamp time.Time `json:"time"` // time stamp, read only Pin bool `json:"pin"` // pinned status, read only + Delete bool `json:"delete"` // delete status, read only } type Locator struct { diff --git a/app/store/bolt.go b/app/store/bolt.go index 597d989d..71181361 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -130,14 +130,31 @@ func (b *BoltDB) Delete(locator Locator, commentID string) error { } return bdb.Update(func(tx *bolt.Tx) error { - // delete from post bucket + + // mark deleted from post bucket, but don't remove. Needed to keep tree bucket := tx.Bucket([]byte(locator.URL)) if bucket == nil { return errors.Errorf("no bucket %s in store", locator.URL) } - if err := bucket.Delete([]byte(commentID)); err != nil { - return errors.Wrapf(err, "can't delete key %s from bucket %s", commentID, locator.URL) + + comment, err := b.load(bucket, []byte(commentID)) + if err != nil { + return errors.Wrapf(err, "can't load key %s from bucket %s", commentID, locator.URL) } + // set deleted status and clear fields + comment.Deleted = true + comment.Text = "this comment was deleted" + comment.Score = 0 + comment.Votes = map[string]bool{} + comment.Edit = nil + + if err := b.save(bucket, []byte(commentID), comment); err != nil { + return errors.Wrapf(err, "can't save deleted comment for key %s from bucket %s", commentID, locator.URL) + } + + // if err := bucket.Delete([]byte(commentID)); err != nil { + // return errors.Wrapf(err, "can't delete key %s from bucket %s", commentID, locator.URL) + // } // delete from "last" bucket bucket = tx.Bucket([]byte(lastBucketName)) diff --git a/app/store/bolt_test.go b/app/store/bolt_test.go index 94b107e0..d08d08d5 100644 --- a/app/store/bolt_test.go +++ b/app/store/bolt_test.go @@ -40,12 +40,17 @@ func TestBoltDB_Delete(t *testing.T) { res, err = b.Find(loc, "time") assert.Nil(t, err) - assert.Equal(t, 1, len(res)) - assert.Equal(t, "some text2", res[0].Text) + assert.Equal(t, 2, len(res)) + assert.Equal(t, "this comment was deleted", res[0].Text) + assert.True(t, res[0].Deleted, "marked deleted") + assert.Equal(t, "some text2", res[1].Text) + assert.False(t, res[1].Deleted) comments, err := b.Last("radio-t", 10) assert.Nil(t, err) - assert.Equal(t, 1, len(comments), "only 1 left in last") + assert.Equal(t, 2, len(comments), "2 in last, nothing removed") + assert.Equal(t, "this comment was deleted", comments[1].Text) + assert.True(t, comments[1].Deleted, "marked deleted") } func TestBoltDB_Get(t *testing.T) { diff --git a/app/store/store.go b/app/store/store.go index 32b5ab42..d58adff3 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -27,6 +27,7 @@ type Comment struct { Timestamp time.Time `json:"time"` Pin bool `json:"pin,omitempty"` Edit *Edit `json:"edit,omitempty"` + Deleted bool `json:"delete,omitempty"` } // Locator keeps site and url of the post