diff --git a/backend/app/store/engine/bolt.go b/backend/app/store/engine/bolt.go index 0eb11f42..25725a0e 100644 --- a/backend/app/store/engine/bolt.go +++ b/backend/app/store/engine/bolt.go @@ -797,6 +797,14 @@ func (b *BoltDB) deleteComment(bdb *bolt.DB, locator store.Locator, commentID st if e = b.load(postBkt, commentID, &comment); e != nil { return errors.Wrapf(e, "can't load key %s from bucket %s", commentID, locator.URL) } + + if !comment.Deleted { + // decrement comments count for post url + if _, e = b.count(tx, comment.Locator.URL, -1); e != nil { + return errors.Wrapf(e, "failed to decrement count for %s", comment.Locator) + } + } + // set deleted status and clear fields comment.SetDeleted(mode) @@ -810,11 +818,6 @@ func (b *BoltDB) deleteComment(bdb *bolt.DB, locator store.Locator, commentID st return errors.Wrapf(e, "can't delete key %s from bucket %s", commentID, lastBucketName) } - // decrement comments count for post url - if _, e = b.count(tx, comment.Locator.URL, -1); e != nil { - return errors.Wrapf(e, "failed to decrement count for %s", comment.Locator) - } - return nil }) } diff --git a/backend/app/store/engine/bolt_test.go b/backend/app/store/engine/bolt_test.go index dd30303b..61461c7c 100644 --- a/backend/app/store/engine/bolt_test.go +++ b/backend/app/store/engine/bolt_test.go @@ -694,6 +694,10 @@ func TestBolt_DeleteComment(t *testing.T) { assert.True(t, res[0].Deleted, "marked deleted") assert.Equal(t, store.User{Name: "user name", ID: "user1", Picture: "", Admin: false, Blocked: false, IP: ""}, res[0].User) + // repeated deletion should not decrease comments count + err = b.Delete(delReq) + assert.NoError(t, err) + assert.Equal(t, "some text2", res[1].Text) assert.False(t, res[1].Deleted) @@ -807,10 +811,22 @@ func TestBoltAdmin_DeleteUserHard(t *testing.T) { b, teardown := prep(t) defer teardown() - err := b.Delete(DeleteRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", DeleteMode: store.HardDelete}) + comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) + assert.NoError(t, err) + + // soft delete one comment + delReq := DeleteRequest{ + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + CommentID: comments[0].ID, + DeleteMode: store.SoftDelete, + } + err = b.Delete(delReq) + assert.NoError(t, err) + + err = b.Delete(DeleteRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", DeleteMode: store.HardDelete}) require.NoError(t, err) - comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) + comments, err = b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) assert.NoError(t, err) require.Equal(t, 2, len(comments), "2 comments with deleted info") assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[0].User) @@ -836,10 +852,22 @@ func TestBoltAdmin_DeleteUserSoft(t *testing.T) { b, teardown := prep(t) defer teardown() - err := b.Delete(DeleteRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", DeleteMode: store.SoftDelete}) + comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) + assert.NoError(t, err) + + // soft delete one comment + delReq := DeleteRequest{ + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + CommentID: comments[0].ID, + DeleteMode: store.SoftDelete, + } + err = b.Delete(delReq) + assert.NoError(t, err) + + err = b.Delete(DeleteRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", DeleteMode: store.SoftDelete}) require.NoError(t, err) - comments, err := b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) + comments, err = b.Find(FindRequest{Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}, Sort: "time"}) assert.NoError(t, err) require.Equal(t, 2, len(comments), "2 comments with deleted info") assert.Equal(t, store.User{Name: "user name", ID: "user1", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[0].User)