Major database refactor (again)
This commit is contained in:
@@ -7,7 +7,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class CpuStatService
|
||||
{
|
||||
private ILogger _logger;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public double LastCpuMeasure { get; private set; }
|
||||
|
||||
|
@@ -15,40 +15,38 @@ using PluralKit.Core;
|
||||
namespace PluralKit.Bot {
|
||||
public class EmbedService
|
||||
{
|
||||
private IDataStore _data;
|
||||
private IDatabase _db;
|
||||
private DiscordShardedClient _client;
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly DiscordShardedClient _client;
|
||||
|
||||
public EmbedService(DiscordShardedClient client, IDataStore data, IDatabase db)
|
||||
public EmbedService(DiscordShardedClient client, IDatabase db, ModelRepository repo)
|
||||
{
|
||||
_client = client;
|
||||
_data = data;
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<DiscordEmbed> CreateSystemEmbed(DiscordClient client, PKSystem system, LookupContext ctx)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
// Fetch/render info for all accounts simultaneously
|
||||
var accounts = await conn.GetLinkedAccounts(system.Id);
|
||||
var accounts = await _repo.GetSystemAccounts(conn, system.Id);
|
||||
var users = await Task.WhenAll(accounts.Select(async uid => (await client.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})"));
|
||||
|
||||
var memberCount = await conn.GetSystemMemberCount(system.Id, PrivacyLevel.Public);
|
||||
var memberCount = await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public);
|
||||
var eb = new DiscordEmbedBuilder()
|
||||
.WithColor(DiscordUtils.Gray)
|
||||
.WithTitle(system.Name ?? null)
|
||||
.WithThumbnail(system.AvatarUrl)
|
||||
.WithFooter($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}");
|
||||
|
||||
var latestSwitch = await _data.GetLatestSwitch(system.Id);
|
||||
var latestSwitch = await _repo.GetLatestSwitch(conn, system.Id);
|
||||
if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
|
||||
{
|
||||
var switchMembers = await _data.GetSwitchMembers(latestSwitch).ToListAsync();
|
||||
if (switchMembers.Count > 0)
|
||||
eb.AddField("Fronter".ToQuantity(switchMembers.Count(), ShowQuantityAs.None),
|
||||
var switchMembers = await _repo.GetSwitchMembers(conn, latestSwitch.Id).ToListAsync();
|
||||
if (switchMembers.Count > 0)
|
||||
eb.AddField("Fronter".ToQuantity(switchMembers.Count(), ShowQuantityAs.None),
|
||||
string.Join(", ", switchMembers.Select(m => m.NameFor(ctx))));
|
||||
}
|
||||
|
||||
@@ -105,11 +103,13 @@ namespace PluralKit.Bot {
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
var guildSettings = guild != null ? await conn.QueryOrInsertMemberGuildConfig(guild.Id, member.Id) : null;
|
||||
var guildSettings = guild != null ? await _repo.GetMemberGuild(conn, guild.Id, member.Id) : null;
|
||||
var guildDisplayName = guildSettings?.DisplayName;
|
||||
var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);
|
||||
|
||||
var groups = (await conn.QueryMemberGroups(member.Id)).Where(g => g.Visibility.CanAccess(ctx)).ToList();
|
||||
var groups = await _repo.GetMemberGroups(conn, member.Id)
|
||||
.Where(g => g.Visibility.CanAccess(ctx))
|
||||
.ToListAsync();
|
||||
|
||||
var eb = new DiscordEmbedBuilder()
|
||||
// TODO: add URL of website when that's up
|
||||
@@ -157,7 +157,7 @@ namespace PluralKit.Bot {
|
||||
|
||||
public async Task<DiscordEmbed> CreateFronterEmbed(PKSwitch sw, DateTimeZone zone, LookupContext ctx)
|
||||
{
|
||||
var members = await _data.GetSwitchMembers(sw).ToListAsync();
|
||||
var members = await _db.Execute(c => _repo.GetSwitchMembers(c, sw.Id).ToListAsync().AsTask());
|
||||
var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;
|
||||
return new DiscordEmbedBuilder()
|
||||
.WithColor(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? DiscordUtils.Gray)
|
||||
|
@@ -10,7 +10,7 @@ namespace PluralKit.Bot
|
||||
// TODO: is this still needed after the D#+ migration?
|
||||
public class LastMessageCacheService
|
||||
{
|
||||
private IDictionary<ulong, ulong> _cache = new ConcurrentDictionary<ulong, ulong>();
|
||||
private readonly IDictionary<ulong, ulong> _cache = new ConcurrentDictionary<ulong, ulong>();
|
||||
|
||||
public void AddMessage(ulong channel, ulong message)
|
||||
{
|
||||
|
@@ -15,16 +15,16 @@ namespace PluralKit.Bot {
|
||||
public class LogChannelService {
|
||||
private readonly EmbedService _embed;
|
||||
private readonly IDatabase _db;
|
||||
private readonly IDataStore _data;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly ILogger _logger;
|
||||
private readonly DiscordRestClient _rest;
|
||||
|
||||
public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, IDatabase db, IDataStore data)
|
||||
public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, IDatabase db, ModelRepository repo)
|
||||
{
|
||||
_embed = embed;
|
||||
_rest = rest;
|
||||
_db = db;
|
||||
_data = data;
|
||||
_repo = repo;
|
||||
_logger = logger.ForContext<LogChannelService>();
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ namespace PluralKit.Bot {
|
||||
|
||||
// Send embed!
|
||||
await using var conn = await _db.Obtain();
|
||||
var embed = _embed.CreateLoggedMessageEmbed(await conn.QuerySystem(ctx.SystemId.Value),
|
||||
await conn.QueryMember(proxy.Member.Id), hookMessage, trigger.Id, trigger.Author, proxy.Content,
|
||||
var embed = _embed.CreateLoggedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value),
|
||||
await _repo.GetMember(conn, proxy.Member.Id), hookMessage, trigger.Id, trigger.Author, proxy.Content,
|
||||
trigger.Channel);
|
||||
var url = $"https://discord.com/channels/{trigger.Channel.GuildId}/{trigger.ChannelId}/{hookMessage}";
|
||||
await logChannel.SendMessageFixedAsync(content: url, embed: embed);
|
||||
|
@@ -16,19 +16,19 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class LoggerCleanService
|
||||
{
|
||||
private static Regex _basicRegex = new Regex("(\\d{17,19})");
|
||||
private static Regex _dynoRegex = new Regex("Message ID: (\\d{17,19})");
|
||||
private static Regex _carlRegex = new Regex("ID: (\\d{17,19})");
|
||||
private static Regex _circleRegex = new Regex("\\(`(\\d{17,19})`\\)");
|
||||
private static Regex _loggerARegex = new Regex("Message = (\\d{17,19})");
|
||||
private static Regex _loggerBRegex = new Regex("MessageID:(\\d{17,19})");
|
||||
private static Regex _auttajaRegex = new Regex("Message (\\d{17,19}) deleted");
|
||||
private static Regex _mantaroRegex = new Regex("Message \\(?ID:? (\\d{17,19})\\)? created by .* in channel .* was deleted\\.");
|
||||
private static Regex _pancakeRegex = new Regex("Message from <@(\\d{17,19})> deleted in");
|
||||
private static Regex _unbelievaboatRegex = new Regex("Message ID: (\\d{17,19})");
|
||||
private static Regex _vanessaRegex = new Regex("Message sent by <@!?(\\d{17,19})> deleted in");
|
||||
private static Regex _salRegex = new Regex("\\(ID: (\\d{17,19})\\)");
|
||||
private static Regex _GearBotRegex = new Regex("\\(``(\\d{17,19})``\\) in <#\\d{17,19}> has been removed.");
|
||||
private static readonly Regex _basicRegex = new Regex("(\\d{17,19})");
|
||||
private static readonly Regex _dynoRegex = new Regex("Message ID: (\\d{17,19})");
|
||||
private static readonly Regex _carlRegex = new Regex("ID: (\\d{17,19})");
|
||||
private static readonly Regex _circleRegex = new Regex("\\(`(\\d{17,19})`\\)");
|
||||
private static readonly Regex _loggerARegex = new Regex("Message = (\\d{17,19})");
|
||||
private static readonly Regex _loggerBRegex = new Regex("MessageID:(\\d{17,19})");
|
||||
private static readonly Regex _auttajaRegex = new Regex("Message (\\d{17,19}) deleted");
|
||||
private static readonly Regex _mantaroRegex = new Regex("Message \\(?ID:? (\\d{17,19})\\)? created by .* in channel .* was deleted\\.");
|
||||
private static readonly Regex _pancakeRegex = new Regex("Message from <@(\\d{17,19})> deleted in");
|
||||
private static readonly Regex _unbelievaboatRegex = new Regex("Message ID: (\\d{17,19})");
|
||||
private static readonly Regex _vanessaRegex = new Regex("Message sent by <@!?(\\d{17,19})> deleted in");
|
||||
private static readonly Regex _salRegex = new Regex("\\(ID: (\\d{17,19})\\)");
|
||||
private static readonly Regex _GearBotRegex = new Regex("\\(``(\\d{17,19})``\\) in <#\\d{17,19}> has been removed.");
|
||||
|
||||
private static readonly Dictionary<ulong, LoggerBot> _bots = new[]
|
||||
{
|
||||
@@ -55,7 +55,7 @@ namespace PluralKit.Bot
|
||||
.Where(b => b.WebhookName != null)
|
||||
.ToDictionary(b => b.WebhookName);
|
||||
|
||||
private IDatabase _db;
|
||||
private readonly IDatabase _db;
|
||||
private DiscordShardedClient _client;
|
||||
|
||||
public LoggerCleanService(IDatabase db, DiscordShardedClient client)
|
||||
|
@@ -17,17 +17,17 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class PeriodicStatCollector
|
||||
{
|
||||
private DiscordShardedClient _client;
|
||||
private IMetrics _metrics;
|
||||
private CpuStatService _cpu;
|
||||
private readonly DiscordShardedClient _client;
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly CpuStatService _cpu;
|
||||
|
||||
private IDatabase _db;
|
||||
private readonly IDatabase _db;
|
||||
|
||||
private WebhookCacheService _webhookCache;
|
||||
private readonly WebhookCacheService _webhookCache;
|
||||
|
||||
private DbConnectionCountHolder _countHolder;
|
||||
private readonly DbConnectionCountHolder _countHolder;
|
||||
|
||||
private ILogger _logger;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db)
|
||||
{
|
||||
|
@@ -16,7 +16,6 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class ShardInfoService
|
||||
{
|
||||
|
||||
public class ShardInfo
|
||||
{
|
||||
public bool HasAttachedListeners;
|
||||
@@ -27,10 +26,10 @@ namespace PluralKit.Bot
|
||||
public bool Connected;
|
||||
}
|
||||
|
||||
private IMetrics _metrics;
|
||||
private ILogger _logger;
|
||||
private DiscordShardedClient _client;
|
||||
private Dictionary<int, ShardInfo> _shardInfo = new Dictionary<int, ShardInfo>();
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly ILogger _logger;
|
||||
private readonly DiscordShardedClient _client;
|
||||
private readonly Dictionary<int, ShardInfo> _shardInfo = new Dictionary<int, ShardInfo>();
|
||||
|
||||
public ShardInfoService(ILogger logger, DiscordShardedClient client, IMetrics metrics)
|
||||
{
|
||||
|
@@ -18,11 +18,11 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
||||
|
||||
private DiscordShardedClient _client;
|
||||
private ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>> _webhooks;
|
||||
private readonly DiscordShardedClient _client;
|
||||
private readonly ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>> _webhooks;
|
||||
|
||||
private IMetrics _metrics;
|
||||
private ILogger _logger;
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public WebhookCacheService(DiscordShardedClient client, ILogger logger, IMetrics metrics)
|
||||
{
|
||||
|
@@ -29,10 +29,10 @@ namespace PluralKit.Bot
|
||||
|
||||
public class WebhookExecutorService
|
||||
{
|
||||
private WebhookCacheService _webhookCache;
|
||||
private ILogger _logger;
|
||||
private IMetrics _metrics;
|
||||
private HttpClient _client;
|
||||
private readonly WebhookCacheService _webhookCache;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger, HttpClient client)
|
||||
{
|
||||
|
Reference in New Issue
Block a user