2021-09-27 03:18:17 +00:00
|
|
|
using System;
|
|
|
|
|
2021-08-27 15:03:47 +00:00
|
|
|
using Dapper.Contrib.Extensions;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2021-08-07 22:13:46 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
2021-08-27 15:03:47 +00:00
|
|
|
namespace PluralKit.Core
|
|
|
|
{
|
2021-08-07 20:39:32 +00:00
|
|
|
|
|
|
|
public readonly struct SystemId: INumericId<SystemId, int>
|
|
|
|
{
|
|
|
|
public int Value { get; }
|
|
|
|
|
|
|
|
public SystemId(int value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(SystemId other) => Value == other.Value;
|
|
|
|
|
|
|
|
public override bool Equals(object obj) => obj is SystemId other && Equals(other);
|
|
|
|
|
|
|
|
public override int GetHashCode() => Value;
|
|
|
|
|
|
|
|
public static bool operator ==(SystemId left, SystemId right) => left.Equals(right);
|
|
|
|
|
|
|
|
public static bool operator !=(SystemId left, SystemId right) => !left.Equals(right);
|
|
|
|
|
|
|
|
public int CompareTo(SystemId other) => Value.CompareTo(other.Value);
|
|
|
|
|
|
|
|
public override string ToString() => $"System #{Value}";
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
public class PKSystem
|
|
|
|
{
|
2020-06-14 19:37:04 +00:00
|
|
|
[Key] public SystemId Id { get; }
|
2020-06-13 11:11:08 +00:00
|
|
|
public string Hid { get; }
|
2021-09-27 03:18:17 +00:00
|
|
|
public Guid Uuid { get; private set; }
|
2020-06-29 12:39:19 +00:00
|
|
|
public string Name { get; }
|
|
|
|
public string Description { get; }
|
|
|
|
public string Tag { get; }
|
|
|
|
public string AvatarUrl { get; }
|
2021-08-02 17:46:12 +00:00
|
|
|
public string BannerImage { get; }
|
2021-03-28 10:02:41 +00:00
|
|
|
public string Color { get; }
|
2020-06-29 12:39:19 +00:00
|
|
|
public string Token { get; }
|
2020-06-13 11:11:08 +00:00
|
|
|
public Instant Created { get; }
|
2020-06-13 11:06:41 +00:00
|
|
|
public string UiTz { get; set; }
|
2020-06-29 12:39:19 +00:00
|
|
|
public bool PingsEnabled { get; }
|
2020-12-08 11:57:17 +00:00
|
|
|
public int? LatchTimeout { get; }
|
2021-08-27 15:03:47 +00:00
|
|
|
public PrivacyLevel DescriptionPrivacy { get; }
|
|
|
|
public PrivacyLevel MemberListPrivacy { get; }
|
2020-06-29 12:39:19 +00:00
|
|
|
public PrivacyLevel FrontPrivacy { get; }
|
|
|
|
public PrivacyLevel FrontHistoryPrivacy { get; }
|
2020-08-20 19:43:17 +00:00
|
|
|
public PrivacyLevel GroupListPrivacy { get; }
|
2020-10-09 10:18:29 +00:00
|
|
|
public int? MemberLimitOverride { get; }
|
|
|
|
public int? GroupLimitOverride { get; }
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
|
|
|
|
}
|
2020-08-05 18:24:51 +00:00
|
|
|
|
|
|
|
public static class PKSystemExt
|
|
|
|
{
|
|
|
|
public static string DescriptionFor(this PKSystem system, LookupContext ctx) =>
|
|
|
|
system.DescriptionPrivacy.Get(ctx, system.Description);
|
2021-08-07 22:13:46 +00:00
|
|
|
|
2021-10-12 06:19:42 +00:00
|
|
|
public static JObject ToJson(this PKSystem system, LookupContext ctx, APIVersion v = APIVersion.V1)
|
2021-08-07 22:13:46 +00:00
|
|
|
{
|
|
|
|
var o = new JObject();
|
|
|
|
o.Add("id", system.Hid);
|
2021-10-12 08:25:06 +00:00
|
|
|
if (v == APIVersion.V2)
|
|
|
|
o.Add("uuid", system.Uuid.ToString());
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
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());
|
2021-09-25 19:05:42 +00:00
|
|
|
o.Add("color", system.Color);
|
2021-08-07 22:13:46 +00:00
|
|
|
o.Add("created", system.Created.FormatExport());
|
2021-10-12 06:19:42 +00:00
|
|
|
|
|
|
|
switch (v)
|
|
|
|
{
|
|
|
|
case APIVersion.V1:
|
|
|
|
{
|
|
|
|
o.Add("tz", system.UiTz);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case APIVersion.V2:
|
|
|
|
{
|
|
|
|
o.Add("timezone", system.UiTz);
|
|
|
|
|
|
|
|
if (ctx == LookupContext.ByOwner)
|
|
|
|
{
|
|
|
|
var p = new JObject();
|
|
|
|
|
|
|
|
p.Add("description_privacy", system.DescriptionPrivacy.ToJsonString());
|
|
|
|
p.Add("member_list_privacy", system.MemberListPrivacy.ToJsonString());
|
2021-10-22 15:20:26 +00:00
|
|
|
p.Add("group_list_privacy", system.GroupListPrivacy.ToJsonString());
|
2021-10-12 06:19:42 +00:00
|
|
|
p.Add("front_privacy", system.FrontPrivacy.ToJsonString());
|
|
|
|
p.Add("front_history_privacy", system.FrontHistoryPrivacy.ToJsonString());
|
|
|
|
|
|
|
|
o.Add("privacy", p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
o.Add("privacy", null);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:13:46 +00:00
|
|
|
return o;
|
|
|
|
}
|
2020-08-05 18:24:51 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
}
|