feat(apiv2): reorganize controllers, add stats to meta endpoint
This commit is contained in:
parent
8fe688e4aa
commit
2bf1617737
@ -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
|
||||||
|
@ -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());
|
||||||
|
@ -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()
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
30
PluralKit.API/Controllers/v2/PrivateControllerV2.cs
Normal file
30
PluralKit.API/Controllers/v2/PrivateControllerV2.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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(
|
||||||
|
Loading…
Reference in New Issue
Block a user