Move some extension methods into their respective class files
This commit is contained in:
parent
7ab5e66d7b
commit
a0fc9d3826
@ -197,4 +197,19 @@ namespace PluralKit.Core
|
|||||||
public override T[] Parse(object value) => Array.ConvertAll((TInner[]) value, v => _factory(v));
|
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<IPKConnection, Task> func)
|
||||||
|
{
|
||||||
|
await using var conn = await db.Obtain();
|
||||||
|
await func(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<T> Execute<T>(this IDatabase db, Func<IPKConnection, Task<T>> func)
|
||||||
|
{
|
||||||
|
await using var conn = await db.Obtain();
|
||||||
|
return await func(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -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<IPKConnection, Task> func)
|
|
||||||
{
|
|
||||||
await using var conn = await db.Obtain();
|
|
||||||
await func(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<T> Execute<T>(this IDatabase db, Func<IPKConnection, Task<T>> func)
|
|
||||||
{
|
|
||||||
await using var conn = await db.Obtain();
|
|
||||||
return await func(conn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -50,4 +50,28 @@ namespace PluralKit.Core {
|
|||||||
|
|
||||||
[JsonIgnore] public bool HasProxyTags => ProxyTags.Count > 0;
|
[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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -25,4 +25,10 @@ namespace PluralKit.Core {
|
|||||||
|
|
||||||
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
|
[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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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<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)
|
|
||||||
{
|
|
||||||
output = default;
|
|
||||||
if (!level.CanAccess(ctx))
|
|
||||||
return false;
|
|
||||||
if (Equals(input, absentValue))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
output = input;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,42 @@
|
|||||||
namespace PluralKit.Core
|
using System;
|
||||||
|
|
||||||
|
namespace PluralKit.Core
|
||||||
{
|
{
|
||||||
public enum PrivacyLevel
|
public enum PrivacyLevel
|
||||||
{
|
{
|
||||||
Public = 1,
|
Public = 1,
|
||||||
Private = 2
|
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<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)
|
||||||
|
{
|
||||||
|
output = default;
|
||||||
|
if (!level.CanAccess(ctx))
|
||||||
|
return false;
|
||||||
|
if (Equals(input, absentValue))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
output = input;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user