Add support for multiple proxy tags

Tangentially closes #103.
This commit is contained in:
Ske
2019-10-28 20:15:27 +01:00
committed by Astrid
parent 96b03495a4
commit 393ee16c1b
16 changed files with 190 additions and 69 deletions

View File

@@ -1,12 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper.Contrib.Extensions;
using Newtonsoft.Json;
using NodaTime;
using NodaTime.Text;
using PluralKit.Core;
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);
}
}
}
public class PKSystem
{
// Additions here should be mirrored in SystemStore::Save
@@ -35,9 +65,22 @@ namespace PluralKit
[JsonProperty("birthday")] public LocalDate? Birthday { get; set; }
[JsonProperty("pronouns")] public string Pronouns { get; set; }
[JsonProperty("description")] public string Description { get; set; }
[JsonProperty("prefix")] public string Prefix { get; set; }
[JsonProperty("suffix")] public string Suffix { 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)};
}
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" is hidden
[JsonIgnore] public string BirthdayString
@@ -52,9 +95,7 @@ namespace PluralKit
}
}
[JsonIgnore] public bool HasProxyTags => Prefix != null || Suffix != null;
[JsonIgnore] public string ProxyString => $"{Prefix ?? ""}text{Suffix ?? ""}";
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
public string ProxyName(string systemTag)
{
if (systemTag == null) return DisplayName ?? Name;