From 37294b68da85a9b2a6175cadc410abd1a0279cdc Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 20 Nov 2020 18:34:08 -0500 Subject: [PATCH] Add disabling autoproxy per-account --- PluralKit.Bot/Commands/Autoproxy.cs | 43 ++++++++++++++++++- PluralKit.Bot/Handlers/MessageCreated.cs | 2 +- .../Database/Functions/MessageContext.cs | 1 + .../Database/Functions/functions.sql | 8 ++-- PluralKit.Core/Database/Migrations/12.sql | 2 + .../Repository/ModelRepository.Account.cs | 21 +++++++++ PluralKit.Core/Models/Patch/AccountPatch.cs | 10 +++++ docs/content/user-guide.md | 16 +++++++ 8 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 PluralKit.Core/Database/Repository/ModelRepository.Account.cs create mode 100644 PluralKit.Core/Models/Patch/AccountPatch.cs diff --git a/PluralKit.Bot/Commands/Autoproxy.cs b/PluralKit.Bot/Commands/Autoproxy.cs index 7181c1cf..475e0647 100644 --- a/PluralKit.Bot/Commands/Autoproxy.cs +++ b/PluralKit.Bot/Commands/Autoproxy.cs @@ -22,7 +22,17 @@ namespace PluralKit.Bot public async Task AutoproxyRoot(Context ctx) { - ctx.CheckSystem().CheckGuildContext(); + ctx.CheckSystem(); + + // check account first + // this is ugly, but someone may want to disable autoproxy in DMs (since this is global) + if (ctx.Match("account", "ac")) + { + await AutoproxyAccount(ctx); + return; + } + + ctx.CheckGuildContext(); if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) await AutoproxyOff(ctx); @@ -122,9 +132,40 @@ namespace PluralKit.Bot default: throw new ArgumentOutOfRangeException(); } + if (!ctx.MessageContext.AllowAutoproxy) + eb.AddField("\u200b", $"{Emojis.Note} Autoproxy is currently **disabled** for your account (<@{ctx.Author.Id}>). To enable it, use `pk;autoproxy account enable`."); + return eb.Build(); } + private async Task AutoproxyAccount(Context ctx) + { + if (ctx.Match("enable", "on")) + await AutoproxyEnableDisable(ctx, true); + else if (ctx.Match("disable", "off")) + await AutoproxyEnableDisable(ctx, false); + else if (ctx.HasNext()) + throw new PKSyntaxError("You must pass either \"on\" or \"off\"."); + else + { + var statusString = ctx.MessageContext.AllowAutoproxy ? "enabled" : "disabled"; + await ctx.Reply($"Autoproxy is currently **{statusString}** for account <@{ctx.Author.Id}>.", mentions: new IMention[]{}); + } + } + + private async Task AutoproxyEnableDisable(Context ctx, bool allow) + { + var statusString = allow ? "enabled" : "disabled"; + if (ctx.MessageContext.AllowAutoproxy == allow) + { + await ctx.Reply($"{Emojis.Note} Autoproxy is already {statusString} for account <@{ctx.Author.Id}>.", mentions: new IMention[]{}); + return; + } + var patch = new AccountPatch { AllowAutoproxy = allow }; + await _db.Execute(conn => _repo.UpdateAccount(conn, ctx.Author.Id, patch)); + await ctx.Reply($"{Emojis.Success} Autoproxy {statusString} for account <@{ctx.Author.Id}>.", mentions: new IMention[]{}); + } + private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember) { var patch = new SystemGuildPatch {AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember}; diff --git a/PluralKit.Bot/Handlers/MessageCreated.cs b/PluralKit.Bot/Handlers/MessageCreated.cs index 31c66075..d51fe0fc 100644 --- a/PluralKit.Bot/Handlers/MessageCreated.cs +++ b/PluralKit.Bot/Handlers/MessageCreated.cs @@ -137,7 +137,7 @@ namespace PluralKit.Bot { try { - return await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: true); + return await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: ctx.AllowAutoproxy); } catch (PKError e) { diff --git a/PluralKit.Core/Database/Functions/MessageContext.cs b/PluralKit.Core/Database/Functions/MessageContext.cs index febafdaa..428dfd1c 100644 --- a/PluralKit.Core/Database/Functions/MessageContext.cs +++ b/PluralKit.Core/Database/Functions/MessageContext.cs @@ -24,5 +24,6 @@ namespace PluralKit.Core public Instant? LastSwitchTimestamp { get; } public string? SystemTag { get; } public string? SystemAvatar { get; } + public bool AllowAutoproxy { get; } } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Functions/functions.sql b/PluralKit.Core/Database/Functions/functions.sql index 1a0ff7f9..a6f3d758 100644 --- a/PluralKit.Core/Database/Functions/functions.sql +++ b/PluralKit.Core/Database/Functions/functions.sql @@ -14,12 +14,13 @@ last_switch_members int[], last_switch_timestamp timestamp, system_tag text, - system_avatar text + system_avatar text, + allow_autoproxy bool ) as $$ -- CTEs to query "static" (accessible only through args) data with - system as (select systems.* from accounts inner join systems on systems.id = accounts.system where accounts.uid = account_id), + system as (select systems.*, allow_autoproxy as account_autoproxy from accounts inner join systems on systems.id = accounts.system where accounts.uid = account_id), guild as (select * from servers where id = guild_id), last_message as (select * from messages where messages.guild = guild_id and messages.sender = account_id order by mid desc limit 1) select @@ -37,7 +38,8 @@ as $$ system_last_switch.members as last_switch_members, system_last_switch.timestamp as last_switch_timestamp, system.tag as system_tag, - system.avatar_url as system_avatar + system.avatar_url as system_avatar, + system.account_autoproxy as allow_autoproxy -- We need a "from" clause, so we just use some bogus data that's always present -- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data from (select 1) as _placeholder diff --git a/PluralKit.Core/Database/Migrations/12.sql b/PluralKit.Core/Database/Migrations/12.sql index a02ef7e1..6cc63e80 100644 --- a/PluralKit.Core/Database/Migrations/12.sql +++ b/PluralKit.Core/Database/Migrations/12.sql @@ -1,5 +1,7 @@ -- SCHEMA VERSION 12: -- -- Add disabling front/latch autoproxy per-member -- +-- Add disabling autoproxy per-account -- alter table members add column allow_autoproxy bool not null default true; +alter table accounts add column allow_autoproxy bool not null default true; update info set schema_version = 12; \ No newline at end of file diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Account.cs b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs new file mode 100644 index 00000000..98682b0b --- /dev/null +++ b/PluralKit.Core/Database/Repository/ModelRepository.Account.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Data; +using System.Threading.Tasks; + +using Dapper; + +namespace PluralKit.Core +{ + public partial class ModelRepository + { + public async Task UpdateAccount(IPKConnection conn, ulong id, AccountPatch patch) + { + _logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch); + var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("accounts", "uid = @uid")) + .WithConstant("uid", id) + .Build(); + await conn.ExecuteAsync(query, pms); + } + + } +} \ No newline at end of file diff --git a/PluralKit.Core/Models/Patch/AccountPatch.cs b/PluralKit.Core/Models/Patch/AccountPatch.cs new file mode 100644 index 00000000..4614d4b8 --- /dev/null +++ b/PluralKit.Core/Models/Patch/AccountPatch.cs @@ -0,0 +1,10 @@ +namespace PluralKit.Core +{ + public class AccountPatch: PatchObject + { + public Partial AllowAutoproxy { get; set; } + + public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b + .With("allow_autoproxy", AllowAutoproxy); + } +} \ No newline at end of file diff --git a/docs/content/user-guide.md b/docs/content/user-guide.md index 4336eb03..bdc0a138 100644 --- a/docs/content/user-guide.md +++ b/docs/content/user-guide.md @@ -358,6 +358,22 @@ To re-enable front / latch modes for that member, use the following command: This will *not* disable member mode autoproxy. If you do not wish to autoproxy, please turn off autoproxy instead of setting autoproxy to a specific member. +### Disabling autoproxy per-account + +It is possible to fully disable autoproxy for a certain account linked to your system. For example, you might want to do this if a specific member's name is shown on the account. + +To disable autoproxy for the current account, use the following command: + + pk;autoproxy account disable + +To re-enable autoproxy for the current account, use the following command: + + pk;autoproxy account enable + +::: tip +This subcommand can also be run in DMs. +::: + ## Managing switches PluralKit allows you to log member switches through the bot.