PluralKit/PluralKit.API/Controllers/v2/MemberControllerV2.cs

86 lines
2.5 KiB
C#
Raw Normal View History

2021-09-30 02:30:20 +00:00
using System;
using System.Linq;
2021-09-30 02:30:20 +00:00
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using PluralKit.Core;
namespace PluralKit.API
{
[ApiController]
[ApiVersion("2.0")]
[Route("v{version:apiVersion}")]
public class MemberControllerV2: PKControllerBase
{
public MemberControllerV2(IServiceProvider svc) : base(svc) { }
[HttpGet("systems/{systemRef}/members")]
public async Task<IActionResult> GetSystemMembers(string systemRef)
2021-09-30 02:30:20 +00:00
{
var system = await ResolveSystem(systemRef);
if (system == null)
throw APIErrors.SystemNotFound;
var ctx = this.ContextFor(system);
if (!system.MemberListPrivacy.CanAccess(this.ContextFor(system)))
throw APIErrors.UnauthorizedMemberList;
var members = _repo.GetSystemMembers(system.Id);
return Ok(await members
.Where(m => m.MemberVisibility.CanAccess(ctx))
.Select(m => m.ToJson(ctx, v: APIVersion.V2))
.ToListAsync());
2021-09-30 02:30:20 +00:00
}
[HttpPost("members")]
public async Task<IActionResult> MemberCreate([FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpGet("members/{memberRef}")]
public async Task<IActionResult> MemberGet(string memberRef)
2021-09-30 02:30:20 +00:00
{
var member = await ResolveMember(memberRef);
if (member == null)
throw APIErrors.MemberNotFound;
var system = await _repo.GetSystem(member.System);
return Ok(member.ToJson(this.ContextFor(member), systemStr: system.Hid, v: APIVersion.V2));
2021-09-30 02:30:20 +00:00
}
[HttpPatch("members/{member}")]
public async Task<IActionResult> MemberPatch(string member, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
2021-10-12 10:41:38 +00:00
[HttpDelete("members/{memberRef}")]
public async Task<IActionResult> MemberDelete(string memberRef)
2021-09-30 02:30:20 +00:00
{
2021-10-12 10:41:38 +00:00
var member = await ResolveMember(memberRef);
if (member == null)
throw APIErrors.MemberNotFound;
2021-09-30 02:30:20 +00:00
2021-10-12 10:41:38 +00:00
var system = await ResolveSystem("@me");
if (system.Id != member.System)
throw APIErrors.NotOwnMemberError;
2021-09-30 02:30:20 +00:00
2021-10-12 10:41:38 +00:00
await _repo.DeleteMember(member.Id);
return NoContent();
}
2021-09-30 02:30:20 +00:00
}
}