Skip to content

Commit 09aa73c

Browse files
author
Igor Evdokimov
committed
- chaining
1 parent a50d699 commit 09aa73c

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Botticelli.Framework/Commands/Processors/WaitForClientResponseCommandChainProcessor.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Concurrent;
12
using Botticelli.Client.Analytics;
23
using Botticelli.Framework.Commands.Validators;
34
using Botticelli.Interfaces;
@@ -7,18 +8,32 @@
78

89
namespace Botticelli.Framework.Commands.Processors;
910

11+
public static class ChainStateKeeper
12+
{
13+
private static readonly ConcurrentDictionary<string, bool> IsChainOpened = new();
14+
15+
public static bool GetState(string chatId) => IsChainOpened.TryGetValue(chatId, out var isChainOpened) && isChainOpened;
16+
17+
public static void SetState(string chatId, bool isChainOpened) => IsChainOpened[chatId] = isChainOpened;
18+
19+
public static void SetState(IEnumerable<string> chatIds, bool isChainOpened)
20+
{
21+
foreach (var chatId in chatIds) SetState(chatId, isChainOpened);
22+
}
23+
}
24+
1025
/// <summary>
1126
/// A command with waiting for a response
1227
/// </summary>
1328
/// <typeparam name="TInputCommand"></typeparam>
1429
public abstract class WaitForClientResponseCommandChainProcessor<TInputCommand> : CommandProcessor<TInputCommand>,
15-
ICommandChainProcessor<TInputCommand>
16-
where TInputCommand : class, ICommand
30+
ICommandChainProcessor<TInputCommand>
31+
where TInputCommand : class, ICommand
1732
{
1833
protected WaitForClientResponseCommandChainProcessor(ILogger<CommandChainProcessor<TInputCommand>> logger,
19-
ICommandValidator<TInputCommand> commandValidator,
20-
MetricsProcessor metricsProcessor,
21-
IValidator<Message> messageValidator)
34+
ICommandValidator<TInputCommand> commandValidator,
35+
MetricsProcessor metricsProcessor,
36+
IValidator<Message> messageValidator)
2237
: base(logger, commandValidator, metricsProcessor, messageValidator)
2338
{
2439
}
@@ -32,31 +47,41 @@ public override async Task ProcessAsync(Message message, CancellationToken token
3247
// filters 'not our' chains
3348
if (message.ChainId != null && !ChainIds.Contains(message.ChainId.Value))
3449
return;
35-
50+
51+
message.ChainId ??= Guid.NewGuid();
3652
Classify(ref message);
37-
message.ChainId = Guid.NewGuid();
3853
ChainIds.Add(message.ChainId.Value);
3954

4055
if (message.Type != Message.MessageType.Messaging)
4156
{
57+
// sets input state to true
58+
ChainStateKeeper.SetState(message.ChatIds.Single(), true);
4259
await base.ProcessAsync(message, token);
43-
60+
4461
return;
4562
}
4663

4764
if (DateTime.UtcNow - message.LastModifiedAt > Timeout)
4865
return;
4966

67+
var chatId = message.ChatIds.Single();
68+
69+
// checks if input state = true
70+
if (!ChainStateKeeper.GetState(chatId))
71+
return;
72+
5073
message.ProcessingArgs ??= new List<string>();
51-
message.ProcessingArgs.Add(message.Body);
74+
message.ProcessingArgs.Add(message.Body!);
5275

5376
if (Next != null)
5477
{
5578
Next.ChainIds.Add(message.ChainId.Value);
5679
await Next.ProcessAsync(message, token);
5780
}
5881
else
82+
{
5983
Logger.LogInformation("No Next command for message {uid}", message.Uid);
84+
}
6085
}
6186

6287
public HashSet<Guid> ChainIds { get; } = new(1000);

Botticelli.Shared/ValueObjects/Message.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Message(string uid)
3939
/// <summary>
4040
/// Message type
4141
/// </summary>
42-
public virtual MessageType Type { get; set; } = MessageType.Messaging;
42+
public virtual MessageType Type { get; set; }
4343

4444
/// <summary>
4545
/// Message uid

0 commit comments

Comments
 (0)