From caff9c50aa2d1bb71fe07d246337a20ffe00f8a3 Mon Sep 17 00:00:00 2001 From: ambdroid <61042504+ambdroid@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:26:09 -0400 Subject: [PATCH] feat(core): add dispatch for autoproxy events (#621) --- PluralKit.Core/Dispatch/DispatchModels.cs | 3 ++- PluralKit.Core/Dispatch/DispatchService.cs | 24 ++++++++++++++++--- PluralKit.Core/Models/Patch/AutoproxyPatch.cs | 19 +++++++++++++++ docs/content/api/dispatch.md | 1 + docs/content/api/models.md | 2 ++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/PluralKit.Core/Dispatch/DispatchModels.cs b/PluralKit.Core/Dispatch/DispatchModels.cs index ce758a1d..3d870a92 100644 --- a/PluralKit.Core/Dispatch/DispatchModels.cs +++ b/PluralKit.Core/Dispatch/DispatchModels.cs @@ -28,7 +28,8 @@ public enum DispatchEvent UPDATE_SWITCH, DELETE_SWITCH, DELETE_ALL_SWITCHES, - SUCCESSFUL_IMPORT + SUCCESSFUL_IMPORT, + UPDATE_AUTOPROXY, } public struct UpdateDispatchData diff --git a/PluralKit.Core/Dispatch/DispatchService.cs b/PluralKit.Core/Dispatch/DispatchService.cs index 58f88b56..73c7acd6 100644 --- a/PluralKit.Core/Dispatch/DispatchService.cs +++ b/PluralKit.Core/Dispatch/DispatchService.cs @@ -38,10 +38,28 @@ public class DispatchService } } - public Task Dispatch(SystemId systemId, ulong? guildId, ulong? channelId, AutoproxyPatch patch) + public async Task Dispatch(SystemId systemId, ulong? guildId, ulong? channelId, AutoproxyPatch patch) { - // todo - return Task.CompletedTask; + var repo = _provider.Resolve(); + var system = await repo.GetSystem(systemId); + if (system.WebhookUrl == null) + return; + + var memberUuid = patch.AutoproxyMember.IsPresent && patch.AutoproxyMember.Value is MemberId id + ? (await repo.GetMember(id)).Uuid.ToString() + : null; + + var data = new UpdateDispatchData(); + data.Event = DispatchEvent.UPDATE_AUTOPROXY; + data.SigningToken = system.WebhookToken; + data.SystemId = system.Uuid.ToString(); + data.EventData = patch.ToJson(guildId, channelId, memberUuid); + + _logger.Debug( + "Dispatching webhook for system {SystemId} autoproxy update in guild {GuildId}/{ChannelId}", + system.Id, guildId, channelId + ); + await DoPostRequest(system.Id, system.WebhookUrl, data.GetPayloadBody()); } public async Task Dispatch(SystemId systemId, UpdateDispatchData data) diff --git a/PluralKit.Core/Models/Patch/AutoproxyPatch.cs b/PluralKit.Core/Models/Patch/AutoproxyPatch.cs index 66d3c35c..529fda08 100644 --- a/PluralKit.Core/Models/Patch/AutoproxyPatch.cs +++ b/PluralKit.Core/Models/Patch/AutoproxyPatch.cs @@ -44,4 +44,23 @@ public class AutoproxyPatch: PatchObject return p; } + + public JObject ToJson(ulong? guild_id, ulong? channel_id, string? memberId = null) + { + var o = new JObject(); + + o.Add("guild_id", guild_id?.ToString()); + o.Add("channel_id", channel_id?.ToString()); + + if (AutoproxyMode.IsPresent) + o.Add("autoproxy_mode", AutoproxyMode.Value.ToString().ToLower()); + + if (AutoproxyMember.IsPresent) + o.Add("autoproxy_member", memberId); + + if (LastLatchTimestamp.IsPresent) + o.Add("last_latch_timestamp", LastLatchTimestamp.Value.FormatExport()); + + return o; + } } \ No newline at end of file diff --git a/docs/content/api/dispatch.md b/docs/content/api/dispatch.md index 03ff34fb..c26a8a39 100644 --- a/docs/content/api/dispatch.md +++ b/docs/content/api/dispatch.md @@ -55,3 +55,4 @@ PluralKit will send invalid requests to your endpoint, with `PING` event type, o |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| |SUCCESSFUL_IMPORT|some information was successfully imported through the `pk;import` command to your system|null| +|UPDATE_AUTOPROXY|your system's autoproxy in a specific server and/or channel was updated|[autoproxy settings](/api/models#autoproxy-settings-model) with only modified keys| diff --git a/docs/content/api/models.md b/docs/content/api/models.md index c2542b46..81ff9781 100644 --- a/docs/content/api/models.md +++ b/docs/content/api/models.md @@ -134,6 +134,8 @@ Every PluralKit entity has two IDs: a short (5-character) ID and a longer UUID. ### Autoproxy settings model |key|type|notes| |---|---|---| +|?guild_id|snowflake|only sent if the guild ID isn't already known (in dispatch payloads)| +|?channel_id|snowflake|only sent if the channel ID isn't already known (in dispatch payloads)| |autoproxy_mode|[autoproxy mode](#autoproxy-mode-enum)|| |autoproxy_member|?member id|must be `null` if autoproxy_mode is set to `front`| |last_latch_timestamp|?datetime|read-only|