Major database refactor (again)

This commit is contained in:
Ske
2020-08-29 13:46:27 +02:00
parent 3996cd48c7
commit c7612df37e
55 changed files with 1014 additions and 1100 deletions

View File

@@ -15,7 +15,7 @@ namespace PluralKit.Bot
{
public class Context
{
private ILifetimeScope _provider;
private readonly ILifetimeScope _provider;
private readonly DiscordRestClient _rest;
private readonly DiscordShardedClient _client;
@@ -24,8 +24,8 @@ namespace PluralKit.Bot
private readonly Parameters _parameters;
private readonly MessageContext _messageContext;
private readonly IDataStore _data;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly PKSystem _senderSystem;
private readonly IMetrics _metrics;
@@ -38,10 +38,10 @@ namespace PluralKit.Bot
_client = provider.Resolve<DiscordShardedClient>();
_message = message;
_shard = shard;
_data = provider.Resolve<IDataStore>();
_senderSystem = senderSystem;
_messageContext = messageContext;
_db = provider.Resolve<IDatabase>();
_repo = provider.Resolve<ModelRepository>();
_metrics = provider.Resolve<IMetrics>();
_provider = provider;
_parameters = new Parameters(message.Content.Substring(commandParseOffset));
@@ -61,9 +61,8 @@ namespace PluralKit.Bot
public Parameters Parameters => _parameters;
// TODO: this is just here so the extension methods can access it; should it be public/private/?
internal IDataStore DataStore => _data;
internal IDatabase Database => _db;
internal ModelRepository Repository => _repo;
public Task<DiscordMessage> Reply(string text = null, DiscordEmbed embed = null, IEnumerable<IMention> mentions = null)
{

View File

@@ -47,12 +47,14 @@ namespace PluralKit.Bot
// - A @mention of an account connected to the system (<@uid>)
// - A system hid
await using var conn = await ctx.Database.Obtain();
// Direct IDs and mentions are both handled by the below method:
if (input.TryParseMention(out var id))
return await ctx.DataStore.GetSystemByAccount(id);
return await ctx.Repository.GetSystemByAccount(conn, id);
// Finally, try HID parsing
var system = await ctx.DataStore.GetSystemByHid(input);
var system = await ctx.Repository.GetSystemByHid(conn, input);
return system;
}
@@ -67,15 +69,16 @@ namespace PluralKit.Bot
// - a textual display name of a member *in your own system*
// First, if we have a system, try finding by member name in system
if (ctx.System != null && await ctx.DataStore.GetMemberByName(ctx.System, input) is PKMember memberByName)
await using var conn = await ctx.Database.Obtain();
if (ctx.System != null && await ctx.Repository.GetMemberByName(conn, ctx.System.Id, input) is PKMember memberByName)
return memberByName;
// Then, try member HID parsing:
if (await ctx.DataStore.GetMemberByHid(input) is PKMember memberByHid)
if (await ctx.Repository.GetMemberByHid(conn, input) is PKMember memberByHid)
return memberByHid;
// And if that again fails, we try finding a member with a display name matching the argument from the system
if (ctx.System != null && await ctx.DataStore.GetMemberByDisplayName(ctx.System, input) is PKMember memberByDisplayName)
if (ctx.System != null && await ctx.Repository.GetMemberByDisplayName(conn, ctx.System.Id, input) is PKMember memberByDisplayName)
return memberByDisplayName;
// We didn't find anything, so we return null.
@@ -103,9 +106,9 @@ namespace PluralKit.Bot
var input = ctx.PeekArgument();
await using var conn = await ctx.Database.Obtain();
if (ctx.System != null && await conn.QueryGroupByName(ctx.System.Id, input) is {} byName)
if (ctx.System != null && await ctx.Repository.GetGroupByName(conn, ctx.System.Id, input) is {} byName)
return byName;
if (await conn.QueryGroupByHid(input) is {} byHid)
if (await ctx.Repository.GetGroupByHid(conn, input) is {} byHid)
return byHid;
return null;

View File

@@ -36,15 +36,15 @@ namespace PluralKit.Bot
private struct WordPosition
{
// Start of the word
internal int startPos;
internal readonly int startPos;
// End of the word
internal int endPos;
internal readonly int endPos;
// How much to advance word pointer afterwards to point at the start of the *next* word
internal int advanceAfterWord;
internal readonly int advanceAfterWord;
internal bool wasQuoted;
internal readonly bool wasQuoted;
public WordPosition(int startPos, int endPos, int advanceAfterWord, bool wasQuoted)
{