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.rbindex 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+ endend::Chat::Publisher.publish_processed!(chat_message)diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rbindex 8016a5508d7..918cae3d2e6 100644--- a/plugins/chat/app/models/chat/message.rb+++ b/plugins/chat/app/models/chat/message.rb@@ -198,9 +198,11 @@ module Chatinvalidate_parsed_mentionsend- 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_notificationsargs[:queue] = priority.to_s if priority && priority != :normalJobs.enqueue(Jobs::Chat::ProcessMessage, args)enddiff --git a/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb b/plugins/chat/spec/jobs/regular/chat/process_message_spec.rbindex 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 doexpect(new_cooked).to include("Updated Title")endend++ 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+ endenddiff --git a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rbindex 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)endenddiff --git a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rbindex 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 dodescribed_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+ endendend