Skip to content

Commit eb4f9e4

Browse files
author
Igor Evdokimov
committed
Merge branch 'dev/0.6' into prerelease/0.6
2 parents 099e52b + fa7f9f5 commit eb4f9e4

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace Botticelli.Framework.Commands.Processors;
4+
5+
public static class ChainStateKeeper
6+
{
7+
private static readonly ConcurrentDictionary<string, bool> IsChainOpened = new();
8+
9+
public static bool GetState(string chatId) => IsChainOpened.TryGetValue(chatId, out var isChainOpened) && isChainOpened;
10+
11+
public static void SetState(string chatId, bool isChainOpened) => IsChainOpened[chatId] = isChainOpened;
12+
13+
public static void SetState(IEnumerable<string> chatIds, bool isChainOpened)
14+
{
15+
foreach (var chatId in chatIds) SetState(chatId, isChainOpened);
16+
}
17+
}

Botticelli.Framework/Commands/Processors/WaitForClientResponseCommandChainProcessor.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace Botticelli.Framework.Commands.Processors;
1212
/// </summary>
1313
/// <typeparam name="TInputCommand"></typeparam>
1414
public abstract class WaitForClientResponseCommandChainProcessor<TInputCommand> : CommandProcessor<TInputCommand>,
15-
ICommandChainProcessor<TInputCommand>
16-
where TInputCommand : class, ICommand
15+
ICommandChainProcessor<TInputCommand>
16+
where TInputCommand : class, ICommand
1717
{
1818
protected WaitForClientResponseCommandChainProcessor(ILogger<CommandChainProcessor<TInputCommand>> logger,
19-
ICommandValidator<TInputCommand> commandValidator,
20-
MetricsProcessor metricsProcessor,
21-
IValidator<Message> messageValidator)
19+
ICommandValidator<TInputCommand> commandValidator,
20+
MetricsProcessor metricsProcessor,
21+
IValidator<Message> messageValidator)
2222
: base(logger, commandValidator, metricsProcessor, messageValidator)
2323
{
2424
}
@@ -32,31 +32,41 @@ public override async Task ProcessAsync(Message message, CancellationToken token
3232
// filters 'not our' chains
3333
if (message.ChainId != null && !ChainIds.Contains(message.ChainId.Value))
3434
return;
35-
35+
36+
message.ChainId ??= Guid.NewGuid();
3637
Classify(ref message);
37-
message.ChainId = Guid.NewGuid();
3838
ChainIds.Add(message.ChainId.Value);
3939

4040
if (message.Type != Message.MessageType.Messaging)
4141
{
42+
// sets input state to true
43+
ChainStateKeeper.SetState(message.ChatIds.Single(), true);
4244
await base.ProcessAsync(message, token);
43-
45+
4446
return;
4547
}
4648

4749
if (DateTime.UtcNow - message.LastModifiedAt > Timeout)
4850
return;
4951

52+
var chatId = message.ChatIds.Single();
53+
54+
// checks if input state = true
55+
if (!ChainStateKeeper.GetState(chatId))
56+
return;
57+
5058
message.ProcessingArgs ??= new List<string>();
51-
message.ProcessingArgs.Add(message.Body);
59+
message.ProcessingArgs.Add(message.Body!);
5260

5361
if (Next != null)
5462
{
5563
Next.ChainIds.Add(message.ChainId.Value);
5664
await Next.ProcessAsync(message, token);
5765
}
5866
else
67+
{
5968
Logger.LogInformation("No Next command for message {uid}", message.Uid);
69+
}
6070
}
6171

6272
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)