discourse/plugins/chat/spec
Régis Hanol 71391cd40d PERF: fix performance of chat email notifications
When chat is enabled, there's a scheduled job that runs every 5 minutes to check whether we need to send a "chat summary" email to users with unread chat messages or mentions.

On Discourse with a large number of users, the query used wasn't optimal and sometimes taking minutes. Which isn't good when the query is called every 5 minutes 😬

This PR reworks the query in `Chat::Mailer.send_unread_mentions_summary`.

Instead of starting from the `users` table, it starts from the `user_chat_channel_memberships` table which is the main piece tying everything together.

The new query is mostly similar to the previous one, with some bug fixes (like ensuring the user has `allow_private_messages` enabled for direct messages) and is also slightly simpler since it doesn't keep track of the `memberships_with_unread_messages` anymore. That part has been moved to the `user_notifications.chat_summary` email method.

The `UserEmailExtension` has been deleted since that was using to N+1 update the `user_chat_channel_memberships.last_unread_mention_when_emailed_it`(quite a mouthful 😛) but that's now done directly in the `user_notifications.chat_summary` email method.

The "plat de résistance" of that PR - the `user_notifications.chat_summary` method has been re-worked for improved performances 🚀

Instead of doing everything in one query, it does 4 tiny ones.

- One to retrieve the list of unread mentions (@something) in "category" channels
- One to retrieve the list of unread messages in "direct message" channels (aka. 1-1 and group discussions)
- One to load all the chat messages for each "category" channels from the last unread mention
- One to load all the chat messages for each "direct message" channels from the last unread message

All the specs for both `Chat::Mailer` and `UserNotification.chat_summary` have been rewriten for easier comprehension and faster execution (mostly by not using chat services which makes the specs go 10x slower...)

Internal ref - t/129848
2024-06-10 14:25:06 +02:00
..
components/chat PERF: fix performance of chat email notifications 2024-06-10 14:25:06 +02:00
fabricators PERF: fix performance of chat email notifications 2024-06-10 14:25:06 +02:00
integration PERF: Reduce overhead from chat message excerpt (#26712) 2024-04-25 14:29:00 +02:00
jobs FIX: correctly handle notifications for channels (#27178) 2024-05-24 19:59:24 +02:00
lib DEV: Use site setting mandatory_values for chat allowed groups (#26994) 2024-05-13 14:38:26 +10:00
mailers PERF: fix performance of chat email notifications 2024-06-10 14:25:06 +02:00
models FIX: correct in_thread? message logic (#27151) 2024-05-23 11:08:55 +02:00
policies/chat/channel DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
queries/chat DEV: adds first_messages/last_messages to thread SDK (#26861) 2024-05-03 17:30:39 +02:00
requests FIX: allows listing messages of any thread (#27259) 2024-05-30 10:20:40 +02:00
serializer DEV: bots are always allowed to chat (#26948) 2024-05-09 12:05:31 +02:00
services FIX: chat direct message group user limit is off by 1 (#27014) 2024-06-03 12:11:49 +04:00
support DEV: Move chat service objects into core (#26506) 2024-04-04 10:57:41 -03:00
system FEATURE: load chat channel settings within drawer (#27346) 2024-06-06 14:01:09 +04:00
validators DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00
plugin_helper.rb PERF: Reduce overhead from chat message excerpt (#26712) 2024-04-25 14:29:00 +02:00
plugin_spec.rb DEV: Remove unnecessary rails_helper requiring (#26364) 2024-03-26 11:32:01 +01:00