Merge branch 'main' into feat/apiv2

This commit is contained in:
spiral 2021-10-12 02:33:47 -04:00
commit 9bafc732ab
13 changed files with 71 additions and 12 deletions

View File

@ -217,6 +217,11 @@
"System.Diagnostics.DiagnosticSource": "4.5.1" "System.Diagnostics.DiagnosticSource": "4.5.1"
} }
}, },
"Humanizer.Core": {
"type": "Transitive",
"resolved": "2.8.26",
"contentHash": "OiKusGL20vby4uDEswj2IgkdchC1yQ6rwbIkZDVBPIR6al2b7n3pC91elBul9q33KaBgRKhbZH3+2Ur4fnWx2A=="
},
"Microsoft.AspNetCore.JsonPatch": { "Microsoft.AspNetCore.JsonPatch": {
"type": "Transitive", "type": "Transitive",
"resolved": "3.1.0", "resolved": "3.1.0",
@ -796,6 +801,24 @@
"System.Threading.Timer": "4.0.1" "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": { "Swashbuckle.AspNetCore": {
"type": "Transitive", "type": "Transitive",
"resolved": "5.0.0", "resolved": "5.0.0",
@ -1575,6 +1598,8 @@
"Serilog.Sinks.Console": "4.0.0-dev-00834", "Serilog.Sinks.Console": "4.0.0-dev-00834",
"Serilog.Sinks.Elasticsearch": "8.4.1", "Serilog.Sinks.Elasticsearch": "8.4.1",
"Serilog.Sinks.File": "4.1.0", "Serilog.Sinks.File": "4.1.0",
"SqlKata": "2.3.7",
"SqlKata.Execution": "2.3.7",
"System.Interactive.Async": "5.0.0" "System.Interactive.Async": "5.0.0"
} }
} }

View File

@ -31,7 +31,7 @@ namespace PluralKit.Bot
if (!Regex.IsMatch(newHid, "^[a-z]{5}$")) if (!Regex.IsMatch(newHid, "^[a-z]{5}$"))
throw new PKError($"Invalid new system ID `{newHid}`."); throw new PKError($"Invalid new system ID `{newHid}`.");
var existingSystem = _repo.GetSystemByHid(newHid); var existingSystem = await _repo.GetSystemByHid(newHid);
if (existingSystem != null) if (existingSystem != null)
throw new PKError($"Another system already exists with ID `{newHid}`."); throw new PKError($"Another system already exists with ID `{newHid}`.");
@ -77,7 +77,7 @@ namespace PluralKit.Bot
if (!Regex.IsMatch(newHid, "^[a-z]{5}$")) if (!Regex.IsMatch(newHid, "^[a-z]{5}$"))
throw new PKError($"Invalid new group ID `{newHid}`."); throw new PKError($"Invalid new group ID `{newHid}`.");
var existingGroup = _repo.GetGroupByHid(newHid); var existingGroup = await _repo.GetGroupByHid(newHid);
if (existingGroup != null) if (existingGroup != null)
throw new PKError($"Another group already exists with ID `{newHid}`."); throw new PKError($"Another group already exists with ID `{newHid}`.");

View File

@ -150,6 +150,9 @@ namespace PluralKit.Bot
if (ctx.Guild != null) if (ctx.Guild != null)
await ctx.Rest.DeleteMessage(ctx.Message); await ctx.Rest.DeleteMessage(ctx.Message);
else
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = Emojis.Success });
return; return;
} }
if (ctx.Match("author") || ctx.MatchFlag("author")) if (ctx.Match("author") || ctx.MatchFlag("author"))

View File

@ -100,7 +100,7 @@ namespace PluralKit.Bot
$"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago)\n"; $"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago)\n";
} }
if (sb.Length + stringToAdd.Length >= 1024) if (sb.Length + stringToAdd.Length >= 4096)
break; break;
sb.Append(stringToAdd); sb.Append(stringToAdd);
} }

View File

@ -230,7 +230,7 @@ namespace PluralKit.Bot
var username = nickname ?? repliedTo.Author.Username; var username = nickname ?? repliedTo.Author.Username;
var avatarUrl = avatar != null 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"; : $"https://cdn.discordapp.com/avatars/{repliedTo.Author.Id}/{repliedTo.Author.Avatar}.png";
return new Embed return new Embed

View File

@ -623,6 +623,24 @@
"System.Threading.Timer": "4.0.1" "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": { "System.AppContext": {
"type": "Transitive", "type": "Transitive",
"resolved": "4.3.0", "resolved": "4.3.0",
@ -1378,6 +1396,8 @@
"Serilog.Sinks.Console": "4.0.0-dev-00834", "Serilog.Sinks.Console": "4.0.0-dev-00834",
"Serilog.Sinks.Elasticsearch": "8.4.1", "Serilog.Sinks.Elasticsearch": "8.4.1",
"Serilog.Sinks.File": "4.1.0", "Serilog.Sinks.File": "4.1.0",
"SqlKata": "2.3.7",
"SqlKata.Execution": "2.3.7",
"System.Interactive.Async": "5.0.0" "System.Interactive.Async": "5.0.0"
} }
} }

View File

@ -186,6 +186,16 @@ namespace PluralKit.Core
return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings); return await conn.ExecuteAsync(query.Sql + $" {extraSql}", query.NamedBindings);
} }
public async Task<int> 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<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "") public async Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "")
{ {
var query = _compiler.Compile(q); var query = _compiler.Compile(q);

View File

@ -15,6 +15,7 @@ namespace PluralKit.Core
Task<T> Execute<T>(Func<IPKConnection, Task<T>> func); Task<T> Execute<T>(Func<IPKConnection, Task<T>> func);
IAsyncEnumerable<T> Execute<T>(Func<IPKConnection, IAsyncEnumerable<T>> func); IAsyncEnumerable<T> Execute<T>(Func<IPKConnection, IAsyncEnumerable<T>> func);
Task<int> ExecuteQuery(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task<int> ExecuteQuery(Query q, string extraSql = "", [CallerMemberName] string queryName = "");
Task<int> ExecuteQuery(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = "");
Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task<T> QueryFirst<T>(Query q, string extraSql = "", [CallerMemberName] string queryName = "");
Task<T> QueryFirst<T>(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = ""); Task<T> QueryFirst<T>(IPKConnection? conn, Query q, string extraSql = "", [CallerMemberName] string queryName = "");
Task<IEnumerable<T>> Query<T>(Query q, [CallerMemberName] string queryName = ""); Task<IEnumerable<T>> Query<T>(Query q, [CallerMemberName] string queryName = "");

View File

@ -10,7 +10,7 @@ namespace PluralKit.Core
{ {
_logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch); _logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch);
var query = patch.Apply(new Query("accounts").Where("uid", id)); var query = patch.Apply(new Query("accounts").Where("uid", id));
await _db.ExecuteQuery(query); await _db.ExecuteQuery(query, extraSql: "returning *");
} }
} }
} }

View File

@ -17,7 +17,7 @@ namespace PluralKit.Core
{ {
_logger.Information("Updated guild {GuildId}: {@GuildPatch}", guild, patch); _logger.Information("Updated guild {GuildId}: {@GuildPatch}", guild, patch);
var query = patch.Apply(new Query("servers").Where("id", guild)); 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); _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)); 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); _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)); var query = patch.Apply(new Query("member_guild").Where("member", member).Where("guild", guild));
return _db.ExecuteQuery(query); return _db.ExecuteQuery(query, extraSql: "returning *");
} }
} }
} }

View File

@ -64,7 +64,7 @@ namespace PluralKit.Core
{ {
_logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch); _logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch);
var query = patch.Apply(new Query("members").Where("id", id)); var query = patch.Apply(new Query("members").Where("id", id));
return _db.QueryFirst<PKMember>(conn, query); return _db.QueryFirst<PKMember>(conn, query, extraSql: "returning *");
} }
public Task DeleteMember(MemberId id) public Task DeleteMember(MemberId id)

View File

@ -88,7 +88,7 @@ namespace PluralKit.Core
return _db.QueryFirst<PKSystem>(conn, query, extraSql: "returning *"); return _db.QueryFirst<PKSystem>(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 // 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 // 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); _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) public async Task RemoveAccount(SystemId system, ulong accountId)

View File

@ -49,7 +49,7 @@ namespace PluralKit.Core
if (system == null) if (system == null)
{ {
system = await repo.CreateSystem(null, importer._conn); 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._result.CreatedSystem = system.Hid;
importer._system = system; importer._system = system;
} }