Convert autoproxy timeout to use seconds
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
@@ -80,7 +80,9 @@ 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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,10 @@
 | 
			
		||||
-- SCHEMA VERSION 12: <insert date> --
 | 
			
		||||
-- 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;
 | 
			
		||||
@@ -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; }
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +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 Partial<int?> LatchTimeout { get; set; }
 | 
			
		||||
 | 
			
		||||
        public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
 | 
			
		||||
            .With("name", Name)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user