feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,72 +1,69 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum GroupPrivacySubject
{
public enum GroupPrivacySubject
Description,
Icon,
List,
Visibility
}
public static class GroupPrivacyUtils
{
public static GroupPatch WithPrivacy(this GroupPatch group, GroupPrivacySubject subject, PrivacyLevel level)
{
Description,
Icon,
List,
Visibility
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
GroupPrivacySubject.Description => group.DescriptionPrivacy = level,
GroupPrivacySubject.Icon => group.IconPrivacy = level,
GroupPrivacySubject.List => group.ListPrivacy = level,
GroupPrivacySubject.Visibility => group.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return group;
}
public static class GroupPrivacyUtils
public static GroupPatch WithAllPrivacy(this GroupPatch member, PrivacyLevel level)
{
public static GroupPatch WithPrivacy(this GroupPatch group, GroupPrivacySubject subject, PrivacyLevel level)
{
// what do you mean switch expressions can't be statements >.>
_ = subject switch
{
GroupPrivacySubject.Description => group.DescriptionPrivacy = level,
GroupPrivacySubject.Icon => group.IconPrivacy = level,
GroupPrivacySubject.List => group.ListPrivacy = level,
GroupPrivacySubject.Visibility => group.Visibility = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
foreach (var subject in Enum.GetValues(typeof(GroupPrivacySubject)))
member.WithPrivacy((GroupPrivacySubject)subject, level);
return member;
}
return group;
public static bool TryParseGroupPrivacy(string input, out GroupPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = GroupPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = GroupPrivacySubject.Icon;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
subject = GroupPrivacySubject.Visibility;
break;
case "list":
case "listing":
case "members":
subject = GroupPrivacySubject.List;
break;
default:
subject = default;
return false;
}
public static GroupPatch WithAllPrivacy(this GroupPatch member, PrivacyLevel level)
{
foreach (var subject in Enum.GetValues(typeof(GroupPrivacySubject)))
member.WithPrivacy((GroupPrivacySubject)subject, level);
return member;
}
public static bool TryParseGroupPrivacy(string input, out GroupPrivacySubject subject)
{
switch (input.ToLowerInvariant())
{
case "description":
case "desc":
case "text":
case "info":
subject = GroupPrivacySubject.Description;
break;
case "avatar":
case "pfp":
case "pic":
case "icon":
subject = GroupPrivacySubject.Icon;
break;
case "visibility":
case "hidden":
case "shown":
case "visible":
subject = GroupPrivacySubject.Visibility;
break;
case "list":
case "listing":
case "members":
subject = GroupPrivacySubject.List;
break;
default:
subject = default;
return false;
}
return true;
}
return true;
}
}

View File

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

View File

@@ -1,93 +1,90 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum MemberPrivacySubject
{
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)
{
Visibility,
Name,
Description,
Avatar,
Birthday,
Pronouns,
Metadata
// 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 class MemberPrivacyUtils
public static MemberPatch WithAllPrivacy(this MemberPatch member, PrivacyLevel level)
{
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}")
};
foreach (var subject in Enum.GetValues(typeof(MemberPrivacySubject)))
member.WithPrivacy((MemberPrivacySubject)subject, level);
return member;
}
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;
}
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;
}
return true;
}
}

View File

@@ -1,44 +1,42 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum PrivacyLevel
{
public enum PrivacyLevel
{
Public = 1,
Private = 2
}
Public = 1,
Private = 2
}
public static class PrivacyLevelExt
{
public static bool CanAccess(this PrivacyLevel level, LookupContext ctx) =>
level == PrivacyLevel.Public || ctx == LookupContext.ByOwner;
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 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 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)
public static string Explanation(this PrivacyLevel level) =>
level switch
{
output = default;
if (!level.CanAccess(ctx))
return false;
if (Equals(input, absentValue))
return false;
PrivacyLevel.Private => "**Private** (visible only when queried by you)",
PrivacyLevel.Public => "**Public** (visible to everyone)",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
output = input;
return true;
}
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;
public static string ToJsonString(this PrivacyLevel level) => level.LevelName();
output = input;
return true;
}
public static string ToJsonString(this PrivacyLevel level) => level.LevelName();
}

View File

@@ -1,77 +1,75 @@
using System;
namespace PluralKit.Core;
namespace PluralKit.Core
public enum SystemPrivacySubject
{
public enum SystemPrivacySubject
Description,
MemberList,
GroupList,
Front,
FrontHistory
}
public static class SystemPrivacyUtils
{
public static SystemPatch WithPrivacy(this SystemPatch system, SystemPrivacySubject subject, PrivacyLevel level)
{
Description,
MemberList,
GroupList,
Front,
FrontHistory
// 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,
SystemPrivacySubject.GroupList => system.GroupListPrivacy = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
return system;
}
public static class SystemPrivacyUtils
public static SystemPatch WithAllPrivacy(this SystemPatch system, PrivacyLevel level)
{
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,
SystemPrivacySubject.GroupList => system.GroupListPrivacy = level,
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
};
foreach (var subject in Enum.GetValues(typeof(SystemPrivacySubject)))
WithPrivacy(system, (SystemPrivacySubject)subject, level);
return system;
}
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;
case "groups":
case "gs":
subject = SystemPrivacySubject.GroupList;
break;
default:
subject = default;
return false;
}
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;
case "groups":
case "gs":
subject = SystemPrivacySubject.GroupList;
break;
default:
subject = default;
return false;
}
return true;
}
return true;
}
}