Merge pull request #312 from spiralw/feat/sub-hour-latch-timeout
Add support for sub-hour latch timeout
This commit is contained in:
		@@ -143,40 +143,45 @@ namespace PluralKit.Bot
 | 
				
			|||||||
                    : (Duration?) null;
 | 
					                    : (Duration?) null;
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
                if (timeout == 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()}.");
 | 
					                    await ctx.Reply($"You do not have a custom autoproxy timeout duration set. The default latch timeout duration is {ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize(4)}.");
 | 
				
			||||||
                else if (timeout == Duration.Zero)
 | 
					                else if (timeout == Duration.Zero)
 | 
				
			||||||
                    await ctx.Reply("Latch timeout is currently **disabled** for your system. Latch mode autoproxy will never time out.");
 | 
					                    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 {timeout.Value.ToTimeSpan().Humanize()}.");
 | 
					                    await ctx.Reply($"The current latch timeout duration for your system is {timeout.Value.ToTimeSpan().Humanize(4)}.");
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // todo: somehow parse a more human-friendly date format
 | 
					            Duration? newTimeout;
 | 
				
			||||||
            int newTimeoutHours;
 | 
					            Duration overflow = Duration.Zero;
 | 
				
			||||||
            if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeoutHours = 0;
 | 
					            if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = Duration.Zero;
 | 
				
			||||||
            else if (ctx.Match("reset", "default")) newTimeoutHours = -1;
 | 
					            else if (ctx.Match("reset", "default")) newTimeout = null;
 | 
				
			||||||
            else if (!int.TryParse(ctx.RemainderOrNull(), out newTimeoutHours)) throw new PKError("Duration must be a number of hours.");
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
            int? overflow = null;
 | 
					                var timeoutStr = ctx.RemainderOrNull();
 | 
				
			||||||
            if (newTimeoutHours > 100000)
 | 
					                var timeoutPeriod = DateUtils.ParsePeriod(timeoutStr);
 | 
				
			||||||
 | 
					                if (timeoutPeriod == null) throw new PKError($"Could not parse '{timeoutStr}' as a valid duration. Try using a syntax such as \"3h5m\" (i.e. 3 hours and 5 minutes).");
 | 
				
			||||||
 | 
					                Console.WriteLine(timeoutPeriod.Value.TotalHours);
 | 
				
			||||||
 | 
					                if (timeoutPeriod.Value.TotalHours > (ulong)100000)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    // sanity check to prevent seconds overflow if someone types in 999999999
 | 
					                    // sanity check to prevent seconds overflow if someone types in 999999999
 | 
				
			||||||
                overflow = newTimeoutHours;
 | 
					                    overflow = timeoutPeriod.Value;
 | 
				
			||||||
                newTimeoutHours = 0;
 | 
					                    newTimeout = Duration.Zero;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else newTimeout = timeoutPeriod;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var newTimeout = newTimeoutHours > -1 ? Duration.FromHours(newTimeoutHours) : (Duration?) null;
 | 
					            var timeoutSeconds = newTimeout.HasValue ? newTimeout?.TotalSeconds : -1;
 | 
				
			||||||
            await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, 
 | 
					            await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, 
 | 
				
			||||||
                new SystemPatch { LatchTimeout = (int?) newTimeout?.TotalSeconds }));
 | 
					                new SystemPatch { LatchTimeout = (int?) newTimeout?.TotalSeconds }));
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            if (newTimeoutHours == -1)
 | 
					            if (newTimeout == null)
 | 
				
			||||||
                await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize()}).");
 | 
					                await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({ProxyMatcher.DefaultLatchExpiryTime.ToTimeSpan().Humanize(4)}).");
 | 
				
			||||||
            else if (newTimeoutHours == 0 && overflow != null)
 | 
					            else if (newTimeout == Duration.Zero && overflow != Duration.Zero)
 | 
				
			||||||
                await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never time out. ({overflow} hours is too long)");
 | 
					                await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never time out. ({overflow.ToTimeSpan().Humanize(4)} is too long)");
 | 
				
			||||||
            else if (newTimeoutHours == 0)
 | 
					            else if (newTimeout == Duration.Zero)
 | 
				
			||||||
                await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never time out.");
 | 
					                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.Value!.ToTimeSpan().Humanize()}.");
 | 
					                await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout.Value!.ToTimeSpan().Humanize(4)}.");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public async Task AutoproxyAccount(Context ctx)
 | 
					        public async Task AutoproxyAccount(Context ctx)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user