2019-10-28 19:15:27 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
2019-04-19 18:48:37 +00:00
|
|
|
using Dapper.Contrib.Extensions;
|
2019-07-09 18:39:29 +00:00
|
|
|
using Newtonsoft.Json;
|
2019-12-28 14:52:59 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2019-05-13 20:44:49 +00:00
|
|
|
using NodaTime;
|
|
|
|
using NodaTime.Text;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2019-04-29 15:42:09 +00:00
|
|
|
namespace PluralKit
|
|
|
|
{
|
2019-12-28 14:52:59 +00:00
|
|
|
public class PKParseError: Exception
|
|
|
|
{
|
|
|
|
public PKParseError(string message): base(message) { }
|
|
|
|
}
|
|
|
|
|
2019-10-28 19:15:27 +00:00
|
|
|
public struct ProxyTag
|
|
|
|
{
|
|
|
|
public ProxyTag(string prefix, string suffix)
|
|
|
|
{
|
|
|
|
// Normalize empty strings to null for DB
|
2019-10-30 08:09:09 +00:00
|
|
|
Prefix = prefix?.Length == 0 ? null : prefix;
|
|
|
|
Suffix = suffix?.Length == 0 ? null : suffix;
|
2019-10-28 19:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[JsonProperty("prefix")] public string Prefix { get; set; }
|
|
|
|
[JsonProperty("suffix")] public string Suffix { get; set; }
|
|
|
|
|
|
|
|
[JsonIgnore] public string ProxyString => $"{Prefix ?? ""}text{Suffix ?? ""}";
|
|
|
|
|
|
|
|
public bool Equals(ProxyTag other) => Prefix == other.Prefix && Suffix == other.Suffix;
|
|
|
|
|
|
|
|
public override bool Equals(object obj) => obj is ProxyTag other && Equals(other);
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
unchecked
|
|
|
|
{
|
|
|
|
return ((Prefix != null ? Prefix.GetHashCode() : 0) * 397) ^
|
|
|
|
(Suffix != null ? Suffix.GetHashCode() : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 15:42:09 +00:00
|
|
|
public class PKSystem
|
|
|
|
{
|
2019-08-09 08:12:38 +00:00
|
|
|
// Additions here should be mirrored in SystemStore::Save
|
2019-07-09 18:39:29 +00:00
|
|
|
[Key] [JsonIgnore] public int Id { get; set; }
|
|
|
|
[JsonProperty("id")] public string Hid { get; set; }
|
|
|
|
[JsonProperty("name")] public string Name { get; set; }
|
|
|
|
[JsonProperty("description")] public string Description { get; set; }
|
|
|
|
[JsonProperty("tag")] public string Tag { get; set; }
|
|
|
|
[JsonProperty("avatar_url")] public string AvatarUrl { get; set; }
|
|
|
|
[JsonIgnore] public string Token { get; set; }
|
|
|
|
[JsonProperty("created")] public Instant Created { get; set; }
|
|
|
|
[JsonProperty("tz")] public string UiTz { get; set; }
|
|
|
|
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
|
2019-12-21 23:41:53 +00:00
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
public JObject ToJson()
|
|
|
|
{
|
|
|
|
var o = new JObject();
|
|
|
|
o.Add("id", Hid);
|
|
|
|
o.Add("name", Name);
|
|
|
|
o.Add("description", Description);
|
|
|
|
o.Add("tag", Tag);
|
|
|
|
o.Add("avatar_url", AvatarUrl);
|
|
|
|
o.Add("created", Formats.TimestampExportFormat.Format(Created));
|
|
|
|
o.Add("tz", UiTz);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Apply(JObject o)
|
2019-12-21 23:41:53 +00:00
|
|
|
{
|
2019-12-28 14:52:59 +00:00
|
|
|
if (o.ContainsKey("name")) Name = o.Value<string>("name").NullIfEmpty().BoundsCheck(Limits.MaxSystemNameLength, "System name");
|
|
|
|
if (o.ContainsKey("description")) Description = o.Value<string>("description").NullIfEmpty().BoundsCheck(Limits.MaxDescriptionLength, "System description");
|
|
|
|
if (o.ContainsKey("tag")) Tag = o.Value<string>("tag").NullIfEmpty().BoundsCheck(Limits.MaxSystemTagLength, "System tag");
|
|
|
|
if (o.ContainsKey("avatar_url")) AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("tz")) UiTz = o.Value<string>("tz") ?? "UTC";
|
2019-12-21 23:41:53 +00:00
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 15:42:09 +00:00
|
|
|
public class PKMember
|
|
|
|
{
|
2019-08-09 08:12:38 +00:00
|
|
|
// Additions here should be mirrored in MemberStore::Save
|
2019-07-09 18:39:29 +00:00
|
|
|
[JsonIgnore] public int Id { get; set; }
|
|
|
|
[JsonProperty("id")] public string Hid { get; set; }
|
|
|
|
[JsonIgnore] public int System { get; set; }
|
|
|
|
[JsonProperty("color")] public string Color { get; set; }
|
|
|
|
[JsonProperty("avatar_url")] public string AvatarUrl { get; set; }
|
|
|
|
[JsonProperty("name")] public string Name { get; set; }
|
2019-08-09 08:12:38 +00:00
|
|
|
[JsonProperty("display_name")] public string DisplayName { get; set; }
|
2019-07-09 18:39:29 +00:00
|
|
|
[JsonProperty("birthday")] public LocalDate? Birthday { get; set; }
|
|
|
|
[JsonProperty("pronouns")] public string Pronouns { get; set; }
|
|
|
|
[JsonProperty("description")] public string Description { get; set; }
|
2019-10-28 19:15:27 +00:00
|
|
|
[JsonProperty("proxy_tags")] public ICollection<ProxyTag> ProxyTags { get; set; }
|
2019-10-30 08:26:50 +00:00
|
|
|
[JsonProperty("keep_proxy")] public bool KeepProxy { get; set; }
|
2019-07-09 18:39:29 +00:00
|
|
|
[JsonProperty("created")] public Instant Created { get; set; }
|
2019-04-29 15:42:09 +00:00
|
|
|
|
|
|
|
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" is hidden
|
2019-07-09 18:39:29 +00:00
|
|
|
[JsonIgnore] public string BirthdayString
|
2019-04-29 15:42:09 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (Birthday == null) return null;
|
2019-05-13 20:44:49 +00:00
|
|
|
|
|
|
|
var format = LocalDatePattern.CreateWithInvariantCulture("MMM dd, yyyy");
|
|
|
|
if (Birthday?.Year == 1) format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
|
|
|
|
return format.Format(Birthday.Value);
|
2019-04-29 15:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-29 17:52:07 +00:00
|
|
|
|
2019-10-28 19:15:27 +00:00
|
|
|
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
|
2019-12-26 19:39:47 +00:00
|
|
|
public string ProxyName(string systemTag, string guildDisplayName)
|
2019-08-09 08:12:38 +00:00
|
|
|
{
|
2019-12-26 19:39:47 +00:00
|
|
|
if (systemTag == null) return guildDisplayName ?? DisplayName ?? Name;
|
|
|
|
return $"{guildDisplayName ?? DisplayName ?? Name} {systemTag}";
|
2019-08-09 08:12:38 +00:00
|
|
|
}
|
2019-12-21 23:41:53 +00:00
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
public JObject ToJson()
|
2019-12-21 23:41:53 +00:00
|
|
|
{
|
2019-12-28 14:52:59 +00:00
|
|
|
var o = new JObject();
|
|
|
|
o.Add("id", Hid);
|
|
|
|
o.Add("name", Name);
|
|
|
|
o.Add("color", Color);
|
|
|
|
o.Add("display_name", DisplayName);
|
|
|
|
o.Add("birthday", Birthday.HasValue ? Formats.DateExportFormat.Format(Birthday.Value) : null);
|
|
|
|
o.Add("pronouns", Pronouns);
|
|
|
|
o.Add("description", Description);
|
|
|
|
|
|
|
|
var tagArray = new JArray();
|
|
|
|
foreach (var tag in ProxyTags)
|
|
|
|
tagArray.Add(new JObject {{"prefix", tag.Prefix}, {"suffix", tag.Suffix}});
|
|
|
|
o.Add("proxy_tags", tagArray);
|
|
|
|
|
|
|
|
o.Add("keep_proxy", KeepProxy);
|
|
|
|
o.Add("created", Formats.TimestampExportFormat.Format(Created));
|
2019-12-21 23:41:53 +00:00
|
|
|
|
|
|
|
if (ProxyTags.Count > 0)
|
|
|
|
{
|
|
|
|
// Legacy compatibility only, TODO: remove at some point
|
2019-12-28 14:52:59 +00:00
|
|
|
o.Add("prefix", ProxyTags?.FirstOrDefault().Prefix);
|
|
|
|
o.Add("suffix", ProxyTags?.FirstOrDefault().Suffix);
|
2019-12-21 23:41:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 14:52:59 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Apply(JObject o)
|
|
|
|
{
|
|
|
|
if (o.ContainsKey("name") && o["name"].Type == JTokenType.Null)
|
|
|
|
throw new PKParseError("Member name can not be set to null.");
|
|
|
|
|
|
|
|
if (o.ContainsKey("name")) Name = o.Value<string>("name").BoundsCheck(Limits.MaxMemberNameLength, "Member name");
|
|
|
|
if (o.ContainsKey("color")) Color = o.Value<string>("color").NullIfEmpty();
|
|
|
|
if (o.ContainsKey("display_name")) DisplayName = o.Value<string>("display_name").NullIfEmpty().BoundsCheck(Limits.MaxMemberNameLength, "Member display name");
|
|
|
|
if (o.ContainsKey("birthday"))
|
|
|
|
{
|
|
|
|
var str = o.Value<string>("birthday").NullIfEmpty();
|
|
|
|
var res = Formats.DateExportFormat.Parse(str);
|
|
|
|
if (res.Success) Birthday = res.Value;
|
|
|
|
else if (str == null) Birthday = null;
|
|
|
|
else throw new PKParseError("Could not parse member birthday.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o.ContainsKey("pronouns")) Pronouns = o.Value<string>("pronouns").NullIfEmpty().BoundsCheck(Limits.MaxPronounsLength, "Member pronouns");
|
|
|
|
if (o.ContainsKey("description")) Description = o.Value<string>("description").NullIfEmpty().BoundsCheck(Limits.MaxDescriptionLength, "Member descriptoin");
|
|
|
|
if (o.ContainsKey("keep_proxy")) KeepProxy = o.Value<bool>("keep_proxy");
|
|
|
|
|
|
|
|
if (o.ContainsKey("prefix") || o.ContainsKey("suffix") && !o.ContainsKey("proxy_tags"))
|
|
|
|
ProxyTags = new[] {new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix"))};
|
|
|
|
else if (o.ContainsKey("proxy_tags"))
|
|
|
|
{
|
|
|
|
ProxyTags = o.Value<JArray>("proxy_tags")
|
|
|
|
.OfType<JObject>().Select(o => new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
|
|
|
|
.ToList();
|
|
|
|
}
|
2019-12-21 23:41:53 +00:00
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
2019-06-13 14:53:04 +00:00
|
|
|
|
|
|
|
public class PKSwitch
|
|
|
|
{
|
|
|
|
public int Id { get; set; }
|
|
|
|
public int System { get; set; }
|
|
|
|
public Instant Timestamp { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PKSwitchMember
|
|
|
|
{
|
|
|
|
public int Id { get; set; }
|
|
|
|
public int Switch { get; set; }
|
|
|
|
public int Member { get; set; }
|
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|