discourse/plugins/chat/lib/chat
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
..
bookmark_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
category_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
channel_archive_service.rb FEATURE: Add threads support to chat archives (#24325) 2023-11-27 15:47:35 +01:00
channel_fetcher.rb DEV: Dedicated route for current user notification counts (#26106) 2024-03-15 12:08:37 -04:00
channel_hashtag_data_source.rb FEATURE: Async load of category and chat hashtags (#25526) 2024-02-12 12:07:14 +02:00
channel_membership_manager.rb FEATURE: Auto-remove users without permission from channel (#20344) 2023-03-22 10:19:59 +10:00
duplicate_message_validator.rb
engine.rb DEV: Update chat scheduled job loading to match skeleton (#26853) 2024-05-02 19:20:00 +01:00
group_extension.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
guardian_extensions.rb DEV: Use site setting mandatory_values for chat allowed groups (#26994) 2024-05-13 14:38:26 +10:00
mailer.rb PERF: fix performance of chat email notifications 2024-06-10 14:25:06 +02:00
message_bookmarkable.rb FIX: Show deleted bookmark reminders in user bookmarks menu (#25905) 2024-02-29 09:03:49 +10:00
message_mover.rb FEATURE: Add thread support to the chat message mover (#26147) 2024-04-08 09:03:46 -03:00
message_processor.rb DEV: Rename with_secure_uploads? to should_secure_uploads? on Post (#26549) 2024-04-09 13:23:11 +10:00
message_rate_limiter.rb
message_reactor.rb
messages_exporter.rb FIX: Chat messages exporter (#25461) 2024-01-30 18:37:11 +04:00
notification_levels.rb FIX: Chat NotificationLevels extension breaking in prod (#21484) 2023-05-10 18:46:06 +02:00
notifier.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
onebox_handler.rb DEV: Revert guardian changes (#24742) 2023-12-06 16:37:32 +10:00
outgoing_web_hook_extension.rb FEATURE: granular webhooks (#23070) 2023-10-09 03:35:31 +00:00
parsed_mentions.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
plugin_instance_extension.rb
post_notification_handler.rb
review_queue.rb FIX: Create a reviewable when flagging a chat message for 'something else' (#23264) 2023-08-25 17:38:27 +08:00
reviewable_extension.rb DEV: Refactor STI/polymorphic associations in chat (#20789) 2023-04-17 15:41:56 +02:00
secure_uploads_compatibility.rb
seeder.rb
slack_compatibility.rb
statistics.rb DEV: Add new chat metrics (#23872) 2024-01-04 16:10:03 +04:00
transcript_service.rb FIX: Do not display chat replies as threads in transcripts (#24768) 2023-12-15 09:50:43 -03:00
user_extension.rb DEV: Redesign chat mentions (#24752) 2024-01-17 15:24:01 +04:00
user_notifications_extension.rb PERF: fix performance of chat email notifications 2024-06-10 14:25:06 +02:00
user_option_extension.rb FEATURE: encourage users to set chat thread titles (#26617) 2024-04-29 17:20:01 +08:00