From 467ce78522d15166f9ff07f49afa8b7b944ee2bc Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 29 Jun 2020 14:54:11 +0200 Subject: [PATCH] Retire more IDataStore methods --- .../Controllers/v1/MemberController.cs | 33 ++++--- PluralKit.Bot/Commands/Member.cs | 6 +- PluralKit.Bot/Services/EmbedService.cs | 10 +- PluralKit.Core/Models/ModelQueryExt.cs | 20 +++- PluralKit.Core/Models/Patch/ModelPatchExt.cs | 7 +- PluralKit.Core/Services/DataFileService.cs | 2 +- PluralKit.Core/Services/IDataStore.cs | 36 +------- PluralKit.Core/Services/PostgresDataStore.cs | 92 ------------------- 8 files changed, 54 insertions(+), 152 deletions(-) diff --git a/PluralKit.API/Controllers/v1/MemberController.cs b/PluralKit.API/Controllers/v1/MemberController.cs index 40875070..4200799e 100644 --- a/PluralKit.API/Controllers/v1/MemberController.cs +++ b/PluralKit.API/Controllers/v1/MemberController.cs @@ -1,5 +1,7 @@ using System.Threading.Tasks; +using Dapper; + using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -15,13 +17,11 @@ namespace PluralKit.API [Route( "v{version:apiVersion}/m" )] public class MemberController: ControllerBase { - private IDataStore _data; private IDatabase _db; private IAuthorizationService _auth; - public MemberController(IDataStore data, IAuthorizationService auth, IDatabase db) + public MemberController(IAuthorizationService auth, IDatabase db) { - _data = data; _auth = auth; _db = db; } @@ -29,7 +29,7 @@ namespace PluralKit.API [HttpGet("{hid}")] public async Task> GetMember(string hid) { - var member = await _data.GetMemberByHid(hid); + var member = await _db.Execute(conn => conn.QueryMemberByHid(hid)); if (member == null) return NotFound("Member not found."); return Ok(member.ToJson(User.ContextFor(member))); @@ -40,16 +40,17 @@ namespace PluralKit.API public async Task> PostMember([FromBody] JObject properties) { var system = User.CurrentSystem(); - if (!properties.ContainsKey("name")) return BadRequest("Member name must be specified."); + await using var conn = await _db.Obtain(); + // Enforce per-system member limit - var memberCount = await _data.GetSystemMemberCount(system, true); + var memberCount = await conn.QuerySingleAsync("select count(*) from members where system = @System", new {System = system}); if (memberCount >= Limits.MaxMemberCount) return BadRequest($"Member limit reached ({memberCount} / {Limits.MaxMemberCount})."); - var member = await _data.CreateMember(system, properties.Value("name")); + var member = await conn.CreateMember(system, properties.Value("name")); MemberPatch patch; try { @@ -60,15 +61,17 @@ namespace PluralKit.API return BadRequest(e.Message); } - var newMember = await _db.Execute(conn => conn.UpdateMember(member.Id, patch)); - return Ok(member.ToJson(User.ContextFor(newMember))); + member = await conn.UpdateMember(member.Id, patch); + return Ok(member.ToJson(User.ContextFor(member))); } [HttpPatch("{hid}")] [Authorize] public async Task> PatchMember(string hid, [FromBody] JObject changes) { - var member = await _data.GetMemberByHid(hid); + await using var conn = await _db.Obtain(); + + var member = await conn.QueryMemberByHid(hid); if (member == null) return NotFound("Member not found."); var res = await _auth.AuthorizeAsync(User, member, "EditMember"); @@ -84,7 +87,7 @@ namespace PluralKit.API return BadRequest(e.Message); } - var newMember = await _db.Execute(conn => conn.UpdateMember(member.Id, patch)); + var newMember = await conn.UpdateMember(member.Id, patch); return Ok(member.ToJson(User.ContextFor(newMember))); } @@ -92,13 +95,15 @@ namespace PluralKit.API [Authorize] public async Task DeleteMember(string hid) { - var member = await _data.GetMemberByHid(hid); + await using var conn = await _db.Obtain(); + + var member = await conn.QueryMemberByHid(hid); if (member == null) return NotFound("Member not found."); var res = await _auth.AuthorizeAsync(User, member, "EditMember"); if (!res.Succeeded) return Unauthorized($"Member '{hid}' is not part of your system."); - - await _data.DeleteMember(member); + + await conn.DeleteMember(member.Id); return Ok(); } } diff --git a/PluralKit.Bot/Commands/Member.cs b/PluralKit.Bot/Commands/Member.cs index fbed4bf1..8fc4e9fe 100644 --- a/PluralKit.Bot/Commands/Member.cs +++ b/PluralKit.Bot/Commands/Member.cs @@ -32,13 +32,15 @@ namespace PluralKit.Bot if (!await ctx.PromptYesNo(msg)) throw new PKError("Member creation cancelled."); } + await using var conn = await _db.Obtain(); + // Enforce per-system member limit - var memberCount = await _data.GetSystemMemberCount(ctx.System.Id, true); + var memberCount = await conn.GetSystemMemberCount(ctx.System.Id); if (memberCount >= Limits.MaxMemberCount) throw Errors.MemberLimitReachedError; // Create the member - var member = await _data.CreateMember(ctx.System.Id, memberName); + var member = await conn.CreateMember(ctx.System.Id, memberName); memberCount++; // Send confirmation and space hint diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 52afa29a..55ef3ebf 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -28,13 +28,15 @@ namespace PluralKit.Bot { - public async Task CreateSystemEmbed(DiscordClient client, PKSystem system, LookupContext ctx) { - var accounts = await _data.GetSystemAccounts(system); - + public async Task 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 users = await Task.WhenAll(accounts.Select(async uid => (await client.GetUserAsync(uid))?.NameAndMention() ?? $"(deleted account {uid})")); - var memberCount = await _data.GetSystemMemberCount(system.Id, false); + var memberCount = await conn.GetSystemMemberCount(system.Id, PrivacyLevel.Public); var eb = new DiscordEmbedBuilder() .WithColor(DiscordUtils.Gray) .WithTitle(system.Name ?? null) diff --git a/PluralKit.Core/Models/ModelQueryExt.cs b/PluralKit.Core/Models/ModelQueryExt.cs index 00128fce..68ab41ad 100644 --- a/PluralKit.Core/Models/ModelQueryExt.cs +++ b/PluralKit.Core/Models/ModelQueryExt.cs @@ -1,20 +1,34 @@ #nullable enable +using System.Collections.Generic; +using System.Text; using System.Threading.Tasks; using Dapper; -using PluralKit.Core; - namespace PluralKit.Core { public static class ModelQueryExt { public static Task QuerySystem(this IPKConnection conn, SystemId id) => conn.QueryFirstOrDefaultAsync("select * from systems where id = @id", new {id}); - + + public static Task GetSystemMemberCount(this IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null) + { + var query = new StringBuilder("select count(*) from members where system = @Id"); + if (privacyFilter != null) + query.Append($" and member_visibility = {(int) privacyFilter.Value}"); + return conn.QuerySingleAsync(query.ToString(), new {Id = id}); + } + + public static Task> GetLinkedAccounts(this IPKConnection conn, SystemId id) => + conn.QueryAsync("select uid from accounts where system = @Id", new {Id = id}); + public static Task QueryMember(this IPKConnection conn, MemberId id) => conn.QueryFirstOrDefaultAsync("select * from members where id = @id", new {id}); + public static Task QueryMemberByHid(this IPKConnection conn, string hid) => + conn.QueryFirstOrDefaultAsync("select * from members where hid = @hid", new {hid = hid.ToLowerInvariant()}); + public static Task QueryOrInsertGuildConfig(this IPKConnection conn, ulong guild) => conn.QueryFirstAsync("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild}); diff --git a/PluralKit.Core/Models/Patch/ModelPatchExt.cs b/PluralKit.Core/Models/Patch/ModelPatchExt.cs index edcc90f6..a87632d5 100644 --- a/PluralKit.Core/Models/Patch/ModelPatchExt.cs +++ b/PluralKit.Core/Models/Patch/ModelPatchExt.cs @@ -16,7 +16,12 @@ namespace PluralKit.Core public static Task DeleteSystem(this IPKConnection conn, SystemId id) => conn.ExecuteAsync("delete from systems where id = @Id", new {Id = id}); - + + public static Task CreateMember(this IPKConnection conn, SystemId system, string memberName) => + conn.QueryFirstAsync( + "insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *", + new {SystemId = system, Name = memberName}); + public static Task UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch) { var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id")) diff --git a/PluralKit.Core/Services/DataFileService.cs b/PluralKit.Core/Services/DataFileService.cs index 9a7dec4a..d38a3d7d 100644 --- a/PluralKit.Core/Services/DataFileService.cs +++ b/PluralKit.Core/Services/DataFileService.cs @@ -131,7 +131,7 @@ namespace PluralKit.Core { // Tally up the members that didn't exist before, and check member count on import // If creating the unmatched members would put us over the member limit, abort before creating any members - var memberCountBefore = await _data.GetSystemMemberCount(system.Id, true); + var memberCountBefore = await conn.GetSystemMemberCount(system.Id); var membersToAdd = data.Members.Count(m => imp.IsNewMember(m.Id, m.Name)); if (memberCountBefore + membersToAdd > Limits.MaxMemberCount) { diff --git a/PluralKit.Core/Services/IDataStore.cs b/PluralKit.Core/Services/IDataStore.cs index a84b860e..68a55d46 100644 --- a/PluralKit.Core/Services/IDataStore.cs +++ b/PluralKit.Core/Services/IDataStore.cs @@ -62,25 +62,13 @@ namespace PluralKit.Core { /// /// The with the given linked account, or null if no system was found. Task GetSystemByAccount(ulong linkedAccount); - - /// - /// Gets a system by its API token. - /// - /// The with the given API token, or null if no corresponding system was found. - Task GetSystemByToken(string apiToken); - + /// /// Gets the Discord account IDs linked to a system. /// /// An enumerable of Discord account IDs linked to this system. Task> GetSystemAccounts(PKSystem system); - /// - /// Gets the member count of a system. - /// - /// Whether the returned count should include private members. - Task GetSystemMemberCount(SystemId system, bool includePrivate); - /// /// Creates a system, auto-generating its corresponding IDs. /// @@ -132,28 +120,6 @@ namespace PluralKit.Core { /// An enumerable of structs representing each member in the system, in no particular order. IAsyncEnumerable GetSystemMembers(PKSystem system, bool orderByName = false); - /// - /// Creates a member, auto-generating its corresponding IDs. - /// - /// The system in which to create the member. - /// The name of the member to create. - /// The created system model. - Task CreateMember(SystemId system, string name); - - /// - /// Saves the information within the given struct to the data store. - /// - Task SaveMember(PKMember member); - - /// - /// Deletes the given member from the database. - /// - /// - /// This will remove this member from any switches it's involved in, as well as all the messages - /// proxied by this member. - /// - Task DeleteMember(PKMember member); - /// /// Gets a message and its information by its ID. /// diff --git a/PluralKit.Core/Services/PostgresDataStore.cs b/PluralKit.Core/Services/PostgresDataStore.cs index a90a1f97..68ac0401 100644 --- a/PluralKit.Core/Services/PostgresDataStore.cs +++ b/PluralKit.Core/Services/PostgresDataStore.cs @@ -18,23 +18,6 @@ namespace PluralKit.Core { _conn = conn; _logger = logger; } - - public async Task> GetConflictingProxies(PKSystem system, ProxyTag tag) - { - using (var conn = await _conn.Obtain()) - // return await conn.QueryAsync("select * from (select *, (unnest(proxy_tags)).prefix as prefix, (unnest(proxy_tags)).suffix as suffix from members where system = @System) as _ where prefix ilike @Prefix and suffix ilike @Suffix", new - // { - // System = system.Id, - // Prefix = tag.Prefix.Replace("%", "\\%") + "%", - // Suffix = "%" + tag.Suffix.Replace("%", "\\%") - // }); - return await conn.QueryAsync("select * from (select *, (unnest(proxy_tags)).prefix as prefix, (unnest(proxy_tags)).suffix as suffix from members where system = @System) as _ where prefix = @Prefix and suffix = @Suffix", new - { - System = system.Id, - Prefix = tag.Prefix, - Suffix = tag.Suffix - }); - } public async Task CreateSystem(string systemName = null) { PKSystem system; @@ -72,35 +55,6 @@ namespace PluralKit.Core { return await conn.QuerySingleOrDefaultAsync("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() }); } - public async Task GetSystemByToken(string token) { - using (var conn = await _conn.Obtain()) - return await conn.QuerySingleOrDefaultAsync("select * from systems where token = @Token", new { Token = token }); - } - - public async Task GetSystemById(int id) - { - using (var conn = await _conn.Obtain()) - return await conn.QuerySingleOrDefaultAsync("select * from systems where id = @Id", new { Id = id }); - } - - public async Task SaveSystem(PKSystem system) { - using (var conn = await _conn.Obtain()) - await conn.ExecuteAsync("update systems set name = @Name, description = @Description, tag = @Tag, avatar_url = @AvatarUrl, token = @Token, ui_tz = @UiTz, description_privacy = @DescriptionPrivacy, member_list_privacy = @MemberListPrivacy, front_privacy = @FrontPrivacy, front_history_privacy = @FrontHistoryPrivacy, pings_enabled = @PingsEnabled where id = @Id", system); - - _logger.Information("Updated system {@System}", system); - } - - public async Task DeleteSystem(PKSystem system) - { - using var conn = await _conn.Obtain(); - - // Fetch the list of accounts *before* deletion so we can cache-bust all of those - var accounts = (await conn.QueryAsync("select uid from accounts where system = @Id", system)).ToArray(); - await conn.ExecuteAsync("delete from systems where id = @Id", system); - - _logger.Information("Deleted system {System}", system.Id); - } - public async Task> GetSystemAccounts(PKSystem system) { using (var conn = await _conn.Obtain()) @@ -113,23 +67,6 @@ namespace PluralKit.Core { await conn.ExecuteAsync("delete from switches where system = @Id", system); } - public async Task CreateMember(SystemId system, string name) { - PKMember member; - using (var conn = await _conn.Obtain()) - member = await conn.QuerySingleAsync("insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *", new { - SystemID = system, - Name = name - }); - - _logger.Information("Created member {Member}", member.Id); - return member; - } - - public async Task GetMemberById(MemberId id) { - using (var conn = await _conn.Obtain()) - return await conn.QuerySingleOrDefaultAsync("select * from members where id = @Id", new { Id = id }); - } - public async Task GetMemberByHid(string hid) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from members where hid = @Hid", new { Hid = hid.ToLower() }); @@ -154,35 +91,6 @@ namespace PluralKit.Core { return _conn.QueryStreamAsync(sql, new { SystemID = system.Id }); } - public async Task SaveMember(PKMember member) { - using (var conn = await _conn.Obtain()) - await conn.ExecuteAsync("update members set name = @Name, display_name = @DisplayName, description = @Description, color = @Color, avatar_url = @AvatarUrl, birthday = @Birthday, pronouns = @Pronouns, proxy_tags = @ProxyTags, keep_proxy = @KeepProxy, member_visibility = @MemberVisibility, description_privacy = @DescriptionPrivacy, name_privacy = @NamePrivacy, avatar_privacy = @AvatarPrivacy, birthday_privacy = @BirthdayPrivacy, pronoun_privacy = @PronounPrivacy, metadata_privacy = @MetadataPrivacy where id = @Id", member); - - _logger.Information("Updated member {@Member}", member); - } - - public async Task DeleteMember(PKMember member) { - using (var conn = await _conn.Obtain()) - await conn.ExecuteAsync("delete from members where id = @Id", member); - - _logger.Information("Deleted member {@Member}", member); - } - - public async Task GetSystemMemberCount(SystemId id, bool includePrivate) - { - var query = "select count(*) from members where system = @Id"; - if (!includePrivate) query += " and member_visibility = 1"; // 1 = public - - using (var conn = await _conn.Obtain()) - return await conn.ExecuteScalarAsync(query, new { id }); - } - - public async Task GetTotalMembers() - { - using (var conn = await _conn.Obtain()) - return await conn.ExecuteScalarAsync("select count(id) from members"); - } - public async Task AddMessage(IPKConnection conn, ulong senderId, ulong guildId, ulong channelId, ulong postedMessageId, ulong triggerMessageId, MemberId proxiedMemberId) { // "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before await conn.ExecuteAsync("insert into messages(mid, guild, channel, member, sender, original_mid) values(@MessageId, @GuildId, @ChannelId, @MemberId, @SenderId, @OriginalMid) on conflict do nothing", new {