* Added method for deleting all callbackquery awaiting handlers

* Code cleanup
This commit is contained in:
gutii
2026-04-23 22:33:12 +04:00
parent 5dbf7f4073
commit cb024e52ff
12 changed files with 120 additions and 18 deletions
+33 -4
View File
@@ -2226,7 +2226,7 @@
</member> </member>
<member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.HandlerCollection"> <member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.HandlerCollection">
<summary> <summary>
<see cref="T:Telegrator.Core.IHandlersCollection"/> to ehich new builded handlers is adding <see cref="T:Telegrator.Core.IHandlersCollection"/> to which new builded handlers is adding
</summary> </summary>
</member> </member>
<member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.UpdateType"> <member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.UpdateType">
@@ -2749,6 +2749,14 @@
<param name="updateType">The update type key.</param> <param name="updateType">The update type key.</param>
<returns>The handler descriptor list for the given update type.</returns> <returns>The handler descriptor list for the given update type.</returns>
</member> </member>
<member name="M:Telegrator.Core.IHandlersCollection.TryGetDescriptorList(Telegram.Bot.Types.Enums.UpdateType,Telegrator.Core.Descriptors.HandlerDescriptorList@)">
<summary>
Tryes to get the <see cref="T:Telegrator.Core.Descriptors.HandlerDescriptorList"/> for the specified <see cref="T:Telegram.Bot.Types.Enums.UpdateType"/>.
</summary>
<param name="updateType"></param>
<param name="list"></param>
<returns></returns>
</member>
<member name="M:Telegrator.Core.IHandlersCollection.AddDescriptor(Telegrator.Core.Descriptors.HandlerDescriptor)"> <member name="M:Telegrator.Core.IHandlersCollection.AddDescriptor(Telegrator.Core.Descriptors.HandlerDescriptor)">
<summary> <summary>
Adds a <see cref="T:Telegrator.Core.Descriptors.HandlerDescriptor"/> to the collection and returns the updated collection. Adds a <see cref="T:Telegrator.Core.Descriptors.HandlerDescriptor"/> to the collection and returns the updated collection.
@@ -2774,7 +2782,7 @@
</member> </member>
<member name="M:Telegrator.Core.IHandlersProvider.TryGetDescriptorList(Telegram.Bot.Types.Enums.UpdateType,Telegrator.Core.Descriptors.HandlerDescriptorList@)"> <member name="M:Telegrator.Core.IHandlersProvider.TryGetDescriptorList(Telegram.Bot.Types.Enums.UpdateType,Telegrator.Core.Descriptors.HandlerDescriptorList@)">
<summary> <summary>
Gets the collection of <see cref="T:Telegram.Bot.Types.Enums.UpdateType"/> keys for the handler lists.
</summary> </summary>
<param name="updateType"></param> <param name="updateType"></param>
<param name="list"></param> <param name="list"></param>
@@ -6237,6 +6245,9 @@
<param name="descriptor">The handler descriptor to check for command aliases.</param> <param name="descriptor">The handler descriptor to check for command aliases.</param>
<exception cref="T:System.Exception">Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled.</exception> <exception cref="T:System.Exception">Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled.</exception>
</member> </member>
<member name="M:Telegrator.Providers.HandlersCollection.TryGetDescriptorList(Telegram.Bot.Types.Enums.UpdateType,Telegrator.Core.Descriptors.HandlerDescriptorList@)">
<inheritdoc/>
</member>
<member name="T:Telegrator.Providers.HandlersManagerBase"> <member name="T:Telegrator.Providers.HandlersManagerBase">
<summary> <summary>
Provides functionality of incrementally collecting, organizing and resolving handlers instances. Provides functionality of incrementally collecting, organizing and resolving handlers instances.
@@ -6502,7 +6513,7 @@
<param name="predicate"></param> <param name="predicate"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Telegrator.ColletionsExtensions.SingleSafe``1(System.Collections.Generic.IEnumerable{``0})"> <member name="M:Telegrator.ColletionsExtensions.SingleOrNothing``1(System.Collections.Generic.IEnumerable{``0})">
<summary> <summary>
Returns the only element of a sequence, or a default value if the sequence is empty. Returns the only element of a sequence, or a default value if the sequence is empty.
This method returns default if there is more than one element in the sequence. This method returns default if there is more than one element in the sequence.
@@ -6511,7 +6522,7 @@
<param name="source"></param> <param name="source"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Telegrator.ColletionsExtensions.SingleSafe``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.Boolean})"> <member name="M:Telegrator.ColletionsExtensions.SingleOrNothing``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.Boolean})">
<summary> <summary>
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists. Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists.
This method return default if more than one element satisfies the condition. This method return default if more than one element satisfies the condition.
@@ -6976,6 +6987,16 @@
<param name="handlingUpdate">The update that triggered the awaiter creation.</param> <param name="handlingUpdate">The update that triggered the awaiter creation.</param>
<returns>An awaiter handler builder for the specified update type.</returns> <returns>An awaiter handler builder for the specified update type.</returns>
</member> </member>
<member name="M:Telegrator.AwaitingProviderExtensions.CreateDeleting``1(Telegrator.Core.IAwaitingProvider,Telegram.Bot.Types.Enums.UpdateType,Telegram.Bot.Types.Update)">
<summary>
Creates an awaiter handler builder for a specific update type that will delete the awaiting handler after awaiting is completed.
</summary>
<typeparam name="TUpdate"></typeparam>
<param name="awaitingProvider"></param>
<param name="updateType"></param>
<param name="handlingUpdate"></param>
<returns></returns>
</member>
<member name="M:Telegrator.AwaitingProviderExtensions.AwaitAny(Telegrator.Core.IAwaitingProvider,Telegram.Bot.Types.Update)"> <member name="M:Telegrator.AwaitingProviderExtensions.AwaitAny(Telegrator.Core.IAwaitingProvider,Telegram.Bot.Types.Update)">
<summary> <summary>
Creates an awaiter builder for any update type. Creates an awaiter builder for any update type.
@@ -7000,6 +7021,14 @@
<param name="handlingUpdate"></param> <param name="handlingUpdate"></param>
<returns>An awaiter builder for callback query updates.</returns> <returns>An awaiter builder for callback query updates.</returns>
</member> </member>
<member name="M:Telegrator.AwaitingProviderExtensions.CancellAllCallbacks(Telegrator.Core.IAwaitingProvider,Telegram.Bot.Types.Update)">
<summary>
Deletes all awaiting handlers for callback query updates.
</summary>
<param name="awaitingProvider"></param>
<param name="handlingUpdate"></param>
<returns></returns>
</member>
<member name="T:Telegrator.HandlersProviderExtensions"> <member name="T:Telegrator.HandlersProviderExtensions">
<summary> <summary>
Extesions method for handlers providers Extesions method for handlers providers
@@ -43,7 +43,6 @@ public class HandlerDescriptor
public DescriptorType Type public DescriptorType Type
{ {
get; get;
private set;
} }
/// <summary> /// <summary>
@@ -52,7 +51,6 @@ public class HandlerDescriptor
public Type HandlerType public Type HandlerType
{ {
get; get;
private set;
} }
/// <summary> /// <summary>
@@ -164,7 +162,7 @@ public class HandlerDescriptor
return; return;
UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(handlerType); UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(handlerType);
if (handlerAttribute.ExpectingHandlerType != null && !handlerAttribute.ExpectingHandlerType.Contains(handlerType.BaseType)) if (handlerAttribute.ExpectingHandlerType?.Contains(handlerType.BaseType) == false)
throw new ArgumentException(string.Format("This handler attribute cannot be attached to this class. Attribute can be attached on next handlers : {0}", string.Join(", ", handlerAttribute.ExpectingHandlerType.AsEnumerable()))); throw new ArgumentException(string.Format("This handler attribute cannot be attached to this class. Attribute can be attached on next handlers : {0}", string.Join(", ", handlerAttribute.ExpectingHandlerType.AsEnumerable())));
IFilter<Update>? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType); IFilter<Update>? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType);
@@ -15,7 +15,7 @@ public abstract class HandlerBuilderBase(Type buildingHandlerType, UpdateType up
private static int HandlerServiceKeyIndex = 0; private static int HandlerServiceKeyIndex = 0;
/// <summary> /// <summary>
/// <see cref="IHandlersCollection"/> to ehich new builded handlers is adding /// <see cref="IHandlersCollection"/> to which new builded handlers is adding
/// </summary> /// </summary>
protected readonly IHandlersCollection? HandlerCollection = handlerCollection; protected readonly IHandlersCollection? HandlerCollection = handlerCollection;
@@ -31,6 +31,14 @@ public interface IHandlersCollection
/// <returns>The handler descriptor list for the given update type.</returns> /// <returns>The handler descriptor list for the given update type.</returns>
public HandlerDescriptorList this[UpdateType updateType] { get; } public HandlerDescriptorList this[UpdateType updateType] { get; }
/// <summary>
/// Tryes to get the <see cref="HandlerDescriptorList"/> for the specified <see cref="UpdateType"/>.
/// </summary>
/// <param name="updateType"></param>
/// <param name="list"></param>
/// <returns></returns>
public bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list);
/// <summary> /// <summary>
/// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection. /// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection.
/// </summary> /// </summary>
+1 -1
View File
@@ -6,5 +6,5 @@
/// </summary> /// </summary>
public interface IHandlersManager : IHandlersCollection, IHandlersProvider public interface IHandlersManager : IHandlersCollection, IHandlersProvider
{ {
// :P
} }
+1 -1
View File
@@ -15,7 +15,7 @@ public interface IHandlersProvider
public IEnumerable<UpdateType> AllowedTypes { get; } public IEnumerable<UpdateType> AllowedTypes { get; }
/// <summary> /// <summary>
/// /// Gets the collection of <see cref="UpdateType"/> keys for the handler lists.
/// </summary> /// </summary>
/// <param name="updateType"></param> /// <param name="updateType"></param>
/// <param name="list"></param> /// <param name="list"></param>
@@ -0,0 +1,37 @@
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegrator.Core;
using Telegrator.Core.Descriptors;
using Telegrator.Core.Filters;
using Telegrator.Core.Handlers.Building;
using Telegrator.Core.States;
namespace Telegrator.Handlers.Building;
internal class AwaiterHandlerDeleter<TUpdate>(UpdateType updateType, Update handlingUpdate, IAwaitingProvider handlerProvider) : IAwaiterHandlerBuilder<TUpdate> where TUpdate : class
{
public Task<TUpdate> Await(IStateKeyResolver keyResolver, CancellationToken cancellationToken = default)
{
string? handlingKey = keyResolver.ResolveKey(handlingUpdate);
if (handlingKey is null)
throw new InvalidOperationException("Cannot await update with resolved key as NULL");
if (!handlerProvider.TryGetDescriptorList(updateType, out HandlerDescriptorList? list) || list == null)
return Task.FromResult<TUpdate>(null!);
foreach (DescriptorIndexer handler in list.Where(x => x.UpdateType == updateType && typeof(IAwaiterHandlerBuilder<>).IsAssignableFrom(x.HandlerType)).Select(x => x.Indexer).ToArray())
list.Remove(handler);
return Task.FromResult<TUpdate>(null!);
}
public void AddFilter(IFilter<Update> filter) => throw new NotImplementedException();
public void AddFilters(params IFilter<Update>[] filters) => throw new NotImplementedException();
public void AddTargetedFilter<TFilterTarget>(Func<Update, TFilterTarget?> getFilterringTarget, IFilter<TFilterTarget> filter) where TFilterTarget : class => throw new NotImplementedException();
public void AddTargetedFilters<TFilterTarget>(Func<Update, TFilterTarget?> getFilterringTarget, params IFilter<TFilterTarget>[] filters) where TFilterTarget : class => throw new NotImplementedException();
public void SetConcurreny(int concurrency) => throw new NotImplementedException();
public void SetIndexer(int concurrency, int priority) => throw new NotImplementedException();
public void SetPriority(int priority) => throw new NotImplementedException();
public void SetState<TKey, TValue>(TValue? state) where TKey : IStateKeyResolver, new() where TValue : IEquatable<TValue> => throw new NotImplementedException();
public void SetUpdateValidating(UpdateValidateAction validateAction) => throw new NotImplementedException();
}
@@ -19,6 +19,12 @@ public class AwaitingProvider(TelegratorOptions options) : HandlersProvider([],
/// <inheritdoc/> /// <inheritdoc/>
public override bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list) public override bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list)
{ {
if (HandlersList.HandlingType != updateType)
{
list = null;
return false;
}
list = HandlersList; list = HandlersList;
return true; return true;
} }
@@ -96,8 +96,7 @@ public class HandlersCollection(TelegratorOptions? options) : IHandlersCollectio
protected virtual HandlerDescriptorList GetDescriptorList(HandlerDescriptor descriptor) protected virtual HandlerDescriptorList GetDescriptorList(HandlerDescriptor descriptor)
{ {
UpdateType updateType = UpdateTypeExtensions.SuppressTypes.TryGetValue(descriptor.UpdateType, out UpdateType suppressType) UpdateType updateType = UpdateTypeExtensions.SuppressTypes.TryGetValue(descriptor.UpdateType, out UpdateType suppressType)
? suppressType ? suppressType : descriptor.UpdateType;
: descriptor.UpdateType;
if (!InnerDictionary.TryGetValue(updateType, out HandlerDescriptorList? list)) if (!InnerDictionary.TryGetValue(updateType, out HandlerDescriptorList? list))
{ {
@@ -127,4 +126,10 @@ public class HandlersCollection(TelegratorOptions? options) : IHandlersCollectio
CommandAliasses.AddRange(alliasAttribute.Alliases); CommandAliasses.AddRange(alliasAttribute.Alliases);
} }
/// <inheritdoc/>
public bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList list)
{
return InnerDictionary.TryGetValue(updateType, out list);
}
} }
@@ -95,7 +95,7 @@ public abstract class HandlersManagerBase(TelegratorOptions options) : IHandlers
/// <exception cref="Exception">Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled.</exception> /// <exception cref="Exception">Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled.</exception>
protected virtual void IntersectCommands(HandlerDescriptor descriptor) protected virtual void IntersectCommands(HandlerDescriptor descriptor)
{ {
if (Options == null || !Options.ExceptIntersectingCommandAliases) if (Options?.ExceptIntersectingCommandAliases is not true)
return; return;
CommandAlliasAttribute? alliasAttribute = descriptor.HandlerType.GetCustomAttribute<CommandAlliasAttribute>(); CommandAlliasAttribute? alliasAttribute = descriptor.HandlerType.GetCustomAttribute<CommandAlliasAttribute>();
+3 -3
View File
@@ -136,7 +136,7 @@ public static partial class ColletionsExtensions
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="source"></param> /// <param name="source"></param>
/// <returns></returns> /// <returns></returns>
public static T? SingleSafe<T>(this IEnumerable<T> source) public static T? SingleOrNothing<T>(this IEnumerable<T> source)
=> source.Count() == 1 ? source.ElementAt(0) : default; => source.Count() == 1 ? source.ElementAt(0) : default;
/// <summary> /// <summary>
@@ -147,7 +147,7 @@ public static partial class ColletionsExtensions
/// <param name="source"></param> /// <param name="source"></param>
/// <param name="predicate"></param> /// <param name="predicate"></param>
/// <returns></returns> /// <returns></returns>
public static T? SingleSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate) public static T? SingleOrNothing<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{ {
source = source.Where(predicate); source = source.Where(predicate);
return source.Count() == 1 ? source.ElementAt(0) : default; return source.Count() == 1 ? source.ElementAt(0) : default;
@@ -205,7 +205,7 @@ public static partial class ReflectionExtensions
/// <param name="type"></param> /// <param name="type"></param>
/// <returns></returns> /// <returns></returns>
public static bool HasPublicProperties(this Type type) public static bool HasPublicProperties(this Type type)
=> type.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "IsCollectible").Any(); => type.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Any(prop => prop.Name != "IsCollectible");
/// <summary> /// <summary>
/// Determines whether an instance of a specified type can be assigned to an instance of the current type /// Determines whether an instance of a specified type can be assigned to an instance of the current type
+21 -2
View File
@@ -231,6 +231,17 @@ public static class AwaitingProviderExtensions
public static IAwaiterHandlerBuilder<TUpdate> CreateAbstract<TUpdate>(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class public static IAwaiterHandlerBuilder<TUpdate> CreateAbstract<TUpdate>(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class
=> new AwaiterHandlerBuilder<TUpdate>(updateType, handlingUpdate, awaitingProvider); => new AwaiterHandlerBuilder<TUpdate>(updateType, handlingUpdate, awaitingProvider);
/// <summary>
/// Creates an awaiter handler builder for a specific update type that will delete the awaiting handler after awaiting is completed.
/// </summary>
/// <typeparam name="TUpdate"></typeparam>
/// <param name="awaitingProvider"></param>
/// <param name="updateType"></param>
/// <param name="handlingUpdate"></param>
/// <returns></returns>
public static IAwaiterHandlerBuilder<TUpdate> CreateDeleting<TUpdate>(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class
=> new AwaiterHandlerDeleter<TUpdate>(updateType, handlingUpdate, awaitingProvider);
/// <summary> /// <summary>
/// Creates an awaiter builder for any update type. /// Creates an awaiter builder for any update type.
/// </summary> /// </summary>
@@ -257,6 +268,15 @@ public static class AwaitingProviderExtensions
/// <returns>An awaiter builder for callback query updates.</returns> /// <returns>An awaiter builder for callback query updates.</returns>
public static IAwaiterHandlerBuilder<CallbackQuery> AwaitCallbackQuery(this IAwaitingProvider awaitingProvider, Update handlingUpdate) public static IAwaiterHandlerBuilder<CallbackQuery> AwaitCallbackQuery(this IAwaitingProvider awaitingProvider, Update handlingUpdate)
=> awaitingProvider.CreateAbstract<CallbackQuery>(UpdateType.CallbackQuery, handlingUpdate); => awaitingProvider.CreateAbstract<CallbackQuery>(UpdateType.CallbackQuery, handlingUpdate);
/// <summary>
/// Deletes all awaiting handlers for callback query updates.
/// </summary>
/// <param name="awaitingProvider"></param>
/// <param name="handlingUpdate"></param>
/// <returns></returns>
public static IAwaiterHandlerBuilder<CallbackQuery> CancellAllCallbacks(this IAwaitingProvider awaitingProvider, Update handlingUpdate)
=> awaitingProvider.CreateDeleting<CallbackQuery>(UpdateType.CallbackQuery, handlingUpdate);
} }
/// <summary> /// <summary>
@@ -418,8 +438,7 @@ public static partial class HandlersCollectionExtensions
{ {
(collectingTarget ?? Assembly.GetCallingAssembly()) (collectingTarget ?? Assembly.GetCallingAssembly())
.GetExportedTypes() .GetExportedTypes()
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null) .Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null && type.IsHandlerRealization())
.Where(type => type.IsHandlerRealization())
.ForEach(type => handlers.AddHandler(type)); .ForEach(type => handlers.AddHandler(type));
return handlers; return handlers;