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;
|
|
|
|
using NodaTime.Text;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public readonly struct MemberId: INumericId<MemberId, int>
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public int Value { get; }
|
|
|
|
|
|
|
|
public MemberId(int value)
|
2021-08-07 20:39:32 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
Value = value;
|
|
|
|
}
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public bool Equals(MemberId other) => Value == other.Value;
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override bool Equals(object obj) => obj is MemberId other && Equals(other);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override int GetHashCode() => Value;
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool operator ==(MemberId left, MemberId right) => left.Equals(right);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool operator !=(MemberId left, MemberId right) => !left.Equals(right);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public int CompareTo(MemberId other) => Value.CompareTo(other.Value);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override string ToString() => $"Member #{Value}";
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public class PKMember
|
|
|
|
{
|
|
|
|
// Dapper *can* figure out mapping to getter-only properties, but this doesn't work
|
|
|
|
// when trying to map to *subclasses* (eg. ListedMember). Adding private setters makes it work anyway.
|
|
|
|
public MemberId Id { get; private set; }
|
|
|
|
public string Hid { get; private set; }
|
|
|
|
public Guid Uuid { get; private set; }
|
|
|
|
public SystemId System { get; private set; }
|
|
|
|
public string Color { get; private set; }
|
|
|
|
public string AvatarUrl { get; private set; }
|
|
|
|
public string BannerImage { get; private set; }
|
|
|
|
public string Name { get; private set; }
|
|
|
|
public string DisplayName { get; private set; }
|
|
|
|
public LocalDate? Birthday { get; private set; }
|
|
|
|
public string Pronouns { get; private set; }
|
|
|
|
public string Description { get; private set; }
|
|
|
|
public ICollection<ProxyTag> ProxyTags { get; private set; }
|
|
|
|
public bool KeepProxy { get; private set; }
|
|
|
|
public Instant Created { get; private set; }
|
|
|
|
public int MessageCount { get; private set; }
|
2022-12-10 17:07:07 +00:00
|
|
|
public Instant? LastMessageTimestamp { get; private set; }
|
2021-11-27 02:10:56 +00:00
|
|
|
public bool AllowAutoproxy { get; private set; }
|
|
|
|
|
|
|
|
public PrivacyLevel MemberVisibility { get; private set; }
|
|
|
|
public PrivacyLevel DescriptionPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel AvatarPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel NamePrivacy { get; private set; } //ignore setting if no display name is set
|
|
|
|
public PrivacyLevel BirthdayPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel PronounPrivacy { get; private set; }
|
|
|
|
|
|
|
|
public PrivacyLevel MetadataPrivacy { get; private set; }
|
|
|
|
// public PrivacyLevel ColorPrivacy { get; private set; }
|
|
|
|
|
|
|
|
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" or "0004" is hidden
|
|
|
|
/// Before Feb 10 2020, the sentinel year was 0001, now it is 0004.
|
|
|
|
[JsonIgnore]
|
|
|
|
public string BirthdayString
|
2020-02-12 14:16:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
get
|
2020-02-12 14:16:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (Birthday == null) return null;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var format = LocalDatePattern.CreateWithInvariantCulture("MMM dd, yyyy");
|
|
|
|
if (Birthday?.Year == 1 || Birthday?.Year == 4)
|
|
|
|
format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
|
|
|
|
return format.Format(Birthday.Value);
|
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
}
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class PKMemberExt
|
|
|
|
{
|
|
|
|
public static string NameFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.NamePrivacy.Get(ctx, member.Name, member.DisplayName ?? member.Name);
|
|
|
|
|
|
|
|
public static string AvatarFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.AvatarPrivacy.Get(ctx, member.AvatarUrl.TryGetCleanCdnUrl());
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string DescriptionFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.DescriptionPrivacy.Get(ctx, member.Description);
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static LocalDate? BirthdayFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.BirthdayPrivacy.Get(ctx, member.Birthday);
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string PronounsFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.PronounPrivacy.Get(ctx, member.Pronouns);
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Instant? CreatedFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.MetadataPrivacy.Get(ctx, (Instant?)member.Created);
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static int MessageCountFor(this PKMember member, LookupContext ctx) =>
|
|
|
|
member.MetadataPrivacy.Get(ctx, member.MessageCount);
|
|
|
|
|
|
|
|
public static JObject ToJson(this PKMember member, LookupContext ctx, bool needsLegacyProxyTags = false,
|
2022-04-20 16:20:03 +00:00
|
|
|
string systemStr = null)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
var includePrivacy = ctx == LookupContext.ByOwner;
|
2020-08-05 18:24:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var o = new JObject();
|
|
|
|
o.Add("id", member.Hid);
|
|
|
|
|
2022-04-20 16:20:03 +00:00
|
|
|
o.Add("uuid", member.Uuid.ToString());
|
|
|
|
if (systemStr != null)
|
|
|
|
o.Add("system", systemStr);
|
2021-08-07 22:13:46 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("name", member.NameFor(ctx));
|
|
|
|
|
|
|
|
o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
|
2022-04-20 16:20:03 +00:00
|
|
|
// o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("color", member.Color);
|
|
|
|
o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
|
|
|
|
o.Add("pronouns", member.PronounsFor(ctx));
|
|
|
|
o.Add("avatar_url", member.AvatarFor(ctx).TryGetCleanCdnUrl());
|
|
|
|
o.Add("banner", member.DescriptionPrivacy.Get(ctx, member.BannerImage).TryGetCleanCdnUrl());
|
|
|
|
o.Add("description", member.DescriptionFor(ctx));
|
|
|
|
o.Add("created", member.CreatedFor(ctx)?.FormatExport());
|
|
|
|
o.Add("keep_proxy", member.KeepProxy);
|
|
|
|
|
2022-12-15 00:49:55 +00:00
|
|
|
o.Add("autoproxy_enabled", ctx == LookupContext.ByOwner ? member.AllowAutoproxy : null);
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var tagArray = new JArray();
|
|
|
|
foreach (var tag in member.ProxyTags)
|
|
|
|
tagArray.Add(new JObject { { "prefix", tag.Prefix }, { "suffix", tag.Suffix } });
|
|
|
|
o.Add("proxy_tags", tagArray);
|
|
|
|
|
2022-04-20 16:20:03 +00:00
|
|
|
if (includePrivacy)
|
|
|
|
{
|
|
|
|
var p = new JObject();
|
|
|
|
|
|
|
|
p.Add("visibility", member.MemberVisibility.ToJsonString());
|
|
|
|
p.Add("name_privacy", member.NamePrivacy.ToJsonString());
|
|
|
|
p.Add("description_privacy", member.DescriptionPrivacy.ToJsonString());
|
|
|
|
p.Add("birthday_privacy", member.BirthdayPrivacy.ToJsonString());
|
|
|
|
p.Add("pronoun_privacy", member.PronounPrivacy.ToJsonString());
|
|
|
|
p.Add("avatar_privacy", member.AvatarPrivacy.ToJsonString());
|
|
|
|
p.Add("metadata_privacy", member.MetadataPrivacy.ToJsonString());
|
|
|
|
|
|
|
|
o.Add("privacy", p);
|
|
|
|
}
|
|
|
|
else
|
2021-08-07 22:13:46 +00:00
|
|
|
{
|
2022-04-20 16:20:03 +00:00
|
|
|
o.Add("privacy", null);
|
2021-08-07 22:13:46 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return o;
|
2020-08-05 18:24:51 +00:00
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
}
|