2021-09-30 02:30:20 +00:00
|
|
|
using System;
|
|
|
|
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 GuildControllerV2: PKControllerBase
|
|
|
|
{
|
|
|
|
public GuildControllerV2(IServiceProvider svc) : base(svc) { }
|
|
|
|
|
|
|
|
|
2021-10-13 05:02:34 +00:00
|
|
|
[HttpGet("systems/@me/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> SystemGuildGet(ulong guild_id)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var settings = await _repo.GetSystemGuild(guild_id, system.Id, defaultInsert: false);
|
|
|
|
if (settings == null)
|
|
|
|
throw APIErrors.SystemGuildNotFound;
|
|
|
|
|
|
|
|
PKMember member = null;
|
|
|
|
if (settings.AutoproxyMember != null)
|
|
|
|
member = await _repo.GetMember(settings.AutoproxyMember.Value);
|
|
|
|
|
|
|
|
return Ok(settings.ToJson(member?.Hid));
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 05:02:34 +00:00
|
|
|
[HttpPatch("systems/@me/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> DoSystemGuildPatch(ulong guild_id, [FromBody] JObject data)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var settings = await _repo.GetSystemGuild(guild_id, system.Id, defaultInsert: false);
|
|
|
|
if (settings == null)
|
|
|
|
throw APIErrors.SystemGuildNotFound;
|
|
|
|
|
|
|
|
MemberId? memberId = null;
|
|
|
|
if (data.ContainsKey("autoproxy_member"))
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
if (data["autoproxy_member"].Type != JTokenType.Null)
|
|
|
|
{
|
|
|
|
var member = await ResolveMember(data.Value<string>("autoproxy_member"));
|
|
|
|
if (member == null)
|
|
|
|
throw APIErrors.MemberNotFound;
|
|
|
|
|
|
|
|
memberId = member.Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memberId = settings.AutoproxyMember;
|
|
|
|
|
|
|
|
SystemGuildPatch patch = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
patch = SystemGuildPatch.FromJson(data, memberId);
|
|
|
|
patch.AssertIsValid();
|
|
|
|
}
|
|
|
|
catch (ValidationError e)
|
|
|
|
{
|
|
|
|
// todo
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is less than great, but at least it's legible
|
|
|
|
if (patch.AutoproxyMember.Value == null)
|
|
|
|
if (patch.AutoproxyMode.IsPresent)
|
|
|
|
{
|
|
|
|
if (patch.AutoproxyMode.Value == AutoproxyMode.Member)
|
|
|
|
throw APIErrors.MissingAutoproxyMember;
|
|
|
|
}
|
|
|
|
else if (settings.AutoproxyMode == AutoproxyMode.Member)
|
|
|
|
throw APIErrors.MissingAutoproxyMember;
|
|
|
|
|
|
|
|
var newSettings = await _repo.UpdateSystemGuild(system.Id, guild_id, patch);
|
|
|
|
|
|
|
|
PKMember? newMember = null;
|
|
|
|
if (newSettings.AutoproxyMember != null)
|
|
|
|
newMember = await _repo.GetMember(newSettings.AutoproxyMember.Value);
|
|
|
|
return Ok(newSettings.ToJson(newMember?.Hid));
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 05:02:34 +00:00
|
|
|
[HttpGet("members/{memberRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> MemberGuildGet(string memberRef, ulong guild_id)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var member = await ResolveMember(memberRef);
|
|
|
|
if (member == null)
|
|
|
|
throw APIErrors.MemberNotFound;
|
|
|
|
if (member.System != system.Id)
|
|
|
|
throw APIErrors.NotOwnMemberError;
|
|
|
|
|
|
|
|
var settings = await _repo.GetMemberGuild(guild_id, member.Id, defaultInsert: false);
|
|
|
|
if (settings == null)
|
|
|
|
throw APIErrors.MemberGuildNotFound;
|
|
|
|
|
|
|
|
return Ok(settings.ToJson());
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 05:02:34 +00:00
|
|
|
[HttpPatch("members/{memberRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> DoMemberGuildPatch(string memberRef, ulong guild_id, [FromBody] JObject data)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var member = await ResolveMember(memberRef);
|
|
|
|
if (member == null)
|
|
|
|
throw APIErrors.MemberNotFound;
|
|
|
|
if (member.System != system.Id)
|
|
|
|
throw APIErrors.NotOwnMemberError;
|
|
|
|
|
|
|
|
var settings = await _repo.GetMemberGuild(guild_id, member.Id, defaultInsert: false);
|
|
|
|
if (settings == null)
|
|
|
|
throw APIErrors.MemberGuildNotFound;
|
|
|
|
|
|
|
|
MemberGuildPatch patch = null;
|
|
|
|
try
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-10-13 05:02:34 +00:00
|
|
|
patch = MemberGuildPatch.FromJson(data);
|
|
|
|
patch.AssertIsValid();
|
|
|
|
}
|
|
|
|
catch (ValidationError e)
|
|
|
|
{
|
|
|
|
// todo
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
var newSettings = await _repo.UpdateMemberGuild(member.Id, guild_id, patch);
|
|
|
|
return Ok(newSettings.ToJson());
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|