Split message/proxy data up in MessageContext and ProxyMember
This commit is contained in:
25
PluralKit.Core/Database/Functions/DatabaseFunctionsExt.cs
Normal file
25
PluralKit.Core/Database/Functions/DatabaseFunctionsExt.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public static class DatabaseFunctionsExt
|
||||
{
|
||||
public static Task<MessageContext> QueryMessageContext(this IDbConnection conn, ulong account, ulong guild, ulong channel)
|
||||
{
|
||||
return conn.QueryFirstAsync<MessageContext>("message_context",
|
||||
new { account_id = account, guild_id = guild, channel_id = channel },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<ProxyMember>> QueryProxyMembers(this IDbConnection conn, ulong account, ulong guild)
|
||||
{
|
||||
return conn.QueryAsync<ProxyMember>("proxy_members",
|
||||
new { account_id = account, guild_id = guild },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
}
|
27
PluralKit.Core/Database/Functions/MessageContext.cs
Normal file
27
PluralKit.Core/Database/Functions/MessageContext.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Model for the `message_context` PL/pgSQL function in `functions.sql`
|
||||
/// </summary>
|
||||
public class MessageContext
|
||||
{
|
||||
public int? SystemId { get; set; }
|
||||
public ulong? LogChannel { get; set; }
|
||||
public bool InBlacklist { get; set; }
|
||||
public bool InLogBlacklist { get; set; }
|
||||
public bool LogCleanupEnabled { get; set; }
|
||||
public bool ProxyEnabled { get; set; }
|
||||
public AutoproxyMode AutoproxyMode { get; set; }
|
||||
public int? AutoproxyMember { get; set; }
|
||||
public ulong? LastMessage { get; set; }
|
||||
public int? LastMessageMember { get; set; }
|
||||
public int LastSwitch { get; set; }
|
||||
public IReadOnlyList<int> LastSwitchMembers { get; set; } = new int[0];
|
||||
public Instant LastSwitchTimestamp { get; set; }
|
||||
}
|
||||
}
|
17
PluralKit.Core/Database/Functions/ProxyMember.cs
Normal file
17
PluralKit.Core/Database/Functions/ProxyMember.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Model for the `proxy_members` PL/pgSQL function in `functions.sql`
|
||||
/// </summary>
|
||||
public class ProxyMember
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public IReadOnlyCollection<ProxyTag> ProxyTags { get; set; } = new ProxyTag[0];
|
||||
public bool KeepProxy { get; set; }
|
||||
public string ProxyName { get; set; } = "";
|
||||
public string? ProxyAvatar { get; set; }
|
||||
}
|
||||
}
|
73
PluralKit.Core/Database/Functions/functions.sql
Normal file
73
PluralKit.Core/Database/Functions/functions.sql
Normal file
@@ -0,0 +1,73 @@
|
||||
create function message_context(account_id bigint, guild_id bigint, channel_id bigint)
|
||||
returns table (
|
||||
system_id int,
|
||||
log_channel bigint,
|
||||
in_blacklist bool,
|
||||
in_log_blacklist bool,
|
||||
log_cleanup_enabled bool,
|
||||
proxy_enabled bool,
|
||||
autoproxy_mode int,
|
||||
autoproxy_member int,
|
||||
last_message bigint,
|
||||
last_message_member int,
|
||||
last_switch int,
|
||||
last_switch_members int[],
|
||||
last_switch_timestamp timestamp
|
||||
)
|
||||
as $$
|
||||
with
|
||||
system as (select systems.* from accounts inner join systems on systems.id = accounts.system where accounts.uid = account_id),
|
||||
guild as (select * from servers where id = guild_id),
|
||||
last_message as (select * from messages where messages.guild = guild_id and messages.sender = account_id order by mid desc limit 1)
|
||||
select
|
||||
system.id as system_id,
|
||||
guild.log_channel,
|
||||
(channel_id = any(guild.blacklist)) as in_blacklist,
|
||||
(channel_id = any(guild.log_blacklist)) as in_log_blacklist,
|
||||
guild.log_cleanup_enabled,
|
||||
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
|
||||
coalesce(system_guild.autoproxy_mode, 1) as autoproxy_mode,
|
||||
system_guild.autoproxy_member,
|
||||
last_message.mid as last_message,
|
||||
last_message.member as last_message_member,
|
||||
system_last_switch.switch as last_switch,
|
||||
system_last_switch.members as last_switch_members,
|
||||
system_last_switch.timestamp as last_switch_timestamp
|
||||
from system
|
||||
full join guild on true
|
||||
left join last_message on true
|
||||
|
||||
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,
|
||||
proxy_name text,
|
||||
proxy_avatar text
|
||||
)
|
||||
as $$
|
||||
select
|
||||
-- Basic data
|
||||
members.id as id,
|
||||
members.proxy_tags as proxy_tags,
|
||||
members.keep_proxy as keep_proxy,
|
||||
|
||||
-- Proxy info
|
||||
case
|
||||
when systems.tag is not null then (coalesce(member_guild.display_name, members.display_name, members.name) || ' ' || systems.tag)
|
||||
else coalesce(member_guild.display_name, members.display_name, members.name)
|
||||
end as proxy_name,
|
||||
coalesce(member_guild.avatar_url, members.avatar_url, systems.avatar_url) as proxy_avatar
|
||||
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
|
||||
$$ language sql stable rows 10;
|
@@ -1,26 +0,0 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Model for the `proxy_info` PL/pgSQL function in `functions.sql`
|
||||
/// </summary>
|
||||
public class ProxyMember
|
||||
{
|
||||
public int SystemId { get; set; }
|
||||
public int MemberId { get; set; }
|
||||
public bool ProxyEnabled { get; set; }
|
||||
public AutoproxyMode AutoproxyMode { get; set; }
|
||||
public bool IsAutoproxyMember { get; set; }
|
||||
public ulong? LatchMessage { get; set; }
|
||||
public string ProxyName { get; set; } = "";
|
||||
public string? ProxyAvatar { get; set; }
|
||||
public IReadOnlyCollection<ProxyTag> ProxyTags { get; set; } = new ProxyTag[0];
|
||||
public bool KeepProxy { get; set; }
|
||||
|
||||
public IReadOnlyCollection<ulong> ChannelBlacklist { get; set; } = new ulong[0];
|
||||
public IReadOnlyCollection<ulong> LogBlacklist { get; set; } = new ulong[0];
|
||||
public ulong? LogChannel { get; set; }
|
||||
}
|
||||
}
|
@@ -46,7 +46,7 @@ namespace PluralKit.Core
|
||||
|
||||
// Now, reapply views/functions (we deleted them above, no need to worry about conflicts)
|
||||
await ExecuteSqlFile($"{RootPath}.views.sql", conn, tx);
|
||||
await ExecuteSqlFile($"{RootPath}.functions.sql", conn, tx);
|
||||
await ExecuteSqlFile($"{RootPath}.Functions.functions.sql", conn, tx);
|
||||
|
||||
// Finally, commit tx
|
||||
tx.Commit();
|
||||
|
@@ -1,3 +1,5 @@
|
||||
drop view if exists system_last_switch;
|
||||
drop view if exists member_list;
|
||||
drop function if exists proxy_info;
|
||||
|
||||
drop function if exists message_context;
|
||||
drop function if exists proxy_members;
|
@@ -1,85 +0,0 @@
|
||||
-- Giant "mega-function" to find all information relevant for message proxying
|
||||
-- Returns one row per member, computes several properties from others
|
||||
create function proxy_info(account_id bigint, guild_id bigint)
|
||||
returns table
|
||||
(
|
||||
-- Note: table type gets matched *by index*, not *by name* (make sure order here and in `select` match)
|
||||
system_id int, -- from: systems.id
|
||||
member_id int, -- from: members.id
|
||||
proxy_tags proxy_tag[], -- from: members.proxy_tags
|
||||
keep_proxy bool, -- from: members.keep_proxy
|
||||
proxy_enabled bool, -- from: system_guild.proxy_enabled
|
||||
proxy_name text, -- calculated: name we should proxy under
|
||||
proxy_avatar text, -- calculated: avatar we should proxy with
|
||||
autoproxy_mode int, -- from: system_guild.autoproxy_mode
|
||||
is_autoproxy_member bool, -- calculated: should this member be used for AP?
|
||||
latch_message bigint, -- calculated: last message from this account in this guild
|
||||
channel_blacklist bigint[], -- from: servers.blacklist
|
||||
log_blacklist bigint[], -- from: servers.log_blacklist
|
||||
log_channel bigint -- from: servers.log_channel
|
||||
)
|
||||
as
|
||||
$$
|
||||
select
|
||||
-- Basic data
|
||||
systems.id as system_id,
|
||||
members.id as member_id,
|
||||
members.proxy_tags as proxy_tags,
|
||||
members.keep_proxy as keep_proxy,
|
||||
|
||||
-- Proxy info
|
||||
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
|
||||
case
|
||||
when systems.tag is not null then (coalesce(member_guild.display_name, members.display_name, members.name) || ' ' || systems.tag)
|
||||
else coalesce(member_guild.display_name, members.display_name, members.name)
|
||||
end as proxy_name,
|
||||
coalesce(member_guild.avatar_url, members.avatar_url, systems.avatar_url) as proxy_avatar,
|
||||
|
||||
-- Autoproxy data
|
||||
coalesce(system_guild.autoproxy_mode, 1) as autoproxy_mode,
|
||||
|
||||
-- Autoproxy logic is essentially: "is this member the one we should autoproxy?"
|
||||
case
|
||||
-- Front mode: check if this is the first fronter
|
||||
when system_guild.autoproxy_mode = 2 then members.id = (select sls.members[1]
|
||||
from system_last_switch as sls
|
||||
where sls.system = systems.id)
|
||||
|
||||
-- Latch mode: check if this is the last proxier
|
||||
when system_guild.autoproxy_mode = 3 then members.id = last_message_in_guild.member
|
||||
|
||||
-- Member mode: check if this is the selected memebr
|
||||
when system_guild.autoproxy_mode = 4 then members.id = system_guild.autoproxy_member
|
||||
|
||||
-- no autoproxy: then this member definitely shouldn't be autoproxied :)
|
||||
else false end as is_autoproxy_member,
|
||||
|
||||
last_message_in_guild.mid as latch_message,
|
||||
|
||||
-- Guild info
|
||||
coalesce(servers.blacklist, array[]::bigint[]) as channel_blacklist,
|
||||
coalesce(servers.log_blacklist, array[]::bigint[]) as log_blacklist,
|
||||
servers.log_channel as log_channel
|
||||
from accounts
|
||||
-- Fetch guild info
|
||||
left join servers on servers.id = guild_id
|
||||
|
||||
-- Fetch the system for this account (w/ guild config)
|
||||
inner join systems on systems.id = accounts.system
|
||||
left join system_guild on system_guild.system = accounts.system and system_guild.guild = guild_id
|
||||
|
||||
-- Fetch all members from this system (w/ guild config)
|
||||
inner join members on members.system = systems.id
|
||||
left join member_guild on member_guild.member = members.id and member_guild.guild = guild_id
|
||||
|
||||
-- Find ID and member for the last message sent in this guild
|
||||
left join lateral (select mid, member
|
||||
from messages
|
||||
where messages.guild = guild_id
|
||||
and messages.sender = account_id
|
||||
and system_guild.autoproxy_mode = 3
|
||||
order by mid desc
|
||||
limit 1) as last_message_in_guild on true
|
||||
where accounts.uid = account_id;
|
||||
$$ language sql stable
|
||||
rows 10;
|
Reference in New Issue
Block a user