2020-07-07 22:46:58 +00:00
|
|
|
|
using System;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
2020-07-07 22:46:58 +00:00
|
|
|
|
namespace PluralKit.Core
|
|
|
|
|
{
|
2020-02-12 14:16:19 +00:00
|
|
|
|
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
|
|
|
|
|
2020-06-21 13:51:08 +00:00
|
|
|
|
public static string LevelName(this PrivacyLevel level) =>
|
2020-06-17 21:06:49 +00:00
|
|
|
|
level == PrivacyLevel.Public ? "public" : "private";
|
2020-06-21 13:51:08 +00:00
|
|
|
|
|
|
|
|
|
public static T Get<T>(this PrivacyLevel level, LookupContext ctx, T input, T fallback = default) =>
|
|
|
|
|
level.CanAccess(ctx) ? input : fallback;
|
2020-07-07 22:46:58 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
};
|
2020-06-21 13:51:08 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|