From a4a5f5c17ac53db41a836fda8a5120e050cc2fd2 Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 26 Nov 2021 14:23:57 -0500 Subject: [PATCH] feat: move PKMessage JSON to PluralKit.Core --- .../Controllers/v1/MessageController.cs | 25 +----------- .../Controllers/v2/DiscordControllerV2.cs | 19 ++------- .../Repository/ModelRepository.Message.cs | 17 -------- PluralKit.Core/Dispatch/DispatchModels.cs | 14 ------- PluralKit.Core/Dispatch/DispatchService.cs | 9 ++++- PluralKit.Core/Models/PKMessage.cs | 39 +++++++++++++++++++ docs/content/api/dispatch.md | 2 +- docs/content/api/models.md | 1 + 8 files changed, 55 insertions(+), 71 deletions(-) create mode 100644 PluralKit.Core/Models/PKMessage.cs diff --git a/PluralKit.API/Controllers/v1/MessageController.cs b/PluralKit.API/Controllers/v1/MessageController.cs index bc5ad4fe..00e39e01 100644 --- a/PluralKit.API/Controllers/v1/MessageController.cs +++ b/PluralKit.API/Controllers/v1/MessageController.cs @@ -11,18 +11,6 @@ using PluralKit.Core; namespace PluralKit.API { - public struct MessageReturn - { - [JsonProperty("timestamp")] public Instant Timestamp; - [JsonProperty("id")] public string Id; - [JsonProperty("original")] public string Original; - [JsonProperty("sender")] public string Sender; - [JsonProperty("channel")] public string Channel; - - [JsonProperty("system")] public JObject System; - [JsonProperty("member")] public JObject Member; - } - [ApiController] [ApiVersion("1.0")] [Route("v{version:apiVersion}/msg")] @@ -38,21 +26,12 @@ namespace PluralKit.API } [HttpGet("{mid}")] - public async Task> GetMessage(ulong mid) + public async Task> GetMessage(ulong mid) { var msg = await _db.Execute(c => _repo.GetMessage(c, mid)); if (msg == null) return NotFound("Message not found."); - return new MessageReturn - { - Timestamp = Instant.FromUnixTimeMilliseconds((long)(msg.Message.Mid >> 22) + 1420070400000), - Id = msg.Message.Mid.ToString(), - Channel = msg.Message.Channel.ToString(), - Sender = msg.Message.Sender.ToString(), - Member = msg.Member.ToJson(User.ContextFor(msg.System), needsLegacyProxyTags: true), - System = msg.System.ToJson(User.ContextFor(msg.System)), - Original = msg.Message.OriginalMid?.ToString() - }; + return msg.ToJson(User.ContextFor(msg.System), APIVersion.V1); } } } \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/DiscordControllerV2.cs b/PluralKit.API/Controllers/v2/DiscordControllerV2.cs index c44fc97d..6e604302 100644 --- a/PluralKit.API/Controllers/v2/DiscordControllerV2.cs +++ b/PluralKit.API/Controllers/v2/DiscordControllerV2.cs @@ -14,9 +14,9 @@ namespace PluralKit.API [ApiController] [ApiVersion("2.0")] [Route("v{version:apiVersion}")] - public class GuildControllerV2: PKControllerBase + public class DiscordControllerV2: PKControllerBase { - public GuildControllerV2(IServiceProvider svc) : base(svc) { } + public DiscordControllerV2(IServiceProvider svc) : base(svc) { } [HttpGet("systems/@me/guilds/{guild_id}")] @@ -123,25 +123,14 @@ namespace PluralKit.API } [HttpGet("messages/{messageId}")] - public async Task> MessageGet(ulong messageId) + public async Task> MessageGet(ulong messageId) { var msg = await _db.Execute(c => _repo.GetMessage(c, messageId)); if (msg == null) throw Errors.MessageNotFound; var ctx = this.ContextFor(msg.System); - - // todo: don't rely on v1 stuff - return new MessageReturn - { - Timestamp = Instant.FromUnixTimeMilliseconds((long)(msg.Message.Mid >> 22) + 1420070400000), - Id = msg.Message.Mid.ToString(), - Channel = msg.Message.Channel.ToString(), - Sender = msg.Message.Sender.ToString(), - System = msg.System.ToJson(ctx, v: APIVersion.V2), - Member = msg.Member.ToJson(ctx, v: APIVersion.V2), - Original = msg.Message.OriginalMid?.ToString() - }; + return msg.ToJson(ctx, APIVersion.V2); } } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Message.cs b/PluralKit.Core/Database/Repository/ModelRepository.Message.cs index 715f4264..375d42a0 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Message.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Message.cs @@ -71,21 +71,4 @@ namespace PluralKit.Core return _db.QueryFirst(query); } } - - public class PKMessage - { - public ulong Mid { get; set; } - public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added) - public ulong Channel { get; set; } - public MemberId Member { get; set; } - public ulong Sender { get; set; } - public ulong? OriginalMid { get; set; } - } - - public class FullMessage - { - public PKMessage Message; - public PKMember Member; - public PKSystem System; - } } \ No newline at end of file diff --git a/PluralKit.Core/Dispatch/DispatchModels.cs b/PluralKit.Core/Dispatch/DispatchModels.cs index 30977eef..dc389694 100644 --- a/PluralKit.Core/Dispatch/DispatchModels.cs +++ b/PluralKit.Core/Dispatch/DispatchModels.cs @@ -63,20 +63,6 @@ namespace PluralKit.Core return new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json"); } - public static JObject ToDispatchJson(this PKMessage msg, string memberRef) - { - var o = new JObject(); - - o.Add("timestamp", Instant.FromUnixTimeMilliseconds((long)(msg.Mid >> 22) + 1420070400000).FormatExport()); - o.Add("id", msg.Mid.ToString()); - o.Add("original", msg.OriginalMid.ToString()); - o.Add("sender", msg.Sender.ToString()); - o.Add("channel", msg.Channel.ToString()); - o.Add("member", memberRef); - - return o; - } - public static async Task ValidateUri(string url) { IPHostEntry host = null; diff --git a/PluralKit.Core/Dispatch/DispatchService.cs b/PluralKit.Core/Dispatch/DispatchService.cs index c911101b..ac29c9ec 100644 --- a/PluralKit.Core/Dispatch/DispatchService.cs +++ b/PluralKit.Core/Dispatch/DispatchService.cs @@ -139,11 +139,18 @@ namespace PluralKit.Core var member = await repo.GetMember(newMessage.Member); + var fullMessage = new FullMessage + { + Message = newMessage, + Member = member, + System = system + }; + var data = new UpdateDispatchData(); data.Event = DispatchEvent.CREATE_MESSAGE; data.SigningToken = system.WebhookToken; data.SystemId = system.Uuid.ToString(); - data.EventData = newMessage.ToDispatchJson(member.Uuid.ToString()); + data.EventData = fullMessage.ToJson(LookupContext.ByOwner, APIVersion.V2); _logger.Debug("Dispatching webhook for message create (system {SystemId})", system.Id); await DoPostRequest(system.Id, system.WebhookUrl, data.GetPayloadBody()); diff --git a/PluralKit.Core/Models/PKMessage.cs b/PluralKit.Core/Models/PKMessage.cs new file mode 100644 index 00000000..47e66ef3 --- /dev/null +++ b/PluralKit.Core/Models/PKMessage.cs @@ -0,0 +1,39 @@ +using Newtonsoft.Json.Linq; + +using NodaTime; + +namespace PluralKit.Core +{ + public class PKMessage + { + public ulong Mid { get; set; } + public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added) + public ulong Channel { get; set; } + public MemberId Member { get; set; } + public ulong Sender { get; set; } + public ulong? OriginalMid { get; set; } + } + + public class FullMessage + { + public PKMessage Message; + public PKMember Member; + public PKSystem System; + + public JObject ToJson(LookupContext ctx, APIVersion v) + { + var o = new JObject(); + + o.Add("timestamp", Instant.FromUnixTimeMilliseconds((long)(this.Message.Mid >> 22) + 1420070400000).ToString()); + o.Add("id", this.Message.Mid.ToString()); + o.Add("original", this.Message.OriginalMid.ToString()); + o.Add("sender", this.Message.Sender.ToString()); + o.Add("channel", this.Message.Channel.ToString()); + o.Add("guild", this.Message.Guild?.ToString()); + o.Add("system", this.System.ToJson(ctx, v)); + o.Add("member", this.Member.ToJson(ctx, v: v)); + + return o; + } + } +} \ No newline at end of file diff --git a/docs/content/api/dispatch.md b/docs/content/api/dispatch.md index c1299943..a37070eb 100644 --- a/docs/content/api/dispatch.md +++ b/docs/content/api/dispatch.md @@ -32,7 +32,7 @@ PluralKit will send invalid requests to your endpoint, with `PING` event type, o |guild_id|snowflake?*|the ID of the guild where this event occurred| |data|object?|event data| -\* only sent for guild settings update events. in message create events, the guild id is sent in the `data` object +\* only sent for guild settings update events. in message create events, the guild id is sent in the `data` object as `guild` key ## Dispatch Events diff --git a/docs/content/api/models.md b/docs/content/api/models.md index 69fca8d6..fef43bc5 100644 --- a/docs/content/api/models.md +++ b/docs/content/api/models.md @@ -96,6 +96,7 @@ Every PluralKit entity has two IDs: a short (5-character) ID and a longer UUID. |original| snowflake|The ID of the (now-deleted) message that triggered the proxy. Encoded as string for precision reasons.| |sender|snowflake|The user ID of the account that triggered the proxy. Encoded as string for precision reasons.| |channel|snowflake|The ID of the channel the message was sent in. Encoded as string for precision reasons.| +|guild|snowflake|The ID of the server the message was sent in. Encoded as string for precision reasons.| |system|full System object|The system that proxied the message.| |member|full Member object|The member that proxied the message.|