feat(core): add dispatch for autoproxy events (#621)

This commit is contained in:
ambdroid 2024-03-13 17:26:09 -04:00 committed by GitHub
parent 44bd0d6079
commit caff9c50aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 45 additions and 4 deletions

View file

@ -28,7 +28,8 @@ public enum DispatchEvent
UPDATE_SWITCH,
DELETE_SWITCH,
DELETE_ALL_SWITCHES,
SUCCESSFUL_IMPORT
SUCCESSFUL_IMPORT,
UPDATE_AUTOPROXY,
}
public struct UpdateDispatchData

View file

@ -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<ModelRepository>();
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)

View file

@ -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;
}
}

View file

@ -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|

View file

@ -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|