PluralKit/PluralKit.Core/Models.cs

119 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
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
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);
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("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
{
get => ProxyTags.FirstOrDefault().Prefix;
set => ProxyTags = new[] {new ProxyTag(Prefix, value)};
}
[JsonProperty("suffix")] [Obsolete("Use PKMember.ProxyTags")] public string Suffix
{
get => ProxyTags.FirstOrDefault().Prefix;
set => ProxyTags = new[] {new ProxyTag(Prefix, 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}";
}
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
}