Move system updates to the same patch system as members

This commit is contained in:
Ske
2020-06-29 14:39:19 +02:00
parent c5697b33e2
commit 9c1efc7886
12 changed files with 172 additions and 111 deletions

View File

@@ -26,18 +26,20 @@ namespace PluralKit.API
return o;
}
public static void ApplyJson(this PKSystem system, JObject o)
public static SystemPatch ToSystemPatch(JObject o)
{
if (o.ContainsKey("name")) system.Name = o.Value<string>("name").NullIfEmpty().BoundsCheckField(Limits.MaxSystemNameLength, "System name");
if (o.ContainsKey("description")) system.Description = o.Value<string>("description").NullIfEmpty().BoundsCheckField(Limits.MaxDescriptionLength, "System description");
if (o.ContainsKey("tag")) system.Tag = o.Value<string>("tag").NullIfEmpty().BoundsCheckField(Limits.MaxSystemTagLength, "System tag");
if (o.ContainsKey("avatar_url")) system.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty().BoundsCheckField(Limits.MaxUriLength, "System avatar URL");
if (o.ContainsKey("tz")) system.UiTz = o.Value<string>("tz") ?? "UTC";
var patch = new SystemPatch();
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name").NullIfEmpty().BoundsCheckField(Limits.MaxSystemNameLength, "System name");
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty().BoundsCheckField(Limits.MaxDescriptionLength, "System description");
if (o.ContainsKey("tag")) patch.Tag = o.Value<string>("tag").NullIfEmpty().BoundsCheckField(Limits.MaxSystemTagLength, "System tag");
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty().BoundsCheckField(Limits.MaxUriLength, "System avatar URL");
if (o.ContainsKey("tz")) patch.UiTz = o.Value<string>("tz") ?? "UTC";
if (o.ContainsKey("description_privacy")) system.DescriptionPrivacy = o.Value<string>("description_privacy").ParsePrivacy("description");
if (o.ContainsKey("member_list_privacy")) system.MemberListPrivacy = o.Value<string>("member_list_privacy").ParsePrivacy("member list");
if (o.ContainsKey("front_privacy")) system.FrontPrivacy = o.Value<string>("front_privacy").ParsePrivacy("front");
if (o.ContainsKey("front_history_privacy")) system.FrontHistoryPrivacy = o.Value<string>("front_history_privacy").ParsePrivacy("front history");
if (o.ContainsKey("description_privacy")) patch.DescriptionPrivacy = o.Value<string>("description_privacy").ParsePrivacy("description");
if (o.ContainsKey("member_list_privacy")) patch.MemberListPrivacy = o.Value<string>("member_list_privacy").ParsePrivacy("member list");
if (o.ContainsKey("front_privacy")) patch.FrontPrivacy = o.Value<string>("front_privacy").ParsePrivacy("front");
if (o.ContainsKey("front_history_privacy")) patch.FrontHistoryPrivacy = o.Value<string>("front_history_privacy").ParsePrivacy("front history");
return patch;
}
public static JObject ToJson(this PKMember member, LookupContext ctx)

View File

@@ -41,13 +41,13 @@ namespace PluralKit.API
public class SystemController : ControllerBase
{
private IDataStore _data;
private IDatabase _conn;
private IDatabase _db;
private IAuthorizationService _auth;
public SystemController(IDataStore data, IDatabase conn, IAuthorizationService auth)
public SystemController(IDataStore data, IDatabase db, IAuthorizationService auth)
{
_data = data;
_conn = conn;
_db = db;
_auth = auth;
}
@@ -55,7 +55,7 @@ namespace PluralKit.API
[Authorize]
public async Task<ActionResult<JObject>> GetOwnSystem()
{
var system = await _conn.Execute(c => c.QuerySystem(User.CurrentSystem()));
var system = await _db.Execute(c => c.QuerySystem(User.CurrentSystem()));
return system.ToJson(User.ContextFor(system));
}
@@ -94,7 +94,7 @@ namespace PluralKit.API
var auth = await _auth.AuthorizeAsync(User, system, "ViewFrontHistory");
if (!auth.Succeeded) return StatusCode(StatusCodes.Status403Forbidden, "Unauthorized to view front history.");
using (var conn = await _conn.Obtain())
using (var conn = await _db.Obtain())
{
var res = await conn.QueryAsync<SwitchesReturn>(
@"select *, array(
@@ -132,17 +132,19 @@ namespace PluralKit.API
[Authorize]
public async Task<ActionResult<JObject>> EditSystem([FromBody] JObject changes)
{
var system = await _conn.Execute(c => c.QuerySystem(User.CurrentSystem()));
var system = await _db.Execute(c => c.QuerySystem(User.CurrentSystem()));
SystemPatch patch;
try
{
system.ApplyJson(changes);
patch = JsonModelExt.ToSystemPatch(changes);
}
catch (JsonModelParseError e)
{
return BadRequest(e.Message);
}
await _data.SaveSystem(system);
await _db.Execute(conn => conn.UpdateSystem(system.Id, patch));
return Ok(system.ToJson(User.ContextFor(system)));
}
@@ -166,7 +168,7 @@ namespace PluralKit.API
// Resolve member objects for all given IDs
IEnumerable<PKMember> membersList;
using (var conn = await _conn.Obtain())
using (var conn = await _db.Obtain())
membersList = (await conn.QueryAsync<PKMember>("select * from members where hid = any(@Hids)", new {Hids = param.Members})).ToList();
foreach (var member in membersList)