2019-07-09 18:39:29 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Dapper;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using NodaTime;
|
2019-07-09 22:21:00 +00:00
|
|
|
using PluralKit.Core;
|
2019-07-09 18:39:29 +00:00
|
|
|
|
|
|
|
namespace PluralKit.API.Controllers
|
|
|
|
{
|
|
|
|
public struct SwitchesReturn
|
|
|
|
{
|
|
|
|
[JsonProperty("timestamp")] public Instant Timestamp { get; set; }
|
|
|
|
[JsonProperty("members")] public IEnumerable<string> Members { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct FrontersReturn
|
|
|
|
{
|
|
|
|
[JsonProperty("timestamp")] public Instant Timestamp { get; set; }
|
|
|
|
[JsonProperty("members")] public IEnumerable<PKMember> Members { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct PostSwitchParams
|
|
|
|
{
|
|
|
|
public ICollection<string> Members { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("s")]
|
2019-07-09 22:23:41 +00:00
|
|
|
[Route("v1/s")]
|
2019-07-09 18:39:29 +00:00
|
|
|
public class SystemController : ControllerBase
|
|
|
|
{
|
|
|
|
private SystemStore _systems;
|
|
|
|
private MemberStore _members;
|
|
|
|
private SwitchStore _switches;
|
2019-07-11 19:25:23 +00:00
|
|
|
private DbConnectionFactory _conn;
|
2019-07-09 18:39:29 +00:00
|
|
|
private TokenAuthService _auth;
|
|
|
|
|
2019-07-11 19:25:23 +00:00
|
|
|
public SystemController(SystemStore systems, MemberStore members, SwitchStore switches, DbConnectionFactory conn, TokenAuthService auth)
|
2019-07-09 18:39:29 +00:00
|
|
|
{
|
|
|
|
_systems = systems;
|
|
|
|
_members = members;
|
|
|
|
_switches = switches;
|
|
|
|
_conn = conn;
|
|
|
|
_auth = auth;
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:06:28 +00:00
|
|
|
[HttpGet]
|
|
|
|
[RequiresSystem]
|
|
|
|
public async Task<ActionResult<PKSystem>> GetOwnSystem()
|
|
|
|
{
|
|
|
|
return Ok(_auth.CurrentSystem);
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:39:29 +00:00
|
|
|
[HttpGet("{hid}")]
|
|
|
|
public async Task<ActionResult<PKSystem>> GetSystem(string hid)
|
|
|
|
{
|
|
|
|
var system = await _systems.GetByHid(hid);
|
|
|
|
if (system == null) return NotFound("System not found.");
|
|
|
|
return Ok(system);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{hid}/members")]
|
|
|
|
public async Task<ActionResult<IEnumerable<PKMember>>> GetMembers(string hid)
|
|
|
|
{
|
|
|
|
var system = await _systems.GetByHid(hid);
|
|
|
|
if (system == null) return NotFound("System not found.");
|
|
|
|
|
|
|
|
var members = await _members.GetBySystem(system);
|
|
|
|
return Ok(members);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{hid}/switches")]
|
|
|
|
public async Task<ActionResult<IEnumerable<SwitchesReturn>>> GetSwitches(string hid, [FromQuery(Name = "before")] Instant? before)
|
|
|
|
{
|
2019-07-09 22:23:41 +00:00
|
|
|
if (before == null) before = SystemClock.Instance.GetCurrentInstant();
|
2019-07-09 18:39:29 +00:00
|
|
|
|
|
|
|
var system = await _systems.GetByHid(hid);
|
|
|
|
if (system == null) return NotFound("System not found.");
|
|
|
|
|
2019-07-14 03:23:27 +00:00
|
|
|
using (var conn = await _conn.Obtain())
|
2019-07-11 19:25:23 +00:00
|
|
|
{
|
|
|
|
var res = await conn.QueryAsync<SwitchesReturn>(
|
|
|
|
@"select *, array(
|
2019-07-09 18:39:29 +00:00
|
|
|
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
|
2019-07-11 19:25:23 +00:00
|
|
|
limit 100;", new {System = system.Id, Before = before});
|
|
|
|
return Ok(res);
|
|
|
|
}
|
2019-07-09 18:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{hid}/fronters")]
|
|
|
|
public async Task<ActionResult<FrontersReturn>> GetFronters(string hid)
|
|
|
|
{
|
|
|
|
var system = await _systems.GetByHid(hid);
|
|
|
|
if (system == null) return NotFound("System not found.");
|
|
|
|
|
|
|
|
var sw = await _switches.GetLatestSwitch(system);
|
2019-07-10 10:54:54 +00:00
|
|
|
if (sw == null) return NotFound("System has no registered switches.");
|
|
|
|
|
2019-07-09 18:39:29 +00:00
|
|
|
var members = await _switches.GetSwitchMembers(sw);
|
|
|
|
return Ok(new FrontersReturn
|
|
|
|
{
|
|
|
|
Timestamp = sw.Timestamp,
|
|
|
|
Members = members
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPatch]
|
2019-07-09 22:19:18 +00:00
|
|
|
[RequiresSystem]
|
2019-07-09 18:39:29 +00:00
|
|
|
public async Task<ActionResult<PKSystem>> EditSystem([FromBody] PKSystem newSystem)
|
|
|
|
{
|
|
|
|
var system = _auth.CurrentSystem;
|
|
|
|
|
2019-07-09 22:21:00 +00:00
|
|
|
// Bounds checks
|
|
|
|
if (newSystem.Name.Length > Limits.MaxSystemNameLength)
|
|
|
|
return BadRequest($"System name too long ({newSystem.Name.Length} > {Limits.MaxSystemNameLength}.");
|
|
|
|
if (newSystem.Tag.Length > Limits.MaxSystemTagLength)
|
|
|
|
return BadRequest($"System tag too long ({newSystem.Tag.Length} > {Limits.MaxSystemTagLength}.");
|
|
|
|
if (newSystem.Description.Length > Limits.MaxDescriptionLength)
|
|
|
|
return BadRequest($"System description too long ({newSystem.Description.Length} > {Limits.MaxDescriptionLength}.");
|
|
|
|
|
2019-07-09 18:39:29 +00:00
|
|
|
system.Name = newSystem.Name;
|
|
|
|
system.Description = newSystem.Description;
|
|
|
|
system.Tag = newSystem.Tag;
|
|
|
|
system.AvatarUrl = newSystem.AvatarUrl;
|
|
|
|
system.UiTz = newSystem.UiTz ?? "UTC";
|
|
|
|
|
|
|
|
await _systems.Save(system);
|
|
|
|
return Ok(system);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("switches")]
|
2019-07-09 22:19:18 +00:00
|
|
|
[RequiresSystem]
|
2019-07-09 18:39:29 +00:00
|
|
|
public async Task<IActionResult> PostSwitch([FromBody] PostSwitchParams param)
|
|
|
|
{
|
|
|
|
if (param.Members.Distinct().Count() != param.Members.Count())
|
|
|
|
return BadRequest("Duplicate members in member list.");
|
|
|
|
|
|
|
|
// We get the current switch, if it exists
|
|
|
|
var latestSwitch = await _switches.GetLatestSwitch(_auth.CurrentSystem);
|
2019-07-18 10:09:19 +00:00
|
|
|
if (latestSwitch != null)
|
|
|
|
{
|
|
|
|
var latestSwitchMembers = await _switches.GetSwitchMembers(latestSwitch);
|
|
|
|
|
|
|
|
// Bail if this switch is identical to the latest one
|
|
|
|
if (latestSwitchMembers.Select(m => m.Hid).SequenceEqual(param.Members))
|
|
|
|
return BadRequest("New members identical to existing fronters.");
|
|
|
|
}
|
2019-07-09 18:39:29 +00:00
|
|
|
|
|
|
|
// Resolve member objects for all given IDs
|
2019-07-11 19:25:23 +00:00
|
|
|
IEnumerable<PKMember> membersList;
|
2019-07-14 03:23:27 +00:00
|
|
|
using (var conn = await _conn.Obtain())
|
2019-07-11 19:25:23 +00:00
|
|
|
membersList = (await conn.QueryAsync<PKMember>("select * from members where hid = any(@Hids)", new {Hids = param.Members})).ToList();
|
|
|
|
|
2019-07-09 18:39:29 +00:00
|
|
|
foreach (var member in membersList)
|
|
|
|
if (member.System != _auth.CurrentSystem.Id)
|
|
|
|
return BadRequest($"Cannot switch to member '{member.Hid}' not in system.");
|
|
|
|
|
|
|
|
// membersList is in DB order, and we want it in actual input order
|
|
|
|
// so we go through a dict and map the original input appropriately
|
|
|
|
var membersDict = membersList.ToDictionary(m => m.Hid);
|
|
|
|
|
|
|
|
var membersInOrder = new List<PKMember>();
|
|
|
|
// We do this without .Select() since we want to have the early return bail if it doesn't find the member
|
|
|
|
foreach (var givenMemberId in param.Members)
|
|
|
|
{
|
|
|
|
if (!membersDict.TryGetValue(givenMemberId, out var member)) return BadRequest($"Member '{givenMemberId}' not found.");
|
|
|
|
membersInOrder.Add(member);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, log the switch (yay!)
|
|
|
|
await _switches.RegisterSwitch(_auth.CurrentSystem, membersInOrder);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|