Add endpoint for querying messages by ID

This commit is contained in:
Ske 2019-07-17 14:05:44 +02:00
parent e28ed585ce
commit ca73a215c8
2 changed files with 163 additions and 67 deletions

View File

@ -0,0 +1,48 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NodaTime;
namespace PluralKit.API.Controllers
{
public struct MessageReturn
{
[JsonProperty("timestamp")] public Instant Timestamp;
[JsonProperty("id")] public string Id;
[JsonProperty("sender")] public string Sender;
[JsonProperty("channel")] public string Channel;
[JsonProperty("system")] public PKSystem System;
[JsonProperty("member")] public PKMember Member;
}
[ApiController]
[Route("v1/msg")]
[Route("msg")]
public class MessageController: ControllerBase
{
private MessageStore _messages;
public MessageController(MessageStore messages)
{
_messages = messages;
}
[HttpGet("{mid}")]
public async Task<ActionResult<MessageReturn>> GetMessage(ulong mid)
{
var msg = await _messages.Get(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,
System = msg.System
};
}
}
}

View File

@ -53,6 +53,16 @@ The following three models (usually represented in JSON format) represent the va
|timestamp|datetime||
|members|list of id/Member|Is sometimes in plain ID list form (eg. `GET /s/<id>/switches`), sometimes includes the full Member model (eg. `GET /s/<id>/fronters`).|
### Message model
|Key|Type|Notes|
|---|---|---
|timestamp|datetime||
|id|snowflake|The ID of the message sent by the webhook. 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.|
|system|full System object|The system that proxied the message.|
|member|full Member object|The member that proxied the message.|
## Endpoints
### GET /s
@ -298,9 +308,47 @@ Queries a system by its linked Discord account ID (17/18-digit numeric snowflake
}
```
### GET /msg/\<id>
Looks up a proxied message by its message ID. Returns `404 Not Found` if the message ID is invalid or wasn't found (eg. was deleted or not proxied by PK).
#### Example request
GET https://api.pluralkit.me/v1/msg/601014599386398700
#### Example response
```json
{
"timestamp": "2019-07-17T11:37:26.805Z",
"id": "601014599386398700",
"sender": "466378653216014359",
"channel": "471388251102380000",
"system": {
"id": "abcde",
"name": "My System",
"description": "This is my system description. Yay.",
"tag": "[MySys]",
"avatar_url": "https://path/to/avatar/image.png",
"tz": "Europe/Copenhagen",
"created": "2019-01-01T14:30:00.987654Z"
},
"member": {
"id": "qwert",
"name": "Craig Johnson",
"color": "ff7000",
"avatar_url": "https://path/to/avatar/image.png",
"birthday": "1997-07-14",
"pronouns": "he/him or they/them",
"description": "I am Craig, example user extraordinaire.",
"prefix": "[",
"suffix": "]",
"created": "2019-01-01T15:00:00.654321Z"
}
}
```
## Version history
* 2019-07-17
* Add endpoint for querying system by account
* Add endpoint for querying message contents
* 2019-07-10 **(v1)**
* First specified version
* (prehistory)