update query

This commit is contained in:
David Battersby
2025-02-14 15:03:24 +04:00
parent 7a9def1b03
commit 5e0950d398
2 changed files with 61 additions and 37 deletions

View File

@@ -35,54 +35,73 @@ module Chat
DB.query_single <<~SQL
WITH eligible_users AS (
SELECT DISTINCT u.id, uo.allow_private_messages
SELECT u.id, uo.allow_private_messages
FROM users u
JOIN user_options uo ON uo.user_id = u.id
#{groups_join_sql}
JOIN user_options uo ON uo.user_id = u.id
AND uo.chat_enabled
AND uo.chat_email_frequency = #{UserOption.chat_email_frequencies[:when_away]}
AND uo.email_level <> #{UserOption.email_level_types[:never]}
WHERE u.last_seen_at < now() - interval '15 minutes'
AND uo.chat_enabled
AND uo.chat_email_frequency = #{UserOption.chat_email_frequencies[:when_away]}
AND uo.email_level <> #{UserOption.email_level_types[:never]}
), watched_threads AS (
SELECT DISTINCT user_id, thread_id
FROM user_chat_thread_memberships
WHERE notification_level = #{Chat::NotificationLevels.all[:watching]}
), channel_messages AS (
SELECT DISTINCT ON (chat_channel_id) chat_channel_id, cm.id AS first_unread_id, user_id AS sender_id, cm.thread_id
FROM chat_messages cm
JOIN users sender ON sender.id = cm.user_id
WHERE cm.created_at > now() - interval '1 week'
AND cm.deleted_at IS NULL
AND NOT cm.created_by_sdk
ORDER BY chat_channel_id, cm.id
), unread_dms AS (
SELECT DISTINCT uccm.user_id
FROM user_chat_channel_memberships uccm
JOIN chat_channels cc ON cc.id = uccm.chat_channel_id AND cc.deleted_at IS NULL AND cc.chatable_type = 'DirectMessage'
JOIN channel_messages cm ON cm.chat_channel_id = cc.id AND cm.sender_id <> uccm.user_id
JOIN eligible_users eu ON eu.id = uccm.user_id AND eu.allow_private_messages
LEFT JOIN chat_threads om ON om.original_message_id = cm.first_unread_id
WHERE NOT uccm.muted
AND (uccm.last_read_message_id IS NULL OR cm.first_unread_id > uccm.last_read_message_id)
AND (uccm.last_unread_mention_when_emailed_id IS NULL OR cm.first_unread_id > uccm.last_unread_mention_when_emailed_id)
AND (cm.thread_id IS NULL OR om.id IS NOT NULL OR cm.thread_id IN (SELECT thread_id FROM watched_threads WHERE user_id = uccm.user_id))
), unread_mentions AS (
SELECT DISTINCT uccm.user_id
FROM user_chat_channel_memberships uccm
JOIN chat_channels cc ON cc.id = uccm.chat_channel_id AND cc.deleted_at IS NULL AND cc.chatable_type = 'Category'
JOIN channel_messages cm ON cm.chat_channel_id = cc.id AND cm.sender_id <> uccm.user_id
FROM user_chat_channel_memberships uccm
JOIN eligible_users eu ON eu.id = uccm.user_id
JOIN chat_mentions mn ON mn.chat_message_id = cm.first_unread_id
JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = mn.id
JOIN notifications n ON n.id = cmn.notification_id AND n.user_id = uccm.user_id AND NOT n.read
AND eu.allow_private_messages
JOIN chat_messages cm ON cm.chat_channel_id = uccm.chat_channel_id
AND cm.deleted_at IS NULL
AND (cm.thread_id IS NULL OR cm.thread_id IN (SELECT id FROM chat_threads WHERE original_message_id = cm.id))
AND NOT cm.created_by_sdk
AND cm.created_at > now() - interval '1 day'
JOIN users sender ON sender.id = cm.user_id
JOIN chat_channels cc ON cc.id = cm.chat_channel_id
AND cc.deleted_at IS NULL
AND cc.chatable_type = 'DirectMessage'
WHERE NOT uccm.muted
AND uccm.following
AND (uccm.last_read_message_id IS NULL OR cm.first_unread_id > uccm.last_read_message_id)
AND (uccm.last_unread_mention_when_emailed_id IS NULL OR cm.first_unread_id > uccm.last_unread_mention_when_emailed_id)
AND (uccm.last_read_message_id IS NULL OR uccm.last_read_message_id < cm.id)
AND (uccm.last_unread_mention_when_emailed_id IS NULL OR uccm.last_unread_mention_when_emailed_id < cm.id)
), unread_mentions AS (
SELECT DISTINCT n.user_id
FROM notifications n
JOIN eligible_users eu ON eu.id = n.user_id
JOIN chat_mention_notifications cmn ON cmn.notification_id = n.id
JOIN chat_mentions mn ON mn.id = cmn.chat_mention_id
JOIN chat_messages cm ON cm.id = mn.chat_message_id
AND cm.deleted_at IS NULL
AND cm.thread_id IS NULL
AND NOT cm.created_by_sdk
AND cm.created_at > now() - interval '1 day'
JOIN users sender ON sender.id = cm.user_id
JOIN chat_channels cc ON cc.id = cm.chat_channel_id
AND cc.deleted_at IS NULL
AND cc.chatable_type = 'Category'
JOIN user_chat_channel_memberships uccm ON uccm.chat_channel_id = cc.id
AND uccm.user_id = n.user_id
AND NOT uccm.muted
AND uccm.following
AND (uccm.last_read_message_id IS NULL OR uccm.last_read_message_id < cm.id)
AND (uccm.last_unread_mention_when_emailed_id IS NULL OR uccm.last_unread_mention_when_emailed_id < cm.id)
WHERE NOT n.read
), unread_threads AS (
SELECT DISTINCT uctm.user_id
FROM user_chat_thread_memberships uctm
JOIN eligible_users eu ON eu.id = uctm.user_id
JOIN chat_threads ct ON ct.id = uctm.thread_id
JOIN chat_messages cm ON cm.thread_id = ct.id
AND cm.deleted_at IS NULL
AND NOT cm.created_by_sdk
AND cm.created_at > now() - interval '1 day'
JOIN users sender ON sender.id = cm.user_id
JOIN chat_channels cc ON cc.id = ct.channel_id
AND cc.deleted_at IS NULL
WHERE uctm.notification_level = #{Chat::NotificationLevels.all[:watching]}
AND (uctm.last_read_message_id IS NULL OR uctm.last_read_message_id < cm.id)
)
SELECT user_id FROM unread_dms
UNION
SELECT user_id FROM unread_mentions
UNION
SELECT user_id FROM unread_threads
SQL
end
end

View File

@@ -325,6 +325,11 @@ describe Chat::Mailer do
user.user_option.update!(allow_private_messages: false)
expect_not_enqueued
end
it "queues a chat summary email when message is the original thread message" do
Fabricate(:chat_thread, channel: direct_message, original_message: Chat::Message.last)
expect_enqueued
end
end
describe "in direct message channel with threads" do