diff --git a/docs/Telegrator.xml b/docs/Telegrator.xml index 35a1997..71bbd19 100644 --- a/docs/Telegrator.xml +++ b/docs/Telegrator.xml @@ -2226,7 +2226,7 @@ - to ehich new builded handlers is adding + to which new builded handlers is adding @@ -2749,6 +2749,14 @@ The update type key. The handler descriptor list for the given update type. + + + Tryes to get the for the specified . + + + + + Adds a to the collection and returns the updated collection. @@ -2774,7 +2782,7 @@ - + Gets the collection of keys for the handler lists. @@ -6237,6 +6245,9 @@ The handler descriptor to check for command aliases. Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled. + + + Provides functionality of incrementally collecting, organizing and resolving handlers instances. @@ -6502,7 +6513,7 @@ - + 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 @@ - + 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 @@ The update that triggered the awaiter creation. An awaiter handler builder for the specified update type. + + + Creates an awaiter handler builder for a specific update type that will delete the awaiting handler after awaiting is completed. + + + + + + + Creates an awaiter builder for any update type. @@ -7000,6 +7021,14 @@ An awaiter builder for callback query updates. + + + Deletes all awaiting handlers for callback query updates. + + + + + Extesions method for handlers providers diff --git a/src/Telegrator/Core/Descriptors/HandlerDescriptor.cs b/src/Telegrator/Core/Descriptors/HandlerDescriptor.cs index ad87255..ae427c5 100644 --- a/src/Telegrator/Core/Descriptors/HandlerDescriptor.cs +++ b/src/Telegrator/Core/Descriptors/HandlerDescriptor.cs @@ -43,7 +43,6 @@ public class HandlerDescriptor public DescriptorType Type { get; - private set; } /// @@ -52,7 +51,6 @@ public class HandlerDescriptor public Type HandlerType { get; - private set; } /// @@ -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? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType); diff --git a/src/Telegrator/Core/Handlers/Building/HandlerBuilderBase.cs b/src/Telegrator/Core/Handlers/Building/HandlerBuilderBase.cs index 194c81b..bc95596 100644 --- a/src/Telegrator/Core/Handlers/Building/HandlerBuilderBase.cs +++ b/src/Telegrator/Core/Handlers/Building/HandlerBuilderBase.cs @@ -15,7 +15,7 @@ public abstract class HandlerBuilderBase(Type buildingHandlerType, UpdateType up private static int HandlerServiceKeyIndex = 0; /// - /// to ehich new builded handlers is adding + /// to which new builded handlers is adding /// protected readonly IHandlersCollection? HandlerCollection = handlerCollection; diff --git a/src/Telegrator/Core/IHandlersCollection.cs b/src/Telegrator/Core/IHandlersCollection.cs index 184b82a..75e9eb7 100644 --- a/src/Telegrator/Core/IHandlersCollection.cs +++ b/src/Telegrator/Core/IHandlersCollection.cs @@ -31,6 +31,14 @@ public interface IHandlersCollection /// The handler descriptor list for the given update type. public HandlerDescriptorList this[UpdateType updateType] { get; } + /// + /// Tryes to get the for the specified . + /// + /// + /// + /// + public bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list); + /// /// Adds a to the collection and returns the updated collection. /// diff --git a/src/Telegrator/Core/IHandlersManager.cs b/src/Telegrator/Core/IHandlersManager.cs index fb36737..0e9e673 100644 --- a/src/Telegrator/Core/IHandlersManager.cs +++ b/src/Telegrator/Core/IHandlersManager.cs @@ -6,5 +6,5 @@ /// public interface IHandlersManager : IHandlersCollection, IHandlersProvider { - + // :P } diff --git a/src/Telegrator/Core/IHandlersProvider.cs b/src/Telegrator/Core/IHandlersProvider.cs index 1e32c2d..915ff8f 100644 --- a/src/Telegrator/Core/IHandlersProvider.cs +++ b/src/Telegrator/Core/IHandlersProvider.cs @@ -15,7 +15,7 @@ public interface IHandlersProvider public IEnumerable AllowedTypes { get; } /// - /// + /// Gets the collection of keys for the handler lists. /// /// /// diff --git a/src/Telegrator/Handlers/Building/AwaiterHandlerDeleter.cs b/src/Telegrator/Handlers/Building/AwaiterHandlerDeleter.cs new file mode 100644 index 0000000..cc5696f --- /dev/null +++ b/src/Telegrator/Handlers/Building/AwaiterHandlerDeleter.cs @@ -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(UpdateType updateType, Update handlingUpdate, IAwaitingProvider handlerProvider) : IAwaiterHandlerBuilder where TUpdate : class +{ + public Task 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(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(null!); + } + + public void AddFilter(IFilter filter) => throw new NotImplementedException(); + public void AddFilters(params IFilter[] filters) => throw new NotImplementedException(); + public void AddTargetedFilter(Func getFilterringTarget, IFilter filter) where TFilterTarget : class => throw new NotImplementedException(); + public void AddTargetedFilters(Func getFilterringTarget, params IFilter[] 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(TValue? state) where TKey : IStateKeyResolver, new() where TValue : IEquatable => throw new NotImplementedException(); + public void SetUpdateValidating(UpdateValidateAction validateAction) => throw new NotImplementedException(); +} diff --git a/src/Telegrator/Providers/AwaitingProvider.cs b/src/Telegrator/Providers/AwaitingProvider.cs index 0697abf..47a92a0 100644 --- a/src/Telegrator/Providers/AwaitingProvider.cs +++ b/src/Telegrator/Providers/AwaitingProvider.cs @@ -19,6 +19,12 @@ public class AwaitingProvider(TelegratorOptions options) : HandlersProvider([], /// public override bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list) { + if (HandlersList.HandlingType != updateType) + { + list = null; + return false; + } + list = HandlersList; return true; } diff --git a/src/Telegrator/Providers/HandlersCollection.cs b/src/Telegrator/Providers/HandlersCollection.cs index 65e2274..d25f110 100644 --- a/src/Telegrator/Providers/HandlersCollection.cs +++ b/src/Telegrator/Providers/HandlersCollection.cs @@ -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); } + + /// + public bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList list) + { + return InnerDictionary.TryGetValue(updateType, out list); + } } diff --git a/src/Telegrator/Providers/HandlersManagerBase.cs b/src/Telegrator/Providers/HandlersManagerBase.cs index f73e700..0aed53b 100644 --- a/src/Telegrator/Providers/HandlersManagerBase.cs +++ b/src/Telegrator/Providers/HandlersManagerBase.cs @@ -95,7 +95,7 @@ public abstract class HandlersManagerBase(TelegratorOptions options) : IHandlers /// Thrown when intersecting command aliases are found and ExceptIntersectingCommandAliases is enabled. protected virtual void IntersectCommands(HandlerDescriptor descriptor) { - if (Options == null || !Options.ExceptIntersectingCommandAliases) + if (Options?.ExceptIntersectingCommandAliases is not true) return; CommandAlliasAttribute? alliasAttribute = descriptor.HandlerType.GetCustomAttribute(); diff --git a/src/Telegrator/SimpleTypesExtensions.cs b/src/Telegrator/SimpleTypesExtensions.cs index 4e45485..98aeea9 100644 --- a/src/Telegrator/SimpleTypesExtensions.cs +++ b/src/Telegrator/SimpleTypesExtensions.cs @@ -136,7 +136,7 @@ public static partial class ColletionsExtensions /// /// /// - public static T? SingleSafe(this IEnumerable source) + public static T? SingleOrNothing(this IEnumerable source) => source.Count() == 1 ? source.ElementAt(0) : default; /// @@ -147,7 +147,7 @@ public static partial class ColletionsExtensions /// /// /// - public static T? SingleSafe(this IEnumerable source, Func predicate) + public static T? SingleOrNothing(this IEnumerable source, Func predicate) { source = source.Where(predicate); return source.Count() == 1 ? source.ElementAt(0) : default; @@ -205,7 +205,7 @@ public static partial class ReflectionExtensions /// /// 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"); /// /// Determines whether an instance of a specified type can be assigned to an instance of the current type diff --git a/src/Telegrator/TypesExtensions.cs b/src/Telegrator/TypesExtensions.cs index 59f4300..b6e2904 100644 --- a/src/Telegrator/TypesExtensions.cs +++ b/src/Telegrator/TypesExtensions.cs @@ -231,6 +231,17 @@ public static class AwaitingProviderExtensions public static IAwaiterHandlerBuilder CreateAbstract(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class => new AwaiterHandlerBuilder(updateType, handlingUpdate, awaitingProvider); + /// + /// Creates an awaiter handler builder for a specific update type that will delete the awaiting handler after awaiting is completed. + /// + /// + /// + /// + /// + /// + public static IAwaiterHandlerBuilder CreateDeleting(this IAwaitingProvider awaitingProvider, UpdateType updateType, Update handlingUpdate) where TUpdate : class + => new AwaiterHandlerDeleter(updateType, handlingUpdate, awaitingProvider); + /// /// Creates an awaiter builder for any update type. /// @@ -257,6 +268,15 @@ public static class AwaitingProviderExtensions /// An awaiter builder for callback query updates. public static IAwaiterHandlerBuilder AwaitCallbackQuery(this IAwaitingProvider awaitingProvider, Update handlingUpdate) => awaitingProvider.CreateAbstract(UpdateType.CallbackQuery, handlingUpdate); + + /// + /// Deletes all awaiting handlers for callback query updates. + /// + /// + /// + /// + public static IAwaiterHandlerBuilder CancellAllCallbacks(this IAwaitingProvider awaitingProvider, Update handlingUpdate) + => awaitingProvider.CreateDeleting(UpdateType.CallbackQuery, handlingUpdate); } /// @@ -418,8 +438,7 @@ public static partial class HandlersCollectionExtensions { (collectingTarget ?? Assembly.GetCallingAssembly()) .GetExportedTypes() - .Where(type => type.GetCustomAttribute() == null) - .Where(type => type.IsHandlerRealization()) + .Where(type => type.GetCustomAttribute() == null && type.IsHandlerRealization()) .ForEach(type => handlers.AddHandler(type)); return handlers;