2019-07-09 22:19:18 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2019-07-09 22:19:18 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-12-28 14:52:59 +00:00
|
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2019-07-09 22:19:18 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
namespace PluralKit.API
|
2019-07-09 22:19:18 +00:00
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Route("m")]
|
2019-07-09 22:23:41 +00:00
|
|
|
[Route("v1/m")]
|
2019-07-09 22:19:18 +00:00
|
|
|
public class MemberController: ControllerBase
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
private IDataStore _data;
|
2019-07-09 22:19:18 +00:00
|
|
|
private TokenAuthService _auth;
|
|
|
|
|
2019-10-28 19:15:27 +00:00
|
|
|
public MemberController(IDataStore data, TokenAuthService auth)
|
2019-07-09 22:19:18 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
_data = data;
|
2019-07-09 22:19:18 +00:00
|
|
|
_auth = auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{hid}")]
|
2019-12-28 14:52:59 +00:00
|
|
|
public async Task<ActionResult<JObject>> GetMember(string hid)
|
2019-07-09 22:19:18 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-09 22:19:18 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
2020-01-11 15:49:20 +00:00
|
|
|
return Ok(member.ToJson(_auth.ContextFor(member)));
|
2019-07-09 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 03:37:23 +00:00
|
|
|
[HttpPost]
|
|
|
|
[RequiresSystem]
|
2019-12-28 14:52:59 +00:00
|
|
|
public async Task<ActionResult<JObject>> PostMember([FromBody] JObject properties)
|
2019-07-27 03:37:23 +00:00
|
|
|
{
|
|
|
|
var system = _auth.CurrentSystem;
|
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
if (!properties.ContainsKey("name"))
|
|
|
|
return BadRequest("Member name must be specified.");
|
2019-07-27 03:37:23 +00:00
|
|
|
|
2019-10-20 07:16:57 +00:00
|
|
|
// Enforce per-system member limit
|
2020-01-11 15:49:20 +00:00
|
|
|
var memberCount = await _data.GetSystemMemberCount(system, true);
|
2019-10-20 07:16:57 +00:00
|
|
|
if (memberCount >= Limits.MaxMemberCount)
|
|
|
|
return BadRequest($"Member limit reached ({memberCount} / {Limits.MaxMemberCount}).");
|
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
var member = await _data.CreateMember(system, properties.Value<string>("name"));
|
|
|
|
try
|
|
|
|
{
|
2020-02-12 14:16:19 +00:00
|
|
|
member.ApplyJson(properties);
|
2019-12-28 14:52:59 +00:00
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
catch (JsonModelParseError e)
|
2019-12-28 14:52:59 +00:00
|
|
|
{
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
}
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.SaveMember(member);
|
2020-01-11 15:49:20 +00:00
|
|
|
return Ok(member.ToJson(_auth.ContextFor(member)));
|
2019-07-27 03:37:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:19:18 +00:00
|
|
|
[HttpPatch("{hid}")]
|
|
|
|
[RequiresSystem]
|
2019-12-28 14:52:59 +00:00
|
|
|
public async Task<ActionResult<JObject>> PatchMember(string hid, [FromBody] JObject changes)
|
2019-07-09 22:19:18 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-09 22:19:18 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
|
|
|
if (member.System != _auth.CurrentSystem.Id) return Unauthorized($"Member '{hid}' is not part of your system.");
|
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
try
|
|
|
|
{
|
2020-02-12 14:16:19 +00:00
|
|
|
member.ApplyJson(changes);
|
2019-12-28 14:52:59 +00:00
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
catch (JsonModelParseError e)
|
2019-12-28 14:52:59 +00:00
|
|
|
{
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
}
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.SaveMember(member);
|
2020-01-11 15:49:20 +00:00
|
|
|
return Ok(member.ToJson(_auth.ContextFor(member)));
|
2019-07-09 22:19:18 +00:00
|
|
|
}
|
2019-07-28 22:03:50 +00:00
|
|
|
|
|
|
|
[HttpDelete("{hid}")]
|
|
|
|
[RequiresSystem]
|
2019-12-28 14:52:59 +00:00
|
|
|
public async Task<ActionResult> DeleteMember(string hid)
|
2019-07-28 22:03:50 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
var member = await _data.GetMemberByHid(hid);
|
2019-07-28 22:03:50 +00:00
|
|
|
if (member == null) return NotFound("Member not found.");
|
|
|
|
|
|
|
|
if (member.System != _auth.CurrentSystem.Id) return Unauthorized($"Member '{hid}' is not part of your system.");
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
await _data.DeleteMember(member);
|
2019-07-28 22:03:50 +00:00
|
|
|
return Ok();
|
|
|
|
}
|
2019-07-09 22:19:18 +00:00
|
|
|
}
|
2019-07-28 20:27:54 +00:00
|
|
|
}
|