* Added method for deleting all callbackquery awaiting handlers
* Code cleanup
This commit is contained in:
+33
-4
@@ -2226,7 +2226,7 @@
|
||||
</member>
|
||||
<member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.HandlerCollection">
|
||||
<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>
|
||||
</member>
|
||||
<member name="F:Telegrator.Core.Handlers.Building.HandlerBuilderBase.UpdateType">
|
||||
@@ -2749,6 +2749,14 @@
|
||||
<param name="updateType">The update type key.</param>
|
||||
<returns>The handler descriptor list for the given update type.</returns>
|
||||
</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)">
|
||||
<summary>
|
||||
Adds a <see cref="T:Telegrator.Core.Descriptors.HandlerDescriptor"/> to the collection and returns the updated collection.
|
||||
@@ -2774,7 +2782,7 @@
|
||||
</member>
|
||||
<member name="M:Telegrator.Core.IHandlersProvider.TryGetDescriptorList(Telegram.Bot.Types.Enums.UpdateType,Telegrator.Core.Descriptors.HandlerDescriptorList@)">
|
||||
<summary>
|
||||
|
||||
Gets the collection of <see cref="T:Telegram.Bot.Types.Enums.UpdateType"/> keys for the handler lists.
|
||||
</summary>
|
||||
<param name="updateType"></param>
|
||||
<param name="list"></param>
|
||||
@@ -6237,6 +6245,9 @@
|
||||
<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>
|
||||
</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">
|
||||
<summary>
|
||||
Provides functionality of incrementally collecting, organizing and resolving handlers instances.
|
||||
@@ -6502,7 +6513,7 @@
|
||||
<param name="predicate"></param>
|
||||
<returns></returns>
|
||||
</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>
|
||||
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.
|
||||
@@ -6511,7 +6522,7 @@
|
||||
<param name="source"></param>
|
||||
<returns></returns>
|
||||
</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>
|
||||
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.
|
||||
@@ -6976,6 +6987,16 @@
|
||||
<param name="handlingUpdate">The update that triggered the awaiter creation.</param>
|
||||
<returns>An awaiter handler builder for the specified update type.</returns>
|
||||
</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)">
|
||||
<summary>
|
||||
Creates an awaiter builder for any update type.
|
||||
@@ -7000,6 +7021,14 @@
|
||||
<param name="handlingUpdate"></param>
|
||||
<returns>An awaiter builder for callback query updates.</returns>
|
||||
</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">
|
||||
<summary>
|
||||
Extesions method for handlers providers
|
||||
|
||||
@@ -43,7 +43,6 @@ public class HandlerDescriptor
|
||||
public DescriptorType Type
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,7 +51,6 @@ public class HandlerDescriptor
|
||||
public Type HandlerType
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -164,7 +162,7 @@ public class HandlerDescriptor
|
||||
return;
|
||||
|
||||
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())));
|
||||
|
||||
IFilter<Update>? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType);
|
||||
|
||||
@@ -15,7 +15,7 @@ public abstract class HandlerBuilderBase(Type buildingHandlerType, UpdateType up
|
||||
private static int HandlerServiceKeyIndex = 0;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IHandlersCollection"/> to ehich new builded handlers is adding
|
||||
/// <see cref="IHandlersCollection"/> to which new builded handlers is adding
|
||||
/// </summary>
|
||||
protected readonly IHandlersCollection? HandlerCollection = handlerCollection;
|
||||
|
||||
|
||||
@@ -31,6 +31,14 @@ public interface IHandlersCollection
|
||||
/// <returns>The handler descriptor list for the given update type.</returns>
|
||||
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>
|
||||
/// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection.
|
||||
/// </summary>
|
||||
|
||||
@@ -6,5 +6,5 @@
|
||||
/// </summary>
|
||||
public interface IHandlersManager : IHandlersCollection, IHandlersProvider
|
||||
{
|
||||
|
||||
// :P
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public interface IHandlersProvider
|
||||
public IEnumerable<UpdateType> AllowedTypes { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Gets the collection of <see cref="UpdateType"/> keys for the handler lists.
|
||||
/// </summary>
|
||||
/// <param name="updateType"></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/>
|
||||
public override bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list)
|
||||
{
|
||||
if (HandlersList.HandlingType != updateType)
|
||||
{
|
||||
list = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
list = HandlersList;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -96,8 +96,7 @@ public class HandlersCollection(TelegratorOptions? options) : IHandlersCollectio
|
||||
protected virtual HandlerDescriptorList GetDescriptorList(HandlerDescriptor descriptor)
|
||||
{
|
||||
UpdateType updateType = UpdateTypeExtensions.SuppressTypes.TryGetValue(descriptor.UpdateType, out UpdateType suppressType)
|
||||
? suppressType
|
||||
: descriptor.UpdateType;
|
||||
? suppressType : descriptor.UpdateType;
|
||||
|
||||
if (!InnerDictionary.TryGetValue(updateType, out HandlerDescriptorList? list))
|
||||
{
|
||||
@@ -127,4 +126,10 @@ public class HandlersCollection(TelegratorOptions? options) : IHandlersCollectio
|
||||
|
||||
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>
|
||||
protected virtual void IntersectCommands(HandlerDescriptor descriptor)
|
||||
{
|
||||
if (Options == null || !Options.ExceptIntersectingCommandAliases)
|
||||
if (Options?.ExceptIntersectingCommandAliases is not true)
|
||||
return;
|
||||
|
||||
CommandAlliasAttribute? alliasAttribute = descriptor.HandlerType.GetCustomAttribute<CommandAlliasAttribute>();
|
||||
|
||||
@@ -136,7 +136,7 @@ public static partial class ColletionsExtensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <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;
|
||||
|
||||
/// <summary>
|
||||
@@ -147,7 +147,7 @@ public static partial class ColletionsExtensions
|
||||
/// <param name="source"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <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);
|
||||
return source.Count() == 1 ? source.ElementAt(0) : default;
|
||||
@@ -205,7 +205,7 @@ public static partial class ReflectionExtensions
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
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>
|
||||
/// Determines whether an instance of a specified type can be assigned to an instance of the current type
|
||||
|
||||
@@ -231,6 +231,17 @@ public static class AwaitingProviderExtensions
|
||||
public static IAwaiterHandlerBuilder<TUpdate> CreateAbstract<TUpdate>(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class
|
||||
=> 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>
|
||||
/// Creates an awaiter builder for any update type.
|
||||
/// </summary>
|
||||
@@ -257,6 +268,15 @@ public static class AwaitingProviderExtensions
|
||||
/// <returns>An awaiter builder for callback query updates.</returns>
|
||||
public static IAwaiterHandlerBuilder<CallbackQuery> AwaitCallbackQuery(this IAwaitingProvider awaitingProvider, Update 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>
|
||||
@@ -418,8 +438,7 @@ public static partial class HandlersCollectionExtensions
|
||||
{
|
||||
(collectingTarget ?? Assembly.GetCallingAssembly())
|
||||
.GetExportedTypes()
|
||||
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
|
||||
.Where(type => type.IsHandlerRealization())
|
||||
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null && type.IsHandlerRealization())
|
||||
.ForEach(type => handlers.AddHandler(type));
|
||||
|
||||
return handlers;
|
||||
|
||||
Reference in New Issue
Block a user