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
This commit is contained in:
Régis Hanol
2024-06-08 00:20:37 +02:00
parent 49aac85057
commit 71391cd40d
12 changed files with 913 additions and 1234 deletions

View File

@ -240,18 +240,20 @@ en:
other: "You have new chat messages"
from: "%{site_name}"
subject:
private_message: "[%{email_prefix}] New message"
direct_message_from_1: "[%{email_prefix}] New message from %{username}"
direct_message_from_2: "[%{email_prefix}] New message from %{username1} and %{username2}"
direct_message_from_more:
one: "[%{email_prefix}] New message from %{username} and %{count} other"
other: "[%{email_prefix}] New message from %{username} and %{count} others"
chat_channel_1: "[%{email_prefix}] New message in %{channel}"
chat_channel_2: "[%{email_prefix}] New message in %{channel1} and %{channel2}"
chat_channel_more:
one: "[%{email_prefix}] New message in %{channel} and %{count} other"
other: "[%{email_prefix}] New message in %{channel} and %{count} others"
chat_channel_and_direct_message: "[%{email_prefix}] New message in %{channel} and from %{username}"
private_email:
one: "[%{site_name}] New message"
other: "[%{site_name}] New messages"
chat_dm_1:
one: "[%{site_name}] New message from %{name}"
other: "[%{site_name}] New messages from %{name}"
chat_dm_2: "[%{site_name}] New messages from %{name_1} and %{name_2}"
chat_dm_3_or_more: "[%{site_name}] New messages from %{name} and %{count} others"
chat_channel_1:
one: "[%{site_name}] New message in %{channel}"
other: "[%{site_name}] New messages in %{channel}"
chat_channel_2: "[%{site_name}] New messages in %{channel_1} and %{channel_2}"
chat_channel_3_or_more: "[%{site_name}] New messages in %{channel} and %{count} others"
chat_channel_and_dm: "[%{site_name}] New messages in %{channel} and from %{name}"
unsubscribe: "This chat summary is sent from %{site_link} when you are away. Change your %{email_preferences_link}, or %{unsubscribe_link} to unsubscribe."
unsubscribe_no_link: "This chat summary is sent from %{site_link} when you are away. Change your %{email_preferences_link}."
view_messages: