DEV: Allow chat mentions to have several notifications (#24874)

This PR is a reworked version of https://github.com/discourse/discourse/pull/24670.

In chat, we need the ability to have several notifications per `chat_mention`. 
Currently, we have one_to_one relationship between `chat_mentions` and `notifications`:

d7a09fb08d/plugins/chat/app/models/chat/mention.rb (L9)

We want to have one_to_many relationship. This PR implements that by introducing 
a join table between `chat_mentions` and `notifications`.

The main motivation for this is that we want to solve some performance problems 
with mentions that we're having now. Let's say a user sends a message with @ all 
in a channel with 50 members, we do two things in this case at the moment:

- create 50 chat_mentions
- create 50 notifications

We don't want to change how notifications work in core, but we want to be more 
efficient in chat, and create only 1 `chat_mention` which would link to 50 notifications. 
Also note, that on the side of notifications, having a lot of notifications is not so 
big problem, because notifications processing can be queued.

Apart from improving performance, this change will make the code design better.

Note that I've marked the old `chat_mention.notification_id` column as ignored, but 
I'm not deleting it in this PR. We'll delete it later in https://github.com/discourse/discourse/pull/24800.
This commit is contained in:
Andrei Prigorshnev
2023-12-19 18:53:00 +04:00
committed by GitHub
parent 558c709fef
commit fbd24fa6ae
22 changed files with 141 additions and 41 deletions

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
class AddChatMentionNotifications < ActiveRecord::Migration[7.0]
def change
create_table :chat_mention_notifications, id: false do |t|
t.integer :chat_mention_id, null: false
t.integer :notification_id, null: false
end
add_index :chat_mention_notifications, %i[chat_mention_id]
add_index :chat_mention_notifications, %i[notification_id], unique: true
end
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class UpdateRelationshipBetweenChatMentionsAndNotifications < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
BATCH_SIZE = 5000
def up
begin
inserted_count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)
INSERT INTO chat_mention_notifications(chat_mention_id, notification_id)
SELECT cm.id, cm.notification_id
FROM chat_mentions cm
LEFT JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = cm.id
WHERE cm.notification_id IS NOT NULL and cmn.chat_mention_id IS NULL
LIMIT :batch_size;
SQL
end while inserted_count > 0
end
def down
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class UpdateRelationshipBetweenChatMentionsAndNotificationsPostMigrate < ActiveRecord::Migration[
7.0
]
disable_ddl_transaction!
BATCH_SIZE = 5000
def up
begin
inserted_count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)
INSERT INTO chat_mention_notifications(chat_mention_id, notification_id)
SELECT cm.id, cm.notification_id
FROM chat_mentions cm
LEFT JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = cm.id
WHERE cm.notification_id IS NOT NULL and cmn.chat_mention_id IS NULL
LIMIT :batch_size;
SQL
end while inserted_count > 0
end
def down
end
end