Skip to content

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

FIX: Duplicate notifications when mentioned in chat (#39670)

Rafael dos Santos Silva2834cc795594
diff --git a/public/images/push-notifications/inline_reply.png b/app/assets/images/push-notifications/inline_reply.png
similarity index 100%
rename from public/images/push-notifications/inline_reply.png
rename to app/assets/images/push-notifications/inline_reply.png
diff --git a/plugins/chat/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications.rb b/plugins/chat/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications.rb
new file mode 100644
index 00000000000..a6ab459d3ac
--- /dev/null
+++ b/plugins/chat/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+class RemoveDuplicateChatMentionNotifications < ActiveRecord::Migration[8.0]
+ disable_ddl_transaction!
+
+ # `Notification.types[:chat_mention]` — hardcoded so the migration stays
+ # stable if the enum is ever renumbered.
+ CHAT_MENTION_TYPE = 29
+
+ # PR #39614 deployed on 2026-04-29 introduced a Propshaft asset lookup
+ # in `Chat::Notifier.push_notification_reply_action` that raised on
+ # every chat mention. `Jobs::Chat::NotifyMentioned` calls
+ # `create_notification!` before `send_notifications`, so each Sidekiq
+ # retry inserted another `Notification` row for the same mention. The
+ # follow-up fix landed on 2026-04-30; this sweeps up rows created in
+ # between.
+ WINDOW_START = "2026-04-29".freeze
+
+ def up
+ affected_user_ids = DB.query_single(<<~SQL, type: CHAT_MENTION_TYPE, since: WINDOW_START)
+ SELECT DISTINCT user_id
+ FROM notifications
+ WHERE notification_type = :type
+ AND created_at >= :since
+ SQL
+
+ affected_user_ids.each do |user_id|
+ DB.exec(<<~SQL, type: CHAT_MENTION_TYPE, since: WINDOW_START, user_id: user_id)
+ WITH dups AS (
+ SELECT
+ id,
+ MIN(id) OVER (PARTITION BY data) AS keep_id,
+ BOOL_OR(read) OVER (PARTITION BY data) AS any_read
+ FROM notifications
+ WHERE notification_type = :type
+ AND created_at >= :since
+ AND user_id = :user_id
+ ),
+ to_delete AS (
+ SELECT id FROM dups WHERE id <> keep_id
+ ),
+ cleaned_join AS (
+ DELETE FROM chat_mention_notifications
+ WHERE notification_id IN (SELECT id FROM to_delete)
+ ),
+ marked_read AS (
+ UPDATE notifications n
+ SET read = TRUE
+ FROM dups d
+ WHERE n.id = d.keep_id
+ AND d.any_read
+ AND NOT n.read
+ )
+ DELETE FROM notifications WHERE id IN (SELECT id FROM to_delete)
+ SQL
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/chat/lib/chat/notifier.rb b/plugins/chat/lib/chat/notifier.rb
index 2a64e481fbe..4089248372b 100644
--- a/plugins/chat/lib/chat/notifier.rb
+++ b/plugins/chat/lib/chat/notifier.rb
@@ -61,6 +61,12 @@ module Chat
}.compact,
}
end
+ rescue => e
+ # Never let a quick-reply payload failure abort the notify job —
+ # the job creates DB notifications before sending alerts, and a
+ # Sidekiq retry would re-create them.
+ Discourse.warn_exception(e, message: "Failed to build chat push notification reply action")
+ {}
end
def notify_edit(chat_message:, timestamp:)
diff --git a/plugins/chat/spec/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications_spec.rb b/plugins/chat/spec/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications_spec.rb
new file mode 100644
index 00000000000..05e21348884
--- /dev/null
+++ b/plugins/chat/spec/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications_spec.rb
@@ -0,0 +1,138 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "plugins/chat/db/post_migrate/20260430172653_remove_duplicate_chat_mention_notifications.rb",
+ )
+
+describe RemoveDuplicateChatMentionNotifications do
+ fab!(:user_1, :user)
+ fab!(:user_2, :user)
+
+ before do
+ @original_verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ end
+
+ after { ActiveRecord::Migration.verbose = @original_verbose }
+
+ def insert_notification(
+ user:,
+ type: 29,
+ data: '{"chat_message_id":1}',
+ read: false,
+ created_at: "2026-04-29 12:00:00"
+ )
+ DB.query_single(
+ <<~SQL,
+ INSERT INTO notifications (notification_type, user_id, data, read, high_priority, created_at, updated_at)
+ VALUES (:type, :user_id, :data, :read, false, :created_at, :created_at)
+ RETURNING id
+ SQL
+ user_id: user.id,
+ type: type,
+ data: data,
+ read: read,
+ created_at: created_at,
+ ).first
+ end
+
+ def insert_mention_join(notification_id, chat_mention_id: 1)
+ DB.exec(<<~SQL, notification_id: notification_id, chat_mention_id: chat_mention_id)
+ INSERT INTO chat_mention_notifications (notification_id, chat_mention_id)
+ VALUES (:notification_id, :chat_mention_id)
+ SQL
+ end
+
+ def notification_ids_for(user)
+ DB.query_single("SELECT id FROM notifications WHERE user_id = :id ORDER BY id", id: user.id)
+ end
+
+ def join_ids_for(notification_ids)
+ DB.query_single(
+ "SELECT notification_id FROM chat_mention_notifications WHERE notification_id IN (:ids) ORDER BY notification_id",
+ ids: notification_ids,
+ )
+ end
+
+ it "collapses duplicate chat_mention rows for the same user/data, keeping the oldest" do
+ kept = insert_notification(user: user_1)
+ dup_a = insert_notification(user: user_1)
+ dup_b = insert_notification(user: user_1)
+
+ described_class.new.up
+
+ expect(notification_ids_for(user_1)).to eq([kept])
+ end
+
+ it "preserves distinct chat_mention rows (different data) for the same user" do
+ a = insert_notification(user: user_1, data: '{"chat_message_id":1}')
+ b = insert_notification(user: user_1, data: '{"chat_message_id":2}')
+
+ described_class.new.up
+
+ expect(notification_ids_for(user_1)).to contain_exactly(a, b)
+ end
+
+ it "marks the kept row read when any duplicate sibling was read" do
+ kept = insert_notification(user: user_1, read: false)
+ insert_notification(user: user_1, read: true)
+
+ described_class.new.up
+
+ read_state = DB.query_single("SELECT read FROM notifications WHERE id = :id", id: kept).first
+ expect(read_state).to eq(true)
+ end
+
+ it "leaves the kept row read state unchanged when no sibling was read" do
+ kept = insert_notification(user: user_1, read: false)
+ insert_notification(user: user_1, read: false)
+
+ described_class.new.up
+
+ read_state = DB.query_single("SELECT read FROM notifications WHERE id = :id", id: kept).first
+ expect(read_state).to eq(false)
+ end
+
+ it "deletes orphaned chat_mention_notifications join rows for removed duplicates" do
+ kept = insert_notification(user: user_1)
+ dup = insert_notification(user: user_1)
+ insert_mention_join(kept, chat_mention_id: 100)
+ insert_mention_join(dup, chat_mention_id: 100)
+
+ described_class.new.up
+
+ expect(join_ids_for([kept, dup])).to eq([kept])
+ end
+
+ it "ignores rows created before the deploy window" do
+ a = insert_notification(user: user_1, created_at: "2026-04-28 23:59:00")
+ b = insert_notification(user: user_1, created_at: "2026-04-28 23:59:30")
+
+ described_class.new.up
+
+ expect(notification_ids_for(user_1)).to contain_exactly(a, b)
+ end
+
+ it "ignores other notification types that share user/data shape" do
+ chat_kept = insert_notification(user: user_1, type: 29)
+ insert_notification(user: user_1, type: 29) # dup, should be removed
+ other = insert_notification(user: user_1, type: 1) # mentioned, untouched
+ other_dup = insert_notification(user: user_1, type: 1) # also untouched
+
+ described_class.new.up
+
+ expect(notification_ids_for(user_1)).to contain_exactly(chat_kept, other, other_dup)
+ end
+
+ it "dedupes independently for each affected user" do
+ kept_1 = insert_notification(user: user_1)
+ insert_notification(user: user_1)
+ kept_2 = insert_notification(user: user_2)
+ insert_notification(user: user_2)
+
+ described_class.new.up
+
+ expect(notification_ids_for(user_1)).to eq([kept_1])
+ expect(notification_ids_for(user_2)).to eq([kept_2])
+ end
+end