Refactor system/member privacy commands

This commit is contained in:
Ske
2020-07-08 00:46:58 +02:00
parent 1449234a84
commit 9f523b3c5f
8 changed files with 249 additions and 196 deletions

View File

@@ -0,0 +1,9 @@
namespace PluralKit.Core
{
public enum LookupContext
{
ByOwner,
ByNonOwner,
API
}
}

View File

@@ -0,0 +1,93 @@
using System;
namespace PluralKit.Core
{
public enum MemberPrivacySubject
{
Visibility,
Name,
Description,
Avatar,
Birthday,
Pronouns,
Metadata
}
public static class MemberPrivacyUtils
{
public static MemberPatch WithPrivacy(this MemberPatch member, MemberPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
MemberPrivacySubject.Name => member.NamePrivacy = level,
MemberPrivacySubject.Description => member.DescriptionPrivacy = level,
MemberPrivacySubject.Avatar => member.AvatarPrivacy = level,
MemberPrivacySubject.Pronouns => member.PronounPrivacy = level,
MemberPrivacySubject.Birthday => member.BirthdayPrivacy = level,
MemberPrivacySubject.Metadata => member.MetadataPrivacy = level,
MemberPrivacySubject.Visibility => member.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return member;
}
public static MemberPatch WithAllPrivacy(this MemberPatch member, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(MemberPrivacySubject)))
member.WithPrivacy((MemberPrivacySubject) subject, level);
return member;
}
public static bool TryParseMemberPrivacy(string input, out MemberPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "name":
subject = MemberPrivacySubject.Name;
break;
case "description":
case "desc":
case "text":
case "info":
subject = MemberPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = MemberPrivacySubject.Avatar;
break;
case "birthday":
case "birth":
case "bday":
case "birthdate":
case "bdate":
subject = MemberPrivacySubject.Birthday;
break;
case "pronouns":
case "pronoun":
subject = MemberPrivacySubject.Pronouns;
break;
case "meta":
case "metadata":
case "created":
subject = MemberPrivacySubject.Metadata;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
case "list":
subject = MemberPrivacySubject.Visibility;
break;
default:
subject = MemberPrivacySubject.Name;
return false;
}
return true;
}
}
}

View File

@@ -1,11 +1,7 @@
namespace PluralKit.Core
{
public enum PrivacyLevel
{
Public = 1,
Private = 2
}
using System;
namespace PluralKit.Core
{
public static class PrivacyExt
{
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
@@ -16,6 +12,14 @@
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)
{
@@ -29,11 +33,4 @@
return true;
}
}
public enum LookupContext
{
ByOwner,
ByNonOwner,
API
}
}

View File

@@ -0,0 +1,8 @@
namespace PluralKit.Core
{
public enum PrivacyLevel
{
Public = 1,
Private = 2
}
}

View File

@@ -0,0 +1,71 @@
using System;
namespace PluralKit.Core
{
public enum SystemPrivacySubject
{
Description,
MemberList,
Front,
FrontHistory
}
public static class SystemPrivacyUtils
{
public static SystemPatch WithPrivacy(this SystemPatch system, SystemPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
SystemPrivacySubject.Description => system.DescriptionPrivacy = level,
SystemPrivacySubject.Front => system.FrontPrivacy = level,
SystemPrivacySubject.FrontHistory => system.FrontHistoryPrivacy = level,
SystemPrivacySubject.MemberList => system.MemberListPrivacy = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return system;
}
public static SystemPatch WithAllPrivacy(this SystemPatch system, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(SystemPrivacySubject)))
WithPrivacy(system, (SystemPrivacySubject) subject, level);
return system;
}
public static bool TryParseSystemPrivacy(string input, out SystemPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = SystemPrivacySubject.Description;
break;
case "members":
case "memberlist":
case "list":
case "mlist":
subject = SystemPrivacySubject.MemberList;
break;
case "fronter":
case "fronters":
case "front":
subject = SystemPrivacySubject.Front;
break;
case "switch":
case "switches":
case "fronthistory":
case "fh":
subject = SystemPrivacySubject.FrontHistory;
break;
default:
subject = default;
return false;
}
return true;
}
}
}