Add configurable autoproxy latch timeout

This commit is contained in:
spiral 2020-11-20 19:44:15 -05:00
parent 37294b68da
commit 8e5fb6520b
8 changed files with 70 additions and 10 deletions

View File

@ -25,12 +25,17 @@ namespace PluralKit.Bot
ctx.CheckSystem();
// check account first
// this is ugly, but someone may want to disable autoproxy in DMs (since this is global)
// this is ugly, but these global options should be available in DMs
if (ctx.Match("account", "ac"))
{
await AutoproxyAccount(ctx);
return;
}
else if (ctx.Match("timeout", "tm"))
{
await AutoproxyTimeout(ctx);
return;
}
ctx.CheckGuildContext();
@ -138,6 +143,35 @@ namespace PluralKit.Bot
return eb.Build();
}
private async Task AutoproxyTimeout(Context ctx)
{
if (!ctx.HasNext())
{
if (ctx.System.LatchTimeout == -1)
await ctx.Reply($"You do not have a custom autoproxy timeout duration set. The default latch timeout duration is {PluralKit.Bot.ProxyMatcher.DefaultLatchExpiryTime} hour(s).");
else if (ctx.System.LatchTimeout == 0)
await ctx.Reply("Latch timeout is currently **disabled** for your system. Latch mode autoproxy will never timeout.");
else
await ctx.Reply($"The current latch timeout duration for your system is {ctx.System.LatchTimeout} hour(s).");
return;
}
// todo: somehow parse a more human-friendly date format
int newTimeout;
if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = 0;
else if (ctx.Match("reset", "default")) newTimeout = -1;
else if (!int.TryParse(ctx.RemainderOrNull(), out newTimeout)) throw new PKError("Duration must be an integer.");
await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, new SystemPatch{LatchTimeout = newTimeout}));
if (newTimeout == -1)
await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({PluralKit.Bot.ProxyMatcher.DefaultLatchExpiryTime} hours).");
else if (newTimeout == 0)
await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never timeout.");
else
await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout} hours.");
}
private async Task AutoproxyAccount(Context ctx)
{
if (ctx.Match("enable", "on"))

View File

@ -10,7 +10,7 @@ namespace PluralKit.Bot
public class ProxyMatcher
{
private static readonly char AutoproxyEscapeCharacter = '\\';
private static readonly Duration LatchExpiryTime = Duration.FromHours(6);
public static readonly int DefaultLatchExpiryTime = 6;
private readonly IClock _clock;
private readonly ProxyTagParser _parser;
@ -56,7 +56,7 @@ namespace PluralKit.Bot
AutoproxyMode.Front when ctx.LastSwitchMembers.Length > 0 =>
members.FirstOrDefault(m => m.Id == ctx.LastSwitchMembers[0]),
AutoproxyMode.Latch when ctx.LastMessageMember != null && !IsLatchExpired(ctx.LastMessage) =>
AutoproxyMode.Latch when ctx.LastMessageMember != null && !IsLatchExpired(ctx) =>
members.FirstOrDefault(m => m.Id == ctx.LastMessageMember.Value),
_ => null
@ -75,11 +75,15 @@ namespace PluralKit.Bot
return true;
}
private bool IsLatchExpired(ulong? messageId)
private bool IsLatchExpired(MessageContext ctx)
{
if (messageId == null) return true;
var timestamp = DiscordUtils.SnowflakeToInstant(messageId.Value);
return _clock.GetCurrentInstant() - timestamp > LatchExpiryTime;
if (ctx.LastMessage == null) return true;
if (ctx.LatchTimeout == 0) return false;
var timeout = Duration.FromHours(ctx.LatchTimeout == -1 ? DefaultLatchExpiryTime : ctx.LatchTimeout);
var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value);
return _clock.GetCurrentInstant() - timestamp > timeout;
}
}
}

View File

@ -25,5 +25,6 @@ namespace PluralKit.Core
public string? SystemTag { get; }
public string? SystemAvatar { get; }
public bool AllowAutoproxy { get; }
public int LatchTimeout { get; }
}
}

View File

@ -15,7 +15,8 @@
last_switch_timestamp timestamp,
system_tag text,
system_avatar text,
allow_autoproxy bool
allow_autoproxy bool,
latch_timeout integer
)
as $$
-- CTEs to query "static" (accessible only through args) data
@ -39,7 +40,8 @@ as $$
system_last_switch.timestamp as last_switch_timestamp,
system.tag as system_tag,
system.avatar_url as system_avatar,
system.account_autoproxy as allow_autoproxy
system.account_autoproxy as allow_autoproxy,
system.latch_timeout as latch_timeout
-- 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

View File

@ -1,7 +1,9 @@
-- SCHEMA VERSION 12: <insert date> --
-- Add disabling front/latch autoproxy per-member --
-- Add disabling autoproxy per-account --
-- Add configurable latch timeout --
alter table members add column allow_autoproxy bool not null default true;
alter table accounts add column allow_autoproxy bool not null default true;
alter table systems add column latch_timeout int not null default -1;
update info set schema_version = 12;

View File

@ -18,6 +18,7 @@ namespace PluralKit.Core {
public Instant Created { get; }
public string UiTz { get; set; }
public bool PingsEnabled { get; }
public int LatchTimeout { get; }
public PrivacyLevel DescriptionPrivacy { get; }
public PrivacyLevel MemberListPrivacy { get;}
public PrivacyLevel FrontPrivacy { get; }

View File

@ -15,6 +15,7 @@ namespace PluralKit.Core
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
public Partial<bool> PingsEnabled { get; set; }
public Partial<int> LatchTimeout { get; set; }
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("name", Name)
@ -28,6 +29,7 @@ namespace PluralKit.Core
.With("group_list_privacy", GroupListPrivacy)
.With("front_privacy", FrontPrivacy)
.With("front_history_privacy", FrontHistoryPrivacy)
.With("pings_enabled", PingsEnabled);
.With("pings_enabled", PingsEnabled)
.With("latch_timeout", LatchTimeout);
}
}

View File

@ -340,6 +340,20 @@ To enable latch-mode autoproxying for a given server, use the following command:
pk;autoproxy latch
::: tip
By default, latch mode times out after 6 hours. It is possible to change this:
pk;autoproxy timeout <new duration>
To reset the duration, use the following command:
pk;autoproxy timeout reset
To disable timeout (never timeout), use the following command:
pk;autoproxy timeout disable
:::
#### Member mode
This autoproxy mode will autoproxy for a specific selected member, irrelevant of past proxies or fronters.