A platform for community discussion. Free, open, simple.
FIX: Send TL2 promotion message to correct user (#21978)
Bianca Nenciu10ee92656c8e
diff --git a/lib/system_message.rb b/lib/system_message.rbindex dadd64edbff..14abbe4e0a3 100644--- a/lib/system_message.rb+++ b/lib/system_message.rb@@ -73,7 +73,12 @@ class SystemMessagepost = I18n.with_locale(@recipient.effective_locale) { creator.create }- DiscourseEvent.trigger(:system_message_sent, post: post, message_type: type)+ DiscourseEvent.trigger(+ :system_message_sent,+ post: post,+ message_type: type,+ recipient: @recipient,+ )raise StandardError, creator.errors.full_messages.join(" ") if creator.errors.present?diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rbindex 63d61a038e8..d1987d9a17a 100644--- a/plugins/discourse-narrative-bot/plugin.rb+++ b/plugins/discourse-narrative-bot/plugin.rb@@ -292,8 +292,7 @@ after_initialize donext if !SiteSetting.discourse_narrative_bot_enablednext if args[:message_type] != "tl2_promotion_message"- recipient = args[:post].topic.topic_users.where.not(user_id: args[:post].user_id).last&.user- recipient ||= Discourse.site_contact_user if args[:post].user == Discourse.site_contact_user+ recipient = args[:recipient]next if recipient.nil?I18n.with_locale(recipient.effective_locale) dodiff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rbindex 529ad5c67a1..f391a9c8d23 100644--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb@@ -831,6 +831,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do:system_message_sent,post: Post.last,message_type: "tl2_promotion_message",+ recipient: recipient,)}.to change { Topic.count }expect(Topic.last.title).to eq(@@ -849,6 +850,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do:system_message_sent,post: Post.last,message_type: "tl2_promotion_message",+ recipient: recipient,)}.to change { Topic.count }expect(Topic.last.title).to eq(@@ -879,6 +881,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do:system_message_sent,post: Post.last,message_type: "tl2_promotion_message",+ recipient: recipient,)}.to change { Topic.count }@@ -886,4 +889,17 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative doexpect(topic.title).to eq("german title")expect(topic.first_post.raw).to eq("german body")end++ it "invites the correct user when users in site_contact_group_name are invited to the system message" do+ recipient = Fabricate(:user)+ group = Fabricate(:group)+ group.add(Fabricate(:user))+ SiteSetting.site_contact_group_name = "#{group.name}"++ SystemMessage.new(recipient).create("tl2_promotion_message", {})++ expect(Topic.last.topic_users.map(&:user_id).sort).to eq(+ [DiscourseNarrativeBot::Base.new.discobot_user.id, recipient.id],+ )+ endenddiff --git a/spec/lib/system_message_spec.rb b/spec/lib/system_message_spec.rbindex 7c9a7c0e27e..7f44290a4d2 100644--- a/spec/lib/system_message_spec.rb+++ b/spec/lib/system_message_spec.rb@@ -83,6 +83,7 @@ RSpec.describe SystemMessage doexpect(event[:event_name]).to eq(:system_message_sent)expect(event[:params].first[:post]).to eq(Post.last)expect(event[:params].first[:message_type]).to eq(:tl2_promotion_message)+ expect(event[:params].first[:recipient]).to eq(user)endit "sends an event before the system message is sent" do