FIX: Fix query selecting users not accepting PMs.

FIX: Fix query selecting users not accepting PMs.

From ddd260941e72b9db405bc521f1cf53d1e22a6851 Mon Sep 17 00:00:00 2001
From: Bianca Nenciu <nenciu.bianca@gmail.com>
Date: Wed, 28 Nov 2018 21:47:45 +0200
Subject: [PATCH] FIX: Fix query selecting users not accepting PMs.


diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index aa6c75c..62d2c39 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -114,7 +114,7 @@ class PostCreator
 
       User
         .joins("LEFT JOIN user_options ON user_options.user_id = users.id")
-        .joins("LEFT JOIN muted_users ON muted_users.muted_user_id = #{@user.id.to_i}")
+        .joins("LEFT JOIN muted_users ON muted_users.user_id = users.id AND muted_users.muted_user_id = #{@user.id.to_i}")
         .where("user_options.user_id IS NOT NULL")
         .where("
           (user_options.user_id IN (:user_ids) AND NOT user_options.allow_private_messages) OR
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index 2ad2736..20f1e82 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -1131,6 +1131,7 @@ describe PostCreator do
 
   context "private message to a muted user" do
     let(:muted_me) { Fabricate(:evil_trout) }
+    let(:another_user) { Fabricate(:user) }
 
     it 'should fail' do
       updater = UserUpdater.new(muted_me, muted_me)
@@ -1141,10 +1142,14 @@ describe PostCreator do
         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}"
+        target_usernames: "#{muted_me.username},#{another_user.username}"
       )
+
       expect(pc).not_to be_valid
-      expect(pc.errors).to be_present
+
+      expect(pc.errors.full_messages).to contain_exactly(
+        I18n.t(:not_accepting_pms, username: muted_me.username)
+      )
     end
 
     let(:staff_user) { Fabricate(:admin) }

GitHub

What’s the context for this fix?

Ok, found it: FIX: Fix query selecting users not accepting PMs. by nbianca · Pull Request #6697 · discourse/discourse · GitHub

1 Like