feat: rewrite database schema for localized autoproxy

This commit is contained in:
spiral
2022-03-21 23:43:33 -04:00
parent ca108813b7
commit 982812333b
16 changed files with 245 additions and 188 deletions

View File

@@ -0,0 +1,35 @@
using Dapper;
using SqlKata;
namespace PluralKit.Core;
public partial class ModelRepository
{
public async Task UpdateAutoproxy(SystemId system, ulong? guildId, ulong? channelId, AutoproxyPatch patch)
{
var locationStr = guildId != null ? "guild" : (channelId != null ? "channel" : "global");
_logger.Information("Updated autoproxy for {SystemId} in location {location}: {@AutoproxyPatch}", system, locationStr, patch);
var query = patch.Apply(new Query("autoproxy")
.Where("system", system)
.Where("guild_id", guildId ?? 0)
.Where("channel_id", channelId ?? 0)
);
_ = _dispatch.Dispatch(system, guildId, channelId, patch);
await _db.ExecuteQuery(query);
}
// todo: this might break with differently scoped autoproxy
public async Task<AutoproxySettings> GetAutoproxySettings(SystemId system, ulong? guildId, ulong? channelId)
=> await _db.QueryFirst<AutoproxySettings>(new Query("autoproxy").AsInsert(new {
system = system,
guild_id = guildId ?? 0,
channel_id = channelId ?? 0,
})
.Where("system", system)
.Where("guild_id", guildId ?? 0)
.Where("channel_id", channelId ?? 0),
"on conflict (system, guild_id, channel_id) do update set system = $1 returning *"
);
}