Skip to content

Commit

Permalink
FEATURE: disable post editing when the post has active flag
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitjalan committed Mar 30, 2016
1 parent 79329c8 commit 6f0137d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/post_guardian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions spec/components/guardian_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,29 @@ 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)

PostAction.remove_act(user, post, PostActionType.types[:off_topic])
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
Expand Down

0 comments on commit 6f0137d

Please sign in to comment.