diff --git a/PluralKit.API/Controllers/PKControllerBase.cs b/PluralKit.API/Controllers/PKControllerBase.cs index d2540ab4..a9a3e8d8 100644 --- a/PluralKit.API/Controllers/PKControllerBase.cs +++ b/PluralKit.API/Controllers/PKControllerBase.cs @@ -22,12 +22,14 @@ namespace PluralKit.API protected readonly ApiConfig _config; protected readonly IDatabase _db; protected readonly ModelRepository _repo; + protected readonly DispatchService _dispatch; public PKControllerBase(IServiceProvider svc) { _config = svc.GetRequiredService(); _db = svc.GetRequiredService(); _repo = svc.GetRequiredService(); + _dispatch = svc.GetRequiredService(); } protected Task ResolveSystem(string systemRef) diff --git a/PluralKit.API/Controllers/v2/GroupControllerV2.cs b/PluralKit.API/Controllers/v2/GroupControllerV2.cs index 63f4e6a4..5d177704 100644 --- a/PluralKit.API/Controllers/v2/GroupControllerV2.cs +++ b/PluralKit.API/Controllers/v2/GroupControllerV2.cs @@ -69,6 +69,14 @@ namespace PluralKit.API var newGroup = await _repo.CreateGroup(system.Id, patch.Name.Value, conn); newGroup = await _repo.UpdateGroup(newGroup.Id, patch, conn); + + _ = _dispatch.Dispatch(newGroup.Id, new UpdateDispatchData() + { + Event = DispatchEvent.CREATE_GROUP, + EventData = patch.ToJson(), + }); + + await tx.CommitAsync(); return Ok(newGroup.ToJson(LookupContext.ByOwner)); diff --git a/PluralKit.API/Controllers/v2/MemberControllerV2.cs b/PluralKit.API/Controllers/v2/MemberControllerV2.cs index c7b8c090..eaf81bc3 100644 --- a/PluralKit.API/Controllers/v2/MemberControllerV2.cs +++ b/PluralKit.API/Controllers/v2/MemberControllerV2.cs @@ -55,6 +55,12 @@ namespace PluralKit.API var newMember = await _repo.CreateMember(system.Id, patch.Name.Value, conn); newMember = await _repo.UpdateMember(newMember.Id, patch, conn); + _ = _dispatch.Dispatch(newMember.Id, new() + { + Event = DispatchEvent.CREATE_MEMBER, + EventData = patch.ToJson(), + }); + await tx.CommitAsync(); return Ok(newMember.ToJson(LookupContext.ByOwner, v: APIVersion.V2)); diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index f629c60f..949b85fc 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -10,6 +10,8 @@ using Dapper; using Humanizer; +using Newtonsoft.Json.Linq; + using NodaTime; using Myriad.Builders; @@ -24,13 +26,15 @@ namespace PluralKit.Bot private readonly ModelRepository _repo; private readonly EmbedService _embeds; private readonly HttpClient _client; + private readonly DispatchService _dispatch; - public Groups(IDatabase db, ModelRepository repo, EmbedService embeds, HttpClient client) + public Groups(IDatabase db, ModelRepository repo, EmbedService embeds, HttpClient client, DispatchService dispatch) { _db = db; _repo = repo; _embeds = embeds; _client = client; + _dispatch = dispatch; } public async Task CreateGroup(Context ctx) @@ -59,6 +63,12 @@ namespace PluralKit.Bot var newGroup = await _repo.CreateGroup(ctx.System.Id, groupName); + _ = _dispatch.Dispatch(newGroup.Id, new UpdateDispatchData() + { + Event = DispatchEvent.CREATE_GROUP, + EventData = JObject.FromObject(new { name = groupName }), + }); + var eb = new EmbedBuilder() .Description($"Your new group, **{groupName}**, has been created, with the group ID **`{newGroup.Hid}`**.\nBelow are a couple of useful commands:") .Field(new("View the group card", $"> pk;group **{newGroup.Reference()}**")) diff --git a/PluralKit.Bot/Commands/Member.cs b/PluralKit.Bot/Commands/Member.cs index f46e034b..e600bcc6 100644 --- a/PluralKit.Bot/Commands/Member.cs +++ b/PluralKit.Bot/Commands/Member.cs @@ -21,13 +21,15 @@ namespace PluralKit.Bot private readonly ModelRepository _repo; private readonly EmbedService _embeds; private readonly HttpClient _client; + private readonly DispatchService _dispatch; - public Member(EmbedService embeds, IDatabase db, ModelRepository repo, HttpClient client) + public Member(EmbedService embeds, IDatabase db, ModelRepository repo, HttpClient client, DispatchService dispatch) { _embeds = embeds; _db = db; _repo = repo; _client = client; + _dispatch = dispatch; } public async Task NewMember(Context ctx) @@ -62,12 +64,20 @@ namespace PluralKit.Bot // Try to match an image attached to the message var avatarArg = ctx.Message.Attachments.FirstOrDefault(); Exception imageMatchError = null; + bool sentDispatch = false; if (avatarArg != null) { try { await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Url); - await _repo.UpdateMember(member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }); + await _db.Execute(conn => _repo.UpdateMember(member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }, conn)); + + _ = _dispatch.Dispatch(member.Id, new() + { + Event = DispatchEvent.CREATE_MEMBER, + EventData = JObject.FromObject(new { name = memberName, avatar_url = avatarArg.Url }), + }); + sentDispatch = true; } catch (Exception e) { @@ -75,6 +85,13 @@ namespace PluralKit.Bot } } + if (!sentDispatch) + _ = _dispatch.Dispatch(member.Id, new() + { + Event = DispatchEvent.CREATE_MEMBER, + EventData = JObject.FromObject(new { name = memberName }), + }); + // Send confirmation and space hint await ctx.Reply($"{Emojis.Success} Member \"{memberName}\" (`{member.Hid}`) registered! Check out the getting started page for how to get a member up and running: https://pluralkit.me/start#create-a-member"); // todo: move this to ModelRepository diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs index 4a51968c..f3bc27a3 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs @@ -64,11 +64,6 @@ namespace PluralKit.Core name = name }); var group = await _db.QueryFirst(conn, query, extraSql: "returning *"); - _ = _dispatch.Dispatch(group.Id, new UpdateDispatchData() - { - Event = DispatchEvent.CREATE_GROUP, - EventData = JObject.FromObject(new { name = name }), - }); _logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name); return group; } @@ -78,11 +73,13 @@ namespace PluralKit.Core _logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch); var query = patch.Apply(new Query("groups").Where("id", id)); var group = await _db.QueryFirst(conn, query, extraSql: "returning *"); - _ = _dispatch.Dispatch(id, new() - { - Event = DispatchEvent.UPDATE_GROUP, - EventData = patch.ToJson(), - }); + + if (conn == null) + _ = _dispatch.Dispatch(id, new() + { + Event = DispatchEvent.UPDATE_GROUP, + EventData = patch.ToJson(), + }); return group; } diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs index dc32b7f8..889f550b 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs @@ -69,11 +69,6 @@ namespace PluralKit.Core var member = await _db.QueryFirst(conn, query, "returning *"); _logger.Information("Created {MemberId} in {SystemId}: {MemberName}", member.Id, systemId, memberName); - _ = _dispatch.Dispatch(member.Id, new() - { - Event = DispatchEvent.CREATE_MEMBER, - EventData = JObject.FromObject(new { name = memberName }), - }); return member; } @@ -81,11 +76,13 @@ namespace PluralKit.Core { _logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch); var query = patch.Apply(new Query("members").Where("id", id)); - _ = _dispatch.Dispatch(id, new() - { - Event = DispatchEvent.UPDATE_MEMBER, - EventData = patch.ToJson(), - }); + + if (conn == null) + _ = _dispatch.Dispatch(id, new() + { + Event = DispatchEvent.UPDATE_MEMBER, + EventData = patch.ToJson(), + }); return _db.QueryFirst(conn, query, extraSql: "returning *"); } diff --git a/PluralKit.Core/Dispatch/DispatchModels.cs b/PluralKit.Core/Dispatch/DispatchModels.cs index b1342d46..ce6ed715 100644 --- a/PluralKit.Core/Dispatch/DispatchModels.cs +++ b/PluralKit.Core/Dispatch/DispatchModels.cs @@ -33,6 +33,7 @@ namespace PluralKit.Core UPDATE_SWITCH_MEMBERS, DELETE_SWITCH, DELETE_ALL_SWITCHES, + SUCCESSFUL_IMPORT, } public struct UpdateDispatchData diff --git a/PluralKit.Core/Services/DataFileService.cs b/PluralKit.Core/Services/DataFileService.cs index 7e96f8be..235b10cc 100644 --- a/PluralKit.Core/Services/DataFileService.cs +++ b/PluralKit.Core/Services/DataFileService.cs @@ -18,12 +18,14 @@ namespace PluralKit.Core private readonly IDatabase _db; private readonly ModelRepository _repo; private readonly ILogger _logger; + private readonly DispatchService _dispatch; - public DataFileService(IDatabase db, ModelRepository repo, ILogger logger) + public DataFileService(IDatabase db, ModelRepository repo, ILogger logger, DispatchService dispatch) { _db = db; _repo = repo; _logger = logger; + _dispatch = dispatch; } public async Task ExportSystem(PKSystem system) @@ -72,7 +74,7 @@ namespace PluralKit.Core await using var conn = await _db.Obtain(); await using var tx = await conn.BeginTransactionAsync(); - return await BulkImporter.PerformImport(conn, tx, _repo, _logger, userId, system, importFile, confirmFunc); + return await BulkImporter.PerformImport(conn, tx, _repo, _logger, _dispatch, userId, system, importFile, confirmFunc); } } diff --git a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs index c7bae342..be609196 100644 --- a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs +++ b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs @@ -34,7 +34,7 @@ namespace PluralKit.Core private ImportResultNew _result = new(); internal static async Task PerformImport(IPKConnection conn, IPKTransaction tx, ModelRepository repo, ILogger logger, - ulong userId, PKSystem? system, JObject importFile, Func confirmFunc) + DispatchService dispatch, ulong userId, PKSystem? system, JObject importFile, Func confirmFunc) { await using var importer = new BulkImporter() { @@ -82,6 +82,11 @@ namespace PluralKit.Core throw new ImportException("File type is unknown."); importer._result.Success = true; await tx.CommitAsync(); + + _ = dispatch.Dispatch(system.Id, new UpdateDispatchData() + { + Event = DispatchEvent.SUCCESSFUL_IMPORT + }); } catch (ImportException e) { diff --git a/docs/content/api/dispatch.md b/docs/content/api/dispatch.md index 386d3507..71a336b9 100644 --- a/docs/content/api/dispatch.md +++ b/docs/content/api/dispatch.md @@ -54,4 +54,5 @@ PluralKit will send invalid requests to your endpoint, with `PING` event type, o |UPDATE_SWITCH|a switch was updated|[switch object](/api/models#switch-model) with only modified keys| |UPDATE_SWITCH_MEMBERS|the member list of a switch was updated|list of member IDs| |DELETE_SWITCH|a switch was deleted|null|old switch ID can be found in top-level `id` key| -|DELETE_ALL_SWITCHES|your system's switches were bulk deleted|null| \ No newline at end of file +|DELETE_ALL_SWITCHES|your system's switches were bulk deleted|null| +|SUCCESSFUL_IMPORT|some information was successfully imported through the `pk;import` command to your system|null|