From 6f0137dec999bb7ab5bed6d1b4782fe9f54c0b34 Mon Sep 17 00:00:00 2001 From: Arpit Jalan <arpit@techapj.com> 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 | 19 +++++++++++++++---- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 31a60e760..521073d3b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -356,6 +356,10 @@ class Post < ActiveRecord::Base 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 9c07db338..a0f212d5a 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -90,7 +90,7 @@ module PostGuardian 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 8f5ef5ae1..1b41bb7f4 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -1008,6 +1008,29 @@ describe Guardian do 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 369763284..33f9f7d4c 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -137,11 +137,12 @@ describe Post do end describe 'flagging helpers' do - it 'isFlagged is accurate' do - post = Fabricate(:post) - user = Fabricate(:coding_horror) - PostAction.act(user, post, PostActionType.types[:off_topic]) + let(:post) { Fabricate(:post) } + let(:user) { Fabricate(:coding_horror) } + let(:admin) { Fabricate(:admin) } + it 'isFlagged is accurate' do + PostAction.act(user, post, PostActionType.types[:off_topic]) post.reload expect(post.is_flagged?).to eq(true) @@ -149,6 +150,16 @@ describe Post do 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