PluralKit/PluralKit.Core/Models.cs

165 lines
6.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
2019-04-19 18:48:37 +00:00
using Dapper.Contrib.Extensions;
using Newtonsoft.Json;
2019-05-13 20:44:49 +00:00
using NodaTime;
using NodaTime.Text;
2019-04-19 18:48:37 +00:00
2019-04-29 15:42:09 +00:00
namespace PluralKit
{
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;
}
[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
[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);
public void ToJson(System.Text.Json.Utf8JsonWriter w)
{
w.WriteStartObject();
w.WriteString("id", Hid);
w.WriteString("description", Description);
w.WriteString("tag", Tag);
w.WriteString("avatar_url", AvatarUrl);
w.WriteString("created", Formats.TimestampExportFormat.Format(Created));
w.WriteString("tz", UiTz);
w.WriteEndObject();
}
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
[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; }
[JsonProperty("birthday")] public LocalDate? Birthday { get; set; }
[JsonProperty("pronouns")] public string Pronouns { get; set; }
[JsonProperty("description")] public string Description { get; set; }
[JsonProperty("proxy_tags")] public ICollection<ProxyTag> ProxyTags { get; set; }
[JsonProperty("keep_proxy")] public bool KeepProxy { get; set; }
[JsonProperty("created")] public Instant Created { get; set; }
// These are deprecated as fuck, and are kinda hacky
// Don't use, unless you're the API's serialization library
[JsonProperty("prefix")] [Obsolete("Use PKMember.ProxyTags")] public string Prefix
{
2019-10-30 08:09:09 +00:00
get => ProxyTags?.FirstOrDefault().Prefix;
set => ProxyTags = new[] {new ProxyTag(Prefix, value)};
}
[JsonProperty("suffix")] [Obsolete("Use PKMember.ProxyTags")] public string Suffix
{
2019-10-30 08:19:49 +00:00
get => ProxyTags?.FirstOrDefault().Suffix;
set => ProxyTags = new[] {new ProxyTag(Suffix, value)};
}
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
[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
}
}
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
2019-08-09 08:12:38 +00:00
public string ProxyName(string systemTag)
{
if (systemTag == null) return DisplayName ?? Name;
return $"{DisplayName ?? Name} {systemTag}";
}
public void ToJson(Utf8JsonWriter w)
{
w.WriteStartObject();
w.WriteString("id", Hid);
w.WriteString("name", Name);
w.WriteString("color", Color);
w.WriteString("display_name", DisplayName);
w.WriteString("birthday", Birthday.HasValue ? Formats.DateExportFormat.Format(Birthday.Value) : null);
w.WriteString("pronouns", Pronouns);
w.WriteString("description", Description);
w.WriteStartArray("proxy_tags");
foreach (var tag in ProxyTags)
{
w.WriteStartObject();
w.WriteString("prefix", tag.Prefix);
w.WriteString("suffix", tag.Suffix);
w.WriteEndObject();
}
w.WriteEndArray();
w.WriteBoolean("keep_proxy", KeepProxy);
w.WriteString("created", Formats.TimestampExportFormat.Format(Created));
if (ProxyTags.Count > 0)
{
// Legacy compatibility only, TODO: remove at some point
w.WriteString("prefix", ProxyTags?.FirstOrDefault().Prefix);
w.WriteString("suffix", ProxyTags?.FirstOrDefault().Suffix);
}
w.WriteEndObject();
}
2019-04-19 18:48:37 +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
}