Migrate to more privacy helper extensions
This commit is contained in:
@@ -1,11 +1,31 @@
|
||||
namespace PluralKit.Core
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public static class ModelExtensions
|
||||
{
|
||||
public static string NameFor(this PKMember member, LookupContext ctx) =>
|
||||
member.NamePrivacy.CanAccess(ctx) ? member.Name : member.DisplayName ?? member.Name;
|
||||
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.CanAccess(ctx) ? member.AvatarUrl : null;
|
||||
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);
|
||||
}
|
||||
}
|
@@ -11,8 +11,23 @@
|
||||
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
|
||||
level == PrivacyLevel.Public || ctx == LookupContext.ByOwner;
|
||||
|
||||
public static string Name(this PrivacyLevel level) =>
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
public enum LookupContext
|
||||
|
Reference in New Issue
Block a user