diff --git a/lib/guardian.rb b/lib/guardian.rb index 00bad7075cb..ccf6a8b5578 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -292,10 +292,8 @@ class Guardian (is_group || is_user) && # User is authenticated authenticated? && - # Have to be a basic level at least, or are contacting moderators - (@user.has_trust_level?(SiteSetting.min_trust_to_send_messages) || - (target.is_a?(User) && target.moderator?) || - (target.name == Group[:moderators].name)) && + # Have to be a basic level at least + @user.has_trust_level?(SiteSetting.min_trust_to_send_messages) && # User disabled private message (is_staff? || is_group || target.user_option.allow_private_messages) && # PMs are enabled diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index d0af49efb97..d1374d38473 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -154,22 +154,9 @@ describe Guardian do expect(Guardian.new(user).can_send_private_message?(user)).to be_truthy end - context "when user is untrusted " do - before do - user.trust_level = TrustLevel[0] - end - - it "returns false to another user" do - expect(Guardian.new(user).can_send_private_message?(another_user)).to be_falsey - end - - it "returns true to moderator user" do - expect(Guardian.new(user).can_send_private_message?(moderator)).to be_truthy - end - - it "returns true to moderator group" do - expect(Guardian.new(user).can_send_private_message?(Group[:moderators])).to be_truthy - end + it "returns false when you are untrusted" do + user.trust_level = TrustLevel[0] + expect(Guardian.new(user).can_send_private_message?(another_user)).to be_falsey end it "returns true to another user" do @@ -193,10 +180,6 @@ describe Guardian do expect(Guardian.new(moderator).can_send_private_message?(another_user)).to be_truthy expect(Guardian.new(admin).can_send_private_message?(another_user)).to be_truthy end - - it "returns false even to a moderator" do - expect(Guardian.new(trust_level_4).can_send_private_message?(moderator)).to be_falsey - end end context "target user is suspended" do diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb index 7aa8330c872..0ed814cae12 100644 --- a/spec/components/topic_creator_spec.rb +++ b/spec/components/topic_creator_spec.rb @@ -3,12 +3,11 @@ require 'rails_helper' describe TopicCreator do let(:user) { Fabricate(:user, trust_level: TrustLevel[2]) } - let(:user2) { Fabricate(:user, trust_level: TrustLevel[2]) } let(:moderator) { Fabricate(:moderator) } let(:admin) { Fabricate(:admin) } let(:valid_attrs) { Fabricate.attributes_for(:topic) } - let(:pm_valid_attrs) { { raw: 'this is a new post', title: 'this is a new title', archetype: Archetype.private_message, target_usernames: user2.username } } + let(:pm_valid_attrs) { { raw: 'this is a new post', title: 'this is a new title', archetype: Archetype.private_message, target_usernames: moderator.username } } let(:pm_to_email_valid_attrs) do { diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index d54df929130..aaa1af0b0b8 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -42,88 +42,62 @@ describe PostAction do expect { PostAction.act(admin, post, PostActionType.types[:notify_user], message: "WAT") }.not_to raise_error end - context "notify moderators integration test" do - let!(:mod) { moderator } + it "notify moderators integration test" do + post = create_post + mod = moderator + Group.refresh_automatic_groups! - before { Group.refresh_automatic_groups! } + action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message") - it "flag from TL1 user" do - post = create_post + posts = Post.joins(:topic) + .select('posts.id, topics.subtype, posts.topic_id') + .where('topics.archetype' => Archetype.private_message) + .to_a - action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message") + expect(posts.count).to eq(1) + expect(action.related_post_id).to eq(posts[0].id.to_i) + expect(posts[0].subtype).to eq(TopicSubtype.notify_moderators) - posts = Post.joins(:topic) - .select('posts.id, topics.subtype, posts.topic_id') - .where('topics.archetype' => Archetype.private_message) - .to_a + topic = posts[0].topic - expect(posts.count).to eq(1) - expect(action.related_post_id).to eq(posts[0].id.to_i) - expect(posts[0].subtype).to eq(TopicSubtype.notify_moderators) + # Moderators should be invited to the private topic, otherwise they're not permitted to see it + topic_user_ids = topic.reload.topic_users.map { |x| x.user_id } + expect(topic_user_ids).to include(codinghorror.id) + expect(topic_user_ids).to include(mod.id) - topic = posts[0].topic + expect(topic.topic_users.where(user_id: mod.id) + .pluck(:notification_level).first).to eq(TopicUser.notification_levels[:tracking]) - # Moderators should be invited to the private topic, otherwise they're not permitted to see it - topic_user_ids = topic.reload.topic_users.map { |x| x.user_id } - expect(topic_user_ids).to include(codinghorror.id) - expect(topic_user_ids).to include(mod.id) + expect(topic.topic_users.where(user_id: codinghorror.id) + .pluck(:notification_level).first).to eq(TopicUser.notification_levels[:watching]) - expect(topic.topic_users.where(user_id: mod.id) - .pluck(:notification_level).first).to eq(TopicUser.notification_levels[:tracking]) + # reply to PM should not clear flag + PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags").create + action.reload + expect(action.deleted_at).to eq(nil) - expect(topic.topic_users.where(user_id: codinghorror.id) - .pluck(:notification_level).first).to eq(TopicUser.notification_levels[:watching]) + # Acting on the flag should not post an automated status message (since a moderator already replied) + expect(topic.posts.count).to eq(2) + PostAction.agree_flags!(post, admin) + topic.reload + expect(topic.posts.count).to eq(2) - # reply to PM should not clear flag - PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags").create - action.reload - expect(action.deleted_at).to eq(nil) + # Clearing the flags should not post an automated status message + PostAction.act(mod, post, PostActionType.types[:notify_moderators], message: "another special message") + PostAction.clear_flags!(post, admin) + topic.reload + expect(topic.posts.count).to eq(2) - # Acting on the flag should not post an automated status message (since a moderator already replied) - expect(topic.posts.count).to eq(2) - PostAction.agree_flags!(post, admin) - topic.reload - expect(topic.posts.count).to eq(2) + # Acting on the flag should post an automated status message + another_post = create_post + action = PostAction.act(codinghorror, another_post, PostActionType.types[:notify_moderators], message: "foobar") + topic = action.related_post.topic - # Clearing the flags should not post an automated status message - PostAction.act(mod, post, PostActionType.types[:notify_moderators], message: "another special message") - PostAction.clear_flags!(post, admin) - topic.reload - expect(topic.posts.count).to eq(2) - - # Acting on the flag should post an automated status message - another_post = create_post - action = PostAction.act(codinghorror, another_post, PostActionType.types[:notify_moderators], message: "foobar") - topic = action.related_post.topic - - expect(topic.posts.count).to eq(1) - PostAction.agree_flags!(another_post, admin) - topic.reload - expect(topic.posts.count).to eq(2) - expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action]) - end - - it "flag from TL0 user" do - tl0_user = Fabricate(:user, trust_level: 0) - first_post = Fabricate(:post, user: Fabricate(:user, trust_level: 1)) - - pm_topic = Fabricate(:private_message_topic, - first_post: first_post, - topic_allowed_users: [ - Fabricate.build(:topic_allowed_user, user: first_post.user), - Fabricate.build(:topic_allowed_user, user: tl0_user), - ] - ) - - action = PostAction.act(tl0_user, first_post, PostActionType.types[:notify_moderators], message: 'my special message') - - expect(action.related_post_id).to be_present - - notify_moderators_post = action.related_post - - expect(notify_moderators_post.topic&.subtype).to eq(TopicSubtype.notify_moderators) - expect(notify_moderators_post.raw).to include('my special message') - end + expect(topic.posts.count).to eq(1) + PostAction.agree_flags!(another_post, admin) + topic.reload + expect(topic.posts.count).to eq(2) + expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action]) end describe 'notify_moderators' do