Refactor sort/filter code once again

Now we handle sorting on the bot side, but still filter in the database
This commit is contained in:
Ske
2020-06-13 21:49:31 +02:00
parent 0bb8d2b917
commit 6d06474d26
12 changed files with 164 additions and 126 deletions

View File

@@ -82,7 +82,7 @@ namespace PluralKit.Core
await ApplyMigrations(conn, tx);
// Now, reapply views/functions (we deleted them above, no need to worry about conflicts)
await ExecuteSqlFile($"{RootPath}.views.sql", conn, tx);
await ExecuteSqlFile($"{RootPath}.Views.views.sql", conn, tx);
await ExecuteSqlFile($"{RootPath}.Functions.functions.sql", conn, tx);
// Finally, commit tx

View File

@@ -0,0 +1,34 @@
#nullable enable
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public static class DatabaseViewsExt
{
public static Task<IEnumerable<SystemFronter>> QueryCurrentFronters(this IPKConnection conn, int system) =>
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new {system});
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, int system, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false)
{
StringBuilder query = new StringBuilder("select * from member_list where system = @system");
if (privacyFilter != null)
query.Append($" and member_privacy = {(int) privacyFilter}");
if (filter != null)
{
static string Filter(string column) => $"position(lower(@filter) in lower(coalesce({column}, ''))) > 0";
query.Append($" and ({Filter("name")} or {Filter("display_name")}");
if (includeDescriptionInNameFilter) query.Append($" or {Filter("description")}");
query.Append(")");
}
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter});
}
}
}

View File

@@ -0,0 +1,17 @@
#nullable enable
using NodaTime;
namespace PluralKit.Core
{
// TODO: is inheritance here correct?
public class ListedMember: PKMember
{
public ulong? LastMessage { get; }
public Instant? LastSwitchTime { get; }
public AnnualDate? AnnualBirthday =>
Birthday != null
? new AnnualDate(Birthday.Value.Month, Birthday.Value.Day)
: (AnnualDate?) null;
}
}

View File

@@ -0,0 +1,14 @@
using NodaTime;
namespace PluralKit.Core
{
public class SystemFronter
{
public int SystemId { get; }
public int SwitchId { get; }
public Instant SwitchTimestamp { get; }
public int MemberId { get; }
public string MemberHid { get; }
public string MemberName { get; }
}
}

View File

@@ -1,4 +1,5 @@
create view system_last_switch as
-- Returns one row per system, containing info about latest switch + array of member IDs (for future joins)
create view system_last_switch as
select systems.id as system,
last_switch.id as switch,
last_switch.timestamp as timestamp,
@@ -6,6 +7,25 @@ select systems.id as system,
from systems
inner join lateral (select * from switches where switches.system = systems.id order by timestamp desc limit 1) as last_switch on true;
-- Returns one row for every current fronter in a system, w/ some member info
create view system_fronters as
select
systems.id as system_id,
last_switch.id as switch_id,
last_switch.timestamp as switch_timestamp,
members.id as member_id,
members.hid as member_hid,
members.name as member_name
from systems
-- TODO: is there a more efficient way of doing this search? might need to index on timestamp if we haven't in prod
inner join lateral (select * from switches where switches.system = systems.id order by timestamp desc limit 1) as last_switch on true
-- change to left join to handle memberless switches?
inner join switch_members on switch_members.switch = last_switch.system
inner join members on members.id = switch_members.member
-- return them in order of the switch itself
order by switch_members.id;
create view member_list as
select members.*,
-- Find last message ID

View File

@@ -1,4 +1,9 @@
drop view if exists system_last_switch;
-- This gets run on every bot startup and makes sure we're starting from a clean slate
-- Then, the views/functions.sql files get run, and they recreate the necessary objects
-- This does mean we can't use any functions in row triggers, etc. Still unsure how to handle this.
drop view if exists system_last_switch;
drop view if exists system_fronters;
drop view if exists member_list;
drop function if exists message_context;