From a0fc9d3826acba83d9e64407c13859cccb3aa8bf Mon Sep 17 00:00:00 2001 From: Ske Date: Wed, 5 Aug 2020 20:24:51 +0200 Subject: [PATCH] Move some extension methods into their respective class files --- PluralKit.Core/Database/Database.cs | 15 ++++++++ PluralKit.Core/Database/DatabaseExt.cs | 20 ----------- PluralKit.Core/Models/ModelExtensions.cs | 31 ---------------- PluralKit.Core/Models/PKMember.cs | 24 +++++++++++++ PluralKit.Core/Models/PKSystem.cs | 6 ++++ PluralKit.Core/Models/Privacy/PrivacyExt.cs | 36 ------------------- PluralKit.Core/Models/Privacy/PrivacyLevel.cs | 36 ++++++++++++++++++- 7 files changed, 80 insertions(+), 88 deletions(-) delete mode 100644 PluralKit.Core/Database/DatabaseExt.cs delete mode 100644 PluralKit.Core/Models/ModelExtensions.cs delete mode 100644 PluralKit.Core/Models/Privacy/PrivacyExt.cs diff --git a/PluralKit.Core/Database/Database.cs b/PluralKit.Core/Database/Database.cs index 09e6ff20..0071c629 100644 --- a/PluralKit.Core/Database/Database.cs +++ b/PluralKit.Core/Database/Database.cs @@ -197,4 +197,19 @@ namespace PluralKit.Core public override T[] Parse(object value) => Array.ConvertAll((TInner[]) value, v => _factory(v)); } } + + public static class DatabaseExt + { + public static async Task Execute(this IDatabase db, Func func) + { + await using var conn = await db.Obtain(); + await func(conn); + } + + public static async Task Execute(this IDatabase db, Func> func) + { + await using var conn = await db.Obtain(); + return await func(conn); + } + } } \ No newline at end of file diff --git a/PluralKit.Core/Database/DatabaseExt.cs b/PluralKit.Core/Database/DatabaseExt.cs deleted file mode 100644 index afae07f7..00000000 --- a/PluralKit.Core/Database/DatabaseExt.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace PluralKit.Core -{ - public static class DatabaseExt - { - public static async Task Execute(this IDatabase db, Func func) - { - await using var conn = await db.Obtain(); - await func(conn); - } - - public static async Task Execute(this IDatabase db, Func> func) - { - await using var conn = await db.Obtain(); - return await func(conn); - } - } -} \ No newline at end of file diff --git a/PluralKit.Core/Models/ModelExtensions.cs b/PluralKit.Core/Models/ModelExtensions.cs deleted file mode 100644 index 6092fa24..00000000 --- a/PluralKit.Core/Models/ModelExtensions.cs +++ /dev/null @@ -1,31 +0,0 @@ -using NodaTime; - -namespace PluralKit.Core -{ - public static class ModelExtensions - { - public static string DescriptionFor(this PKSystem system, LookupContext ctx) => - system.DescriptionPrivacy.Get(ctx, system.Description); - - 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); - - 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); - } -} \ No newline at end of file diff --git a/PluralKit.Core/Models/PKMember.cs b/PluralKit.Core/Models/PKMember.cs index 57efa792..40a6ad8a 100644 --- a/PluralKit.Core/Models/PKMember.cs +++ b/PluralKit.Core/Models/PKMember.cs @@ -50,4 +50,28 @@ namespace PluralKit.Core { [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); + + 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); + } } \ No newline at end of file diff --git a/PluralKit.Core/Models/PKSystem.cs b/PluralKit.Core/Models/PKSystem.cs index aff336af..fdc6ceea 100644 --- a/PluralKit.Core/Models/PKSystem.cs +++ b/PluralKit.Core/Models/PKSystem.cs @@ -25,4 +25,10 @@ namespace PluralKit.Core { [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); + } } diff --git a/PluralKit.Core/Models/Privacy/PrivacyExt.cs b/PluralKit.Core/Models/Privacy/PrivacyExt.cs deleted file mode 100644 index 548f963e..00000000 --- a/PluralKit.Core/Models/Privacy/PrivacyExt.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; - -namespace PluralKit.Core -{ - public static class PrivacyExt - { - 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 T Get(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(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; - - output = input; - return true; - } - } -} \ No newline at end of file diff --git a/PluralKit.Core/Models/Privacy/PrivacyLevel.cs b/PluralKit.Core/Models/Privacy/PrivacyLevel.cs index 8dad4658..03a6ea99 100644 --- a/PluralKit.Core/Models/Privacy/PrivacyLevel.cs +++ b/PluralKit.Core/Models/Privacy/PrivacyLevel.cs @@ -1,8 +1,42 @@ -namespace PluralKit.Core +using System; + +namespace PluralKit.Core { public enum PrivacyLevel { 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 string LevelName(this PrivacyLevel level) => + level == PrivacyLevel.Public ? "public" : "private"; + + public static T Get(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(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; + + output = input; + return true; + } + } } \ No newline at end of file