2021-09-30 02:30:20 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.API;
|
|
|
|
|
|
|
|
[ApiController]
|
2022-01-20 00:25:04 +00:00
|
|
|
[Route("v2")]
|
2021-11-27 02:10:56 +00:00
|
|
|
public class DiscordControllerV2: PKControllerBase
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public DiscordControllerV2(IServiceProvider svc) : base(svc) { }
|
|
|
|
|
|
|
|
|
2021-11-28 23:33:41 +00:00
|
|
|
[HttpGet("systems/{systemRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> SystemGuildGet(string systemRef, ulong guild_id)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-11-28 23:33:41 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (ContextFor(system) != LookupContext.ByOwner)
|
|
|
|
throw Errors.GenericMissingPermissions;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var settings = await _repo.GetSystemGuild(guild_id, system.Id, false);
|
|
|
|
if (settings == null)
|
|
|
|
throw Errors.SystemGuildNotFound;
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2022-03-22 03:43:33 +00:00
|
|
|
return Ok(settings.ToJson());
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2021-11-28 23:33:41 +00:00
|
|
|
[HttpPatch("systems/{systemRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> DoSystemGuildPatch(string systemRef, ulong guild_id, [FromBody] JObject data)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2021-11-28 23:33:41 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (ContextFor(system) != LookupContext.ByOwner)
|
|
|
|
throw Errors.GenericMissingPermissions;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var settings = await _repo.GetSystemGuild(guild_id, system.Id, false);
|
|
|
|
if (settings == null)
|
|
|
|
throw Errors.SystemGuildNotFound;
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2022-03-22 03:43:33 +00:00
|
|
|
var patch = SystemGuildPatch.FromJson(data);
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
patch.AssertIsValid();
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
|
|
|
|
|
|
|
var newSettings = await _repo.UpdateSystemGuild(system.Id, guild_id, patch);
|
2022-03-22 03:43:33 +00:00
|
|
|
return Ok(newSettings.ToJson());
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
[HttpGet("members/{memberRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> MemberGuildGet(string memberRef, ulong guild_id)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var member = await ResolveMember(memberRef);
|
|
|
|
if (member == null)
|
|
|
|
throw Errors.MemberNotFound;
|
|
|
|
if (member.System != system.Id)
|
|
|
|
throw Errors.NotOwnMemberError;
|
|
|
|
|
|
|
|
var settings = await _repo.GetMemberGuild(guild_id, member.Id, false);
|
|
|
|
if (settings == null)
|
|
|
|
throw Errors.MemberGuildNotFound;
|
|
|
|
|
|
|
|
return Ok(settings.ToJson());
|
|
|
|
}
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
[HttpPatch("members/{memberRef}/guilds/{guild_id}")]
|
|
|
|
public async Task<IActionResult> DoMemberGuildPatch(string memberRef, ulong guild_id, [FromBody] JObject data)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var member = await ResolveMember(memberRef);
|
|
|
|
if (member == null)
|
|
|
|
throw Errors.MemberNotFound;
|
|
|
|
if (member.System != system.Id)
|
|
|
|
throw Errors.NotOwnMemberError;
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var settings = await _repo.GetMemberGuild(guild_id, member.Id, false);
|
|
|
|
if (settings == null)
|
|
|
|
throw Errors.MemberGuildNotFound;
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var patch = MemberGuildPatch.FromJson(data);
|
2021-10-13 12:37:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
patch.AssertIsValid();
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
2021-10-13 05:02:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newSettings = await _repo.UpdateMemberGuild(member.Id, guild_id, patch);
|
|
|
|
return Ok(newSettings.ToJson());
|
|
|
|
}
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
[HttpGet("messages/{messageId}")]
|
|
|
|
public async Task<ActionResult<JObject>> MessageGet(ulong messageId)
|
|
|
|
{
|
2022-11-24 06:32:55 +00:00
|
|
|
var messageByOriginal = await _redis.GetOriginalMid(messageId);
|
|
|
|
|
|
|
|
var msg = await _repo.GetFullMessage(messageByOriginal ?? messageId);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (msg == null)
|
|
|
|
throw Errors.MessageNotFound;
|
2021-10-15 11:08:41 +00:00
|
|
|
|
2022-01-11 14:43:55 +00:00
|
|
|
var ctx = msg.System == null ? LookupContext.ByNonOwner : ContextFor(msg.System);
|
2022-04-20 16:20:03 +00:00
|
|
|
return msg.ToJson(ctx);
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
}
|