2020-08-05 18:24:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace PluralKit.Core
|
2020-07-07 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
public enum PrivacyLevel
|
|
|
|
|
{
|
|
|
|
|
Public = 1,
|
|
|
|
|
Private = 2
|
|
|
|
|
}
|
2020-08-05 18:24:51 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-08-07 22:13:46 +00:00
|
|
|
|
|
|
|
|
|
public static string ToJsonString(this PrivacyLevel level) => level.LevelName();
|
|
|
|
|
|
|
|
|
|
public static PrivacyLevel ParsePrivacy(this string input, string errorName)
|
|
|
|
|
{
|
|
|
|
|
if (input == null) return PrivacyLevel.Public;
|
|
|
|
|
if (input == "") return PrivacyLevel.Private;
|
|
|
|
|
if (input == "private") return PrivacyLevel.Private;
|
|
|
|
|
if (input == "public") return PrivacyLevel.Public;
|
|
|
|
|
throw new JsonModelParseError($"Could not parse {errorName} privacy.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 18:24:51 +00:00
|
|
|
|
}
|
2020-07-07 22:46:58 +00:00
|
|
|
|
}
|