feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,11 +1,10 @@
namespace PluralKit.Core
namespace PluralKit.Core;
public class GuildConfig
{
public class GuildConfig
{
public ulong Id { get; }
public ulong? LogChannel { get; }
public ulong[] LogBlacklist { get; }
public ulong[] Blacklist { get; }
public bool LogCleanupEnabled { get; }
}
public ulong Id { get; }
public ulong? LogChannel { get; }
public ulong[] LogBlacklist { get; }
public ulong[] Blacklist { get; }
public bool LogCleanupEnabled { get; }
}

View File

@@ -1,26 +1,25 @@
using Newtonsoft.Json.Linq;
#nullable enable
namespace PluralKit.Core
namespace PluralKit.Core;
public class MemberGuildSettings
{
public class MemberGuildSettings
public MemberId Member { get; }
public ulong Guild { get; }
public string? DisplayName { get; }
public string? AvatarUrl { get; }
}
public static class MemberGuildExt
{
public static JObject ToJson(this MemberGuildSettings settings)
{
public MemberId Member { get; }
public ulong Guild { get; }
public string? DisplayName { get; }
public string? AvatarUrl { get; }
}
var o = new JObject();
public static class MemberGuildExt
{
public static JObject ToJson(this MemberGuildSettings settings)
{
var o = new JObject();
o.Add("display_name", settings.DisplayName);
o.Add("avatar_url", settings.AvatarUrl);
o.Add("display_name", settings.DisplayName);
o.Add("avatar_url", settings.AvatarUrl);
return o;
}
return o;
}
}

View File

@@ -1,8 +1,3 @@
namespace PluralKit.Core
{
public enum APIVersion
{
V1,
V2,
}
}
namespace PluralKit.Core;
public enum APIVersion { V1, V2 }

View File

@@ -1,11 +1,8 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public interface INumericId<T, out TInner>: IEquatable<T>, IComparable<T>
where T : INumericId<T, TInner>
where TInner : IEquatable<TInner>, IComparable<TInner>
{
public interface INumericId<T, out TInner>: IEquatable<T>, IComparable<T>
where T : INumericId<T, TInner>
where TInner : IEquatable<TInner>, IComparable<TInner>
{
public TInner Value { get; }
}
public TInner Value { get; }
}

View File

@@ -1,89 +1,86 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using Dapper;
using Newtonsoft.Json;
namespace PluralKit.Core
namespace PluralKit.Core;
[JsonConverter(typeof(PartialConverter))]
public struct Partial<T>: IEnumerable<T>, IPartial
{
[JsonConverter(typeof(PartialConverter))]
public struct Partial<T>: IEnumerable<T>, IPartial
public bool IsPresent { get; }
public T Value { get; }
public object? RawValue => Value;
private Partial(bool isPresent, T value)
{
public bool IsPresent { get; }
public T Value { get; }
public object? RawValue => Value;
private Partial(bool isPresent, T value)
{
IsPresent = isPresent;
Value = value;
}
public static Partial<T> Null() => new Partial<T>(true, default!);
public static Partial<T> Present(T obj) => new Partial<T>(true, obj);
public static Partial<T> Absent = new Partial<T>(false, default!);
public IEnumerable<T> ToArray() => IsPresent ? new[] { Value } : new T[0];
public IEnumerator<T> GetEnumerator() => ToArray().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ToArray().GetEnumerator();
public static implicit operator Partial<T>(T val) => Present(val);
IsPresent = isPresent;
Value = value;
}
public interface IPartial
public static Partial<T> Null() => new(true, default!);
public static Partial<T> Present(T obj) => new(true, obj);
public static Partial<T> Absent = new(false, default!);
public IEnumerable<T> ToArray() => IsPresent ? new[] { Value } : new T[0];
public IEnumerator<T> GetEnumerator() => ToArray().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ToArray().GetEnumerator();
public static implicit operator Partial<T>(T val) => Present(val);
}
public interface IPartial
{
public bool IsPresent { get; }
public object? RawValue { get; }
}
public class PartialConverter: JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
JsonSerializer serializer)
{
public bool IsPresent { get; }
public object? RawValue { get; }
var innerType = objectType.GenericTypeArguments[0];
var innerValue = serializer.Deserialize(reader, innerType);
return typeof(Partial<>)
.MakeGenericType(innerType)
.GetMethod(nameof(Partial<object>.Present))!
.Invoke(null, new[] { innerValue });
}
public class PartialConverter: JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) =>
throw new NotImplementedException();
public override bool CanConvert(Type objectType) => true;
}
public static class PartialExt
{
public static bool TryGet<T>(this Partial<T> pt, out T value)
{
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
JsonSerializer serializer)
{
var innerType = objectType.GenericTypeArguments[0];
var innerValue = serializer.Deserialize(reader, innerType);
return typeof(Partial<>)
.MakeGenericType(innerType)
.GetMethod(nameof(Partial<object>.Present))!
.Invoke(null, new[] { innerValue });
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) =>
throw new NotImplementedException();
public override bool CanConvert(Type objectType) => true;
public override bool CanRead => true;
public override bool CanWrite => false;
value = pt.IsPresent ? pt.Value : default!;
return pt.IsPresent;
}
public static class PartialExt
public static T Or<T>(this Partial<T> pt, T fallback) => pt.IsPresent ? pt.Value : fallback;
public static T Or<T>(this Partial<T> pt, Func<T> fallback) => pt.IsPresent ? pt.Value : fallback.Invoke();
public static Partial<TOut> Map<TIn, TOut>(this Partial<TIn> pt, Func<TIn, TOut> fn) =>
pt.IsPresent ? Partial<TOut>.Present(fn.Invoke(pt.Value)) : Partial<TOut>.Absent;
public static void Apply<T>(this Partial<T> pt, DynamicParameters bag, QueryBuilder qb, string fieldName)
{
public static bool TryGet<T>(this Partial<T> pt, out T value)
{
value = pt.IsPresent ? pt.Value : default!;
return pt.IsPresent;
}
if (!pt.IsPresent) return;
public static T Or<T>(this Partial<T> pt, T fallback) => pt.IsPresent ? pt.Value : fallback;
public static T Or<T>(this Partial<T> pt, Func<T> fallback) => pt.IsPresent ? pt.Value : fallback.Invoke();
public static Partial<TOut> Map<TIn, TOut>(this Partial<TIn> pt, Func<TIn, TOut> fn) =>
pt.IsPresent ? Partial<TOut>.Present(fn.Invoke(pt.Value)) : Partial<TOut>.Absent;
public static void Apply<T>(this Partial<T> pt, DynamicParameters bag, QueryBuilder qb, string fieldName)
{
if (!pt.IsPresent) return;
bag.Add(fieldName, pt.Value);
qb.Variable(fieldName, $"@{fieldName}");
}
bag.Add(fieldName, pt.Value);
qb.Variable(fieldName, $"@{fieldName}");
}
}

View File

@@ -1,29 +1,25 @@
using System;
namespace PluralKit.Core;
using Newtonsoft.Json;
namespace PluralKit.Core
public class ValidationError
{
public class ValidationError
public string Key;
public string? Text;
public ValidationError(string key, string? text = null)
{
public string Key;
public string? Text;
public ValidationError(string key, string? text = null)
{
Key = key;
Text = text;
}
Key = key;
Text = text;
}
}
public class FieldTooLongError: ValidationError
public class FieldTooLongError: ValidationError
{
public int ActualLength;
public int MaxLength;
public FieldTooLongError(string key, int maxLength, int actualLength) : base(key)
{
public int MaxLength;
public int ActualLength;
public FieldTooLongError(string key, int maxLength, int actualLength) : base(key)
{
MaxLength = maxLength;
ActualLength = actualLength;
}
MaxLength = maxLength;
ActualLength = actualLength;
}
}

View File

@@ -1,103 +1,103 @@
using System;
using Newtonsoft.Json.Linq;
using NodaTime;
using Newtonsoft.Json.Linq;
namespace PluralKit.Core;
namespace PluralKit.Core
public readonly struct GroupId: INumericId<GroupId, int>
{
public readonly struct GroupId: INumericId<GroupId, int>
public int Value { get; }
public GroupId(int value)
{
public int Value { get; }
public GroupId(int value)
{
Value = value;
}
public bool Equals(GroupId other) => Value == other.Value;
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Group #{Value}";
Value = value;
}
public bool Equals(GroupId other) => Value == other.Value;
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Group #{Value}";
}
#nullable enable
public class PKGroup
public class PKGroup
{
public GroupId Id { get; private set; }
public string Hid { get; private set; } = null!;
public Guid Uuid { get; private set; }
public SystemId System { get; private set; }
public string Name { get; private set; } = null!;
public string? DisplayName { get; private set; }
public string? Description { get; private set; }
public string? Icon { get; private set; }
public string? BannerImage { get; private set; }
public string? Color { get; private set; }
public PrivacyLevel DescriptionPrivacy { get; private set; }
public PrivacyLevel IconPrivacy { get; private set; }
public PrivacyLevel ListPrivacy { get; private set; }
public PrivacyLevel Visibility { get; private set; }
public Instant Created { get; private set; }
}
public static class PKGroupExt
{
public static string? DescriptionFor(this PKGroup group, LookupContext ctx) =>
group.DescriptionPrivacy.Get(ctx, group.Description);
public static string? IconFor(this PKGroup group, LookupContext ctx) =>
group.IconPrivacy.Get(ctx, group.Icon?.TryGetCleanCdnUrl());
public static JObject ToJson(this PKGroup group, LookupContext ctx, string? systemStr = null,
bool needsMembersArray = false)
{
public GroupId Id { get; private set; }
public string Hid { get; private set; } = null!;
public Guid Uuid { get; private set; }
public SystemId System { get; private set; }
var o = new JObject();
public string Name { get; private set; } = null!;
public string? DisplayName { get; private set; }
public string? Description { get; private set; }
public string? Icon { get; private set; }
public string? BannerImage { get; private set; }
public string? Color { get; private set; }
o.Add("id", group.Hid);
o.Add("uuid", group.Uuid.ToString());
o.Add("name", group.Name);
public PrivacyLevel DescriptionPrivacy { get; private set; }
public PrivacyLevel IconPrivacy { get; private set; }
public PrivacyLevel ListPrivacy { get; private set; }
public PrivacyLevel Visibility { get; private set; }
if (systemStr != null)
o.Add("system", systemStr);
public Instant Created { get; private set; }
}
o.Add("display_name", group.DisplayName);
o.Add("description", group.DescriptionPrivacy.Get(ctx, group.Description));
o.Add("icon", group.Icon);
o.Add("banner", group.DescriptionPrivacy.Get(ctx, group.BannerImage));
o.Add("color", group.Color);
public static class PKGroupExt
{
public static string? DescriptionFor(this PKGroup group, LookupContext ctx) =>
group.DescriptionPrivacy.Get(ctx, group.Description);
o.Add("created", group.Created.FormatExport());
public static string? IconFor(this PKGroup group, LookupContext ctx) =>
group.IconPrivacy.Get(ctx, group.Icon?.TryGetCleanCdnUrl());
if (needsMembersArray)
o.Add("members", new JArray());
public static JObject ToJson(this PKGroup group, LookupContext ctx, string? systemStr = null, bool needsMembersArray = false)
if (ctx == LookupContext.ByOwner)
{
var o = new JObject();
var p = new JObject();
o.Add("id", group.Hid);
o.Add("uuid", group.Uuid.ToString());
o.Add("name", group.Name);
p.Add("description_privacy", group.DescriptionPrivacy.ToJsonString());
p.Add("icon_privacy", group.IconPrivacy.ToJsonString());
p.Add("list_privacy", group.ListPrivacy.ToJsonString());
p.Add("visibility", group.Visibility.ToJsonString());
if (systemStr != null)
o.Add("system", systemStr);
o.Add("display_name", group.DisplayName);
o.Add("description", group.DescriptionPrivacy.Get(ctx, group.Description));
o.Add("icon", group.Icon);
o.Add("banner", group.DescriptionPrivacy.Get(ctx, group.BannerImage));
o.Add("color", group.Color);
o.Add("created", group.Created.FormatExport());
if (needsMembersArray)
o.Add("members", new JArray());
if (ctx == LookupContext.ByOwner)
{
var p = new JObject();
p.Add("description_privacy", group.DescriptionPrivacy.ToJsonString());
p.Add("icon_privacy", group.IconPrivacy.ToJsonString());
p.Add("list_privacy", group.ListPrivacy.ToJsonString());
p.Add("visibility", group.Visibility.ToJsonString());
o.Add("privacy", p);
}
else
o.Add("privacy", null);
return o;
o.Add("privacy", p);
}
else
{
o.Add("privacy", null);
}
return o;
}
}

View File

@@ -1,191 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
using NodaTime.Text;
namespace PluralKit.Core
namespace PluralKit.Core;
public readonly struct MemberId: INumericId<MemberId, int>
{
public readonly struct MemberId: INumericId<MemberId, int>
public int Value { get; }
public MemberId(int value)
{
public int Value { get; }
public MemberId(int value)
{
Value = value;
}
public bool Equals(MemberId other) => Value == other.Value;
public override bool Equals(object obj) => obj is MemberId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(MemberId left, MemberId right) => left.Equals(right);
public static bool operator !=(MemberId left, MemberId right) => !left.Equals(right);
public int CompareTo(MemberId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Member #{Value}";
Value = value;
}
public class PKMember
public bool Equals(MemberId other) => Value == other.Value;
public override bool Equals(object obj) => obj is MemberId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(MemberId left, MemberId right) => left.Equals(right);
public static bool operator !=(MemberId left, MemberId right) => !left.Equals(right);
public int CompareTo(MemberId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Member #{Value}";
}
public class PKMember
{
// Dapper *can* figure out mapping to getter-only properties, but this doesn't work
// when trying to map to *subclasses* (eg. ListedMember). Adding private setters makes it work anyway.
public MemberId Id { get; private set; }
public string Hid { get; private set; }
public Guid Uuid { get; private set; }
public SystemId System { get; private set; }
public string Color { get; private set; }
public string AvatarUrl { get; private set; }
public string BannerImage { get; private set; }
public string Name { get; private set; }
public string DisplayName { get; private set; }
public LocalDate? Birthday { get; private set; }
public string Pronouns { get; private set; }
public string Description { get; private set; }
public ICollection<ProxyTag> ProxyTags { get; private set; }
public bool KeepProxy { get; private set; }
public Instant Created { get; private set; }
public int MessageCount { get; private set; }
public bool AllowAutoproxy { get; private set; }
public PrivacyLevel MemberVisibility { get; private set; }
public PrivacyLevel DescriptionPrivacy { get; private set; }
public PrivacyLevel AvatarPrivacy { get; private set; }
public PrivacyLevel NamePrivacy { get; private set; } //ignore setting if no display name is set
public PrivacyLevel BirthdayPrivacy { get; private set; }
public PrivacyLevel PronounPrivacy { get; private set; }
public PrivacyLevel MetadataPrivacy { get; private set; }
// public PrivacyLevel ColorPrivacy { get; private set; }
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" or "0004" is hidden
/// Before Feb 10 2020, the sentinel year was 0001, now it is 0004.
[JsonIgnore]
public string BirthdayString
{
// Dapper *can* figure out mapping to getter-only properties, but this doesn't work
// when trying to map to *subclasses* (eg. ListedMember). Adding private setters makes it work anyway.
public MemberId Id { get; private set; }
public string Hid { get; private set; }
public Guid Uuid { get; private set; }
public SystemId System { get; private set; }
public string Color { get; private set; }
public string AvatarUrl { get; private set; }
public string BannerImage { get; private set; }
public string Name { get; private set; }
public string DisplayName { get; private set; }
public LocalDate? Birthday { get; private set; }
public string Pronouns { get; private set; }
public string Description { get; private set; }
public ICollection<ProxyTag> ProxyTags { get; private set; }
public bool KeepProxy { get; private set; }
public Instant Created { get; private set; }
public int MessageCount { get; private set; }
public bool AllowAutoproxy { get; private set; }
public PrivacyLevel MemberVisibility { get; private set; }
public PrivacyLevel DescriptionPrivacy { get; private set; }
public PrivacyLevel AvatarPrivacy { get; private set; }
public PrivacyLevel NamePrivacy { get; private set; } //ignore setting if no display name is set
public PrivacyLevel BirthdayPrivacy { get; private set; }
public PrivacyLevel PronounPrivacy { get; private set; }
public PrivacyLevel MetadataPrivacy { get; private set; }
// public PrivacyLevel ColorPrivacy { get; private set; }
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" or "0004" is hidden
/// Before Feb 10 2020, the sentinel year was 0001, now it is 0004.
[JsonIgnore]
public string BirthdayString
get
{
get
{
if (Birthday == null) return null;
if (Birthday == null) return null;
var format = LocalDatePattern.CreateWithInvariantCulture("MMM dd, yyyy");
if (Birthday?.Year == 1 || Birthday?.Year == 4) format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
return format.Format(Birthday.Value);
}
var format = LocalDatePattern.CreateWithInvariantCulture("MMM dd, yyyy");
if (Birthday?.Year == 1 || Birthday?.Year == 4)
format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
return format.Format(Birthday.Value);
}
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
}
public static class PKMemberExt
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
}
public static class PKMemberExt
{
public static string NameFor(this PKMember member, LookupContext ctx) =>
member.NamePrivacy.Get(ctx, member.Name, member.DisplayName ?? member.Name);
public static string AvatarFor(this PKMember member, LookupContext ctx) =>
member.AvatarPrivacy.Get(ctx, member.AvatarUrl.TryGetCleanCdnUrl());
public static string DescriptionFor(this PKMember member, LookupContext ctx) =>
member.DescriptionPrivacy.Get(ctx, member.Description);
public static LocalDate? BirthdayFor(this PKMember member, LookupContext ctx) =>
member.BirthdayPrivacy.Get(ctx, member.Birthday);
public static string PronounsFor(this PKMember member, LookupContext ctx) =>
member.PronounPrivacy.Get(ctx, member.Pronouns);
public static Instant? CreatedFor(this PKMember member, LookupContext ctx) =>
member.MetadataPrivacy.Get(ctx, (Instant?)member.Created);
public static int MessageCountFor(this PKMember member, LookupContext ctx) =>
member.MetadataPrivacy.Get(ctx, member.MessageCount);
public static JObject ToJson(this PKMember member, LookupContext ctx, bool needsLegacyProxyTags = false,
string systemStr = null, APIVersion v = APIVersion.V1)
{
public static string NameFor(this PKMember member, LookupContext ctx) =>
member.NamePrivacy.Get(ctx, member.Name, member.DisplayName ?? member.Name);
var includePrivacy = ctx == LookupContext.ByOwner;
public static string AvatarFor(this PKMember member, LookupContext ctx) =>
member.AvatarPrivacy.Get(ctx, member.AvatarUrl.TryGetCleanCdnUrl());
var o = new JObject();
o.Add("id", member.Hid);
public static string DescriptionFor(this PKMember member, LookupContext ctx) =>
member.DescriptionPrivacy.Get(ctx, member.Description);
public static LocalDate? BirthdayFor(this PKMember member, LookupContext ctx) =>
member.BirthdayPrivacy.Get(ctx, member.Birthday);
public static string PronounsFor(this PKMember member, LookupContext ctx) =>
member.PronounPrivacy.Get(ctx, member.Pronouns);
public static Instant? CreatedFor(this PKMember member, LookupContext ctx) =>
member.MetadataPrivacy.Get(ctx, (Instant?)member.Created);
public static int MessageCountFor(this PKMember member, LookupContext ctx) =>
member.MetadataPrivacy.Get(ctx, member.MessageCount);
public static JObject ToJson(this PKMember member, LookupContext ctx, bool needsLegacyProxyTags = false, string systemStr = null, APIVersion v = APIVersion.V1)
if (v == APIVersion.V2)
{
var includePrivacy = ctx == LookupContext.ByOwner;
var o = new JObject();
o.Add("id", member.Hid);
if (v == APIVersion.V2)
{
o.Add("uuid", member.Uuid.ToString());
if (systemStr != null)
o.Add("system", systemStr);
}
o.Add("name", member.NameFor(ctx));
// o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
o.Add("color", member.Color);
o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
o.Add("pronouns", member.PronounsFor(ctx));
o.Add("avatar_url", member.AvatarFor(ctx).TryGetCleanCdnUrl());
o.Add("banner", member.DescriptionPrivacy.Get(ctx, member.BannerImage).TryGetCleanCdnUrl());
o.Add("description", member.DescriptionFor(ctx));
o.Add("created", member.CreatedFor(ctx)?.FormatExport());
o.Add("keep_proxy", member.KeepProxy);
var tagArray = new JArray();
foreach (var tag in member.ProxyTags)
tagArray.Add(new JObject { { "prefix", tag.Prefix }, { "suffix", tag.Suffix } });
o.Add("proxy_tags", tagArray);
switch (v)
{
case APIVersion.V1:
{
o.Add("privacy", includePrivacy ? (member.MemberVisibility.LevelName()) : null);
o.Add("visibility", includePrivacy ? (member.MemberVisibility.LevelName()) : null);
o.Add("name_privacy", includePrivacy ? (member.NamePrivacy.LevelName()) : null);
o.Add("description_privacy", includePrivacy ? (member.DescriptionPrivacy.LevelName()) : null);
o.Add("birthday_privacy", includePrivacy ? (member.BirthdayPrivacy.LevelName()) : null);
o.Add("pronoun_privacy", includePrivacy ? (member.PronounPrivacy.LevelName()) : null);
o.Add("avatar_privacy", includePrivacy ? (member.AvatarPrivacy.LevelName()) : null);
// o.Add("color_privacy", ctx == LookupContext.ByOwner ? (member.ColorPrivacy.LevelName()) : null);
o.Add("metadata_privacy", includePrivacy ? (member.MetadataPrivacy.LevelName()) : null);
if (member.ProxyTags.Count > 0 && needsLegacyProxyTags)
{
// Legacy compatibility only, TODO: remove at some point
o.Add("prefix", member.ProxyTags?.FirstOrDefault().Prefix);
o.Add("suffix", member.ProxyTags?.FirstOrDefault().Suffix);
}
break;
}
case APIVersion.V2:
{
if (includePrivacy)
{
var p = new JObject();
p.Add("visibility", member.MemberVisibility.ToJsonString());
p.Add("name_privacy", member.NamePrivacy.ToJsonString());
p.Add("description_privacy", member.DescriptionPrivacy.ToJsonString());
p.Add("birthday_privacy", member.BirthdayPrivacy.ToJsonString());
p.Add("pronoun_privacy", member.PronounPrivacy.ToJsonString());
p.Add("avatar_privacy", member.AvatarPrivacy.ToJsonString());
p.Add("metadata_privacy", member.MetadataPrivacy.ToJsonString());
o.Add("privacy", p);
}
else
o.Add("privacy", null);
break;
}
}
return o;
o.Add("uuid", member.Uuid.ToString());
if (systemStr != null)
o.Add("system", systemStr);
}
o.Add("name", member.NameFor(ctx));
// o.Add("color", member.ColorPrivacy.CanAccess(ctx) ? member.Color : null);
o.Add("display_name", member.NamePrivacy.CanAccess(ctx) ? member.DisplayName : null);
o.Add("color", member.Color);
o.Add("birthday", member.BirthdayFor(ctx)?.FormatExport());
o.Add("pronouns", member.PronounsFor(ctx));
o.Add("avatar_url", member.AvatarFor(ctx).TryGetCleanCdnUrl());
o.Add("banner", member.DescriptionPrivacy.Get(ctx, member.BannerImage).TryGetCleanCdnUrl());
o.Add("description", member.DescriptionFor(ctx));
o.Add("created", member.CreatedFor(ctx)?.FormatExport());
o.Add("keep_proxy", member.KeepProxy);
var tagArray = new JArray();
foreach (var tag in member.ProxyTags)
tagArray.Add(new JObject { { "prefix", tag.Prefix }, { "suffix", tag.Suffix } });
o.Add("proxy_tags", tagArray);
switch (v)
{
case APIVersion.V1:
{
o.Add("privacy", includePrivacy ? member.MemberVisibility.LevelName() : null);
o.Add("visibility", includePrivacy ? member.MemberVisibility.LevelName() : null);
o.Add("name_privacy", includePrivacy ? member.NamePrivacy.LevelName() : null);
o.Add("description_privacy", includePrivacy ? member.DescriptionPrivacy.LevelName() : null);
o.Add("birthday_privacy", includePrivacy ? member.BirthdayPrivacy.LevelName() : null);
o.Add("pronoun_privacy", includePrivacy ? member.PronounPrivacy.LevelName() : null);
o.Add("avatar_privacy", includePrivacy ? member.AvatarPrivacy.LevelName() : null);
// o.Add("color_privacy", ctx == LookupContext.ByOwner ? (member.ColorPrivacy.LevelName()) : null);
o.Add("metadata_privacy", includePrivacy ? member.MetadataPrivacy.LevelName() : null);
if (member.ProxyTags.Count > 0 && needsLegacyProxyTags)
{
// Legacy compatibility only, TODO: remove at some point
o.Add("prefix", member.ProxyTags?.FirstOrDefault().Prefix);
o.Add("suffix", member.ProxyTags?.FirstOrDefault().Suffix);
}
break;
}
case APIVersion.V2:
{
if (includePrivacy)
{
var p = new JObject();
p.Add("visibility", member.MemberVisibility.ToJsonString());
p.Add("name_privacy", member.NamePrivacy.ToJsonString());
p.Add("description_privacy", member.DescriptionPrivacy.ToJsonString());
p.Add("birthday_privacy", member.BirthdayPrivacy.ToJsonString());
p.Add("pronoun_privacy", member.PronounPrivacy.ToJsonString());
p.Add("avatar_privacy", member.AvatarPrivacy.ToJsonString());
p.Add("metadata_privacy", member.MetadataPrivacy.ToJsonString());
o.Add("privacy", p);
}
else
{
o.Add("privacy", null);
}
break;
}
}
return o;
}
}

View File

@@ -2,38 +2,37 @@ using Newtonsoft.Json.Linq;
using NodaTime;
namespace PluralKit.Core
namespace PluralKit.Core;
public class PKMessage
{
public class PKMessage
public ulong Mid { get; set; }
public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added)
public ulong Channel { get; set; }
public MemberId Member { get; set; }
public ulong Sender { get; set; }
public ulong? OriginalMid { get; set; }
}
public class FullMessage
{
public PKMessage Message;
public PKMember Member;
public PKSystem System;
public JObject ToJson(LookupContext ctx, APIVersion v)
{
public ulong Mid { get; set; }
public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added)
public ulong Channel { get; set; }
public MemberId Member { get; set; }
public ulong Sender { get; set; }
public ulong? OriginalMid { get; set; }
}
var o = new JObject();
public class FullMessage
{
public PKMessage Message;
public PKMember Member;
public PKSystem System;
o.Add("timestamp", Instant.FromUnixTimeMilliseconds((long)(Message.Mid >> 22) + 1420070400000).ToString());
o.Add("id", Message.Mid.ToString());
o.Add("original", Message.OriginalMid.ToString());
o.Add("sender", Message.Sender.ToString());
o.Add("channel", Message.Channel.ToString());
o.Add("guild", Message.Guild?.ToString());
o.Add("system", System.ToJson(ctx, v));
o.Add("member", Member.ToJson(ctx, v: v));
public JObject ToJson(LookupContext ctx, APIVersion v)
{
var o = new JObject();
o.Add("timestamp", Instant.FromUnixTimeMilliseconds((long)(this.Message.Mid >> 22) + 1420070400000).ToString());
o.Add("id", this.Message.Mid.ToString());
o.Add("original", this.Message.OriginalMid.ToString());
o.Add("sender", this.Message.Sender.ToString());
o.Add("channel", this.Message.Channel.ToString());
o.Add("guild", this.Message.Guild?.ToString());
o.Add("system", this.System.ToJson(ctx, v));
o.Add("member", this.Member.ToJson(ctx, v: v));
return o;
}
return o;
}
}

View File

@@ -1,19 +1,18 @@
using NodaTime;
namespace PluralKit.Core
{
public class PKShardInfo
{
public int Id { get; }
public ShardStatus Status { get; }
public float? Ping { get; }
public Instant? LastHeartbeat { get; }
public Instant? LastConnection { get; }
namespace PluralKit.Core;
public enum ShardStatus
{
Down = 0,
Up = 1
}
public class PKShardInfo
{
public enum ShardStatus
{
Down = 0,
Up = 1
}
public int Id { get; }
public ShardStatus Status { get; }
public float? Ping { get; }
public Instant? LastHeartbeat { get; }
public Instant? LastConnection { get; }
}

View File

@@ -1,39 +1,35 @@
using System;
using NodaTime;
namespace PluralKit.Core
namespace PluralKit.Core;
public readonly struct SwitchId: INumericId<SwitchId, int>
{
public int Value { get; }
public readonly struct SwitchId: INumericId<SwitchId, int>
public SwitchId(int value)
{
public int Value { get; }
public SwitchId(int value)
{
Value = value;
}
public bool Equals(SwitchId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SwitchId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SwitchId left, SwitchId right) => left.Equals(right);
public static bool operator !=(SwitchId left, SwitchId right) => !left.Equals(right);
public int CompareTo(SwitchId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Switch #{Value}";
Value = value;
}
public class PKSwitch
{
public SwitchId Id { get; }
public Guid Uuid { get; private set; }
public SystemId System { get; set; }
public Instant Timestamp { get; }
}
public bool Equals(SwitchId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SwitchId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SwitchId left, SwitchId right) => left.Equals(right);
public static bool operator !=(SwitchId left, SwitchId right) => !left.Equals(right);
public int CompareTo(SwitchId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Switch #{Value}";
}
public class PKSwitch
{
public SwitchId Id { get; }
public Guid Uuid { get; private set; }
public SystemId System { get; set; }
public Instant Timestamp { get; }
}

View File

@@ -1,5 +1,3 @@
using System;
using Dapper.Contrib.Extensions;
using Newtonsoft.Json;
@@ -7,123 +5,126 @@ using Newtonsoft.Json.Linq;
using NodaTime;
namespace PluralKit.Core
namespace PluralKit.Core;
public readonly struct SystemId: INumericId<SystemId, int>
{
public int Value { get; }
public readonly struct SystemId: INumericId<SystemId, int>
public SystemId(int value)
{
public int Value { get; }
public SystemId(int value)
{
Value = value;
}
public bool Equals(SystemId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SystemId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SystemId left, SystemId right) => left.Equals(right);
public static bool operator !=(SystemId left, SystemId right) => !left.Equals(right);
public int CompareTo(SystemId other) => Value.CompareTo(other.Value);
public override string ToString() => $"System #{Value}";
Value = value;
}
public class PKSystem
public bool Equals(SystemId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SystemId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SystemId left, SystemId right) => left.Equals(right);
public static bool operator !=(SystemId left, SystemId right) => !left.Equals(right);
public int CompareTo(SystemId other) => Value.CompareTo(other.Value);
public override string ToString() => $"System #{Value}";
}
public class PKSystem
{
[Key] public SystemId Id { get; }
public string Hid { get; }
public Guid Uuid { get; private set; }
public string Name { get; }
public string Description { get; }
public string Tag { get; }
public string AvatarUrl { get; }
public string BannerImage { get; }
public string Color { get; }
public string Token { get; }
public string WebhookUrl { get; }
public string WebhookToken { get; }
public Instant Created { get; }
public string UiTz { get; set; }
public bool PingsEnabled { get; }
public int? LatchTimeout { get; }
public PrivacyLevel DescriptionPrivacy { get; }
public PrivacyLevel MemberListPrivacy { get; }
public PrivacyLevel FrontPrivacy { get; }
public PrivacyLevel FrontHistoryPrivacy { get; }
public PrivacyLevel GroupListPrivacy { get; }
public int? MemberLimitOverride { get; }
public int? GroupLimitOverride { get; }
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
}
public static class PKSystemExt
{
public static string DescriptionFor(this PKSystem system, LookupContext ctx) =>
system.DescriptionPrivacy.Get(ctx, system.Description);
public static JObject ToJson(this PKSystem system, LookupContext ctx, APIVersion v = APIVersion.V1)
{
[Key] public SystemId Id { get; }
public string Hid { get; }
public Guid Uuid { get; private set; }
public string Name { get; }
public string Description { get; }
public string Tag { get; }
public string AvatarUrl { get; }
public string BannerImage { get; }
public string Color { get; }
public string Token { get; }
public string WebhookUrl { get; }
public string WebhookToken { get; }
public Instant Created { get; }
public string UiTz { get; set; }
public bool PingsEnabled { get; }
public int? LatchTimeout { get; }
public PrivacyLevel DescriptionPrivacy { get; }
public PrivacyLevel MemberListPrivacy { get; }
public PrivacyLevel FrontPrivacy { get; }
public PrivacyLevel FrontHistoryPrivacy { get; }
public PrivacyLevel GroupListPrivacy { get; }
public int? MemberLimitOverride { get; }
public int? GroupLimitOverride { get; }
var o = new JObject();
o.Add("id", system.Hid);
if (v == APIVersion.V2)
o.Add("uuid", system.Uuid.ToString());
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
}
o.Add("name", system.Name);
o.Add("description", system.DescriptionFor(ctx));
o.Add("tag", system.Tag);
o.Add("avatar_url", system.AvatarUrl.TryGetCleanCdnUrl());
o.Add("banner", system.DescriptionPrivacy.Get(ctx, system.BannerImage).TryGetCleanCdnUrl());
o.Add("color", system.Color);
o.Add("created", system.Created.FormatExport());
public static class PKSystemExt
{
public static string DescriptionFor(this PKSystem system, LookupContext ctx) =>
system.DescriptionPrivacy.Get(ctx, system.Description);
public static JObject ToJson(this PKSystem system, LookupContext ctx, APIVersion v = APIVersion.V1)
switch (v)
{
var o = new JObject();
o.Add("id", system.Hid);
if (v == APIVersion.V2)
o.Add("uuid", system.Uuid.ToString());
case APIVersion.V1:
{
o.Add("tz", system.UiTz);
o.Add("name", system.Name);
o.Add("description", system.DescriptionFor(ctx));
o.Add("tag", system.Tag);
o.Add("avatar_url", system.AvatarUrl.TryGetCleanCdnUrl());
o.Add("banner", system.DescriptionPrivacy.Get(ctx, system.BannerImage).TryGetCleanCdnUrl());
o.Add("color", system.Color);
o.Add("created", system.Created.FormatExport());
o.Add("description_privacy",
ctx == LookupContext.ByOwner ? system.DescriptionPrivacy.ToJsonString() : null);
o.Add("member_list_privacy",
ctx == LookupContext.ByOwner ? system.MemberListPrivacy.ToJsonString() : null);
o.Add("front_privacy", ctx == LookupContext.ByOwner ? system.FrontPrivacy.ToJsonString() : null);
o.Add("front_history_privacy",
ctx == LookupContext.ByOwner ? system.FrontHistoryPrivacy.ToJsonString() : null);
switch (v)
{
case APIVersion.V1:
break;
}
case APIVersion.V2:
{
o.Add("timezone", system.UiTz);
if (ctx == LookupContext.ByOwner)
{
o.Add("tz", system.UiTz);
// todo: should this be moved to a different JSON model?
o.Add("webhook_url", system.WebhookUrl);
// o.Add("webhook_token", system.WebhookToken);
o.Add("description_privacy", ctx == LookupContext.ByOwner ? system.DescriptionPrivacy.ToJsonString() : null);
o.Add("member_list_privacy", ctx == LookupContext.ByOwner ? system.MemberListPrivacy.ToJsonString() : null);
o.Add("front_privacy", ctx == LookupContext.ByOwner ? system.FrontPrivacy.ToJsonString() : null);
o.Add("front_history_privacy", ctx == LookupContext.ByOwner ? system.FrontHistoryPrivacy.ToJsonString() : null);
var p = new JObject();
break;
p.Add("description_privacy", system.DescriptionPrivacy.ToJsonString());
p.Add("member_list_privacy", system.MemberListPrivacy.ToJsonString());
p.Add("group_list_privacy", system.GroupListPrivacy.ToJsonString());
p.Add("front_privacy", system.FrontPrivacy.ToJsonString());
p.Add("front_history_privacy", system.FrontHistoryPrivacy.ToJsonString());
o.Add("privacy", p);
}
case APIVersion.V2:
else
{
o.Add("timezone", system.UiTz);
if (ctx == LookupContext.ByOwner)
{
// todo: should this be moved to a different JSON model?
o.Add("webhook_url", system.WebhookUrl);
// o.Add("webhook_token", system.WebhookToken);
var p = new JObject();
p.Add("description_privacy", system.DescriptionPrivacy.ToJsonString());
p.Add("member_list_privacy", system.MemberListPrivacy.ToJsonString());
p.Add("group_list_privacy", system.GroupListPrivacy.ToJsonString());
p.Add("front_privacy", system.FrontPrivacy.ToJsonString());
p.Add("front_history_privacy", system.FrontHistoryPrivacy.ToJsonString());
o.Add("privacy", p);
}
else
o.Add("privacy", null);
break;
o.Add("privacy", null);
}
}
return o;
break;
}
}
return o;
}
}

View File

@@ -1,25 +1,24 @@
using SqlKata;
using Newtonsoft.Json.Linq;
namespace PluralKit.Core
using SqlKata;
namespace PluralKit.Core;
public class AccountPatch: PatchObject
{
public class AccountPatch: PatchObject
public Partial<bool> AllowAutoproxy { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("allow_autoproxy", AllowAutoproxy)
);
public JObject ToJson()
{
public Partial<bool> AllowAutoproxy { get; set; }
var o = new JObject();
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("allow_autoproxy", AllowAutoproxy)
);
if (AllowAutoproxy.IsPresent)
o.Add("allow_autoproxy", AllowAutoproxy.Value);
public JObject ToJson()
{
var o = new JObject();
if (AllowAutoproxy.IsPresent)
o.Add("allow_autoproxy", AllowAutoproxy.Value);
return o;
}
return o;
}
}

View File

@@ -1,141 +1,138 @@
#nullable enable
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public class GroupPatch: PatchObject
{
public class GroupPatch: PatchObject
public Partial<string> Name { get; set; }
public Partial<string> Hid { get; set; }
public Partial<string?> DisplayName { get; set; }
public Partial<string?> Description { get; set; }
public Partial<string?> Icon { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> IconPrivacy { get; set; }
public Partial<PrivacyLevel> ListPrivacy { get; set; }
public Partial<PrivacyLevel> Visibility { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("display_name", DisplayName)
.With("description", Description)
.With("icon", Icon)
.With("banner_image", BannerImage)
.With("color", Color)
.With("description_privacy", DescriptionPrivacy)
.With("icon_privacy", IconPrivacy)
.With("list_privacy", ListPrivacy)
.With("visibility", Visibility)
);
public new void AssertIsValid()
{
public Partial<string> Name { get; set; }
public Partial<string> Hid { get; set; }
public Partial<string?> DisplayName { get; set; }
public Partial<string?> Description { get; set; }
public Partial<string?> Icon { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> IconPrivacy { get; set; }
public Partial<PrivacyLevel> ListPrivacy { get; set; }
public Partial<PrivacyLevel> Visibility { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("display_name", DisplayName)
.With("description", Description)
.With("icon", Icon)
.With("banner_image", BannerImage)
.With("color", Color)
.With("description_privacy", DescriptionPrivacy)
.With("icon_privacy", IconPrivacy)
.With("list_privacy", ListPrivacy)
.With("visibility", Visibility)
);
public new void AssertIsValid()
{
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxGroupNameLength);
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxGroupNameLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (Icon.Value != null)
AssertValid(Icon.Value, "icon", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
}
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxGroupNameLength);
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxGroupNameLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (Icon.Value != null)
AssertValid(Icon.Value, "icon", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
}
#nullable disable
public static GroupPatch FromJson(JObject o)
public static GroupPatch FromJson(JObject o)
{
var patch = new GroupPatch();
if (o.ContainsKey("name"))
{
var patch = new GroupPatch();
if (o.ContainsKey("name"))
{
patch.Name = o.Value<string>("name").NullIfEmpty();
if (patch.Name.Value == null)
patch.Errors.Add(new ValidationError("name", "Group name can not be set to null."));
}
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("icon")) patch.Icon = o.Value<string>("icon").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var privacy = o.Value<JObject>("privacy");
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
if (privacy.ContainsKey("icon_privacy"))
patch.IconPrivacy = patch.ParsePrivacy(privacy, "icon_privacy");
if (privacy.ContainsKey("list_privacy"))
patch.ListPrivacy = patch.ParsePrivacy(privacy, "list_privacy");
if (privacy.ContainsKey("visibility"))
patch.Visibility = patch.ParsePrivacy(privacy, "visibility");
}
return patch;
patch.Name = o.Value<string>("name").NullIfEmpty();
if (patch.Name.Value == null)
patch.Errors.Add(new ValidationError("name", "Group name can not be set to null."));
}
public JObject ToJson()
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("icon")) patch.Icon = o.Value<string>("icon").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var o = new JObject();
var privacy = o.Value<JObject>("privacy");
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (Icon.IsPresent)
o.Add("icon", Icon.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
if (
DescriptionPrivacy.IsPresent
|| IconPrivacy.IsPresent
|| ListPrivacy.IsPresent
|| Visibility.IsPresent
)
{
var p = new JObject();
if (privacy.ContainsKey("icon_privacy"))
patch.IconPrivacy = patch.ParsePrivacy(privacy, "icon_privacy");
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (privacy.ContainsKey("list_privacy"))
patch.ListPrivacy = patch.ParsePrivacy(privacy, "list_privacy");
if (IconPrivacy.IsPresent)
p.Add("icon_privacy", IconPrivacy.Value.ToJsonString());
if (ListPrivacy.IsPresent)
p.Add("list_privacy", ListPrivacy.Value.ToJsonString());
if (Visibility.IsPresent)
p.Add("visibility", Visibility.Value.ToJsonString());
o.Add("privacy", p);
}
return o;
if (privacy.ContainsKey("visibility"))
patch.Visibility = patch.ParsePrivacy(privacy, "visibility");
}
return patch;
}
public JObject ToJson()
{
var o = new JObject();
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (Icon.IsPresent)
o.Add("icon", Icon.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (
DescriptionPrivacy.IsPresent
|| IconPrivacy.IsPresent
|| ListPrivacy.IsPresent
|| Visibility.IsPresent
)
{
var p = new JObject();
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (IconPrivacy.IsPresent)
p.Add("icon_privacy", IconPrivacy.Value.ToJsonString());
if (ListPrivacy.IsPresent)
p.Add("list_privacy", ListPrivacy.Value.ToJsonString());
if (Visibility.IsPresent)
p.Add("visibility", Visibility.Value.ToJsonString());
o.Add("privacy", p);
}
return o;
}
}

View File

@@ -1,19 +1,18 @@
using SqlKata;
namespace PluralKit.Core
{
public class GuildPatch: PatchObject
{
public Partial<ulong?> LogChannel { get; set; }
public Partial<ulong[]> LogBlacklist { get; set; }
public Partial<ulong[]> Blacklist { get; set; }
public Partial<bool> LogCleanupEnabled { get; set; }
namespace PluralKit.Core;
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("log_channel", LogChannel)
.With("log_blacklist", LogBlacklist)
.With("blacklist", Blacklist)
.With("log_cleanup_enabled", LogCleanupEnabled)
);
}
public class GuildPatch: PatchObject
{
public Partial<ulong?> LogChannel { get; set; }
public Partial<ulong[]> LogBlacklist { get; set; }
public Partial<ulong[]> Blacklist { get; set; }
public Partial<bool> LogCleanupEnabled { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("log_channel", LogChannel)
.With("log_blacklist", LogBlacklist)
.With("blacklist", Blacklist)
.With("log_cleanup_enabled", LogCleanupEnabled)
);
}

View File

@@ -4,54 +4,53 @@ using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public class MemberGuildPatch: PatchObject
{
public class MemberGuildPatch: PatchObject
public Partial<string?> DisplayName { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
);
public new void AssertIsValid()
{
public Partial<string?> DisplayName { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
);
public new void AssertIsValid()
{
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
}
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
}
#nullable disable
public static MemberGuildPatch FromJson(JObject o)
{
var patch = new MemberGuildPatch();
public static MemberGuildPatch FromJson(JObject o)
{
var patch = new MemberGuildPatch();
if (o.ContainsKey("display_name"))
patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("display_name"))
patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("avatar_url"))
patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
if (o.ContainsKey("avatar_url"))
patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
return patch;
}
return patch;
}
public JObject ToJson(ulong guild_id)
{
var o = new JObject();
public JObject ToJson(ulong guild_id)
{
var o = new JObject();
o.Add("guild_id", guild_id.ToString());
o.Add("guild_id", guild_id.ToString());
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
return o;
}
return o;
}
}

View File

@@ -1,268 +1,271 @@
#nullable enable
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using NodaTime;
using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public class MemberPatch: PatchObject
{
public class MemberPatch: PatchObject
public Partial<string> Name { get; set; }
public Partial<string> Hid { get; set; }
public Partial<string?> DisplayName { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<LocalDate?> Birthday { get; set; }
public Partial<string?> Pronouns { get; set; }
public Partial<string?> Description { get; set; }
public Partial<ProxyTag[]> ProxyTags { get; set; }
public Partial<bool> KeepProxy { get; set; }
public Partial<int> MessageCount { get; set; }
public Partial<bool> AllowAutoproxy { get; set; }
public Partial<PrivacyLevel> Visibility { get; set; }
public Partial<PrivacyLevel> NamePrivacy { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> PronounPrivacy { get; set; }
public Partial<PrivacyLevel> BirthdayPrivacy { get; set; }
public Partial<PrivacyLevel> AvatarPrivacy { get; set; }
public Partial<PrivacyLevel> MetadataPrivacy { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
.With("banner_image", BannerImage)
.With("color", Color)
.With("birthday", Birthday)
.With("pronouns", Pronouns)
.With("description", Description)
.With("proxy_tags", ProxyTags)
.With("keep_proxy", KeepProxy)
.With("message_count", MessageCount)
.With("allow_autoproxy", AllowAutoproxy)
.With("member_visibility", Visibility)
.With("name_privacy", NamePrivacy)
.With("description_privacy", DescriptionPrivacy)
.With("pronoun_privacy", PronounPrivacy)
.With("birthday_privacy", BirthdayPrivacy)
.With("avatar_privacy", AvatarPrivacy)
.With("metadata_privacy", MetadataPrivacy)
);
public new void AssertIsValid()
{
public Partial<string> Name { get; set; }
public Partial<string> Hid { get; set; }
public Partial<string?> DisplayName { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<LocalDate?> Birthday { get; set; }
public Partial<string?> Pronouns { get; set; }
public Partial<string?> Description { get; set; }
public Partial<ProxyTag[]> ProxyTags { get; set; }
public Partial<bool> KeepProxy { get; set; }
public Partial<int> MessageCount { get; set; }
public Partial<bool> AllowAutoproxy { get; set; }
public Partial<PrivacyLevel> Visibility { get; set; }
public Partial<PrivacyLevel> NamePrivacy { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> PronounPrivacy { get; set; }
public Partial<PrivacyLevel> BirthdayPrivacy { get; set; }
public Partial<PrivacyLevel> AvatarPrivacy { get; set; }
public Partial<PrivacyLevel> MetadataPrivacy { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
.With("banner_image", BannerImage)
.With("color", Color)
.With("birthday", Birthday)
.With("pronouns", Pronouns)
.With("description", Description)
.With("proxy_tags", ProxyTags)
.With("keep_proxy", KeepProxy)
.With("message_count", MessageCount)
.With("allow_autoproxy", AllowAutoproxy)
.With("member_visibility", Visibility)
.With("name_privacy", NamePrivacy)
.With("description_privacy", DescriptionPrivacy)
.With("pronoun_privacy", PronounPrivacy)
.With("birthday_privacy", BirthdayPrivacy)
.With("avatar_privacy", AvatarPrivacy)
.With("metadata_privacy", MetadataPrivacy)
);
public new void AssertIsValid()
{
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxMemberNameLength);
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
if (Pronouns.Value != null)
AssertValid(Pronouns.Value, "pronouns", Limits.MaxPronounsLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (ProxyTags.IsPresent && (ProxyTags.Value.Length > 100 ||
ProxyTags.Value.Any(tag => tag.ProxyString.IsLongerThan(100))))
// todo: have a better error for this
Errors.Add(new ValidationError("proxy_tags"));
}
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxMemberNameLength);
if (DisplayName.Value != null)
AssertValid(DisplayName.Value, "display_name", Limits.MaxMemberNameLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
if (Pronouns.Value != null)
AssertValid(Pronouns.Value, "pronouns", Limits.MaxPronounsLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (ProxyTags.IsPresent && (ProxyTags.Value.Length > 100 ||
ProxyTags.Value.Any(tag => tag.ProxyString.IsLongerThan(100))))
// todo: have a better error for this
Errors.Add(new ValidationError("proxy_tags"));
}
#nullable disable
public static MemberPatch FromJSON(JObject o, APIVersion v = APIVersion.V1)
public static MemberPatch FromJSON(JObject o, APIVersion v = APIVersion.V1)
{
var patch = new MemberPatch();
if (o.ContainsKey("name"))
{
var patch = new MemberPatch();
if (o.ContainsKey("name"))
{
patch.Name = o.Value<string>("name").NullIfEmpty();
if (patch.Name.Value == null)
patch.Errors.Add(new ValidationError("name", "Member name can not be set to null."));
}
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name");
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("birthday"))
{
var str = o.Value<string>("birthday").NullIfEmpty();
var res = DateTimeFormats.DateExportFormat.Parse(str);
if (res.Success) patch.Birthday = res.Value;
else if (str == null) patch.Birthday = null;
else patch.Errors.Add(new ValidationError("birthday"));
}
if (o.ContainsKey("pronouns")) patch.Pronouns = o.Value<string>("pronouns").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("keep_proxy")) patch.KeepProxy = o.Value<bool>("keep_proxy");
switch (v)
{
case APIVersion.V1:
{
// legacy: used in old export files and APIv1
if (o.ContainsKey("prefix") || o.ContainsKey("suffix") && !o.ContainsKey("proxy_tags"))
patch.ProxyTags = new[] { new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")) };
else if (o.ContainsKey("proxy_tags"))
patch.ProxyTags = o.Value<JArray>("proxy_tags")
.OfType<JObject>().Select(o => new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
.Where(p => p.Valid)
.ToArray();
if (o.ContainsKey("privacy"))
{
var plevel = patch.ParsePrivacy(o, "privacy");
patch.Visibility = plevel;
patch.NamePrivacy = plevel;
patch.AvatarPrivacy = plevel;
patch.DescriptionPrivacy = plevel;
patch.BirthdayPrivacy = plevel;
patch.PronounPrivacy = plevel;
// member.ColorPrivacy = plevel;
patch.MetadataPrivacy = plevel;
}
else
{
if (o.ContainsKey("visibility")) patch.Visibility = patch.ParsePrivacy(o, "visibility");
if (o.ContainsKey("name_privacy")) patch.NamePrivacy = patch.ParsePrivacy(o, "name_privacy");
if (o.ContainsKey("description_privacy")) patch.DescriptionPrivacy = patch.ParsePrivacy(o, "description_privacy");
if (o.ContainsKey("avatar_privacy")) patch.AvatarPrivacy = patch.ParsePrivacy(o, "avatar_privacy");
if (o.ContainsKey("birthday_privacy")) patch.BirthdayPrivacy = patch.ParsePrivacy(o, "birthday_privacy");
if (o.ContainsKey("pronoun_privacy")) patch.PronounPrivacy = patch.ParsePrivacy(o, "pronoun_privacy");
// if (o.ContainsKey("color_privacy")) member.ColorPrivacy = o.ParsePrivacy("member");
if (o.ContainsKey("metadata_privacy")) patch.MetadataPrivacy = patch.ParsePrivacy(o, "metadata_privacy");
}
break;
}
case APIVersion.V2:
{
if (o.ContainsKey("proxy_tags"))
patch.ProxyTags = o.Value<JArray>("proxy_tags")
.OfType<JObject>().Select(o => new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
.Where(p => p.Valid)
.ToArray();
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var privacy = o.Value<JObject>("privacy");
if (privacy.ContainsKey("visibility"))
patch.Visibility = patch.ParsePrivacy(privacy, "visibility");
if (privacy.ContainsKey("name_privacy"))
patch.NamePrivacy = patch.ParsePrivacy(privacy, "name_privacy");
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
if (privacy.ContainsKey("avatar_privacy"))
patch.AvatarPrivacy = patch.ParsePrivacy(privacy, "avatar_privacy");
if (privacy.ContainsKey("birthday_privacy"))
patch.BirthdayPrivacy = patch.ParsePrivacy(privacy, "birthday_privacy");
if (privacy.ContainsKey("pronoun_privacy"))
patch.PronounPrivacy = patch.ParsePrivacy(privacy, "pronoun_privacy");
if (privacy.ContainsKey("metadata_privacy"))
patch.MetadataPrivacy = patch.ParsePrivacy(privacy, "metadata_privacy");
}
break;
}
}
return patch;
patch.Name = o.Value<string>("name").NullIfEmpty();
if (patch.Name.Value == null)
patch.Errors.Add(new ValidationError("name", "Member name can not be set to null."));
}
public JObject ToJson()
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name");
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty()?.ToLower();
if (o.ContainsKey("display_name")) patch.DisplayName = o.Value<string>("display_name").NullIfEmpty();
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("birthday"))
{
var o = new JObject();
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (Birthday.IsPresent)
o.Add("birthday", Birthday.Value?.FormatExport());
if (Pronouns.IsPresent)
o.Add("pronouns", Pronouns.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (ProxyTags.IsPresent)
{
var tagArray = new JArray();
foreach (var tag in ProxyTags.Value)
tagArray.Add(new JObject { { "prefix", tag.Prefix }, { "suffix", tag.Suffix } });
o.Add("proxy_tags", tagArray);
}
if (KeepProxy.IsPresent)
o.Add("keep_proxy", KeepProxy.Value);
if (
Visibility.IsPresent
|| NamePrivacy.IsPresent
|| DescriptionPrivacy.IsPresent
|| PronounPrivacy.IsPresent
|| BirthdayPrivacy.IsPresent
|| AvatarPrivacy.IsPresent
|| MetadataPrivacy.IsPresent
)
{
var p = new JObject();
if (Visibility.IsPresent)
p.Add("visibility", Visibility.Value.ToJsonString());
if (NamePrivacy.IsPresent)
p.Add("name_privacy", NamePrivacy.Value.ToJsonString());
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (PronounPrivacy.IsPresent)
p.Add("pronoun_privacy", PronounPrivacy.Value.ToJsonString());
if (BirthdayPrivacy.IsPresent)
p.Add("birthday_privacy", BirthdayPrivacy.Value.ToJsonString());
if (AvatarPrivacy.IsPresent)
p.Add("avatar_privacy", AvatarPrivacy.Value.ToJsonString());
if (MetadataPrivacy.IsPresent)
p.Add("metadata_privacy", MetadataPrivacy.Value.ToJsonString());
o.Add("privacy", p);
}
return o;
var str = o.Value<string>("birthday").NullIfEmpty();
var res = DateTimeFormats.DateExportFormat.Parse(str);
if (res.Success) patch.Birthday = res.Value;
else if (str == null) patch.Birthday = null;
else patch.Errors.Add(new ValidationError("birthday"));
}
if (o.ContainsKey("pronouns")) patch.Pronouns = o.Value<string>("pronouns").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("keep_proxy")) patch.KeepProxy = o.Value<bool>("keep_proxy");
switch (v)
{
case APIVersion.V1:
{
// legacy: used in old export files and APIv1
if (o.ContainsKey("prefix") || o.ContainsKey("suffix") && !o.ContainsKey("proxy_tags"))
patch.ProxyTags = new[] { new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")) };
else if (o.ContainsKey("proxy_tags"))
patch.ProxyTags = o.Value<JArray>("proxy_tags")
.OfType<JObject>().Select(o =>
new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
.Where(p => p.Valid)
.ToArray();
if (o.ContainsKey("privacy"))
{
var plevel = patch.ParsePrivacy(o, "privacy");
patch.Visibility = plevel;
patch.NamePrivacy = plevel;
patch.AvatarPrivacy = plevel;
patch.DescriptionPrivacy = plevel;
patch.BirthdayPrivacy = plevel;
patch.PronounPrivacy = plevel;
// member.ColorPrivacy = plevel;
patch.MetadataPrivacy = plevel;
}
else
{
if (o.ContainsKey("visibility")) patch.Visibility = patch.ParsePrivacy(o, "visibility");
if (o.ContainsKey("name_privacy")) patch.NamePrivacy = patch.ParsePrivacy(o, "name_privacy");
if (o.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(o, "description_privacy");
if (o.ContainsKey("avatar_privacy"))
patch.AvatarPrivacy = patch.ParsePrivacy(o, "avatar_privacy");
if (o.ContainsKey("birthday_privacy"))
patch.BirthdayPrivacy = patch.ParsePrivacy(o, "birthday_privacy");
if (o.ContainsKey("pronoun_privacy"))
patch.PronounPrivacy = patch.ParsePrivacy(o, "pronoun_privacy");
// if (o.ContainsKey("color_privacy")) member.ColorPrivacy = o.ParsePrivacy("member");
if (o.ContainsKey("metadata_privacy"))
patch.MetadataPrivacy = patch.ParsePrivacy(o, "metadata_privacy");
}
break;
}
case APIVersion.V2:
{
if (o.ContainsKey("proxy_tags"))
patch.ProxyTags = o.Value<JArray>("proxy_tags")
.OfType<JObject>().Select(o =>
new ProxyTag(o.Value<string>("prefix"), o.Value<string>("suffix")))
.Where(p => p.Valid)
.ToArray();
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var privacy = o.Value<JObject>("privacy");
if (privacy.ContainsKey("visibility"))
patch.Visibility = patch.ParsePrivacy(privacy, "visibility");
if (privacy.ContainsKey("name_privacy"))
patch.NamePrivacy = patch.ParsePrivacy(privacy, "name_privacy");
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
if (privacy.ContainsKey("avatar_privacy"))
patch.AvatarPrivacy = patch.ParsePrivacy(privacy, "avatar_privacy");
if (privacy.ContainsKey("birthday_privacy"))
patch.BirthdayPrivacy = patch.ParsePrivacy(privacy, "birthday_privacy");
if (privacy.ContainsKey("pronoun_privacy"))
patch.PronounPrivacy = patch.ParsePrivacy(privacy, "pronoun_privacy");
if (privacy.ContainsKey("metadata_privacy"))
patch.MetadataPrivacy = patch.ParsePrivacy(privacy, "metadata_privacy");
}
break;
}
}
return patch;
}
public JObject ToJson()
{
var o = new JObject();
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (DisplayName.IsPresent)
o.Add("display_name", DisplayName.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (Birthday.IsPresent)
o.Add("birthday", Birthday.Value?.FormatExport());
if (Pronouns.IsPresent)
o.Add("pronouns", Pronouns.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (ProxyTags.IsPresent)
{
var tagArray = new JArray();
foreach (var tag in ProxyTags.Value)
tagArray.Add(new JObject { { "prefix", tag.Prefix }, { "suffix", tag.Suffix } });
o.Add("proxy_tags", tagArray);
}
if (KeepProxy.IsPresent)
o.Add("keep_proxy", KeepProxy.Value);
if (
Visibility.IsPresent
|| NamePrivacy.IsPresent
|| DescriptionPrivacy.IsPresent
|| PronounPrivacy.IsPresent
|| BirthdayPrivacy.IsPresent
|| AvatarPrivacy.IsPresent
|| MetadataPrivacy.IsPresent
)
{
var p = new JObject();
if (Visibility.IsPresent)
p.Add("visibility", Visibility.Value.ToJsonString());
if (NamePrivacy.IsPresent)
p.Add("name_privacy", NamePrivacy.Value.ToJsonString());
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (PronounPrivacy.IsPresent)
p.Add("pronoun_privacy", PronounPrivacy.Value.ToJsonString());
if (BirthdayPrivacy.IsPresent)
p.Add("birthday_privacy", BirthdayPrivacy.Value.ToJsonString());
if (AvatarPrivacy.IsPresent)
p.Add("avatar_privacy", AvatarPrivacy.Value.ToJsonString());
if (MetadataPrivacy.IsPresent)
p.Add("metadata_privacy", MetadataPrivacy.Value.ToJsonString());
o.Add("privacy", p);
}
return o;
}
}

View File

@@ -1,46 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public abstract class PatchObject
{
public abstract class PatchObject
public List<ValidationError> Errors = new();
public abstract Query Apply(Query q);
public void AssertIsValid() { }
protected void AssertValid(string input, string name, int maxLength, Func<string, bool>? validate = null)
{
public List<ValidationError> Errors = new();
public abstract Query Apply(Query q);
if (input.Length > maxLength)
Errors.Add(new FieldTooLongError(name, maxLength, input.Length));
if (validate != null && !validate(input))
Errors.Add(new ValidationError(name));
}
public void AssertIsValid() { }
protected void AssertValid(string input, string name, string pattern)
{
if (!Regex.IsMatch(input, pattern))
Errors.Add(new ValidationError(name));
}
protected void AssertValid(string input, string name, int maxLength, Func<string, bool>? validate = null)
{
if (input.Length > maxLength)
Errors.Add(new FieldTooLongError(name, maxLength, input.Length));
if (validate != null && !validate(input))
Errors.Add(new ValidationError(name));
}
public PrivacyLevel ParsePrivacy(JObject o, string propertyName)
{
var input = o.Value<string>(propertyName);
protected void AssertValid(string input, string name, string pattern)
{
if (!Regex.IsMatch(input, pattern))
Errors.Add(new ValidationError(name));
}
if (input == null) return PrivacyLevel.Public;
if (input == "") return PrivacyLevel.Private;
if (input == "private") return PrivacyLevel.Private;
if (input == "public") return PrivacyLevel.Public;
public PrivacyLevel ParsePrivacy(JObject o, string propertyName)
{
var input = o.Value<string>(propertyName);
if (input == null) return PrivacyLevel.Public;
if (input == "") return PrivacyLevel.Private;
if (input == "private") return PrivacyLevel.Private;
if (input == "public") return PrivacyLevel.Public;
Errors.Add(new ValidationError(propertyName));
// unused, but the compiler will complain if this isn't here
return PrivacyLevel.Private;
}
Errors.Add(new ValidationError(propertyName));
// unused, but the compiler will complain if this isn't here
return PrivacyLevel.Private;
}
}

View File

@@ -4,80 +4,79 @@ using Newtonsoft.Json.Linq;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public class SystemGuildPatch: PatchObject
{
public class SystemGuildPatch: PatchObject
public Partial<bool> ProxyEnabled { get; set; }
public Partial<AutoproxyMode> AutoproxyMode { get; set; }
public Partial<MemberId?> AutoproxyMember { get; set; }
public Partial<string?> Tag { get; set; }
public Partial<bool?> TagEnabled { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("proxy_enabled", ProxyEnabled)
.With("autoproxy_mode", AutoproxyMode)
.With("autoproxy_member", AutoproxyMember)
.With("tag", Tag)
.With("tag_enabled", TagEnabled)
);
public new void AssertIsValid()
{
public Partial<bool> ProxyEnabled { get; set; }
public Partial<AutoproxyMode> AutoproxyMode { get; set; }
public Partial<MemberId?> AutoproxyMember { get; set; }
public Partial<string?> Tag { get; set; }
public Partial<bool?> TagEnabled { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("proxy_enabled", ProxyEnabled)
.With("autoproxy_mode", AutoproxyMode)
.With("autoproxy_member", AutoproxyMember)
.With("tag", Tag)
.With("tag_enabled", TagEnabled)
);
public new void AssertIsValid()
{
if (Tag.Value != null)
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
}
if (Tag.Value != null)
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
}
#nullable disable
public static SystemGuildPatch FromJson(JObject o, MemberId? memberId)
public static SystemGuildPatch FromJson(JObject o, MemberId? memberId)
{
var patch = new SystemGuildPatch();
if (o.ContainsKey("proxying_enabled") && o["proxying_enabled"].Type != JTokenType.Null)
patch.ProxyEnabled = o.Value<bool>("proxying_enabled");
if (o.ContainsKey("autoproxy_mode"))
{
var patch = new SystemGuildPatch();
if (o.ContainsKey("proxying_enabled") && o["proxying_enabled"].Type != JTokenType.Null)
patch.ProxyEnabled = o.Value<bool>("proxying_enabled");
if (o.ContainsKey("autoproxy_mode"))
{
var (val, err) = o["autoproxy_mode"].ParseAutoproxyMode();
if (err != null)
patch.Errors.Add(err);
else
patch.AutoproxyMode = val.Value;
}
patch.AutoproxyMember = memberId;
if (o.ContainsKey("tag"))
patch.Tag = o.Value<string>("tag").NullIfEmpty();
if (o.ContainsKey("tag_enabled") && o["tag_enabled"].Type != JTokenType.Null)
patch.TagEnabled = o.Value<bool>("tag_enabled");
return patch;
var (val, err) = o["autoproxy_mode"].ParseAutoproxyMode();
if (err != null)
patch.Errors.Add(err);
else
patch.AutoproxyMode = val.Value;
}
public JObject ToJson(string memberRef, ulong guild_id)
{
var o = new JObject();
patch.AutoproxyMember = memberId;
o.Add("guild_id", guild_id.ToString());
if (o.ContainsKey("tag"))
patch.Tag = o.Value<string>("tag").NullIfEmpty();
if (ProxyEnabled.IsPresent)
o.Add("proxying_enabled", ProxyEnabled.Value);
if (o.ContainsKey("tag_enabled") && o["tag_enabled"].Type != JTokenType.Null)
patch.TagEnabled = o.Value<bool>("tag_enabled");
if (AutoproxyMode.IsPresent)
o.Add("autoproxy_mode", AutoproxyMode.Value.ToString().ToLower());
return patch;
}
if (AutoproxyMember.IsPresent)
o.Add("autoproxy_member", memberRef);
public JObject ToJson(string memberRef, ulong guild_id)
{
var o = new JObject();
if (Tag.IsPresent)
o.Add("tag", Tag.Value);
o.Add("guild_id", guild_id.ToString());
if (TagEnabled.IsPresent)
o.Add("tag_enabled", TagEnabled.Value);
if (ProxyEnabled.IsPresent)
o.Add("proxying_enabled", ProxyEnabled.Value);
return o;
}
if (AutoproxyMode.IsPresent)
o.Add("autoproxy_mode", AutoproxyMode.Value.ToString().ToLower());
if (AutoproxyMember.IsPresent)
o.Add("autoproxy_member", memberRef);
if (Tag.IsPresent)
o.Add("tag", Tag.Value);
if (TagEnabled.IsPresent)
o.Add("tag_enabled", TagEnabled.Value);
return o;
}
}

View File

@@ -1,186 +1,185 @@
#nullable enable
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using NodaTime;
using SqlKata;
namespace PluralKit.Core
namespace PluralKit.Core;
public class SystemPatch: PatchObject
{
public class SystemPatch: PatchObject
public Partial<string?> Name { get; set; }
public Partial<string?> Hid { get; set; }
public Partial<string?> Description { get; set; }
public Partial<string?> Tag { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<string?> Token { get; set; }
public Partial<string?> WebhookUrl { get; set; }
public Partial<string?> WebhookToken { get; set; }
public Partial<string> UiTz { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> MemberListPrivacy { get; set; }
public Partial<PrivacyLevel> GroupListPrivacy { get; set; }
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
public Partial<bool> PingsEnabled { get; set; }
public Partial<int?> LatchTimeout { get; set; }
public Partial<int?> MemberLimitOverride { get; set; }
public Partial<int?> GroupLimitOverride { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("description", Description)
.With("tag", Tag)
.With("avatar_url", AvatarUrl)
.With("banner_image", BannerImage)
.With("color", Color)
.With("token", Token)
.With("webhook_url", WebhookUrl)
.With("webhook_token", WebhookToken)
.With("ui_tz", UiTz)
.With("description_privacy", DescriptionPrivacy)
.With("member_list_privacy", MemberListPrivacy)
.With("group_list_privacy", GroupListPrivacy)
.With("front_privacy", FrontPrivacy)
.With("front_history_privacy", FrontHistoryPrivacy)
.With("pings_enabled", PingsEnabled)
.With("latch_timeout", LatchTimeout)
.With("member_limit_override", MemberLimitOverride)
.With("group_limit_override", GroupLimitOverride)
);
public new void AssertIsValid()
{
public Partial<string?> Name { get; set; }
public Partial<string?> Hid { get; set; }
public Partial<string?> Description { get; set; }
public Partial<string?> Tag { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> BannerImage { get; set; }
public Partial<string?> Color { get; set; }
public Partial<string?> Token { get; set; }
public Partial<string?> WebhookUrl { get; set; }
public Partial<string?> WebhookToken { get; set; }
public Partial<string> UiTz { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> MemberListPrivacy { get; set; }
public Partial<PrivacyLevel> GroupListPrivacy { get; set; }
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
public Partial<bool> PingsEnabled { get; set; }
public Partial<int?> LatchTimeout { get; set; }
public Partial<int?> MemberLimitOverride { get; set; }
public Partial<int?> GroupLimitOverride { get; set; }
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
.With("name", Name)
.With("hid", Hid)
.With("description", Description)
.With("tag", Tag)
.With("avatar_url", AvatarUrl)
.With("banner_image", BannerImage)
.With("color", Color)
.With("token", Token)
.With("webhook_url", WebhookUrl)
.With("webhook_token", WebhookToken)
.With("ui_tz", UiTz)
.With("description_privacy", DescriptionPrivacy)
.With("member_list_privacy", MemberListPrivacy)
.With("group_list_privacy", GroupListPrivacy)
.With("front_privacy", FrontPrivacy)
.With("front_history_privacy", FrontHistoryPrivacy)
.With("pings_enabled", PingsEnabled)
.With("latch_timeout", LatchTimeout)
.With("member_limit_override", MemberLimitOverride)
.With("group_limit_override", GroupLimitOverride)
);
public new void AssertIsValid()
{
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxSystemNameLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (Tag.Value != null)
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
if (UiTz.IsPresent && DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz.Value) == null)
Errors.Add(new ValidationError("timezone"));
}
if (Name.Value != null)
AssertValid(Name.Value, "name", Limits.MaxSystemNameLength);
if (Description.Value != null)
AssertValid(Description.Value, "description", Limits.MaxDescriptionLength);
if (Tag.Value != null)
AssertValid(Tag.Value, "tag", Limits.MaxSystemTagLength);
if (AvatarUrl.Value != null)
AssertValid(AvatarUrl.Value, "avatar_url", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var avatarUri));
if (BannerImage.Value != null)
AssertValid(BannerImage.Value, "banner", Limits.MaxUriLength,
s => MiscUtils.TryMatchUri(s, out var bannerUri));
if (Color.Value != null)
AssertValid(Color.Value, "color", "^[0-9a-fA-F]{6}$");
if (UiTz.IsPresent && DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz.Value) == null)
Errors.Add(new ValidationError("timezone"));
}
#nullable disable
public static SystemPatch FromJSON(JObject o, APIVersion v = APIVersion.V1)
public static SystemPatch FromJSON(JObject o, APIVersion v = APIVersion.V1)
{
var patch = new SystemPatch();
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("tag")) patch.Tag = o.Value<string>("tag").NullIfEmpty();
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty();
if (o.ContainsKey("timezone")) patch.UiTz = o.Value<string>("timezone") ?? "UTC";
switch (v)
{
var patch = new SystemPatch();
if (o.ContainsKey("name")) patch.Name = o.Value<string>("name").NullIfEmpty();
if (o.ContainsKey("description")) patch.Description = o.Value<string>("description").NullIfEmpty();
if (o.ContainsKey("tag")) patch.Tag = o.Value<string>("tag").NullIfEmpty();
if (o.ContainsKey("avatar_url")) patch.AvatarUrl = o.Value<string>("avatar_url").NullIfEmpty();
if (o.ContainsKey("banner")) patch.BannerImage = o.Value<string>("banner").NullIfEmpty();
if (o.ContainsKey("color")) patch.Color = o.Value<string>("color").NullIfEmpty();
if (o.ContainsKey("timezone")) patch.UiTz = o.Value<string>("timezone") ?? "UTC";
case APIVersion.V1:
{
if (o.ContainsKey("tz")) patch.UiTz = o.Value<string>("tz") ?? "UTC";
switch (v)
{
case APIVersion.V1:
if (o.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(o, "description_privacy");
if (o.ContainsKey("member_list_privacy"))
patch.MemberListPrivacy = patch.ParsePrivacy(o, "member_list_privacy");
if (o.ContainsKey("front_privacy")) patch.FrontPrivacy = patch.ParsePrivacy(o, "front_privacy");
if (o.ContainsKey("front_history_privacy"))
patch.FrontHistoryPrivacy = patch.ParsePrivacy(o, "front_history_privacy");
break;
}
case APIVersion.V2:
{
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
if (o.ContainsKey("tz")) patch.UiTz = o.Value<string>("tz") ?? "UTC";
var privacy = o.Value<JObject>("privacy");
if (o.ContainsKey("description_privacy")) patch.DescriptionPrivacy = patch.ParsePrivacy(o, "description_privacy");
if (o.ContainsKey("member_list_privacy")) patch.MemberListPrivacy = patch.ParsePrivacy(o, "member_list_privacy");
if (o.ContainsKey("front_privacy")) patch.FrontPrivacy = patch.ParsePrivacy(o, "front_privacy");
if (o.ContainsKey("front_history_privacy")) patch.FrontHistoryPrivacy = patch.ParsePrivacy(o, "front_history_privacy");
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
break;
if (privacy.ContainsKey("member_list_privacy"))
patch.MemberListPrivacy = patch.ParsePrivacy(privacy, "member_list_privacy");
if (privacy.ContainsKey("group_list_privacy"))
patch.GroupListPrivacy = patch.ParsePrivacy(privacy, "group_list_privacy");
if (privacy.ContainsKey("front_privacy"))
patch.FrontPrivacy = patch.ParsePrivacy(privacy, "front_privacy");
if (privacy.ContainsKey("front_history_privacy"))
patch.FrontHistoryPrivacy = patch.ParsePrivacy(privacy, "front_history_privacy");
}
case APIVersion.V2:
{
if (o.ContainsKey("privacy") && o["privacy"].Type != JTokenType.Null)
{
var privacy = o.Value<JObject>("privacy");
if (privacy.ContainsKey("description_privacy"))
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
if (privacy.ContainsKey("member_list_privacy"))
patch.MemberListPrivacy = patch.ParsePrivacy(privacy, "member_list_privacy");
if (privacy.ContainsKey("group_list_privacy"))
patch.GroupListPrivacy = patch.ParsePrivacy(privacy, "group_list_privacy");
if (privacy.ContainsKey("front_privacy"))
patch.FrontPrivacy = patch.ParsePrivacy(privacy, "front_privacy");
if (privacy.ContainsKey("front_history_privacy"))
patch.FrontHistoryPrivacy = patch.ParsePrivacy(privacy, "front_history_privacy");
}
break;
}
}
return patch;
break;
}
}
public JObject ToJson()
return patch;
}
public JObject ToJson()
{
var o = new JObject();
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (Tag.IsPresent)
o.Add("tag", Tag.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (UiTz.IsPresent)
o.Add("timezone", UiTz.Value);
if (
DescriptionPrivacy.IsPresent
|| MemberListPrivacy.IsPresent
|| GroupListPrivacy.IsPresent
|| FrontPrivacy.IsPresent
|| FrontHistoryPrivacy.IsPresent
)
{
var o = new JObject();
var p = new JObject();
if (Name.IsPresent)
o.Add("name", Name.Value);
if (Hid.IsPresent)
o.Add("id", Hid.Value);
if (Description.IsPresent)
o.Add("description", Description.Value);
if (Tag.IsPresent)
o.Add("tag", Tag.Value);
if (AvatarUrl.IsPresent)
o.Add("avatar_url", AvatarUrl.Value);
if (BannerImage.IsPresent)
o.Add("banner", BannerImage.Value);
if (Color.IsPresent)
o.Add("color", Color.Value);
if (UiTz.IsPresent)
o.Add("timezone", UiTz.Value);
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (
DescriptionPrivacy.IsPresent
|| MemberListPrivacy.IsPresent
|| GroupListPrivacy.IsPresent
|| FrontPrivacy.IsPresent
|| FrontHistoryPrivacy.IsPresent
)
{
var p = new JObject();
if (MemberListPrivacy.IsPresent)
p.Add("member_list_privacy", MemberListPrivacy.Value.ToJsonString());
if (DescriptionPrivacy.IsPresent)
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
if (GroupListPrivacy.IsPresent)
p.Add("group_list_privacy", GroupListPrivacy.Value.ToJsonString());
if (MemberListPrivacy.IsPresent)
p.Add("member_list_privacy", MemberListPrivacy.Value.ToJsonString());
if (FrontPrivacy.IsPresent)
p.Add("front_privacy", FrontPrivacy.Value.ToJsonString());
if (GroupListPrivacy.IsPresent)
p.Add("group_list_privacy", GroupListPrivacy.Value.ToJsonString());
if (FrontHistoryPrivacy.IsPresent)
p.Add("front_history_privacy", FrontHistoryPrivacy.Value.ToJsonString());
if (FrontPrivacy.IsPresent)
p.Add("front_privacy", FrontPrivacy.Value.ToJsonString());
if (FrontHistoryPrivacy.IsPresent)
p.Add("front_history_privacy", FrontHistoryPrivacy.Value.ToJsonString());
o.Add("privacy", p);
}
return o;
o.Add("privacy", p);
}
return o;
}
}

View File

@@ -1,72 +1,69 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum GroupPrivacySubject
{
public enum GroupPrivacySubject
Description,
Icon,
List,
Visibility
}
public static class GroupPrivacyUtils
{
public static GroupPatch WithPrivacy(this GroupPatch group, GroupPrivacySubject subject, PrivacyLevel level)
{
Description,
Icon,
List,
Visibility
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
GroupPrivacySubject.Description => group.DescriptionPrivacy = level,
GroupPrivacySubject.Icon => group.IconPrivacy = level,
GroupPrivacySubject.List => group.ListPrivacy = level,
GroupPrivacySubject.Visibility => group.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return group;
}
public static class GroupPrivacyUtils
public static GroupPatch WithAllPrivacy(this GroupPatch member, PrivacyLevel level)
{
public static GroupPatch WithPrivacy(this GroupPatch group, GroupPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
GroupPrivacySubject.Description => group.DescriptionPrivacy = level,
GroupPrivacySubject.Icon => group.IconPrivacy = level,
GroupPrivacySubject.List => group.ListPrivacy = level,
GroupPrivacySubject.Visibility => group.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
foreach (var subject in Enum.GetValues(typeof(GroupPrivacySubject)))
member.WithPrivacy((GroupPrivacySubject)subject, level);
return member;
}
return group;
public static bool TryParseGroupPrivacy(string input, out GroupPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = GroupPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = GroupPrivacySubject.Icon;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
subject = GroupPrivacySubject.Visibility;
break;
case "list":
case "listing":
case "members":
subject = GroupPrivacySubject.List;
break;
default:
subject = default;
return false;
}
public static GroupPatch WithAllPrivacy(this GroupPatch member, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(GroupPrivacySubject)))
member.WithPrivacy((GroupPrivacySubject)subject, level);
return member;
}
public static bool TryParseGroupPrivacy(string input, out GroupPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = GroupPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = GroupPrivacySubject.Icon;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
subject = GroupPrivacySubject.Visibility;
break;
case "list":
case "listing":
case "members":
subject = GroupPrivacySubject.List;
break;
default:
subject = default;
return false;
}
return true;
}
return true;
}
}

View File

@@ -1,9 +1,8 @@
namespace PluralKit.Core
namespace PluralKit.Core;
public enum LookupContext
{
public enum LookupContext
{
ByOwner,
ByNonOwner,
API
}
ByOwner,
ByNonOwner,
API
}

View File

@@ -1,93 +1,90 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum MemberPrivacySubject
{
public enum MemberPrivacySubject
Visibility,
Name,
Description,
Avatar,
Birthday,
Pronouns,
Metadata
}
public static class MemberPrivacyUtils
{
public static MemberPatch WithPrivacy(this MemberPatch member, MemberPrivacySubject subject, PrivacyLevel level)
{
Visibility,
Name,
Description,
Avatar,
Birthday,
Pronouns,
Metadata
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
MemberPrivacySubject.Name => member.NamePrivacy = level,
MemberPrivacySubject.Description => member.DescriptionPrivacy = level,
MemberPrivacySubject.Avatar => member.AvatarPrivacy = level,
MemberPrivacySubject.Pronouns => member.PronounPrivacy = level,
MemberPrivacySubject.Birthday => member.BirthdayPrivacy = level,
MemberPrivacySubject.Metadata => member.MetadataPrivacy = level,
MemberPrivacySubject.Visibility => member.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return member;
}
public static class MemberPrivacyUtils
public static MemberPatch WithAllPrivacy(this MemberPatch member, PrivacyLevel level)
{
public static MemberPatch WithPrivacy(this MemberPatch member, MemberPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
MemberPrivacySubject.Name => member.NamePrivacy = level,
MemberPrivacySubject.Description => member.DescriptionPrivacy = level,
MemberPrivacySubject.Avatar => member.AvatarPrivacy = level,
MemberPrivacySubject.Pronouns => member.PronounPrivacy = level,
MemberPrivacySubject.Birthday => member.BirthdayPrivacy = level,
MemberPrivacySubject.Metadata => member.MetadataPrivacy = level,
MemberPrivacySubject.Visibility => member.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
foreach (var subject in Enum.GetValues(typeof(MemberPrivacySubject)))
member.WithPrivacy((MemberPrivacySubject)subject, level);
return member;
}
return member;
public static bool TryParseMemberPrivacy(string input, out MemberPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "name":
subject = MemberPrivacySubject.Name;
break;
case "description":
case "desc":
case "text":
case "info":
subject = MemberPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = MemberPrivacySubject.Avatar;
break;
case "birthday":
case "birth":
case "bday":
case "birthdate":
case "bdate":
subject = MemberPrivacySubject.Birthday;
break;
case "pronouns":
case "pronoun":
subject = MemberPrivacySubject.Pronouns;
break;
case "meta":
case "metadata":
case "created":
subject = MemberPrivacySubject.Metadata;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
case "list":
subject = MemberPrivacySubject.Visibility;
break;
default:
subject = MemberPrivacySubject.Name;
return false;
}
public static MemberPatch WithAllPrivacy(this MemberPatch member, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(MemberPrivacySubject)))
member.WithPrivacy((MemberPrivacySubject)subject, level);
return member;
}
public static bool TryParseMemberPrivacy(string input, out MemberPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "name":
subject = MemberPrivacySubject.Name;
break;
case "description":
case "desc":
case "text":
case "info":
subject = MemberPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = MemberPrivacySubject.Avatar;
break;
case "birthday":
case "birth":
case "bday":
case "birthdate":
case "bdate":
subject = MemberPrivacySubject.Birthday;
break;
case "pronouns":
case "pronoun":
subject = MemberPrivacySubject.Pronouns;
break;
case "meta":
case "metadata":
case "created":
subject = MemberPrivacySubject.Metadata;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
case "list":
subject = MemberPrivacySubject.Visibility;
break;
default:
subject = MemberPrivacySubject.Name;
return false;
}
return true;
}
return true;
}
}

View File

@@ -1,44 +1,42 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum PrivacyLevel
{
public enum PrivacyLevel
{
Public = 1,
Private = 2
}
Public = 1,
Private = 2
}
public static class PrivacyLevelExt
{
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
level == PrivacyLevel.Public || ctx == LookupContext.ByOwner;
public static class PrivacyLevelExt
{
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
level == PrivacyLevel.Public || ctx == LookupContext.ByOwner;
public static string LevelName(this PrivacyLevel level) =>
level == PrivacyLevel.Public ? "public" : "private";
public static string LevelName(this PrivacyLevel level) =>
level == PrivacyLevel.Public ? "public" : "private";
public static T Get<T>(this PrivacyLevel level, LookupContext ctx, T input, T fallback = default) =>
level.CanAccess(ctx) ? input : fallback;
public static T Get<T>(this PrivacyLevel level, LookupContext ctx, T input, T fallback = default) =>
level.CanAccess(ctx) ? input : fallback;
public static string Explanation(this PrivacyLevel level) =>
level switch
{
PrivacyLevel.Private => "**Private** (visible only when queried by you)",
PrivacyLevel.Public => "**Public** (visible to everyone)",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
public static bool TryGet<T>(this PrivacyLevel level, LookupContext ctx, T input, out T output, T absentValue = default)
public static string Explanation(this PrivacyLevel level) =>
level switch
{
output = default;
if (!level.CanAccess(ctx))
return false;
if (Equals(input, absentValue))
return false;
PrivacyLevel.Private => "**Private** (visible only when queried by you)",
PrivacyLevel.Public => "**Public** (visible to everyone)",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
output = input;
return true;
}
public static bool TryGet<T>(this PrivacyLevel level, LookupContext ctx, T input, out T output,
T absentValue = default)
{
output = default;
if (!level.CanAccess(ctx))
return false;
if (Equals(input, absentValue))
return false;
public static string ToJsonString(this PrivacyLevel level) => level.LevelName();
output = input;
return true;
}
public static string ToJsonString(this PrivacyLevel level) => level.LevelName();
}

View File

@@ -1,77 +1,75 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum SystemPrivacySubject
{
public enum SystemPrivacySubject
Description,
MemberList,
GroupList,
Front,
FrontHistory
}
public static class SystemPrivacyUtils
{
public static SystemPatch WithPrivacy(this SystemPatch system, SystemPrivacySubject subject, PrivacyLevel level)
{
Description,
MemberList,
GroupList,
Front,
FrontHistory
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
SystemPrivacySubject.Description => system.DescriptionPrivacy = level,
SystemPrivacySubject.Front => system.FrontPrivacy = level,
SystemPrivacySubject.FrontHistory => system.FrontHistoryPrivacy = level,
SystemPrivacySubject.MemberList => system.MemberListPrivacy = level,
SystemPrivacySubject.GroupList => system.GroupListPrivacy = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return system;
}
public static class SystemPrivacyUtils
public static SystemPatch WithAllPrivacy(this SystemPatch system, PrivacyLevel level)
{
public static SystemPatch WithPrivacy(this SystemPatch system, SystemPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
SystemPrivacySubject.Description => system.DescriptionPrivacy = level,
SystemPrivacySubject.Front => system.FrontPrivacy = level,
SystemPrivacySubject.FrontHistory => system.FrontHistoryPrivacy = level,
SystemPrivacySubject.MemberList => system.MemberListPrivacy = level,
SystemPrivacySubject.GroupList => system.GroupListPrivacy = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
foreach (var subject in Enum.GetValues(typeof(SystemPrivacySubject)))
WithPrivacy(system, (SystemPrivacySubject)subject, level);
return system;
}
return system;
public static bool TryParseSystemPrivacy(string input, out SystemPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = SystemPrivacySubject.Description;
break;
case "members":
case "memberlist":
case "list":
case "mlist":
subject = SystemPrivacySubject.MemberList;
break;
case "fronter":
case "fronters":
case "front":
subject = SystemPrivacySubject.Front;
break;
case "switch":
case "switches":
case "fronthistory":
case "fh":
subject = SystemPrivacySubject.FrontHistory;
break;
case "groups":
case "gs":
subject = SystemPrivacySubject.GroupList;
break;
default:
subject = default;
return false;
}
public static SystemPatch WithAllPrivacy(this SystemPatch system, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(SystemPrivacySubject)))
WithPrivacy(system, (SystemPrivacySubject)subject, level);
return system;
}
public static bool TryParseSystemPrivacy(string input, out SystemPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = SystemPrivacySubject.Description;
break;
case "members":
case "memberlist":
case "list":
case "mlist":
subject = SystemPrivacySubject.MemberList;
break;
case "fronter":
case "fronters":
case "front":
subject = SystemPrivacySubject.Front;
break;
case "switch":
case "switches":
case "fronthistory":
case "fh":
subject = SystemPrivacySubject.FrontHistory;
break;
case "groups":
case "gs":
subject = SystemPrivacySubject.GroupList;
break;
default:
subject = default;
return false;
}
return true;
}
return true;
}
}

View File

@@ -1,39 +1,38 @@
using Newtonsoft.Json;
namespace PluralKit.Core
namespace PluralKit.Core;
public struct ProxyTag
{
public struct ProxyTag
public ProxyTag(string prefix, string suffix)
{
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 bool Valid =>
Prefix != null || Suffix != null
&& ProxyString.Length <= Limits.MaxProxyTagLength;
[JsonIgnore] public string ProxyString => $"{Prefix ?? ""}text{Suffix ?? ""}";
[JsonIgnore] public bool IsEmpty => Prefix == null && Suffix == null;
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
{
// 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 bool Valid =>
Prefix != null || Suffix != null
&& ProxyString.Length <= Limits.MaxProxyTagLength;
[JsonIgnore] public string ProxyString => $"{Prefix ?? ""}text{Suffix ?? ""}";
[JsonIgnore] public bool IsEmpty => Prefix == null && Suffix == null;
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);
}
return ((Prefix != null ? Prefix.GetHashCode() : 0) * 397) ^
(Suffix != null ? Suffix.GetHashCode() : 0);
}
}
}

View File

@@ -1,68 +1,68 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace PluralKit.Core
namespace PluralKit.Core;
[JsonConverter(typeof(StringEnumConverter))]
public enum AutoproxyMode
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AutoproxyMode
Off = 1,
Front = 2,
Latch = 3,
Member = 4
}
public class SystemGuildSettings
{
public ulong Guild { get; }
public SystemId System { get; }
public bool ProxyEnabled { get; } = true;
public AutoproxyMode AutoproxyMode { get; } = AutoproxyMode.Off;
public MemberId? AutoproxyMember { get; }
public string? Tag { get; }
public bool TagEnabled { get; }
}
public static class SystemGuildExt
{
public static JObject ToJson(this SystemGuildSettings settings, string? memberHid = null)
{
Off = 1,
Front = 2,
Latch = 3,
Member = 4
var o = new JObject();
o.Add("proxying_enabled", settings.ProxyEnabled);
o.Add("autoproxy_mode", settings.AutoproxyMode.ToString().ToLower());
o.Add("autoproxy_member", memberHid);
o.Add("tag", settings.Tag);
o.Add("tag_enabled", settings.TagEnabled);
return o;
}
public class SystemGuildSettings
public static (AutoproxyMode?, ValidationError?) ParseAutoproxyMode(this JToken o)
{
public ulong Guild { get; }
public SystemId System { get; }
public bool ProxyEnabled { get; } = true;
if (o.Type == JTokenType.Null)
return (AutoproxyMode.Off, null);
if (o.Type != JTokenType.String)
return (null, new ValidationError("autoproxy_mode"));
public AutoproxyMode AutoproxyMode { get; } = AutoproxyMode.Off;
public MemberId? AutoproxyMember { get; }
var value = o.Value<string>();
public string? Tag { get; }
public bool TagEnabled { get; }
}
public static class SystemGuildExt
{
public static JObject ToJson(this SystemGuildSettings settings, string? memberHid = null)
switch (value)
{
var o = new JObject();
o.Add("proxying_enabled", settings.ProxyEnabled);
o.Add("autoproxy_mode", settings.AutoproxyMode.ToString().ToLower());
o.Add("autoproxy_member", memberHid);
o.Add("tag", settings.Tag);
o.Add("tag_enabled", settings.TagEnabled);
return o;
}
public static (AutoproxyMode?, ValidationError?) ParseAutoproxyMode(this JToken o)
{
if (o.Type == JTokenType.Null)
case "off":
return (AutoproxyMode.Off, null);
else if (o.Type != JTokenType.String)
return (null, new ValidationError("autoproxy_mode"));
var value = o.Value<string>();
switch (value)
{
case "off":
return (AutoproxyMode.Off, null);
case "front":
return (AutoproxyMode.Front, null);
case "latch":
return (AutoproxyMode.Latch, null);
case "member":
return (AutoproxyMode.Member, null);
default:
return (null, new ValidationError("autoproxy_mode", $"Value '{value}' is not a valid autoproxy mode."));
}
case "front":
return (AutoproxyMode.Front, null);
case "latch":
return (AutoproxyMode.Latch, null);
case "member":
return (AutoproxyMode.Member, null);
default:
return (null,
new ValidationError("autoproxy_mode", $"Value '{value}' is not a valid autoproxy mode."));
}
}
}