2021-09-30 02:30:20 +00:00
|
|
|
using System;
|
2021-10-12 09:17:54 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-09-30 02:30:20 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
using Dapper;
|
|
|
|
|
2021-09-30 02:30:20 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
using Newtonsoft.Json;
|
2021-09-30 02:30:20 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
2021-09-30 02:30:20 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.API
|
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
public struct SwitchesReturnNew
|
|
|
|
{
|
|
|
|
[JsonProperty("timestamp")] public Instant Timestamp { get; set; }
|
|
|
|
[JsonProperty("id")] public Guid Uuid { get; set; }
|
|
|
|
[JsonProperty("members")] public IEnumerable<string> Members { get; set; }
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:30:20 +00:00
|
|
|
[ApiController]
|
|
|
|
[ApiVersion("2.0")]
|
|
|
|
[Route("v{version:apiVersion}")]
|
|
|
|
public class SwitchControllerV2: PKControllerBase
|
|
|
|
{
|
|
|
|
public SwitchControllerV2(IServiceProvider svc) : base(svc) { }
|
|
|
|
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
[HttpGet("systems/{systemRef}/switches")]
|
|
|
|
public async Task<IActionResult> GetSystemSwitches(string systemRef, [FromQuery(Name = "before")] Instant? before, [FromQuery(Name = "limit")] int? limit)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null)
|
|
|
|
throw APIErrors.SystemNotFound;
|
|
|
|
|
|
|
|
var ctx = this.ContextFor(system);
|
|
|
|
|
|
|
|
if (!system.FrontHistoryPrivacy.CanAccess(ctx))
|
|
|
|
throw APIErrors.UnauthorizedFrontHistory;
|
|
|
|
|
|
|
|
if (before == null)
|
|
|
|
before = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
|
|
|
|
if (limit == null || limit > 100)
|
|
|
|
limit = 100;
|
|
|
|
|
|
|
|
var res = await _db.Execute(conn => conn.QueryAsync<SwitchesReturnNew>(
|
|
|
|
@"select *, array(
|
|
|
|
select members.hid from switch_members, members
|
|
|
|
where switch_members.switch = switches.id and members.id = switch_members.member
|
|
|
|
) as members from switches
|
|
|
|
where switches.system = @System and switches.timestamp <= @Before
|
|
|
|
order by switches.timestamp desc
|
|
|
|
limit @Limit;", new { System = system.Id, Before = before, Limit = limit }));
|
|
|
|
return Ok(res);
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
[HttpGet("systems/{systemRef}/fronters")]
|
|
|
|
public async Task<IActionResult> GetSystemFronters(string systemRef)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null)
|
|
|
|
throw APIErrors.SystemNotFound;
|
|
|
|
|
|
|
|
var ctx = this.ContextFor(system);
|
|
|
|
|
|
|
|
if (!system.FrontPrivacy.CanAccess(ctx))
|
|
|
|
throw APIErrors.UnauthorizedCurrentFronters;
|
|
|
|
|
|
|
|
var sw = await _repo.GetLatestSwitch(system.Id);
|
|
|
|
if (sw == null)
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
var members = _db.Execute(conn => _repo.GetSwitchMembers(conn, sw.Id));
|
|
|
|
return Ok(new FrontersReturn
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
Timestamp = sw.Timestamp,
|
|
|
|
Members = await members.Select(m => m.ToJson(ctx, v: APIVersion.V2)).ToListAsync()
|
|
|
|
});
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("systems/{system}/switches")]
|
|
|
|
public async Task<IActionResult> SwitchCreate(string system, [FromBody] JObject data)
|
|
|
|
{
|
|
|
|
return new ObjectResult("Unimplemented")
|
|
|
|
{
|
|
|
|
StatusCode = 501
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
[HttpGet("systems/{systemRef}/switches/{switchRef}")]
|
|
|
|
public async Task<IActionResult> SwitchGet(string systemRef, string switchRef)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
if (!Guid.TryParse(switchRef, out var switchId))
|
|
|
|
throw APIErrors.SwitchNotFound;
|
|
|
|
|
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null)
|
|
|
|
throw APIErrors.SystemNotFound;
|
|
|
|
|
2021-10-12 10:18:54 +00:00
|
|
|
var sw = await _repo.GetSwitchByUuid(switchId);
|
|
|
|
if (sw == null || system.Id != sw.System)
|
|
|
|
throw APIErrors.SwitchNotFound;
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
var ctx = this.ContextFor(system);
|
|
|
|
|
|
|
|
if (!system.FrontHistoryPrivacy.CanAccess(ctx))
|
|
|
|
throw APIErrors.SwitchNotFound;
|
|
|
|
|
|
|
|
var members = _db.Execute(conn => _repo.GetSwitchMembers(conn, sw.Id));
|
|
|
|
return Ok(new FrontersReturn
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
Timestamp = sw.Timestamp,
|
|
|
|
Members = await members.Select(m => m.ToJson(ctx, v: APIVersion.V2)).ToListAsync()
|
|
|
|
});
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPatch("systems/{system}/switches/{switch_id}")]
|
|
|
|
public async Task<IActionResult> SwitchPatch(string system, [FromBody] JObject data)
|
|
|
|
{
|
|
|
|
return new ObjectResult("Unimplemented")
|
|
|
|
{
|
|
|
|
StatusCode = 501
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-12 10:41:38 +00:00
|
|
|
[HttpDelete("systems/@me/switches/{switchRef}")]
|
|
|
|
public async Task<IActionResult> SwitchDelete(string switchRef)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-12 10:41:38 +00:00
|
|
|
if (!Guid.TryParse(switchRef, out var switchId))
|
|
|
|
throw APIErrors.SwitchNotFound;
|
|
|
|
|
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var sw = await _repo.GetSwitchByUuid(switchId);
|
|
|
|
if (sw == null || system.Id != sw.System)
|
|
|
|
throw APIErrors.SwitchNotFound;
|
|
|
|
|
|
|
|
await _repo.DeleteSwitch(sw.Id);
|
|
|
|
|
|
|
|
return NoContent();
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|