From d4c1a6b1fcabc8ed244d4e165ab7c21f96273581 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 22 Dec 2019 00:41:53 +0100 Subject: [PATCH] Add beginnings of new export/data system --- PluralKit.Core/Models.cs | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/PluralKit.Core/Models.cs b/PluralKit.Core/Models.cs index 40d265fc..181d4ac2 100644 --- a/PluralKit.Core/Models.cs +++ b/PluralKit.Core/Models.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.Json; using Dapper.Contrib.Extensions; using Newtonsoft.Json; @@ -50,6 +51,18 @@ namespace PluralKit [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(); + } } public class PKMember @@ -102,6 +115,38 @@ namespace PluralKit 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(); + } } public class PKSwitch