General bits of cleanup
This commit is contained in:
@@ -64,7 +64,8 @@ namespace PluralKit.Bot
|
||||
{
|
||||
PrivacyFilter.PrivateOnly => PrivacyLevel.Private,
|
||||
PrivacyFilter.PublicOnly => PrivacyLevel.Public,
|
||||
PrivacyFilter.All => null
|
||||
PrivacyFilter.All => null,
|
||||
_ => throw new ArgumentOutOfRangeException($"Unknown privacy filter {PrivacyFilter}")
|
||||
}, Filter, SearchInDescription);
|
||||
|
||||
private IEnumerable<ListedMember> Sort(IEnumerable<ListedMember> input)
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class ProxyMatcher
|
||||
{
|
||||
public static readonly Duration LatchExpiryTime = Duration.FromHours(6);
|
||||
private static readonly Duration LatchExpiryTime = Duration.FromHours(6);
|
||||
|
||||
private IClock _clock;
|
||||
private ProxyTagParser _parser;
|
||||
private readonly IClock _clock;
|
||||
private readonly ProxyTagParser _parser;
|
||||
|
||||
public ProxyMatcher(ProxyTagParser parser, IClock clock)
|
||||
{
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class ProxyService
|
||||
{
|
||||
public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
|
||||
private static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
|
||||
|
||||
private readonly LogChannelService _logChannel;
|
||||
private readonly IDatabase _db;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -49,20 +48,6 @@ namespace PluralKit.Bot
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryMatchTags(string input, ProxyTag tag, out string inner)
|
||||
{
|
||||
// This just wraps TryMatchTagsInner w/ support for leading mentions
|
||||
var leadingMention = ExtractLeadingMention(ref input);
|
||||
|
||||
inner = "";
|
||||
if (!TryMatchTagsInner(input, tag, out var innerRaw)) return false;
|
||||
|
||||
// Add leading mentions back
|
||||
inner = leadingMention == null ? innerRaw : $"{leadingMention} {innerRaw}";
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private bool TryMatchTagsInner(string input, ProxyTag tag, out string inner)
|
||||
{
|
||||
inner = "";
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class HandlerQueue<T>
|
||||
{
|
||||
private readonly List<HandlerEntry> _handlers = new List<HandlerEntry>();
|
||||
|
||||
public HandlerEntry Add(Func<T, Task<bool>> handler)
|
||||
{
|
||||
var entry = new HandlerEntry {Handler = handler};
|
||||
_handlers.Add(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public async Task<T> WaitFor(Func<T, bool> predicate, Duration? timeout = null, CancellationToken ct = default)
|
||||
{
|
||||
var timeoutTask = Task.Delay(timeout?.ToTimeSpan() ?? TimeSpan.FromMilliseconds(-1), ct);
|
||||
var tcs = new TaskCompletionSource<T>();
|
||||
|
||||
Task<bool> Handler(T e)
|
||||
{
|
||||
var matches = predicate(e);
|
||||
if (matches) tcs.SetResult(e);
|
||||
return Task.FromResult(matches);
|
||||
}
|
||||
|
||||
var entry = new HandlerEntry {Handler = Handler};
|
||||
_handlers.Add(entry);
|
||||
|
||||
// Wait for either the event task or the timeout task
|
||||
// If the timeout task finishes first, raise, otherwise pass event through
|
||||
try
|
||||
{
|
||||
var theTask = await Task.WhenAny(timeoutTask, tcs.Task);
|
||||
if (theTask == timeoutTask)
|
||||
throw new TimeoutException();
|
||||
}
|
||||
finally
|
||||
{
|
||||
entry.Remove();
|
||||
}
|
||||
|
||||
return await tcs.Task;
|
||||
}
|
||||
|
||||
public async Task<bool> TryHandle(T evt)
|
||||
{
|
||||
_handlers.RemoveAll(he => !he.Alive);
|
||||
|
||||
var now = SystemClock.Instance.GetCurrentInstant();
|
||||
foreach (var entry in _handlers)
|
||||
{
|
||||
if (entry.Expiry < now) entry.Alive = false;
|
||||
else if (entry.Alive && await entry.Handler(evt))
|
||||
{
|
||||
entry.Alive = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public class HandlerEntry
|
||||
{
|
||||
internal Func<T, Task<bool>> Handler;
|
||||
internal bool Alive = true;
|
||||
internal Instant Expiry = SystemClock.Instance.GetCurrentInstant() + Duration.FromMinutes(30);
|
||||
|
||||
public void Remove() => Alive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user