2023-03-01 17:11:35 +00:00
|
|
|
create function message_context(account_id bigint, guild_id bigint, channel_id bigint)
|
2020-06-12 21:13:21 +00:00
|
|
|
returns table (
|
|
|
|
system_id int,
|
|
|
|
log_channel bigint,
|
|
|
|
in_blacklist bool,
|
|
|
|
in_log_blacklist bool,
|
|
|
|
log_cleanup_enabled bool,
|
|
|
|
proxy_enabled bool,
|
|
|
|
last_switch int,
|
|
|
|
last_switch_members int[],
|
2020-06-12 21:30:10 +00:00
|
|
|
last_switch_timestamp timestamp,
|
|
|
|
system_tag text,
|
2021-08-02 21:22:06 +00:00
|
|
|
system_guild_tag text,
|
|
|
|
tag_enabled bool,
|
2020-11-20 23:34:08 +00:00
|
|
|
system_avatar text,
|
2020-11-21 00:44:15 +00:00
|
|
|
allow_autoproxy bool,
|
2022-11-23 08:48:24 +00:00
|
|
|
latch_timeout integer,
|
2023-03-25 22:42:47 +00:00
|
|
|
case_sensitive_proxy_tags bool,
|
|
|
|
proxy_error_message_enabled bool
|
2020-06-12 21:13:21 +00:00
|
|
|
)
|
|
|
|
as $$
|
2020-06-15 10:54:00 +00:00
|
|
|
-- CTEs to query "static" (accessible only through args) data
|
2020-06-12 21:13:21 +00:00
|
|
|
with
|
2022-11-23 08:48:24 +00:00
|
|
|
system as (select systems.*, system_config.latch_timeout, system_guild.tag as guild_tag, system_guild.tag_enabled as tag_enabled,
|
2023-03-25 22:42:47 +00:00
|
|
|
allow_autoproxy as account_autoproxy, system_config.case_sensitive_proxy_tags, system_config.proxy_error_message_enabled from accounts
|
2021-08-02 21:22:06 +00:00
|
|
|
left join systems on systems.id = accounts.system
|
2021-12-01 14:16:44 +00:00
|
|
|
left join system_config on system_config.system = accounts.system
|
2021-08-02 21:22:06 +00:00
|
|
|
left join system_guild on system_guild.system = accounts.system and system_guild.guild = guild_id
|
|
|
|
where accounts.uid = account_id),
|
2022-03-22 03:43:33 +00:00
|
|
|
guild as (select * from servers where id = guild_id)
|
2020-06-12 21:13:21 +00:00
|
|
|
select
|
2022-03-30 08:36:22 +00:00
|
|
|
system.id as system_id,
|
2020-06-12 21:13:21 +00:00
|
|
|
guild.log_channel,
|
2022-03-30 08:36:22 +00:00
|
|
|
(channel_id = any (guild.blacklist)) as in_blacklist,
|
|
|
|
(channel_id = any (guild.log_blacklist)) as in_log_blacklist,
|
2020-06-15 10:54:00 +00:00
|
|
|
coalesce(guild.log_cleanup_enabled, false),
|
2020-06-12 21:13:21 +00:00
|
|
|
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
|
2022-03-30 08:36:22 +00:00
|
|
|
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.account_autoproxy as allow_autoproxy,
|
2022-11-23 08:48:24 +00:00
|
|
|
system.latch_timeout as latch_timeout,
|
2023-03-25 22:42:47 +00:00
|
|
|
system.case_sensitive_proxy_tags as case_sensitive_proxy_tags,
|
|
|
|
system.proxy_error_message_enabled as proxy_error_message_enabled
|
2020-06-15 10:54:00 +00:00
|
|
|
-- 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
|
2020-06-12 21:13:21 +00:00
|
|
|
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
|
|
|
|
$$ 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)
|
|
|
|
returns table (
|
|
|
|
id int,
|
|
|
|
proxy_tags proxy_tag[],
|
|
|
|
keep_proxy bool,
|
2022-03-30 08:36:22 +00:00
|
|
|
|
2020-06-12 21:24:36 +00:00
|
|
|
server_name text,
|
|
|
|
display_name text,
|
|
|
|
name text,
|
2021-05-01 18:20:00 +00:00
|
|
|
|
2020-06-12 21:24:36 +00:00
|
|
|
server_avatar text,
|
2023-03-01 17:11:35 +00:00
|
|
|
webhook_avatar text,
|
2020-11-20 21:40:36 +00:00
|
|
|
avatar text,
|
|
|
|
|
2021-05-01 18:20:00 +00:00
|
|
|
color char(6),
|
|
|
|
|
2020-11-20 21:40:36 +00:00
|
|
|
allow_autoproxy bool
|
2020-06-12 21:13:21 +00:00
|
|
|
)
|
|
|
|
as $$
|
|
|
|
select
|
|
|
|
-- Basic data
|
2023-03-01 17:11:35 +00:00
|
|
|
members.id as id,
|
|
|
|
members.proxy_tags as proxy_tags,
|
|
|
|
members.keep_proxy as keep_proxy,
|
2022-03-30 08:36:22 +00:00
|
|
|
|
2020-06-12 21:24:36 +00:00
|
|
|
-- Name info
|
2023-03-01 17:11:35 +00:00
|
|
|
member_guild.display_name as server_name,
|
|
|
|
members.display_name as display_name,
|
|
|
|
members.name as name,
|
2022-03-30 08:36:22 +00:00
|
|
|
|
2020-06-12 21:24:36 +00:00
|
|
|
-- Avatar info
|
2023-03-01 17:11:35 +00:00
|
|
|
member_guild.avatar_url as server_avatar,
|
|
|
|
members.webhook_avatar_url as webhook_avatar,
|
|
|
|
members.avatar_url as avatar,
|
2020-11-20 21:40:36 +00:00
|
|
|
|
2023-03-01 17:11:35 +00:00
|
|
|
members.color as color,
|
2021-05-01 18:20:00 +00:00
|
|
|
|
2023-03-01 17:11:35 +00:00
|
|
|
members.allow_autoproxy as allow_autoproxy
|
2020-06-12 21:13:21 +00:00
|
|
|
from accounts
|
|
|
|
inner join systems on systems.id = accounts.system
|
|
|
|
inner join members on members.system = systems.id
|
|
|
|
left join member_guild on member_guild.member = members.id and member_guild.guild = guild_id
|
|
|
|
where accounts.uid = account_id
|
2020-06-12 22:43:48 +00:00
|
|
|
$$ language sql stable rows 10;
|
|
|
|
|
2020-10-19 08:50:51 +00:00
|
|
|
create function has_private_members(system_hid int) returns bool as $$
|
|
|
|
declare m int;
|
|
|
|
begin
|
|
|
|
m := count(id) from members where system = system_hid and member_visibility = 2;
|
|
|
|
if m > 0 then return true;
|
|
|
|
else return false;
|
|
|
|
end if;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2020-06-12 22:43:48 +00:00
|
|
|
|
2020-06-12 23:54:29 +00:00
|
|
|
create function generate_hid() returns char(5) as $$
|
2020-06-12 22:43:48 +00:00
|
|
|
select string_agg(substr('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1), '') from generate_series(1, 5)
|
|
|
|
$$ language sql volatile;
|
|
|
|
|
|
|
|
|
2020-06-12 23:54:29 +00:00
|
|
|
create function find_free_system_hid() returns char(5) as $$
|
|
|
|
declare new_hid char(5);
|
2020-06-12 22:43:48 +00:00
|
|
|
begin
|
|
|
|
loop
|
|
|
|
new_hid := generate_hid();
|
|
|
|
if not exists (select 1 from systems where hid = new_hid) then return new_hid; end if;
|
|
|
|
end loop;
|
|
|
|
end
|
|
|
|
$$ language plpgsql volatile;
|
|
|
|
|
|
|
|
|
2020-06-12 23:54:29 +00:00
|
|
|
create function find_free_member_hid() returns char(5) as $$
|
|
|
|
declare new_hid char(5);
|
2020-06-12 22:43:48 +00:00
|
|
|
begin
|
|
|
|
loop
|
|
|
|
new_hid := generate_hid();
|
|
|
|
if not exists (select 1 from members where hid = new_hid) then return new_hid; end if;
|
|
|
|
end loop;
|
|
|
|
end
|
2020-06-29 21:51:12 +00:00
|
|
|
$$ language plpgsql volatile;
|
|
|
|
|
|
|
|
create function find_free_group_hid() returns char(5) as $$
|
|
|
|
declare new_hid char(5);
|
|
|
|
begin
|
|
|
|
loop
|
|
|
|
new_hid := generate_hid();
|
|
|
|
if not exists (select 1 from groups where hid = new_hid) then return new_hid; end if;
|
|
|
|
end loop;
|
|
|
|
end
|
2020-06-12 22:43:48 +00:00
|
|
|
$$ language plpgsql volatile;
|