chore: clean up message_context function

remove CTEs that were confusing and led to developer error
This commit is contained in:
alyssa 2024-11-07 04:54:37 +09:00
parent b2322c6366
commit f65c20f31e

View file

@ -21,50 +21,55 @@
deny_bot_usage bool
)
as $$
-- CTEs to query "static" (accessible only through args) data
with
system as (select systems.*, system_config.latch_timeout, system_guild.tag as guild_tag, system_guild.tag_enabled as tag_enabled,
system_guild.avatar_url as guild_avatar,
allow_autoproxy as account_autoproxy, system_config.case_sensitive_proxy_tags, system_config.proxy_error_message_enabled from accounts
left join systems on systems.id = accounts.system
left join system_config on system_config.system = accounts.system
left join system_guild on system_guild.system = accounts.system and system_guild.guild = guild_id
where accounts.uid = account_id),
guild as (select * from servers where id = guild_id)
select
system.id as system_id,
guild.log_channel,
((channel_id = any (guild.blacklist))
or (thread_id = any (guild.blacklist))) as in_blacklist,
((channel_id = any (guild.log_blacklist))
or (thread_id = any (guild.log_blacklist))) as in_log_blacklist,
coalesce(guild.log_cleanup_enabled, false),
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
system_last_switch.switch as last_switch,
system_last_switch.members as last_switch_members,
system_last_switch.timestamp as last_switch_timestamp,
system.tag as system_tag,
system.guild_tag as system_guild_tag,
coalesce(system.tag_enabled, true) as tag_enabled,
system.avatar_url as system_avatar,
system.guild_avatar as system_guild_avatar,
system.account_autoproxy as allow_autoproxy,
system.latch_timeout as latch_timeout,
system.case_sensitive_proxy_tags as case_sensitive_proxy_tags,
system.proxy_error_message_enabled as proxy_error_message_enabled,
coalesce(abuse_logs.deny_bot_usage, false) as deny_bot_usage
-- accounts table
accounts.allow_autoproxy as allow_autoproxy,
-- systems table
systems.id as system_id,
systems.tag as system_tag,
systems.avatar_url as system_avatar,
-- system_config table
system_config.latch_timeout as latch_timeout,
system_config.case_sensitive_proxy_tags as case_sensitive_proxy_tags,
system_config.proxy_error_message_enabled as proxy_error_message_enabled
-- system_guild table
coalesce(system_guild.tag_enabled, true) as tag_enabled,
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
system_guild.tag as system_guild_tag,
system_guild.avatar_url as system_guild_avatar,
-- system_last_switch view
system_last_switch.switch as last_switch,
system_last_switch.members as last_switch_members,
system_last_switch.timestamp as last_switch_timestamp,
-- servers table
servers.log_channel as log_channel,
((channel_id = any (servers.blacklist))
or (thread_id = any (servers.blacklist))) as in_blacklist,
((channel_id = any (servers.log_blacklist))
or (thread_id = any (servers.log_blacklist))) as in_log_blacklist,
coalesce(servers.log_cleanup_enabled, false) as log_cleanup_enabled,
-- abuse_logs table
coalesce(abuse_logs.deny_bot_usage, false) as deny_bot_usage
-- We need a "from" clause, so we just use some bogus data that's always present
-- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data
from (select 1) as _placeholder
left join system on true
left join guild on true
left join accounts on accounts.uid = account_id
left join system_last_switch on system_last_switch.system = system.id
left join system_guild on system_guild.system = system.id and system_guild.guild = guild_id
left join servers on servers.id = guild_id
left join systems on systems.id = accounts.system
left join system_config on system_config.system = accounts.system
left join system_guild on system_guild.system = accounts.system
and system_guild.guild = guild_id
left join system_last_switch on system_last_switch.system = accounts.system
left join abuse_logs on abuse_logs.id = accounts.abuse_log
$$ language sql stable rows 1;
-- Fetches info about proxying related to a given account/guild
-- Returns one row per member in system, should be used in conjuction with `message_context` too
create function proxy_members(account_id bigint, guild_id bigint)