refactor: add SqlKata for SQL generation, move connection handling into ModelRepository

This commit is contained in:
spiral
2021-09-29 21:51:38 -04:00
parent 6251d29abb
commit 92e45a07ff
60 changed files with 806 additions and 640 deletions

View File

@@ -71,9 +71,8 @@ namespace PluralKit.Bot
// Get message context from DB (tracking w/ metrics)
MessageContext ctx;
await using (var conn = await _db.Obtain())
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(conn, evt.Author.Id, evt.GuildId ?? default, rootChannel.Id);
ctx = await _repo.GetMessageContext(evt.Author.Id, evt.GuildId ?? default, rootChannel.Id);
// Try each handler until we find one that succeeds
if (await TryHandleLogClean(evt, ctx))
@@ -114,7 +113,7 @@ namespace PluralKit.Bot
try
{
var system = ctx.SystemId != null ? await _db.Execute(c => _repo.GetSystem(c, ctx.SystemId.Value)) : null;
var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx));
}
catch (PKError)

View File

@@ -36,7 +36,7 @@ namespace PluralKit.Bot
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
await _db.Execute(c => _repo.DeleteMessage(c, evt.Id));
await _repo.DeleteMessage(evt.Id);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Id);
@@ -56,7 +56,7 @@ namespace PluralKit.Bot
_logger.Information("Bulk deleting {Count} messages in channel {Channel}",
evt.Ids.Length, evt.ChannelId);
await _db.Execute(c => _repo.DeleteMessagesBulk(c, evt.Ids));
await _repo.DeleteMessagesBulk(evt.Ids);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Ids.ToList());

View File

@@ -63,9 +63,8 @@ namespace PluralKit.Bot
// Just run the normal message handling code, with a flag to disable autoproxying
MessageContext ctx;
await using (var conn = await _db.Obtain())
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(conn, evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = _bot.PermissionsIn(channel.Id);

View File

@@ -73,7 +73,7 @@ namespace PluralKit.Bot
return;
}
var commandMsg = await _db.Execute(c => _commandMessageService.GetCommandMessage(c, evt.MessageId));
var commandMsg = await _commandMessageService.GetCommandMessage(evt.MessageId);
if (commandMsg != null)
{
await HandleCommandDeleteReaction(evt, commandMsg);
@@ -124,7 +124,7 @@ namespace PluralKit.Bot
if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
return;
var system = await _db.Execute(c => _repo.GetSystemByAccount(c, evt.UserId));
var system = await _repo.GetSystemByAccount(evt.UserId);
// Can only delete your own message
if (msg.System.Id != system?.Id) return;
@@ -138,7 +138,7 @@ namespace PluralKit.Bot
// Message was deleted by something/someone else before we got to it
}
await _db.Execute(c => _repo.DeleteMessage(c, evt.MessageId));
await _repo.DeleteMessage(evt.MessageId);
}
private async ValueTask HandleCommandDeleteReaction(MessageReactionAddEvent evt, CommandMessage? msg)