Skip to content

A platform for community discussion. Free, open, simple.

FIX: Skip notifications when rebaking chat messages from GitHub webhooks (#36577)

Régis Hanolb45703646a7f
diff --git a/plugins/chat/app/jobs/regular/chat/process_message.rb b/plugins/chat/app/jobs/regular/chat/process_message.rb
index 683db70535e..fced8eb3826 100644
--- a/plugins/chat/app/jobs/regular/chat/process_message.rb
+++ b/plugins/chat/app/jobs/regular/chat/process_message.rb
@@ -34,11 +34,12 @@ module Jobs
# extract external links for webhook-based rebaking
::Chat::MessageLink.extract_from(chat_message)
- # notifier should be idempotent and not re-notify
- if args[:edit_timestamp]
- ::Chat::Notifier.new(chat_message, args[:edit_timestamp]).notify_edit
- else
- ::Chat::Notifier.new(chat_message, chat_message.created_at).notify_new
+ unless args[:skip_notifications]
+ if args[:edit_timestamp]
+ ::Chat::Notifier.new(chat_message, args[:edit_timestamp]).notify_edit
+ else
+ ::Chat::Notifier.new(chat_message, chat_message.created_at).notify_new
+ end
end
::Chat::Publisher.publish_processed!(chat_message)
diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb
index 8016a5508d7..918cae3d2e6 100644
--- a/plugins/chat/app/models/chat/message.rb
+++ b/plugins/chat/app/models/chat/message.rb
@@ -198,9 +198,11 @@ module Chat
invalidate_parsed_mentions
end
- def rebake!(invalidate_oneboxes: false, priority: nil)
+ def rebake!(invalidate_oneboxes: false, priority: nil, skip_notifications: false)
ensure_last_editor_id
- args = { chat_message_id: self.id, invalidate_oneboxes: invalidate_oneboxes }
+ args = { chat_message_id: self.id }
+ args[:invalidate_oneboxes] = true if invalidate_oneboxes
+ args[:skip_notifications] = true if skip_notifications
args[:queue] = priority.to_s if priority && priority != :normal
Jobs.enqueue(Jobs::Chat::ProcessMessage, args)
end
diff --git a/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb b/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
index af34db939b9..18fa17aaa14 100644
--- a/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
+++ b/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
@@ -59,4 +59,42 @@ describe Jobs::Chat::ProcessMessage do
expect(new_cooked).to include("Updated Title")
end
end
+
+ describe "skip_notifications" do
+ fab!(:user)
+ fab!(:mentioned_user, :user)
+ fab!(:chat_channel)
+ fab!(:message_with_mention) do
+ Fabricate(:chat_message, chat_channel:, user:, message: "Hey @#{mentioned_user.username}!")
+ end
+
+ before do
+ chat_channel.add(user)
+ chat_channel.add(mentioned_user)
+ end
+
+ it "sends notifications by default" do
+ expect_enqueued_with(job: Jobs::Chat::NotifyMentioned) do
+ described_class.new.execute(chat_message_id: message_with_mention.id)
+ end
+ end
+
+ it "skips notifications when skip_notifications is true" do
+ expect_not_enqueued_with(job: Jobs::Chat::NotifyMentioned) do
+ described_class.new.execute(
+ chat_message_id: message_with_mention.id,
+ skip_notifications: true,
+ )
+ end
+ end
+
+ it "skips watching notifications when skip_notifications is true" do
+ expect_not_enqueued_with(job: Jobs::Chat::NotifyWatching) do
+ described_class.new.execute(
+ chat_message_id: message_with_mention.id,
+ skip_notifications: true,
+ )
+ end
+ end
+ end
end
diff --git a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
index a01f0bc813c..dd4348bfa7e 100644
--- a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
+++ b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
@@ -40,7 +40,7 @@ module Jobs
.where(id: message_ids)
.find_each do |message|
next unless has_github_pr_onebox?(message.cooked, pr_url)
- message.rebake!(invalidate_oneboxes: true, priority: :low)
+ message.rebake!(invalidate_oneboxes: true, priority: :low, skip_notifications: true)
end
end
diff --git a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
index a67ab65cb75..6b6cf69e163 100644
--- a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
+++ b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
@@ -56,5 +56,42 @@ RSpec.describe Jobs::RebakeGithubPrPosts do
described_class.new.execute(pr_url:)
end
+
+ context "with chat enabled" do
+ fab!(:chat_channel)
+
+ before do
+ skip("Chat plugin not loaded") unless defined?(::Chat)
+ SiteSetting.chat_enabled = true
+ end
+
+ def create_chat_message_with_link(cooked)
+ Fabricate(:chat_message, chat_channel:, user:, cooked:).tap do |message|
+ Chat::MessageLink.create!(chat_message: message, url: pr_url)
+ end
+ end
+
+ it "rebakes chat messages with GitHub PR oneboxes and skips notifications" do
+ create_chat_message_with_link(<<~HTML)
+ <aside class="onebox githubpullrequest"><a href="#{pr_url}">PR</a></aside>
+ HTML
+
+ expect_any_instance_of(Chat::Message).to receive(:rebake!).with(
+ invalidate_oneboxes: true,
+ priority: :low,
+ skip_notifications: true,
+ )
+
+ described_class.new.execute(pr_url:)
+ end
+
+ it "does not rebake chat messages without GitHub PR oneboxes" do
+ create_chat_message_with_link(%(<a href="#{pr_url}">plain link</a>))
+
+ expect_any_instance_of(Chat::Message).not_to receive(:rebake!)
+
+ described_class.new.execute(pr_url:)
+ end
+ end
end
end