* Code cleanup
* Moved handler execution logic from "DescribedHandlerInfo" to "UpdateHandlerBase" * Added logging message on handler described
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Filters.Components;
|
||||
using Telegrator.Handlers.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
|
||||
@@ -12,51 +11,26 @@ namespace Telegrator.Handlers
|
||||
/// Provides access to the update, client, filters, and other execution context.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update being handled.</typeparam>
|
||||
public class AbstractHandlerContainer<TUpdate> : IAbstractHandlerContainer<TUpdate> where TUpdate : class
|
||||
public class AbstractHandlerContainer<TUpdate>(DescribedHandlerInfo handlerInfo) : IAbstractHandlerContainer<TUpdate> where TUpdate : class
|
||||
{
|
||||
private readonly TUpdate _actualUpdate;
|
||||
private readonly Update _handlingUpdate;
|
||||
private readonly ITelegramBotClient _client;
|
||||
private readonly Dictionary<string, object> _extraData;
|
||||
private readonly CompletedFiltersList _completedFilters;
|
||||
private readonly IAwaitingProvider _awaitingProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the actual update object of type TUpdate.
|
||||
/// </summary>
|
||||
public TUpdate ActualUpdate => _actualUpdate;
|
||||
public TUpdate ActualUpdate { get; } = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Update HandlingUpdate => _handlingUpdate;
|
||||
public Update HandlingUpdate { get; } = handlerInfo.HandlingUpdate;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ITelegramBotClient Client => _client;
|
||||
public ITelegramBotClient Client { get; } = handlerInfo.Client;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Dictionary<string, object> ExtraData => _extraData;
|
||||
public Dictionary<string, object> ExtraData { get; } = handlerInfo.ExtraData;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public CompletedFiltersList CompletedFilters => _completedFilters;
|
||||
|
||||
/// <inheritdoc cref="IHandlerContainer.AwaitingProvider"/>
|
||||
public IAwaitingProvider AwaitingProvider => _awaitingProvider;
|
||||
public CompletedFiltersList CompletedFilters { get; } = handlerInfo.CompletedFilters;
|
||||
|
||||
/// <inheritdoc/>
|
||||
IAwaitingProvider IHandlerContainer.AwaitingProvider => AwaitingProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractHandlerContainer{TUpdate}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="awaitingProvider">The awaiting provider for managing async operations.</param>
|
||||
/// <param name="handlerInfo">The handler information containing execution context.</param>
|
||||
public AbstractHandlerContainer(IAwaitingProvider awaitingProvider, DescribedHandlerInfo handlerInfo)
|
||||
{
|
||||
_actualUpdate = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>();
|
||||
_handlingUpdate = handlerInfo.HandlingUpdate;
|
||||
_client = handlerInfo.Client;
|
||||
_extraData = handlerInfo.ExtraData;
|
||||
_completedFilters = handlerInfo.CompletedFilters;
|
||||
_awaitingProvider = awaitingProvider;
|
||||
}
|
||||
public IAwaitingProvider AwaitingProvider { get; } = handlerInfo.AwaitingProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,9 @@ namespace Telegrator.Handlers.Building
|
||||
/// <summary>
|
||||
/// Creates a handler container for this awaiter handler.
|
||||
/// </summary>
|
||||
/// <param name="_">The awaiting provider (unused).</param>
|
||||
/// <param name="describedHandler">The handler information containing the update.</param>
|
||||
/// <returns>An empty handler container.</returns>
|
||||
public IHandlerContainer CreateContainer(IAwaitingProvider _, DescribedHandlerInfo describedHandler)
|
||||
public IHandlerContainer CreateContainer(DescribedHandlerInfo describedHandler)
|
||||
{
|
||||
HandlingUpdate = describedHandler.HandlingUpdate;
|
||||
return new EmptyHandlerContainer();
|
||||
|
||||
@@ -60,12 +60,11 @@ namespace Telegrator.Handlers.Components
|
||||
/// <summary>
|
||||
/// Creates a handler container for the specified awaiting provider and handler info.
|
||||
/// </summary>
|
||||
/// <param name="awaitingProvider">The awaiting provider.</param>
|
||||
/// <param name="handlerInfo">The handler descriptor info.</param>
|
||||
/// <returns>The created handler container.</returns>
|
||||
public virtual IHandlerContainer CreateContainer(IAwaitingProvider awaitingProvider, DescribedHandlerInfo handlerInfo)
|
||||
public virtual IHandlerContainer CreateContainer(DescribedHandlerInfo handlerInfo)
|
||||
{
|
||||
return new AbstractHandlerContainer<TUpdate>(awaitingProvider, handlerInfo);
|
||||
return new AbstractHandlerContainer<TUpdate>(handlerInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -103,13 +103,12 @@ namespace Telegrator.Handlers.Components
|
||||
/// <summary>
|
||||
/// Creates a handler container for this branching handler.
|
||||
/// </summary>
|
||||
/// <param name="awaitingProvider">The awaiting provider for the container.</param>
|
||||
/// <param name="handlerInfo">The handler information.</param>
|
||||
/// <returns>A handler container for this branching handler.</returns>
|
||||
/// <exception cref="Exception">Thrown when the awaiting provider is not of the expected type.</exception>
|
||||
public override IHandlerContainer CreateContainer(IAwaitingProvider awaitingProvider, DescribedHandlerInfo handlerInfo)
|
||||
public override IHandlerContainer CreateContainer(DescribedHandlerInfo handlerInfo)
|
||||
{
|
||||
return new AbstractHandlerContainer<TUpdate>(awaitingProvider, handlerInfo);
|
||||
return new AbstractHandlerContainer<TUpdate>(handlerInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -12,9 +12,8 @@ namespace Telegrator.Handlers.Components
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IHandlerContainer"/> for the specified awaiting provider and handler info.
|
||||
/// </summary>
|
||||
/// <param name="awaitingProvider">The <see cref="IAwaitingProvider"/> to use.</param>
|
||||
/// <param name="handlerInfo">The <see cref="DescribedHandlerInfo"/> for the handler.</param>
|
||||
/// <returns>A new <see cref="IHandlerContainer"/> instance.</returns>
|
||||
public IHandlerContainer CreateContainer(IAwaitingProvider awaitingProvider, DescribedHandlerInfo handlerInfo);
|
||||
public IHandlerContainer CreateContainer(DescribedHandlerInfo handlerInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
using Telegrator.Polling;
|
||||
|
||||
namespace Telegrator.Handlers.Components
|
||||
{
|
||||
@@ -21,14 +26,57 @@ namespace Telegrator.Handlers.Components
|
||||
/// <summary>
|
||||
/// Executes the handler logic and marks the lifetime as ended after execution.
|
||||
/// </summary>
|
||||
/// <param name="container">The <see cref="IHandlerContainer"/> for the update.</param>
|
||||
/// <param name="described"></param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
public async Task<Result> Execute(IHandlerContainer container, CancellationToken cancellationToken = default)
|
||||
public async Task<Result> Execute(DescribedHandlerInfo described, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (LifetimeToken.IsEnded)
|
||||
throw new Exception();
|
||||
|
||||
try
|
||||
{
|
||||
return await ExecuteInternal(container, cancellationToken);
|
||||
// Creating container
|
||||
IHandlerContainer container = GetContainer(described);
|
||||
DescriptorAspectsSet? aspects = described.From.Aspects;
|
||||
|
||||
// Executing pre processor
|
||||
if (aspects != null)
|
||||
{
|
||||
Result? preResult = await aspects.ExecutePre(this, container, cancellationToken).ConfigureAwait(false);
|
||||
if (!preResult.Positive)
|
||||
return preResult;
|
||||
}
|
||||
|
||||
// Executing handler
|
||||
Result execResult = await ExecuteInternal(container, cancellationToken).ConfigureAwait(false);
|
||||
if (!execResult.Positive)
|
||||
return execResult;
|
||||
|
||||
// Executing post processor
|
||||
if (aspects != null)
|
||||
{
|
||||
Result postResult = await aspects.ExecutePost(this, container, cancellationToken).ConfigureAwait(false);
|
||||
if (!postResult.Positive)
|
||||
return postResult;
|
||||
}
|
||||
|
||||
// Success
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Cancelled
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
await described.UpdateRouter
|
||||
.HandleErrorAsync(described.Client, exception, HandleErrorSource.HandleUpdateError, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return Result.Fault();
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -36,6 +84,17 @@ namespace Telegrator.Handlers.Components
|
||||
}
|
||||
}
|
||||
|
||||
private IHandlerContainer GetContainer(DescribedHandlerInfo handlerInfo)
|
||||
{
|
||||
if (this is IHandlerContainerFactory handlerDefainedContainerFactory)
|
||||
return handlerDefainedContainerFactory.CreateContainer(handlerInfo);
|
||||
|
||||
if (handlerInfo.UpdateRouter.DefaultContainerFactory is not null)
|
||||
return handlerInfo.UpdateRouter.DefaultContainerFactory.CreateContainer(handlerInfo);
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the handler logic for the given container and cancellation token.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user