mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-09 23:37:54 +00:00
feat: cache Discord DM channels in database
This commit is contained in:
parent
ddbf0e8691
commit
89c44a3482
14 changed files with 127 additions and 48 deletions
12
PluralKit.Core/Database/Migrations/26.sql
Normal file
12
PluralKit.Core/Database/Migrations/26.sql
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-- schema version 26
|
||||
-- cache Discord DM channels in the database
|
||||
|
||||
alter table accounts alter column system drop not null;
|
||||
alter table accounts drop constraint accounts_system_fkey;
|
||||
alter table accounts
|
||||
add constraint accounts_system_fkey
|
||||
foreign key (system) references systems(id) on delete set null;
|
||||
|
||||
alter table accounts add column dm_channel bigint;
|
||||
|
||||
update info set schema_version = 26;
|
||||
|
|
@ -1,9 +1,14 @@
|
|||
using Dapper;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public async Task<ulong?> GetDmChannel(ulong id)
|
||||
=> await _db.Execute(c => c.QueryFirstOrDefaultAsync<ulong?>("select dm_channel from accounts where uid = @id", new { id = id }));
|
||||
|
||||
public async Task UpdateAccount(ulong id, AccountPatch patch)
|
||||
{
|
||||
_logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch);
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@ public partial class ModelRepository
|
|||
var query = new Query("accounts")
|
||||
.Select("systems.*")
|
||||
.LeftJoin("systems", "systems.id", "accounts.system")
|
||||
.Where("uid", accountId);
|
||||
.Where("uid", accountId)
|
||||
.WhereNotNull("system");
|
||||
return _db.QueryFirst<PKSystem?>(query);
|
||||
}
|
||||
|
||||
|
|
@ -111,10 +112,13 @@ public partial class ModelRepository
|
|||
// We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent
|
||||
// This is used in import/export, although the pk;link command checks for this case beforehand
|
||||
|
||||
// update 2022-01: the accounts table is now independent of systems
|
||||
// we MUST check for the presence of a system before inserting, or it will move the new account to the current system
|
||||
|
||||
var query = new Query("accounts").AsInsert(new { system, uid = accountId });
|
||||
await _db.ExecuteQuery(conn, query, "on conflict (uid) do update set system = @p0");
|
||||
|
||||
_logger.Information("Linked account {UserId} to {SystemId}", accountId, system);
|
||||
await _db.ExecuteQuery(conn, query, "on conflict do nothing");
|
||||
|
||||
_ = _dispatch.Dispatch(system, new UpdateDispatchData
|
||||
{
|
||||
|
|
@ -125,7 +129,10 @@ public partial class ModelRepository
|
|||
|
||||
public async Task RemoveAccount(SystemId system, ulong accountId)
|
||||
{
|
||||
var query = new Query("accounts").AsDelete().Where("uid", accountId).Where("system", system);
|
||||
var query = new Query("accounts").AsUpdate(new
|
||||
{
|
||||
system = (ulong?)null
|
||||
}).Where("uid", accountId).Where("system", system);
|
||||
await _db.ExecuteQuery(query);
|
||||
_logger.Information("Unlinked account {UserId} from {SystemId}", accountId, system);
|
||||
_ = _dispatch.Dispatch(system, new UpdateDispatchData
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace PluralKit.Core;
|
|||
internal class DatabaseMigrator
|
||||
{
|
||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||
private const int TargetSchemaVersion = 25;
|
||||
private const int TargetSchemaVersion = 26;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public DatabaseMigrator(ILogger logger)
|
||||
|
|
|
|||
|
|
@ -6,9 +6,11 @@ namespace PluralKit.Core;
|
|||
|
||||
public class AccountPatch: PatchObject
|
||||
{
|
||||
public Partial<ulong> DmChannel { get; set; }
|
||||
public Partial<bool> AllowAutoproxy { get; set; }
|
||||
|
||||
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
||||
.With("dm_channel", DmChannel)
|
||||
.With("allow_autoproxy", AllowAutoproxy)
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue