From db4e41a232d9220c6c170520bd1af96529c41f4d Mon Sep 17 00:00:00 2001 From: Ske Date: Tue, 8 Dec 2020 12:57:17 +0100 Subject: [PATCH] Convert autoproxy timeout to use seconds --- PluralKit.Bot/Commands/Autoproxy.cs | 42 +++++++++++-------- PluralKit.Bot/Proxy/ProxyMatcher.cs | 8 ++-- .../Database/Functions/MessageContext.cs | 2 +- PluralKit.Core/Database/Migrations/12.sql | 5 ++- PluralKit.Core/Models/PKSystem.cs | 2 +- PluralKit.Core/Models/Patch/SystemPatch.cs | 2 +- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/PluralKit.Bot/Commands/Autoproxy.cs b/PluralKit.Bot/Commands/Autoproxy.cs index 11d940fe..94e94dd2 100644 --- a/PluralKit.Bot/Commands/Autoproxy.cs +++ b/PluralKit.Bot/Commands/Autoproxy.cs @@ -1,10 +1,12 @@ using System; using System.Threading.Tasks; -using Dapper; - using DSharpPlus.Entities; +using Humanizer; + +using NodaTime; + using PluralKit.Core; namespace PluralKit.Bot @@ -133,29 +135,35 @@ namespace PluralKit.Bot { 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."); + var timeout = ctx.System.LatchTimeout.HasValue + ? Duration.FromSeconds(ctx.System.LatchTimeout.Value) + : (Duration?) null; + + if (timeout == null) + await ctx.Reply($"You do not have a custom autoproxy timeout duration set. The default latch timeout duration is {ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize()}."); + else if (timeout == Duration.Zero) + await ctx.Reply("Latch timeout is currently **disabled** for your system. Latch mode autoproxy will never time out."); else - await ctx.Reply($"The current latch timeout duration for your system is {ctx.System.LatchTimeout} hour(s)."); + await ctx.Reply($"The current latch timeout duration for your system is {timeout.Value.ToTimeSpan().Humanize()}."); 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."); + int newTimeoutHours; + if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeoutHours = 0; + else if (ctx.Match("reset", "default")) newTimeoutHours = -1; + else if (!int.TryParse(ctx.RemainderOrNull(), out newTimeoutHours)) throw new PKError("Duration must be a number of hours."); - await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, new SystemPatch{LatchTimeout = newTimeout})); + var newTimeout = newTimeoutHours > -1 ? Duration.FromHours(newTimeoutHours) : (Duration?) null; + await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, + new SystemPatch { LatchTimeout = (int?) newTimeout?.TotalSeconds })); - 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."); + if (newTimeoutHours == -1) + await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize()})."); + else if (newTimeoutHours == 0) + await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never time out."); else - await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout} hours."); + await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout.Value!.ToTimeSpan().Humanize()} hours."); } public async Task AutoproxyAccount(Context ctx) diff --git a/PluralKit.Bot/Proxy/ProxyMatcher.cs b/PluralKit.Bot/Proxy/ProxyMatcher.cs index 7e9aa0cf..e2a3c5e7 100644 --- a/PluralKit.Bot/Proxy/ProxyMatcher.cs +++ b/PluralKit.Bot/Proxy/ProxyMatcher.cs @@ -10,7 +10,7 @@ namespace PluralKit.Bot public class ProxyMatcher { private static readonly char AutoproxyEscapeCharacter = '\\'; - public static readonly int DefaultLatchExpiryTime = 6; + public static readonly Duration DefaultLatchExpiryTime = Duration.FromHours(6); private readonly IClock _clock; private readonly ProxyTagParser _parser; @@ -79,8 +79,10 @@ namespace PluralKit.Bot { if (ctx.LastMessage == null) return true; if (ctx.LatchTimeout == 0) return false; - - var timeout = Duration.FromHours(ctx.LatchTimeout == -1 ? DefaultLatchExpiryTime : ctx.LatchTimeout); + + var timeout = ctx.LatchTimeout.HasValue + ? Duration.FromSeconds(ctx.LatchTimeout.Value) + : DefaultLatchExpiryTime; var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value); return _clock.GetCurrentInstant() - timestamp > timeout; diff --git a/PluralKit.Core/Database/Functions/MessageContext.cs b/PluralKit.Core/Database/Functions/MessageContext.cs index 5d76890f..a3a6444f 100644 --- a/PluralKit.Core/Database/Functions/MessageContext.cs +++ b/PluralKit.Core/Database/Functions/MessageContext.cs @@ -25,6 +25,6 @@ namespace PluralKit.Core public string? SystemTag { get; } public string? SystemAvatar { get; } public bool AllowAutoproxy { get; } - public int LatchTimeout { get; } + public int? LatchTimeout { get; } } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Migrations/12.sql b/PluralKit.Core/Database/Migrations/12.sql index c00d8e74..54bf8397 100644 --- a/PluralKit.Core/Database/Migrations/12.sql +++ b/PluralKit.Core/Database/Migrations/12.sql @@ -1,9 +1,10 @@ --- SCHEMA VERSION 12: -- +-- SCHEMA VERSION 12: 2020-12-08 -- -- 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; +alter table systems add column latch_timeout int; -- in seconds + update info set schema_version = 12; \ No newline at end of file diff --git a/PluralKit.Core/Models/PKSystem.cs b/PluralKit.Core/Models/PKSystem.cs index 462e04a4..436f9cc9 100644 --- a/PluralKit.Core/Models/PKSystem.cs +++ b/PluralKit.Core/Models/PKSystem.cs @@ -18,7 +18,7 @@ namespace PluralKit.Core { public Instant Created { get; } public string UiTz { get; set; } public bool PingsEnabled { get; } - public int LatchTimeout { get; } + public int? LatchTimeout { get; } public PrivacyLevel DescriptionPrivacy { get; } public PrivacyLevel MemberListPrivacy { get;} public PrivacyLevel FrontPrivacy { get; } diff --git a/PluralKit.Core/Models/Patch/SystemPatch.cs b/PluralKit.Core/Models/Patch/SystemPatch.cs index 76fdfbb1..7c3a9551 100644 --- a/PluralKit.Core/Models/Patch/SystemPatch.cs +++ b/PluralKit.Core/Models/Patch/SystemPatch.cs @@ -15,7 +15,7 @@ namespace PluralKit.Core public Partial FrontPrivacy { get; set; } public Partial FrontHistoryPrivacy { get; set; } public Partial PingsEnabled { get; set; } - public Partial LatchTimeout { get; set; } + public Partial LatchTimeout { get; set; } public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b .With("name", Name)