2021-10-02 01:50:01 +00:00
|
|
|
using System;
|
2021-10-12 09:17:54 +00:00
|
|
|
using System.Linq;
|
2021-10-02 01:50:01 +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 GroupControllerV2: PKControllerBase
|
|
|
|
{
|
|
|
|
public GroupControllerV2(IServiceProvider svc) : base(svc) { }
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
[HttpGet("systems/{systemRef}/groups")]
|
2021-11-01 04:12:11 +00:00
|
|
|
public async Task<IActionResult> GetSystemGroups(string systemRef, [FromQuery] bool with_members)
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
var system = await ResolveSystem(systemRef);
|
|
|
|
if (system == null)
|
2021-10-13 13:08:17 +00:00
|
|
|
throw Errors.SystemNotFound;
|
2021-10-12 09:17:54 +00:00
|
|
|
|
|
|
|
var ctx = this.ContextFor(system);
|
|
|
|
|
2021-11-01 04:12:11 +00:00
|
|
|
if (with_members && !system.MemberListPrivacy.CanAccess(ctx))
|
|
|
|
throw Errors.UnauthorizedMemberList;
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
if (!system.GroupListPrivacy.CanAccess(User.ContextFor(system)))
|
2021-10-13 13:08:17 +00:00
|
|
|
throw Errors.UnauthorizedGroupList;
|
2021-10-12 09:17:54 +00:00
|
|
|
|
|
|
|
var groups = _repo.GetSystemGroups(system.Id);
|
2021-11-01 04:12:11 +00:00
|
|
|
|
|
|
|
var j_groups = await groups
|
2021-10-12 09:17:54 +00:00
|
|
|
.Where(g => g.Visibility.CanAccess(ctx))
|
2021-11-01 04:12:11 +00:00
|
|
|
.Select(g => g.ToJson(ctx, needsMembersArray: with_members))
|
|
|
|
.ToListAsync();
|
|
|
|
|
2021-11-19 15:18:12 +00:00
|
|
|
if (with_members && !system.MemberListPrivacy.CanAccess(ctx))
|
|
|
|
throw Errors.UnauthorizedMemberList;
|
|
|
|
|
2021-11-01 04:12:11 +00:00
|
|
|
if (with_members && j_groups.Count > 0)
|
|
|
|
{
|
|
|
|
var q = await _repo.GetGroupMemberInfo(await groups.Select(x => x.Id).ToListAsync());
|
|
|
|
|
|
|
|
foreach (var row in q)
|
|
|
|
if (row.MemberVisibility.CanAccess(ctx))
|
|
|
|
((JArray)j_groups.Find(x => x.Value<string>("id") == row.Group)["members"]).Add(row.MemberUuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(j_groups);
|
2021-10-02 01:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("groups")]
|
2021-10-14 13:35:20 +00:00
|
|
|
public async Task<IActionResult> GroupCreate([FromBody] JObject data)
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-10-14 13:35:20 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
|
2021-11-03 06:42:37 +00:00
|
|
|
// Check group cap
|
|
|
|
var existingGroupCount = await _repo.GetSystemGroupCount(system.Id);
|
|
|
|
var groupLimit = system.GroupLimitOverride ?? Limits.MaxGroupCount;
|
|
|
|
if (existingGroupCount >= groupLimit)
|
|
|
|
throw Errors.GroupLimitReached;
|
|
|
|
|
2021-10-14 13:35:20 +00:00
|
|
|
var patch = GroupPatch.FromJson(data);
|
|
|
|
patch.AssertIsValid();
|
|
|
|
if (!patch.Name.IsPresent)
|
|
|
|
patch.Errors.Add(new ValidationError("name", $"Key 'name' is required when creating new group."));
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
|
|
|
|
|
|
|
using var conn = await _db.Obtain();
|
|
|
|
using var tx = await conn.BeginTransactionAsync();
|
|
|
|
|
|
|
|
var newGroup = await _repo.CreateGroup(system.Id, patch.Name.Value, conn);
|
|
|
|
newGroup = await _repo.UpdateGroup(newGroup.Id, patch, conn);
|
|
|
|
|
|
|
|
await tx.CommitAsync();
|
|
|
|
|
|
|
|
return Ok(newGroup.ToJson(LookupContext.ByOwner));
|
2021-10-02 01:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:17:54 +00:00
|
|
|
[HttpGet("groups/{groupRef}")]
|
|
|
|
public async Task<IActionResult> GroupGet(string groupRef)
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-10-12 09:17:54 +00:00
|
|
|
var group = await ResolveGroup(groupRef);
|
|
|
|
if (group == null)
|
2021-10-13 13:08:17 +00:00
|
|
|
throw Errors.GroupNotFound;
|
2021-10-12 09:17:54 +00:00
|
|
|
|
|
|
|
var system = await _repo.GetSystem(group.System);
|
|
|
|
|
|
|
|
return Ok(group.ToJson(this.ContextFor(group), systemStr: system.Hid));
|
2021-10-02 01:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 13:35:20 +00:00
|
|
|
[HttpPatch("groups/{groupRef}")]
|
|
|
|
public async Task<IActionResult> DoGroupPatch(string groupRef, [FromBody] JObject data)
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-10-14 13:35:20 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
var group = await ResolveGroup(groupRef);
|
|
|
|
if (group == null)
|
|
|
|
throw Errors.GroupNotFound;
|
|
|
|
if (group.System != system.Id)
|
|
|
|
throw Errors.NotOwnGroupError;
|
|
|
|
|
|
|
|
var patch = GroupPatch.FromJson(data);
|
|
|
|
|
|
|
|
patch.AssertIsValid();
|
|
|
|
if (patch.Errors.Count > 0)
|
|
|
|
throw new ModelParseError(patch.Errors);
|
|
|
|
|
|
|
|
var newGroup = await _repo.UpdateGroup(group.Id, patch);
|
|
|
|
return Ok(newGroup.ToJson(LookupContext.ByOwner));
|
2021-10-02 01:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:41:38 +00:00
|
|
|
[HttpDelete("groups/{groupRef}")]
|
|
|
|
public async Task<IActionResult> GroupDelete(string groupRef)
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-10-12 10:41:38 +00:00
|
|
|
var group = await ResolveGroup(groupRef);
|
|
|
|
if (group == null)
|
2021-10-13 13:08:17 +00:00
|
|
|
throw Errors.GroupNotFound;
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-10-12 10:41:38 +00:00
|
|
|
var system = await ResolveSystem("@me");
|
|
|
|
if (system.Id != group.System)
|
2021-10-13 13:08:17 +00:00
|
|
|
throw Errors.NotOwnGroupError;
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-10-12 10:41:38 +00:00
|
|
|
await _repo.DeleteGroup(group.Id);
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
}
|
2021-10-02 01:50:01 +00:00
|
|
|
}
|
|
|
|
}
|