feat: move PKMessage JSON to PluralKit.Core

This commit is contained in:
spiral 2021-11-26 14:23:57 -05:00
parent ddd966ad58
commit a4a5f5c17a
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
8 changed files with 55 additions and 71 deletions

View File

@ -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<ActionResult<MessageReturn>> GetMessage(ulong mid)
public async Task<ActionResult<JObject>> 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);
}
}
}

View File

@ -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<ActionResult<MessageReturn>> MessageGet(ulong messageId)
public async Task<ActionResult<JObject>> 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);
}
}
}

View File

@ -71,21 +71,4 @@ namespace PluralKit.Core
return _db.QueryFirst<PKMessage?>(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;
}
}

View File

@ -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<bool> ValidateUri(string url)
{
IPHostEntry host = null;

View File

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

View File

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

View File

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

View File

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