FEATURE: disable post editing when the post has active flag

This commit is contained in:
Arpit Jalan 2016-03-30 22:57:34 +05:30
parent 79329c8e85
commit 6f0137dec9
4 changed files with 43 additions and 5 deletions

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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