1+ using System . Collections . Concurrent ;
12using Botticelli . Client . Analytics ;
23using Botticelli . Framework . Commands . Validators ;
34using Botticelli . Interfaces ;
78
89namespace 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>
1429public 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 ) ;
0 commit comments