From 6f0137dec999bb7ab5bed6d1b4782fe9f54c0b34 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 30 Mar 2016 22:57:34 +0530 Subject: [PATCH] FEATURE: disable post editing when the post has active flag --- app/models/post.rb | 4 ++++ lib/guardian/post_guardian.rb | 2 +- spec/components/guardian_spec.rb | 23 +++++++++++++++++++++++ spec/models/post_spec.rb | 17 ++++++++++++++--- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 31a60e760a5af..521073d3b4404 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -356,6 +356,10 @@ def is_flagged? post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0 end + def has_active_flag? + post_actions.active.where(post_action_type_id: PostActionType.flag_types.values).count != 0 + end + def unhide! self.update_attributes(hidden: false) self.topic.update_attributes(visible: true) if is_first_post? diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb index 9c07db3380d3e..a0f212d5ae09a 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -90,7 +90,7 @@ def can_edit_post?(post) return true end - if post.topic.archived? || post.user_deleted || post.deleted_at + if post.topic.archived? || post.user_deleted || post.deleted_at || post.has_active_flag? return false end diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index 8f5ef5ae10e6a..1b41bb7f4e692 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -1008,6 +1008,29 @@ expect(Guardian.new(admin).can_edit?(tos_first_post)).to be_truthy end end + + context "flagged post" do + let(:user) { Fabricate(:user) } + let(:post) { Fabricate(:post) } + before { PostAction.act(user, post, PostActionType.types[:off_topic]) } + + it 'returns false when post owner tries to edit active flagged post' do + expect(Guardian.new(post.user).can_edit?(post)).to be_falsey + end + + it 'returns true when trust level 4 user tries to edit active flagged post' do + expect(Guardian.new(trust_level_4).can_edit?(post)).to be_truthy + end + + it 'returns true when staff tries to edit active flagged post' do + expect(Guardian.new(moderator).can_edit?(post)).to be_truthy + end + + it 'returns true when post owner tries to edit post with inactive flag' do + PostAction.defer_flags!(post, admin) + expect(Guardian.new(post.user).can_edit?(post)).to be_truthy + end + end end describe 'a Topic' do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 369763284edfc..33f9f7d4c23a1 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -137,11 +137,12 @@ def post_with_body(body, user=nil) end describe 'flagging helpers' do + let(:post) { Fabricate(:post) } + let(:user) { Fabricate(:coding_horror) } + let(:admin) { Fabricate(:admin) } + it 'isFlagged is accurate' do - post = Fabricate(:post) - user = Fabricate(:coding_horror) PostAction.act(user, post, PostActionType.types[:off_topic]) - post.reload expect(post.is_flagged?).to eq(true) @@ -149,6 +150,16 @@ def post_with_body(body, user=nil) post.reload expect(post.is_flagged?).to eq(false) end + + it 'has_active_flag is accurate' do + PostAction.act(user, post, PostActionType.types[:spam]) + post.reload + expect(post.has_active_flag?).to eq(true) + + PostAction.defer_flags!(post, admin) + post.reload + expect(post.has_active_flag?).to eq(false) + end end describe "maximum images" do