-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWaitForClientResponseCommandChainProcessor.cs
More file actions
75 lines (62 loc) · 2.62 KB
/
WaitForClientResponseCommandChainProcessor.cs
File metadata and controls
75 lines (62 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Botticelli.Client.Analytics;
using Botticelli.Framework.Commands.Validators;
using Botticelli.Shared.ValueObjects;
using FluentValidation;
using Microsoft.Extensions.Logging;
namespace Botticelli.Framework.Commands.Processors;
/// <summary>
/// A command with waiting for a response
/// </summary>
/// <typeparam name="TInputCommand"></typeparam>
public abstract class WaitForClientResponseCommandChainProcessor<TInputCommand> : CommandProcessor<TInputCommand>,
ICommandChainProcessor<TInputCommand>
where TInputCommand : class, ICommand
{
protected WaitForClientResponseCommandChainProcessor(ILogger<CommandChainProcessor<TInputCommand>> logger,
ICommandValidator<TInputCommand> commandValidator,
MetricsProcessor metricsProcessor,
IValidator<Message> messageValidator)
: base(logger,
commandValidator,
messageValidator,
metricsProcessor)
{
}
private TimeSpan Timeout { get; } = TimeSpan.FromMinutes(10);
public override async Task ProcessAsync(Message message, CancellationToken token)
{
// filters 'not our' chains
if (message.ChainId != null && !ChainIds.Contains(message.ChainId.Value)) return;
message.ChainId ??= Guid.NewGuid();
Classify(ref message);
ChainIds.Add(message.ChainId.Value);
if (message.Type != Message.MessageType.Messaging)
{
// sets input state to true
ChainStateKeeper.SetState(message.ChatIds.Single(), true);
await base.ProcessAsync(message, token);
return;
}
if (DateTime.UtcNow - message.LastModifiedAt > Timeout) return;
var chatId = message.ChatIds.Single();
// checks if input state = true
if (!ChainStateKeeper.GetState(chatId)) return;
message.ProcessingArgs ??= new List<string>();
message.ProcessingArgs.Add(message.Body!);
if (Next != null)
{
Next.ChainIds.Add(message.ChainId.Value);
if (_bot != null)
{
Next.SetBot(_bot);
await Next.ProcessAsync(message, token);
}
}
else
{
Logger.LogInformation("No Next command for message {uid}", message.Uid);
}
}
public HashSet<Guid> ChainIds { get; } = new(1000);
public ICommandChainProcessor Next { get; set; }
}