refactor: move JsonModelExt to PluralKit.Core (in individual model/patch files)

This commit is contained in:
spiral
2021-08-07 18:13:46 -04:00
parent 26c18d2f48
commit 11bd66e8d8
9 changed files with 193 additions and 186 deletions

View File

@@ -1,11 +1,10 @@
using Dapper.Contrib.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
namespace PluralKit.Core {
public readonly struct SystemId: INumericId<SystemId, int>
@@ -34,7 +33,6 @@ namespace PluralKit.Core {
public class PKSystem
{
// Additions here should be mirrored in SystemStore::Save
[Key] public SystemId Id { get; }
public string Hid { get; }
public string Name { get; }
@@ -63,5 +61,25 @@ namespace PluralKit.Core {
{
public static string DescriptionFor(this PKSystem system, LookupContext ctx) =>
system.DescriptionPrivacy.Get(ctx, system.Description);
public static JObject ToJson(this PKSystem system, LookupContext ctx)
{
var o = new JObject();
o.Add("id", system.Hid);
o.Add("name", system.Name);
o.Add("description", system.DescriptionFor(ctx));
o.Add("tag", system.Tag);
o.Add("avatar_url", system.AvatarUrl.TryGetCleanCdnUrl());
o.Add("banner", system.DescriptionPrivacy.Get(ctx, system.BannerImage).TryGetCleanCdnUrl());
o.Add("created", system.Created.FormatExport());
// todo: change this to "timezone"
o.Add("tz", system.UiTz);
// todo: just don't include these if not ByOwner
o.Add("description_privacy", ctx == LookupContext.ByOwner ? system.DescriptionPrivacy.ToJsonString() : null);
o.Add("member_list_privacy", ctx == LookupContext.ByOwner ? system.MemberListPrivacy.ToJsonString() : null);
o.Add("front_privacy", ctx == LookupContext.ByOwner ? system.FrontPrivacy.ToJsonString() : null);
o.Add("front_history_privacy", ctx == LookupContext.ByOwner ? system.FrontHistoryPrivacy.ToJsonString() : null);
return o;
}
}
}