From c31ea56bc9a3ee0258366fd04a14e631c9862437 Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 30 Sep 2021 15:36:01 -0400 Subject: [PATCH 1/8] fix Discord per-guild avatars in replies because Discord changed the URL format, for some reason --- PluralKit.Bot/Proxy/ProxyService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index 74d61293..fa59578c 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -230,7 +230,7 @@ namespace PluralKit.Bot var username = nickname ?? repliedTo.Author.Username; var avatarUrl = avatar != null - ? $"https://cdn.discordapp.com/guilds/{trigger.GuildId}/users/{repliedTo.Author.Id}/{avatar}.png" + ? $"https://cdn.discordapp.com/guilds/{trigger.GuildId}/users/{repliedTo.Author.Id}/avatars/{avatar}.png" : $"https://cdn.discordapp.com/avatars/{repliedTo.Author.Id}/{repliedTo.Author.Avatar}.png"; return new Embed From a546ebc88886d20f1f34da4a465c2763c7074738 Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 3 Oct 2021 04:34:26 -0400 Subject: [PATCH 2/8] fix: check correct length for fronthistory embed --- PluralKit.Bot/Commands/SystemFront.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/SystemFront.cs b/PluralKit.Bot/Commands/SystemFront.cs index 201a258a..b813ced3 100644 --- a/PluralKit.Bot/Commands/SystemFront.cs +++ b/PluralKit.Bot/Commands/SystemFront.cs @@ -100,7 +100,7 @@ namespace PluralKit.Bot $"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago)\n"; } - if (sb.Length + stringToAdd.Length >= 1024) + if (sb.Length + stringToAdd.Length >= 4096) break; sb.Append(stringToAdd); } From 0dd20c64ce826ba83c0ec9abce13de4acef06ffa Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 3 Oct 2021 05:34:33 -0400 Subject: [PATCH 3/8] feat: update lock files --- PluralKit.API/packages.lock.json | 25 +++++++++++++++++++++++++ PluralKit.Bot/packages.lock.json | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/PluralKit.API/packages.lock.json b/PluralKit.API/packages.lock.json index cde6cbf4..dcd71ead 100644 --- a/PluralKit.API/packages.lock.json +++ b/PluralKit.API/packages.lock.json @@ -217,6 +217,11 @@ "System.Diagnostics.DiagnosticSource": "4.5.1" } }, + "Humanizer.Core": { + "type": "Transitive", + "resolved": "2.8.26", + "contentHash": "OiKusGL20vby4uDEswj2IgkdchC1yQ6rwbIkZDVBPIR6al2b7n3pC91elBul9q33KaBgRKhbZH3+2Ur4fnWx2A==" + }, "Microsoft.AspNetCore.JsonPatch": { "type": "Transitive", "resolved": "3.1.0", @@ -796,6 +801,24 @@ "System.Threading.Timer": "4.0.1" } }, + "SqlKata": { + "type": "Transitive", + "resolved": "2.3.7", + "contentHash": "erKffEMhrS2IFKXjYV83M4uc1IOCl91yeP/3uY5yIm6pRNFDNrqnTk3La1en6EGDlMRol9abTNO1erQCYf08tg==", + "dependencies": { + "System.Collections.Concurrent": "4.3.0" + } + }, + "SqlKata.Execution": { + "type": "Transitive", + "resolved": "2.3.7", + "contentHash": "LybTYj99riLRH7YQNt9Kuc8VpZOvaQ7H4sQBrj2zefktS8LASOaXsHRYC/k8NEcj25w6huQpOi+HrEZ5qHXl0w==", + "dependencies": { + "Humanizer.Core": "2.8.26", + "SqlKata": "2.3.7", + "dapper": "1.50.5" + } + }, "Swashbuckle.AspNetCore": { "type": "Transitive", "resolved": "5.0.0", @@ -1575,6 +1598,8 @@ "Serilog.Sinks.Console": "4.0.0-dev-00834", "Serilog.Sinks.Elasticsearch": "8.4.1", "Serilog.Sinks.File": "4.1.0", + "SqlKata": "2.3.7", + "SqlKata.Execution": "2.3.7", "System.Interactive.Async": "5.0.0" } } diff --git a/PluralKit.Bot/packages.lock.json b/PluralKit.Bot/packages.lock.json index be4cd0f8..edb3e3e7 100644 --- a/PluralKit.Bot/packages.lock.json +++ b/PluralKit.Bot/packages.lock.json @@ -623,6 +623,24 @@ "System.Threading.Timer": "4.0.1" } }, + "SqlKata": { + "type": "Transitive", + "resolved": "2.3.7", + "contentHash": "erKffEMhrS2IFKXjYV83M4uc1IOCl91yeP/3uY5yIm6pRNFDNrqnTk3La1en6EGDlMRol9abTNO1erQCYf08tg==", + "dependencies": { + "System.Collections.Concurrent": "4.3.0" + } + }, + "SqlKata.Execution": { + "type": "Transitive", + "resolved": "2.3.7", + "contentHash": "LybTYj99riLRH7YQNt9Kuc8VpZOvaQ7H4sQBrj2zefktS8LASOaXsHRYC/k8NEcj25w6huQpOi+HrEZ5qHXl0w==", + "dependencies": { + "Humanizer.Core": "2.8.26", + "SqlKata": "2.3.7", + "dapper": "1.50.5" + } + }, "System.AppContext": { "type": "Transitive", "resolved": "4.3.0", @@ -1378,6 +1396,8 @@ "Serilog.Sinks.Console": "4.0.0-dev-00834", "Serilog.Sinks.Elasticsearch": "8.4.1", "Serilog.Sinks.File": "4.1.0", + "SqlKata": "2.3.7", + "SqlKata.Execution": "2.3.7", "System.Interactive.Async": "5.0.0" } } From 502265973c0fe93d51099c6d2de0179f782d195d Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 3 Oct 2021 12:02:26 -0400 Subject: [PATCH 4/8] fix: explicitly return * in Patch queries This is needed because the API takes the object from the database query to return to the user --- .../Database/Repository/ModelRepository.Account.cs | 2 +- PluralKit.Core/Database/Repository/ModelRepository.Guild.cs | 6 +++--- .../Database/Repository/ModelRepository.Member.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Account.cs b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs index a0c606dc..bba07815 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Account.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs @@ -10,7 +10,7 @@ namespace PluralKit.Core { _logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch); var query = patch.Apply(new Query("accounts").Where("uid", id)); - await _db.ExecuteQuery(query); + await _db.ExecuteQuery(query, extraSql: "returning *"); } } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Guild.cs b/PluralKit.Core/Database/Repository/ModelRepository.Guild.cs index 538fce39..5eb8ed26 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Guild.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Guild.cs @@ -17,7 +17,7 @@ namespace PluralKit.Core { _logger.Information("Updated guild {GuildId}: {@GuildPatch}", guild, patch); var query = patch.Apply(new Query("servers").Where("id", guild)); - return _db.ExecuteQuery(query); + return _db.ExecuteQuery(query, extraSql: "returning *"); } @@ -37,7 +37,7 @@ namespace PluralKit.Core { _logger.Information("Updated {SystemId} in guild {GuildId}: {@SystemGuildPatch}", system, guild, patch); var query = patch.Apply(new Query("system_guild").Where("system", system).Where("guild", guild)); - return _db.ExecuteQuery(query); + return _db.ExecuteQuery(query, extraSql: "returning *"); } @@ -57,7 +57,7 @@ namespace PluralKit.Core { _logger.Information("Updated {MemberId} in guild {GuildId}: {@MemberGuildPatch}", member, guild, patch); var query = patch.Apply(new Query("member_guild").Where("member", member).Where("guild", guild)); - return _db.ExecuteQuery(query); + return _db.ExecuteQuery(query, extraSql: "returning *"); } } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs index 646a5235..d2cf4e4f 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs @@ -64,7 +64,7 @@ namespace PluralKit.Core { _logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch); var query = patch.Apply(new Query("members").Where("id", id)); - return _db.QueryFirst(conn, query); + return _db.QueryFirst(conn, query, extraSql: "returning *"); } public Task DeleteMember(MemberId id) From ecce906c99f0d1d4cb1e49a2381a8bddf392650b Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 5 Oct 2021 03:17:51 -0400 Subject: [PATCH 5/8] fix: add missing await --- PluralKit.Bot/Commands/Admin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/Admin.cs b/PluralKit.Bot/Commands/Admin.cs index db563da9..5a5fe9fa 100644 --- a/PluralKit.Bot/Commands/Admin.cs +++ b/PluralKit.Bot/Commands/Admin.cs @@ -77,7 +77,7 @@ namespace PluralKit.Bot if (!Regex.IsMatch(newHid, "^[a-z]{5}$")) throw new PKError($"Invalid new group ID `{newHid}`."); - var existingGroup = _repo.GetGroupByHid(newHid); + var existingGroup = await _repo.GetGroupByHid(newHid); if (existingGroup != null) throw new PKError($"Another group already exists with ID `{newHid}`."); From 92c1b4de561da3987733a6375f1c6cc1374ce31b Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 6 Oct 2021 19:46:49 -0400 Subject: [PATCH 6/8] feat: add reaction checkmark on command message delete in DMs --- PluralKit.Bot/Commands/Message.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 6f7ba28f..0f8bc225 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -150,6 +150,9 @@ namespace PluralKit.Bot if (ctx.Guild != null) await ctx.Rest.DeleteMessage(ctx.Message); + else + await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = Emojis.Success }); + return; } if (ctx.Match("author") || ctx.MatchFlag("author")) From 655d0d6c448f6198788a72aa6066577358596760 Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 6 Oct 2021 19:49:11 -0400 Subject: [PATCH 7/8] fix: add missing await --- PluralKit.Bot/Commands/Admin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/Admin.cs b/PluralKit.Bot/Commands/Admin.cs index 5a5fe9fa..984b0919 100644 --- a/PluralKit.Bot/Commands/Admin.cs +++ b/PluralKit.Bot/Commands/Admin.cs @@ -31,7 +31,7 @@ namespace PluralKit.Bot if (!Regex.IsMatch(newHid, "^[a-z]{5}$")) throw new PKError($"Invalid new system ID `{newHid}`."); - var existingSystem = _repo.GetSystemByHid(newHid); + var existingSystem = await _repo.GetSystemByHid(newHid); if (existingSystem != null) throw new PKError($"Another system already exists with ID `{newHid}`."); From 2e89310129b118fa355b74411f87076244a29cbc Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 6 Oct 2021 20:24:29 -0400 Subject: [PATCH 8/8] fix(import): add AddAccount to tx --- PluralKit.Core/Database/Database.cs | 10 ++++++++++ PluralKit.Core/Database/IDatabase.cs | 1 + .../Database/Repository/ModelRepository.System.cs | 4 ++-- PluralKit.Core/Utils/BulkImporter/BulkImporter.cs | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/PluralKit.Core/Database/Database.cs b/PluralKit.Core/Database/Database.cs index de98f468..679674b3 100644 --- a/PluralKit.Core/Database/Database.cs +++ b/PluralKit.Core/Database/Database.cs @@ -186,6 +186,16 @@ namespace PluralKit.Core return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings); } + public async Task ExecuteQuery(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = "") + { + if (conn == null) + return await ExecuteQuery(q, extraSql, queryName); + + var query = _compiler.Compile(q); + using (_metrics.Measure.Timer.Time(CoreMetrics.DatabaseQuery, new MetricTags("Query", queryName))) + return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings); + } + public async Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = "") { var query = _compiler.Compile(q); diff --git a/PluralKit.Core/Database/IDatabase.cs b/PluralKit.Core/Database/IDatabase.cs index 6c48d6be..ead9458c 100644 --- a/PluralKit.Core/Database/IDatabase.cs +++ b/PluralKit.Core/Database/IDatabase.cs @@ -15,6 +15,7 @@ namespace PluralKit.Core Task Execute(Func> func); IAsyncEnumerable Execute(Func> func); Task ExecuteQuery(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); + Task ExecuteQuery(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task QueryFirst(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task QueryFirst(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task> Query(Query q, [CallerMemberName] string queryName = ""); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.System.cs b/PluralKit.Core/Database/Repository/ModelRepository.System.cs index a9a48e28..805892c9 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.System.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.System.cs @@ -88,7 +88,7 @@ namespace PluralKit.Core return _db.QueryFirst(conn, query, extraSql: "returning *"); } - public Task AddAccount(SystemId system, ulong accountId) + public Task AddAccount(SystemId system, ulong accountId, IPKConnection? conn = null) { // We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent // This is used in import/export, although the pk;link command checks for this case beforehand @@ -100,7 +100,7 @@ namespace PluralKit.Core }); _logger.Information("Linked account {UserId} to {SystemId}", accountId, system); - return _db.ExecuteQuery(query, extraSql: "on conflict do nothing"); + return _db.ExecuteQuery(conn, query, extraSql: "on conflict do nothing"); } public async Task RemoveAccount(SystemId system, ulong accountId) diff --git a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs index 5ad0297e..c7bae342 100644 --- a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs +++ b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs @@ -49,7 +49,7 @@ namespace PluralKit.Core if (system == null) { system = await repo.CreateSystem(null, importer._conn); - await repo.AddAccount(system.Id, userId); + await repo.AddAccount(system.Id, userId, importer._conn); importer._result.CreatedSystem = system.Hid; importer._system = system; }