diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 90074a84883..85c5913f0a1 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -199,6 +199,7 @@ en: spamming_host: "Sorry you cannot post a link to that host." user_is_suspended: "Suspended users are not allowed to post." topic_not_found: "Something has gone wrong. Perhaps this topic was closed or deleted while you were looking at it?" + not_accepting_pms: "Sorry, %{username} is not accepting messages at the moment." just_posted_that: "is too similar to what you recently posted" invalid_characters: "contains invalid characters" diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 1c9de20e6ea..928c8473623 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -92,6 +92,17 @@ class PostCreator return false end + # Make sure none of the users have muted the creator + names = @opts[:target_usernames] + if names.present? && !skip_validations? && !@user.staff? + users = User.where(username: names.split(',').flatten).pluck(:id, :username).to_h + + MutedUser.where(user_id: users.keys, muted_user_id: @user.id).pluck(:user_id).each do |m| + errors[:base] << I18n.t(:not_accepting_pms, username: users[m]) + end + return false if errors[:base].present? + end + if new_topic? topic_creator = TopicCreator.new(@user, guardian, @opts) return false unless skip_validations? || validate_child(topic_creator) diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 6c144923947..b59758adb41 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -904,4 +904,39 @@ describe PostCreator do end end + context "private message to a muted user" do + let(:muted_me) { Fabricate(:evil_trout) } + + it 'should fail' do + updater = UserUpdater.new(muted_me, muted_me) + updater.update_muted_users("#{user.username}") + + pc = PostCreator.new( + user, + title: 'this message is to someone who muted me!', + raw: "you will have to see this even if you muted me!", + archetype: Archetype.private_message, + target_usernames: "#{muted_me.username}" + ) + expect(pc).not_to be_valid + expect(pc.errors).to be_present + end + + let(:staff_user) { Fabricate(:admin) } + + it 'succeeds if the user is staff' do + updater = UserUpdater.new(muted_me, muted_me) + updater.update_muted_users("#{staff_user.username}") + + pc = PostCreator.new( + staff_user, + title: 'this message is to someone who muted me!', + raw: "you will have to see this even if you muted me!", + archetype: Archetype.private_message, + target_usernames: "#{muted_me.username}" + ) + expect(pc).to be_valid + expect(pc.errors).to be_blank + end + end end