Skip to content

Commit 08acf16

Browse files
authored
Merge pull request #400 from iceljc/features/clean-conv-code
clean code
2 parents 3e3760f + 4723c64 commit 08acf16

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
5858

5959
if (ContainsState(name) && _curStates.TryGetValue(name, out var pair))
6060
{
61-
var lastNode = pair?.Values?.LastOrDefault();
62-
preActiveRounds = lastNode?.ActiveRounds;
63-
preValue = lastNode?.Data ?? string.Empty;
61+
var leafNode = pair?.Values?.LastOrDefault();
62+
preActiveRounds = leafNode?.ActiveRounds;
63+
preValue = leafNode?.Data ?? string.Empty;
6464
}
6565

6666
_logger.LogInformation($"[STATE] {name} = {value}");
@@ -139,7 +139,7 @@ public Dictionary<string, string> Load(string conversationId)
139139
{
140140
var key = state.Key;
141141
var value = state.Value;
142-
var leafNode = value.Values.LastOrDefault();
142+
var leafNode = value?.Values?.LastOrDefault();
143143
if (leafNode == null) continue;
144144

145145
_curStates[key] = new StateKeyValue
@@ -261,7 +261,7 @@ public bool RemoveState(string name)
261261
AfterActiveRounds = leafNode.ActiveRounds,
262262
DataType = leafNode.DataType,
263263
Source = leafNode.Source,
264-
Readonly = _curStates[name].Readonly
264+
Readonly = value.Readonly
265265
}).Wait();
266266
}
267267

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Loggers.Models;
2-
using BotSharp.Abstraction.Repositories.Filters;
32
using BotSharp.Abstraction.Repositories.Models;
43
using System.Globalization;
54
using System.IO;
@@ -35,30 +34,13 @@ public void CreateNewConversation(Conversation conversation)
3534
var stateFile = Path.Combine(dir, STATE_FILE);
3635
if (!File.Exists(stateFile))
3736
{
38-
var states = conversation.States ?? new Dictionary<string, string>();
39-
var initialStates = states.Select(x => new StateKeyValue
40-
{
41-
Key = x.Key,
42-
Values = new List<StateValue>
43-
{
44-
new StateValue { Data = x.Value, UpdateTime = DateTime.UtcNow }
45-
}
46-
}).ToList();
47-
File.WriteAllText(stateFile, JsonSerializer.Serialize(initialStates, _options));
37+
File.WriteAllText(stateFile, JsonSerializer.Serialize(new List<StateKeyValue>(), _options));
4838
}
4939

5040
var breakpointFile = Path.Combine(dir, BREAKPOINT_FILE);
5141
if (!File.Exists(breakpointFile))
5242
{
53-
var initialBreakpoints = new List<ConversationBreakpoint>
54-
{
55-
new ConversationBreakpoint()
56-
{
57-
Breakpoint = utcNow.AddMilliseconds(-100),
58-
CreatedTime = DateTime.UtcNow
59-
}
60-
};
61-
File.WriteAllText(breakpointFile, JsonSerializer.Serialize(initialBreakpoints, _options));
43+
File.WriteAllText(breakpointFile, JsonSerializer.Serialize(new List<ConversationBreakpoint>(), _options));
6244
}
6345
}
6446

@@ -180,8 +162,8 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea
180162
{
181163
MessageId = breakpoint.MessageId,
182164
Breakpoint = breakpoint.Breakpoint,
183-
CreatedTime = DateTime.UtcNow,
184165
Reason = breakpoint.Reason,
166+
CreatedTime = DateTime.UtcNow,
185167
}
186168
};
187169

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStateDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace BotSharp.Plugin.MongoStorage.Collections;
55
public class ConversationStateDocument : MongoBase
66
{
77
public string ConversationId { get; set; }
8-
public List<StateMongoElement> States { get; set; }
9-
public List<BreakpointMongoElement> Breakpoints { get; set; }
8+
public List<StateMongoElement> States { get; set; } = new List<StateMongoElement>();
9+
public List<BreakpointMongoElement> Breakpoints { get; set; } = new List<BreakpointMongoElement>();
1010
}

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Repositories.Models;
44
using BotSharp.Plugin.MongoStorage.Collections;
55
using BotSharp.Plugin.MongoStorage.Models;
6-
using System.Text.RegularExpressions;
76

87
namespace BotSharp.Plugin.MongoStorage.Repository;
98

@@ -34,21 +33,11 @@ public void CreateNewConversation(Conversation conversation)
3433
Dialogs = new List<DialogMongoElement>()
3534
};
3635

37-
var states = conversation.States ?? new Dictionary<string, string>();
38-
var initialStates = states.Select(x => new StateMongoElement
39-
{
40-
Key = x.Key,
41-
Values = new List<StateValueMongoElement>
42-
{
43-
new StateValueMongoElement { Data = x.Value, UpdateTime = DateTime.UtcNow }
44-
}
45-
}).ToList();
46-
4736
var stateDoc = new ConversationStateDocument
4837
{
4938
Id = Guid.NewGuid().ToString(),
5039
ConversationId = convDoc.Id,
51-
States = initialStates,
40+
States = new List<StateMongoElement>(),
5241
Breakpoints = new List<BreakpointMongoElement>()
5342
};
5443

@@ -169,19 +158,20 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea
169158

170159
var filter = Builders<ConversationStateDocument>.Filter.Eq(x => x.ConversationId, conversationId);
171160
var state = _dc.ConversationStates.Find(filter).FirstOrDefault();
161+
var leafNode = state?.Breakpoints?.LastOrDefault();
172162

173-
if (state == null || state.Breakpoints.IsNullOrEmpty())
163+
if (leafNode == null)
174164
{
175165
return null;
176166
}
177167

178-
return state.Breakpoints.Select(x => new ConversationBreakpoint
168+
return new ConversationBreakpoint
179169
{
180-
Breakpoint = x.Breakpoint,
181-
CreatedTime = x.CreatedTime,
182-
MessageId = x.MessageId,
183-
Reason = x.Reason,
184-
}).LastOrDefault();
170+
Breakpoint = leafNode.Breakpoint,
171+
MessageId = leafNode.MessageId,
172+
Reason = leafNode.Reason,
173+
CreatedTime = leafNode.CreatedTime,
174+
};
185175
}
186176

187177
public ConversationState GetConversationStates(string conversationId)

0 commit comments

Comments
 (0)