Convert autoproxy timeout to use seconds
This commit is contained in:
		@@ -1,10 +1,12 @@
 | 
				
			|||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using Dapper;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
using DSharpPlus.Entities;
 | 
					using DSharpPlus.Entities;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using Humanizer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using NodaTime;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using PluralKit.Core;
 | 
					using PluralKit.Core;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace PluralKit.Bot
 | 
					namespace PluralKit.Bot
 | 
				
			||||||
@@ -133,29 +135,35 @@ namespace PluralKit.Bot
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            if (!ctx.HasNext())
 | 
					            if (!ctx.HasNext())
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (ctx.System.LatchTimeout == -1)
 | 
					                var timeout = ctx.System.LatchTimeout.HasValue
 | 
				
			||||||
                    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).");
 | 
					                    ? Duration.FromSeconds(ctx.System.LatchTimeout.Value) 
 | 
				
			||||||
                else if (ctx.System.LatchTimeout == 0)
 | 
					                    : (Duration?) null;
 | 
				
			||||||
                    await ctx.Reply("Latch timeout is currently **disabled** for your system. Latch mode autoproxy will never timeout.");
 | 
					                
 | 
				
			||||||
 | 
					                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
 | 
					                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;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // todo: somehow parse a more human-friendly date format
 | 
					            // todo: somehow parse a more human-friendly date format
 | 
				
			||||||
            int newTimeout;
 | 
					            int newTimeoutHours;
 | 
				
			||||||
            if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = 0;
 | 
					            if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeoutHours = 0;
 | 
				
			||||||
            else if (ctx.Match("reset", "default")) newTimeout = -1;
 | 
					            else if (ctx.Match("reset", "default")) newTimeoutHours = -1;
 | 
				
			||||||
            else if (!int.TryParse(ctx.RemainderOrNull(), out newTimeout)) throw new PKError("Duration must be an integer.");
 | 
					            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)
 | 
					            if (newTimeoutHours == -1)
 | 
				
			||||||
                await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({PluralKit.Bot.ProxyMatcher.DefaultLatchExpiryTime} hours).");
 | 
					                await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize()}).");
 | 
				
			||||||
            else if (newTimeout == 0)
 | 
					            else if (newTimeoutHours == 0)
 | 
				
			||||||
                await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never timeout.");
 | 
					                await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never time out.");
 | 
				
			||||||
            else
 | 
					            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)
 | 
					        public async Task AutoproxyAccount(Context ctx)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ namespace PluralKit.Bot
 | 
				
			|||||||
    public class ProxyMatcher
 | 
					    public class ProxyMatcher
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private static readonly char AutoproxyEscapeCharacter = '\\';
 | 
					        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 IClock _clock;
 | 
				
			||||||
        private readonly ProxyTagParser _parser;
 | 
					        private readonly ProxyTagParser _parser;
 | 
				
			||||||
@@ -79,8 +79,10 @@ namespace PluralKit.Bot
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            if (ctx.LastMessage == null) return true;
 | 
					            if (ctx.LastMessage == null) return true;
 | 
				
			||||||
            if (ctx.LatchTimeout == 0) return false;
 | 
					            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);
 | 
					            var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value);
 | 
				
			||||||
            return _clock.GetCurrentInstant() - timestamp > timeout;
 | 
					            return _clock.GetCurrentInstant() - timestamp > timeout;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,6 +25,6 @@ namespace PluralKit.Core
 | 
				
			|||||||
        public string? SystemTag { get; }
 | 
					        public string? SystemTag { get; }
 | 
				
			||||||
        public string? SystemAvatar { get; }
 | 
					        public string? SystemAvatar { get; }
 | 
				
			||||||
        public bool AllowAutoproxy { 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 front/latch autoproxy per-member --
 | 
				
			||||||
-- Add disabling autoproxy per-account --
 | 
					-- Add disabling autoproxy per-account --
 | 
				
			||||||
-- Add configurable latch timeout --
 | 
					-- Add configurable latch timeout --
 | 
				
			||||||
 | 
					
 | 
				
			||||||
alter table members add column allow_autoproxy bool not null default true;
 | 
					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 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;
 | 
					update info set schema_version = 12;
 | 
				
			||||||
@@ -18,7 +18,7 @@ namespace PluralKit.Core {
 | 
				
			|||||||
        public Instant Created { get; }
 | 
					        public Instant Created { get; }
 | 
				
			||||||
        public string UiTz { get; set; }
 | 
					        public string UiTz { get; set; }
 | 
				
			||||||
        public bool PingsEnabled { get; }
 | 
					        public bool PingsEnabled { get; }
 | 
				
			||||||
        public int LatchTimeout { get; }
 | 
					        public int? LatchTimeout { get; }
 | 
				
			||||||
	    public PrivacyLevel DescriptionPrivacy { get; }
 | 
						    public PrivacyLevel DescriptionPrivacy { get; }
 | 
				
			||||||
        public PrivacyLevel MemberListPrivacy { get;}
 | 
					        public PrivacyLevel MemberListPrivacy { get;}
 | 
				
			||||||
        public PrivacyLevel FrontPrivacy { get; }
 | 
					        public PrivacyLevel FrontPrivacy { get; }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,7 @@ namespace PluralKit.Core
 | 
				
			|||||||
        public Partial<PrivacyLevel> FrontPrivacy { get; set; }
 | 
					        public Partial<PrivacyLevel> FrontPrivacy { get; set; }
 | 
				
			||||||
        public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
 | 
					        public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
 | 
				
			||||||
        public Partial<bool> PingsEnabled { 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
 | 
					        public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
 | 
				
			||||||
            .With("name", Name)
 | 
					            .With("name", Name)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user