feat: alternate proxy switch mode "add" (#702)

* feat: alternate proxy switch mode "add"

* docs: add proxy switch add to docs

* chore: change default alias to proxy switch new from proxy switch on
This commit is contained in:
Petal Ladenson 2024-12-05 18:24:20 -07:00 committed by GitHub
parent e44d813b73
commit 6043d31b08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 76 additions and 19 deletions

View file

@ -30,7 +30,7 @@ public partial class CommandTree
public static Command ConfigShowPrivate = new Command("config show private", "config show private [on|off]", "Sets whether private information is shown to linked accounts by default");
public static Command ConfigMemberDefaultPrivacy = new("config private member", "config private member [on|off]", "Sets whether member privacy is automatically set to private when creating a new member");
public static Command ConfigGroupDefaultPrivacy = new("config private group", "config private group [on|off]", "Sets whether group privacy is automatically set to private when creating a new group");
public static Command ConfigProxySwitch = new Command("config proxyswitch", "config proxyswitch [on|off]", "Sets whether to log a switch every time a proxy tag is used");
public static Command ConfigProxySwitch = new Command("config proxyswitch", "config proxyswitch [new|add|off]", "Switching behavior when proxy tags are used");
public static Command ConfigNameFormat = new Command("config nameformat", "config nameformat [format]", "Changes your system's username formatting");
public static Command ConfigServerNameFormat = new Command("config servernameformat", "config servernameformat [format]", "Changes your system's username formatting in the current server");
public static Command AutoproxySet = new Command("autoproxy", "autoproxy [off|front|latch|member]", "Sets your system's autoproxy mode for the current server");

View file

@ -125,9 +125,9 @@ public class Config
items.Add(new(
"Proxy Switch",
"Whether using a proxy tag logs a switch",
EnabledDisabled(ctx.Config.ProxySwitch),
"disabled"
"Switching behavior when proxy tags are used",
ctx.Config.ProxySwitch.ToUserString(),
"off"
));
items.Add(new(
@ -574,14 +574,29 @@ public class Config
{
if (!ctx.HasNext())
{
var msg = $"Logging a switch every time a proxy tag is used is currently **{EnabledDisabled(ctx.Config.ProxySwitch)}**.";
string msg = ctx.Config.ProxySwitch switch
{
SystemConfig.ProxySwitchAction.Off => "Currently, when you proxy as a member, no switches are logged or changed.",
SystemConfig.ProxySwitchAction.New => "When you proxy as a member, currently it makes a new switch.",
SystemConfig.ProxySwitchAction.Add => "When you proxy as a member, currently it adds them to the current switch.",
_ => throw new Exception("unreachable"),
};
await ctx.Reply(msg);
return;
}
var newVal = ctx.MatchToggle(false);
// toggle = false means off, toggle = true means new, otherwise if they said add that means add or if they said new they mean new. If none of those, error
var toggle = ctx.MatchToggleOrNull(false);
var newVal = toggle == false ? SystemConfig.ProxySwitchAction.Off : toggle == true ? SystemConfig.ProxySwitchAction.New : ctx.Match("add", "a") ? SystemConfig.ProxySwitchAction.Add : ctx.Match("new", "n") ? SystemConfig.ProxySwitchAction.New : throw new PKError("You must pass either \"new\", \"add\", or \"off\" to this command.");
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { ProxySwitch = newVal });
await ctx.Reply($"Logging a switch every time a proxy tag is used is now {EnabledDisabled(newVal)}.");
switch (newVal)
{
case SystemConfig.ProxySwitchAction.Off: await ctx.Reply("Now when you proxy as a member, no switches are logged or changed."); break;
case SystemConfig.ProxySwitchAction.New: await ctx.Reply("When you proxy as a member, it now makes a new switch."); break;
case SystemConfig.ProxySwitchAction.Add: await ctx.Reply("When you proxy as a member, it now adds them to the current switch."); break;
default: throw new Exception("unreachable");
}
}
public async Task NameFormat(Context ctx)

View file

@ -521,9 +521,22 @@ public class ProxyService
Task DispatchWebhook() => _dispatch.Dispatch(ctx.SystemId.Value, sentMessage);
Task MaybeLogSwitch() => (ctx.ProxySwitch && !Array.Exists(ctx.LastSwitchMembers, element => element == match.Member.Id))
? _db.Execute(conn => _repo.AddSwitch(conn, (SystemId)ctx.SystemId, new[] { match.Member.Id }))
: Task.CompletedTask;
async Task MaybeLogSwitch()
{
if (ctx.ProxySwitch == SystemConfig.ProxySwitchAction.New && !Array.Exists(ctx.LastSwitchMembers, element => element == match.Member.Id))
await _db.Execute(conn => _repo.AddSwitch(conn, (SystemId)ctx.SystemId, new[] { match.Member.Id }));
else if (ctx.ProxySwitch == SystemConfig.ProxySwitchAction.Add)
{
if (ctx.LastSwitchMembers.Length == 0)
{
await _db.Execute(conn => _repo.AddSwitch(conn, (SystemId)ctx.SystemId, new[] { match.Member.Id }));
}
else if (!Array.Exists(ctx.LastSwitchMembers, element => element == match.Member.Id))
{
await _db.Execute(conn => _repo.EditSwitch(conn, (SwitchId)ctx.LastSwitch, ctx.LastSwitchMembers.Append(match.Member.Id).ToList()));
}
}
}
async Task DeleteProxyTriggerMessage()
{