diff --git a/PluralKit.API/APIJsonExt.cs b/PluralKit.API/APIJsonExt.cs index 29f446cf..ee57a500 100644 --- a/PluralKit.API/APIJsonExt.cs +++ b/PluralKit.API/APIJsonExt.cs @@ -35,6 +35,19 @@ namespace PluralKit.API 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 diff --git a/PluralKit.API/Controllers/v1/MetaController.cs b/PluralKit.API/Controllers/v1/MetaController.cs index 8bfae9e9..0ac04974 100644 --- a/PluralKit.API/Controllers/v1/MetaController.cs +++ b/PluralKit.API/Controllers/v1/MetaController.cs @@ -28,7 +28,7 @@ namespace PluralKit.API public async Task> GetMeta() { await using var conn = await _db.Obtain(); - var shards = await _repo.GetShards(conn); + var shards = await _repo.GetShards(); var o = new JObject(); o.Add("shards", shards.ToJSON()); diff --git a/PluralKit.API/Controllers/v2/GuildControllerV2.cs b/PluralKit.API/Controllers/v2/DiscordControllerV2.cs similarity index 82% rename from PluralKit.API/Controllers/v2/GuildControllerV2.cs rename to PluralKit.API/Controllers/v2/DiscordControllerV2.cs index 2f5ffc47..c44fc97d 100644 --- a/PluralKit.API/Controllers/v2/GuildControllerV2.cs +++ b/PluralKit.API/Controllers/v2/DiscordControllerV2.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; +using NodaTime; + using PluralKit.Core; namespace PluralKit.API @@ -120,6 +122,26 @@ namespace PluralKit.API return Ok(newSettings.ToJson()); } + [HttpGet("messages/{messageId}")] + public async Task> 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() + }; + } } } \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/MiscControllerV2.cs b/PluralKit.API/Controllers/v2/MiscControllerV2.cs deleted file mode 100644 index 3fdff9a7..00000000 --- a/PluralKit.API/Controllers/v2/MiscControllerV2.cs +++ /dev/null @@ -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> 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> 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() - }; - } - } -} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/PrivateControllerV2.cs b/PluralKit.API/Controllers/v2/PrivateControllerV2.cs new file mode 100644 index 00000000..ce8c7772 --- /dev/null +++ b/PluralKit.API/Controllers/v2/PrivateControllerV2.cs @@ -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> 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); + } + } +} \ No newline at end of file diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Shards.cs b/PluralKit.Core/Database/Repository/ModelRepository.Shards.cs index d0ac6584..157174ca 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Shards.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Shards.cs @@ -9,8 +9,8 @@ namespace PluralKit.Core { public partial class ModelRepository { - public Task> GetShards(IPKConnection conn) => - conn.QueryAsync("select * from shards order by id"); + public Task> GetShards() => + _db.Execute(conn => conn.QueryAsync("select * from shards order by id")); public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) => conn.ExecuteAsync(