feat(apiv2): reorganize controllers, add stats to meta endpoint

This commit is contained in:
spiral 2021-10-15 07:08:41 -04:00
parent 8fe688e4aa
commit 2bf1617737
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
6 changed files with 68 additions and 58 deletions

View File

@ -35,6 +35,19 @@ namespace PluralKit.API
return o; return o;
} }
public static JObject ToJson(this ModelRepository.Counts counts)
{
var o = new JObject();
o.Add("system_count", counts.SystemCount);
o.Add("member_count", counts.MemberCount);
o.Add("group_count", counts.GroupCount);
o.Add("switch_count", counts.SwitchCount);
o.Add("message_count", counts.MessageCount);
return o;
}
} }
public struct FrontersReturnNew public struct FrontersReturnNew

View File

@ -28,7 +28,7 @@ namespace PluralKit.API
public async Task<ActionResult<JObject>> GetMeta() public async Task<ActionResult<JObject>> GetMeta()
{ {
await using var conn = await _db.Obtain(); await using var conn = await _db.Obtain();
var shards = await _repo.GetShards(conn); var shards = await _repo.GetShards();
var o = new JObject(); var o = new JObject();
o.Add("shards", shards.ToJSON()); o.Add("shards", shards.ToJSON());

View File

@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NodaTime;
using PluralKit.Core; using PluralKit.Core;
namespace PluralKit.API namespace PluralKit.API
@ -120,6 +122,26 @@ namespace PluralKit.API
return Ok(newSettings.ToJson()); return Ok(newSettings.ToJson());
} }
[HttpGet("messages/{messageId}")]
public async Task<ActionResult<MessageReturn>> 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()
};
}
} }
} }

View File

@ -1,55 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NodaTime;
using PluralKit.Core;
namespace PluralKit.API
{
[ApiController]
[ApiVersion("2.0")]
[Route("v{version:apiVersion}")]
public class MetaControllerV2: PKControllerBase
{
public MetaControllerV2(IServiceProvider svc) : base(svc) { }
[HttpGet("meta")]
public async Task<ActionResult<JObject>> Meta()
{
await using var conn = await _db.Obtain();
var shards = await _repo.GetShards(conn);
var o = new JObject();
o.Add("shards", shards.ToJSON());
return Ok(o);
}
[HttpGet("messages/{messageId}")]
public async Task<ActionResult<MessageReturn>> 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()
};
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace PluralKit.API
{
[ApiController]
[ApiVersion("2.0")]
[Route("v{version:apiVersion}")]
public class PrivateControllerV2: PKControllerBase
{
public PrivateControllerV2(IServiceProvider svc) : base(svc) { }
[HttpGet("meta")]
public async Task<ActionResult<JObject>> Meta()
{
var shards = await _repo.GetShards();
var stats = await _repo.GetStats();
var o = new JObject();
o.Add("shards", shards.ToJSON());
o.Add("stats", stats.ToJson());
return Ok(o);
}
}
}

View File

@ -9,8 +9,8 @@ namespace PluralKit.Core
{ {
public partial class ModelRepository public partial class ModelRepository
{ {
public Task<IEnumerable<PKShardInfo>> GetShards(IPKConnection conn) => public Task<IEnumerable<PKShardInfo>> GetShards() =>
conn.QueryAsync<PKShardInfo>("select * from shards order by id"); _db.Execute(conn => conn.QueryAsync<PKShardInfo>("select * from shards order by id"));
public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) => public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) =>
conn.ExecuteAsync( conn.ExecuteAsync(