PluralKit/PluralKit.API/Controllers/v1/MessageController.cs

58 lines
1.8 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
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;
}
2021-08-27 15:03:47 +00:00
[ApiController]
[ApiVersion("1.0")]
2021-08-27 15:03:47 +00:00
[Route("v{version:apiVersion}/msg")]
public class MessageController: ControllerBase
{
2020-08-29 11:46:27 +00:00
private readonly IDatabase _db;
private readonly ModelRepository _repo;
2020-08-29 11:46:27 +00:00
public MessageController(ModelRepository repo, IDatabase db)
{
2020-08-29 11:46:27 +00:00
_repo = repo;
_db = db;
}
[HttpGet("{mid}")]
public async Task<ActionResult<MessageReturn>> GetMessage(ulong mid)
{
2020-08-29 11:46:27 +00:00
var msg = await _db.Execute(c => _repo.GetMessage(c, mid));
if (msg == null) return NotFound("Message not found.");
return new MessageReturn
{
2021-08-27 15:03:47 +00:00
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()
};
2021-08-27 15:03:47 +00:00
}
}
}