diff --git a/nomen/notifications.py b/nomen/notifications.py index 4f1d756..8b6943b 100644 --- a/nomen/notifications.py +++ b/nomen/notifications.py @@ -151,26 +151,27 @@ async def handle_triggers(ctx, message): WITH pauses AS (SELECT user_id FROM user_pauses WHERE guild_id=:guild), ignores AS ( - SELECT target, user_id FROM user_ignores WHERE guild_id=:guild + SELECT target, user_id AS ignorer FROM user_ignores WHERE guild_id=:guild UNION - SELECT target, user_id FROM user_blocks + SELECT target, user_id AS ignorer FROM user_blocks ) SELECT keyword, user_id, use_embed FROM ( -- Deduplicate notification recipients by user_id - SELECT keyword, user_id, use_embed, row_number() over (partition by user_id) n + SELECT *, row_number() over (partition by user_id) n FROM keywords LEFT JOIN users USING (user_id) - WHERE likely(disabled IS NOT 1) -- Don't notify users who opted out - AND (unlikely(notify_self IS 1) OR user_id IS NOT :author) -- Don't notify author unless wanted - AND (unlikely(bots_notify IS 1) OR :is_bot IS NOT 1) -- Don't notify from bots unless wanted - AND likely(:author NOT IN (SELECT target FROM ignores WHERE user_id=user_id)) - AND likely(:channel NOT IN (SELECT target FROM ignores WHERE user_id=user_id)) - AND likely(:category NOT IN (SELECT target FROM ignores WHERE user_id=user_id)) - AND likely(:parent NOT IN (SELECT target FROM ignores WHERE user_id=user_id)) - AND likely(user_id NOT IN pauses) - AND guild_id=:guild AND unlikely(contains(:content, keyword, regex)) + WHERE guild_id=:guild + AND likely(disabled IS NOT 1) -- Don't notify users who opted out + AND likely(user_id NOT IN pauses) -- Don't notify users who are paused + AND unlikely(contains(:content, keyword, regex)) ) WHERE n=1 + AND (unlikely(notify_self IS 1) OR user_id IS NOT :author) -- Don't notify author unless wanted + AND (unlikely(bots_notify IS 1) OR :is_bot IS NOT 1) -- Don't notify from bots unless wanted + AND likely(:author NOT IN (SELECT target FROM ignores WHERE ignorer=user_id)) -- Ignored user + AND likely(:channel NOT IN (SELECT target FROM ignores WHERE ignorer=user_id)) -- Ignored channel + AND likely(:category NOT IN (SELECT target FROM ignores WHERE ignorer=user_id)) -- Ignored category + AND likely(:parent NOT IN (SELECT target FROM ignores WHERE ignorer=user_id)) -- Ignored parent channel of thread """ async with TaskGroup() as tg: