Refactor system/member privacy commands
This commit is contained in:
9
PluralKit.Core/Models/Privacy/LookupContext.cs
Normal file
9
PluralKit.Core/Models/Privacy/LookupContext.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public enum LookupContext
|
||||
{
|
||||
ByOwner,
|
||||
ByNonOwner,
|
||||
API
|
||||
}
|
||||
}
|
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public enum MemberPrivacySubject {
|
||||
public enum MemberPrivacySubject
|
||||
{
|
||||
Visibility,
|
||||
Name,
|
||||
Description,
|
||||
@@ -11,48 +12,34 @@ namespace PluralKit.Core
|
||||
Pronouns,
|
||||
Metadata
|
||||
}
|
||||
|
||||
public static class PrivacyUtils
|
||||
{
|
||||
public static string Name(this MemberPrivacySubject subject) => subject switch
|
||||
{
|
||||
MemberPrivacySubject.Name => "name",
|
||||
MemberPrivacySubject.Description => "description",
|
||||
MemberPrivacySubject.Avatar => "avatar",
|
||||
MemberPrivacySubject.Pronouns => "pronouns",
|
||||
MemberPrivacySubject.Birthday => "birthday",
|
||||
MemberPrivacySubject.Metadata => "metadata",
|
||||
MemberPrivacySubject.Visibility => "visibility",
|
||||
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
|
||||
};
|
||||
|
||||
public static void SetPrivacy(this MemberPatch member, MemberPrivacySubject subject, PrivacyLevel level)
|
||||
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 = Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Description => member.DescriptionPrivacy = Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Avatar => member.AvatarPrivacy = Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Pronouns => member.PronounPrivacy = Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Birthday => member.BirthdayPrivacy= Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Metadata => member.MetadataPrivacy = Partial<PrivacyLevel>.Present(level),
|
||||
MemberPrivacySubject.Visibility => member.Visibility = Partial<PrivacyLevel>.Present(level),
|
||||
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 void SetAllPrivacy(this MemberPatch member, PrivacyLevel level)
|
||||
public static MemberPatch WithAllPrivacy(this MemberPatch member, PrivacyLevel level)
|
||||
{
|
||||
member.NamePrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.DescriptionPrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.AvatarPrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.PronounPrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.BirthdayPrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.MetadataPrivacy = Partial<PrivacyLevel>.Present(level);
|
||||
member.Visibility = Partial<PrivacyLevel>.Present(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())
|
||||
@@ -89,7 +76,7 @@ namespace PluralKit.Core
|
||||
subject = MemberPrivacySubject.Metadata;
|
||||
break;
|
||||
case "visibility":
|
||||
case "hidden":
|
||||
case "hidden":
|
||||
case "shown":
|
||||
case "visible":
|
||||
case "list":
|
||||
@@ -99,7 +86,8 @@ namespace PluralKit.Core
|
||||
subject = MemberPrivacySubject.Name;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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
|
||||
}
|
||||
}
|
8
PluralKit.Core/Models/Privacy/PrivacyLevel.cs
Normal file
8
PluralKit.Core/Models/Privacy/PrivacyLevel.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public enum PrivacyLevel
|
||||
{
|
||||
Public = 1,
|
||||
Private = 2
|
||||
}
|
||||
}
|
71
PluralKit.Core/Models/Privacy/SystemPrivacySubject.cs
Normal file
71
PluralKit.Core/Models/Privacy/SystemPrivacySubject.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user