feat(api): add autoproxy endpoints

This commit is contained in:
spiral
2022-06-02 13:32:31 -04:00
parent 67ce371e7e
commit c87979ef03
6 changed files with 152 additions and 6 deletions

View File

@@ -6,7 +6,7 @@ namespace PluralKit.Core;
public partial class ModelRepository
{
public async Task UpdateAutoproxy(SystemId system, ulong? guildId, ulong? channelId, AutoproxyPatch patch)
public Task<AutoproxySettings> UpdateAutoproxy(SystemId system, ulong? guildId, ulong? channelId, AutoproxyPatch patch)
{
var locationStr = guildId != null ? "guild" : (channelId != null ? "channel" : "global");
_logger.Information("Updated autoproxy for {SystemId} in location {location}: {@AutoproxyPatch}", system, locationStr, patch);
@@ -17,7 +17,7 @@ public partial class ModelRepository
.Where("channel_id", channelId ?? 0)
);
_ = _dispatch.Dispatch(system, guildId, channelId, patch);
await _db.ExecuteQuery(query);
return _db.QueryFirst<AutoproxySettings>(query, "returning *");
}
// todo: this might break with differently scoped autoproxy

View File

@@ -16,7 +16,7 @@ public class AutoproxySettings
{
public AutoproxyMode AutoproxyMode { get; }
public MemberId? AutoproxyMember { get; }
public Instant LastLatchTimestamp { get; }
public Instant? LastLatchTimestamp { get; }
}
public static class AutoproxyExt
@@ -27,7 +27,8 @@ public static class AutoproxyExt
// tbd
o.Add("autoproxy_mode", settings.AutoproxyMode.ToString().ToLower());
o.Add("autoproxy_member", memberHid);
o.Add("autoproxy_member", settings.AutoproxyMode == AutoproxyMode.Front ? null : memberHid);
o.Add("last_latch_timestamp", settings.LastLatchTimestamp?.FormatExport());
return o;
}

View File

@@ -1,3 +1,5 @@
using Newtonsoft.Json.Linq;
using NodaTime;
using SqlKata;
@@ -16,4 +18,30 @@ public class AutoproxyPatch: PatchObject
.With("autoproxy_member", AutoproxyMember)
.With("last_latch_timestamp", LastLatchTimestamp)
);
public new void AssertIsValid()
{
// this is checked in FromJson
// not really the best way to do this, maybe fix at some point?
if ((int?)AutoproxyMode.Value == -1)
Errors.Add(new("autoproxy_mode"));
}
public static AutoproxyPatch FromJson(JObject o, MemberId? autoproxyMember = null)
{
var p = new AutoproxyPatch();
if (o.ContainsKey("autoproxy_mode"))
{
var (autoproxyMode, error) = o.Value<JToken>("autoproxy_mode").ParseAutoproxyMode();
if (error != null)
p.AutoproxyMode = Partial<AutoproxyMode>.Present((AutoproxyMode)(-1));
else
p.AutoproxyMode = autoproxyMode.Value;
}
p.AutoproxyMember = autoproxyMember ?? Partial<MemberId?>.Absent;
return p;
}
}