PluralKit/PluralKit.Core/Models/Privacy.cs

39 lines
1.1 KiB
C#
Raw Normal View History

namespace PluralKit.Core
{
public enum PrivacyLevel
{
Public = 1,
Private = 2
}
public static class PrivacyExt
{
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
level == PrivacyLevel.Public || ctx == LookupContext.ByOwner;
2020-06-17 21:06:49 +00:00
public static string LevelName(this PrivacyLevel level) =>
2020-06-17 21:06:49 +00:00
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
{
ByOwner,
ByNonOwner,
API
}
}