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/systems")]
|
2021-11-27 02:10:56 +00:00
|
|
|
public class SystemControllerV2: PKControllerBase
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public SystemControllerV2(IServiceProvider svc) : base(svc) { }
|
|
|
|
|
|
|
|
[HttpGet("{systemRef}")]
|
|
|
|
public async Task<IActionResult> SystemGet(string systemRef)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null) throw Errors.SystemNotFound;
|
2022-04-20 16:20:03 +00:00
|
|
|
return Ok(system.ToJson(ContextFor(system)));
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 05:01:54 +00:00
|
|
|
[HttpGet("{systemRef}/oembed.json")]
|
|
|
|
public async Task<IActionResult> SystemEmbed(string systemRef)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null)
|
|
|
|
throw Errors.SystemNotFound;
|
|
|
|
|
|
|
|
return Ok(APIJsonExt.EmbedJson(system.Name ?? $"System with ID `{system.Hid}`", "System"));
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:43:16 +00:00
|
|
|
[HttpPatch("{systemRef}")]
|
|
|
|
public async Task<IActionResult> DoSystemPatch(string systemRef, [FromBody] JObject data)
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-12-01 13:43:16 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null) throw Errors.SystemNotFound;
|
|
|
|
if (ContextFor(system) != LookupContext.ByOwner)
|
|
|
|
throw Errors.GenericMissingPermissions;
|
2022-04-20 16:20:03 +00:00
|
|
|
var patch = SystemPatch.FromJSON(data);
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
patch.AssertIsValid();
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
|
|
|
|
|
|
|
var newSystem = await _repo.UpdateSystem(system.Id, patch);
|
2022-04-20 16:20:03 +00:00
|
|
|
return Ok(newSystem.ToJson(LookupContext.ByOwner));
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
2021-11-30 02:35:21 +00:00
|
|
|
|
|
|
|
[HttpGet("{systemRef}/settings")]
|
|
|
|
public async Task<IActionResult> GetSystemSettings(string systemRef)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem(systemRef);
|
2021-12-01 13:43:16 +00:00
|
|
|
if (system == null) throw Errors.SystemNotFound;
|
2021-11-30 02:35:21 +00:00
|
|
|
if (ContextFor(system) != LookupContext.ByOwner)
|
|
|
|
throw Errors.GenericMissingPermissions;
|
|
|
|
|
|
|
|
var config = await _repo.GetSystemConfig(system.Id);
|
|
|
|
return Ok(config.ToJson());
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPatch("{systemRef}/settings")]
|
|
|
|
public async Task<IActionResult> DoSystemSettingsPatch(string systemRef, [FromBody] JObject data)
|
|
|
|
{
|
|
|
|
var system = await ResolveSystem(systemRef);
|
2021-12-01 13:43:16 +00:00
|
|
|
if (system == null) throw Errors.SystemNotFound;
|
2021-11-30 02:35:21 +00:00
|
|
|
if (ContextFor(system) != LookupContext.ByOwner)
|
|
|
|
throw Errors.GenericMissingPermissions;
|
|
|
|
|
|
|
|
var patch = SystemConfigPatch.FromJson(data);
|
|
|
|
|
|
|
|
patch.AssertIsValid();
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
|
|
|
|
|
|
|
var newConfig = await _repo.UpdateSystemConfig(system.Id, patch);
|
|
|
|
return Ok(newConfig.ToJson());
|
|
|
|
}
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|