From fbb8cdc8eb1b8925f41a268c8f1c4d8650a3e48d Mon Sep 17 00:00:00 2001 From: David Mzareulyan Date: Thu, 18 May 2017 14:24:44 +0300 Subject: [PATCH] Allow post author to destroy hidden comments --- app/controllers/api/v1/CommentsController.js | 4 +-- app/models/comment.js | 4 +++ test/functional/hidden-comments.js | 26 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/CommentsController.js b/app/controllers/api/v1/CommentsController.js index 51320ca5..c0b5c324 100644 --- a/app/controllers/api/v1/CommentsController.js +++ b/app/controllers/api/v1/CommentsController.js @@ -118,9 +118,9 @@ export default class CommentsController { throw new NotFoundException("Can't find comment") } - if (comment.hideType !== Comment.VISIBLE) { + if (!comment.canBeDestroyed()) { throw new ForbiddenException( - "You can't destroy deleted or hidden comment" + "You can't destroy deleted comment" ) } diff --git a/app/models/comment.js b/app/models/comment.js index 3094589f..14835c74 100644 --- a/app/models/comment.js +++ b/app/models/comment.js @@ -120,6 +120,10 @@ export function addModel(dbAdapter) { return dbAdapter.getPostById(this.postId) } + Comment.prototype.canBeDestroyed = function () { + return this.hideType !== Comment.DELETED; + } + Comment.prototype.destroy = async function () { await dbAdapter.deleteComment(this.id, this.postId); await pubSub.destroyComment(this.id, this.postId); diff --git a/test/functional/hidden-comments.js b/test/functional/hidden-comments.js index 136e7637..4f9ab58b 100644 --- a/test/functional/hidden-comments.js +++ b/test/functional/hidden-comments.js @@ -14,6 +14,7 @@ import { createCommentAsync, banUser, updateUserAsync, + removeCommentAsync, } from './functional_test_helper'; describe('Hidden comments', () => { @@ -118,5 +119,30 @@ describe('Hidden comments', () => { expect(reply.comments[0], 'to not have key', 'hideType'); }); }); + + describe('Delete hidden comment', () => { + beforeEach(async () => { + await banUser(mars, venus); + await updateUserAsync( + mars, + { frontendPreferences: { 'net.freefeed': { comments: { hiddenTypes: [] } } } }, + ); + }); + + it('Mars should be able to delete hidden Venus comment', async () => { + const reply1 = await fetchPost(post.id, mars); + expect(reply1.comments, 'to have length', 2); + const venusComment = reply1.comments.find((c) => c.id === reply1.posts.comments[0]); + expect(venusComment, 'to satisfy', { hideType: Comment.HIDDEN_BANNED }); + + const delReply = await removeCommentAsync(mars, venusComment.id) + delReply.status.should.eql(200) + + const reply2 = await fetchPost(post.id, mars); + expect(reply2.comments, 'to have length', 1); + const lunaComment = reply2.comments.find((c) => c.id === reply2.posts.comments[0]); + expect(lunaComment, 'to satisfy', { hideType: Comment.VISIBLE }); + }); + }); }); });