Skip to content

Commit 096868b

Browse files
author
Jicheng Lu
committed
add conversation filter
1 parent d2cb9a5 commit 096868b

File tree

6 files changed

+51
-82
lines changed

6 files changed

+51
-82
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRou
3535
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
3636
void UpdateConversationStatus(string conversationId, string status);
3737
Conversation GetConversation(string conversationId);
38-
List<Conversation> GetConversations(string userId);
38+
List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null);
3939
void UpdateConversationTitle(string conversationId, string title);
4040
List<Conversation> GetLastConversations();
4141
void AddExectionLogs(string conversationId, List<string> logs);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public async Task<List<Conversation>> GetConversations()
5757
{
5858
var db = _services.GetRequiredService<IBotSharpRepository>();
5959
var user = db.GetUserById(_user.Id);
60-
var conversations = db.GetConversations(user.Role == UserRole.CSR ? null : user?.Id);
60+
var targetUserId = user.Role == UserRole.CSR ? null : user?.Id;
61+
var conversations = db.GetConversations(userId: targetUserId);
6162
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
6263
}
6364

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public Conversation GetConversation(string conversationId)
131131
throw new NotImplementedException();
132132
}
133133

134-
public List<Conversation> GetConversations(string userId)
134+
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
135135
{
136136
throw new NotImplementedException();
137137
}

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

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ private IQueryable<UserAgent> UserAgents
128128

129129
public void Add<TTableInterface>(object entity)
130130
{
131-
if (entity is Conversation conversation)
132-
{
133-
_conversations.Add(conversation);
134-
_changedTableNames.Add(nameof(Conversation));
135-
}
136-
else if (entity is Agent agent)
131+
if (entity is Agent agent)
137132
{
138133
_agents.Add(agent);
139134
_changedTableNames.Add(nameof(Agent));
@@ -159,20 +154,7 @@ public int Transaction<TTableInterface>(Action action)
159154
// Persist to disk
160155
foreach (var table in _changedTableNames)
161156
{
162-
if (table == nameof(Conversation))
163-
{
164-
foreach (var conversation in _conversations)
165-
{
166-
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversation.Id);
167-
if (!Directory.Exists(dir))
168-
{
169-
Directory.CreateDirectory(dir);
170-
}
171-
var path = Path.Combine(dir, "conversation.json");
172-
File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options));
173-
}
174-
}
175-
else if (table == nameof(Agent))
157+
if (table == nameof(Agent))
176158
{
177159
foreach (var agent in _agents)
178160
{
@@ -730,32 +712,29 @@ public void UpdateConversationStatus(string conversationId, string status)
730712
public Conversation GetConversation(string conversationId)
731713
{
732714
var convDir = FindConversationDirectory(conversationId);
733-
if (!string.IsNullOrEmpty(convDir))
734-
{
735-
var convFile = Path.Combine(convDir, "conversation.json");
736-
var content = File.ReadAllText(convFile);
737-
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
715+
if (!string.IsNullOrEmpty(convDir)) return null;
738716

739-
var dialogFile = Path.Combine(convDir, "dialogs.txt");
740-
if (record != null)
741-
{
742-
record.Dialogs = CollectDialogElements(dialogFile);
743-
}
717+
var convFile = Path.Combine(convDir, "conversation.json");
718+
var content = File.ReadAllText(convFile);
719+
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
744720

745-
var stateFile = Path.Combine(convDir, "state.dict");
746-
if (record != null)
747-
{
748-
var states = CollectConversationStates(stateFile);
749-
record.States = new ConversationState(states);
750-
}
721+
var dialogFile = Path.Combine(convDir, "dialogs.txt");
722+
if (record != null)
723+
{
724+
record.Dialogs = CollectDialogElements(dialogFile);
725+
}
751726

752-
return record;
727+
var stateFile = Path.Combine(convDir, "state.dict");
728+
if (record != null)
729+
{
730+
var states = CollectConversationStates(stateFile);
731+
record.States = new ConversationState(states);
753732
}
754733

755-
return null;
734+
return record;
756735
}
757736

758-
public List<Conversation> GetConversations(string userId)
737+
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
759738
{
760739
var records = new List<Conversation>();
761740
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
@@ -767,10 +746,16 @@ public List<Conversation> GetConversations(string userId)
767746

768747
var json = File.ReadAllText(path);
769748
var record = JsonSerializer.Deserialize<Conversation>(json, _options);
770-
if (record != null && (record.UserId == userId || userId == null))
771-
{
772-
records.Add(record);
773-
}
749+
if (record == null) continue;
750+
751+
var matched = true;
752+
if (!string.IsNullOrEmpty(agentId)) matched = matched && record.AgentId == agentId;
753+
if (!string.IsNullOrEmpty(status)) matched = matched && record.Status == status;
754+
if (!string.IsNullOrEmpty(channel)) matched = matched && record.Channel == channel;
755+
if (!string.IsNullOrEmpty(userId)) matched = matched && record.UserId == userId;
756+
757+
if (!matched) continue;
758+
records.Add(record);
774759
}
775760

776761
return records;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class ConversationCollection : MongoBase
77
public string AgentId { get; set; }
88
public string UserId { get; set; }
99
public string Title { get; set; }
10+
public string Channel { get; set; }
1011
public string Status { get; set; }
1112
public List<StateKeyValue> States { get; set; }
1213
public DateTime CreatedTime { get; set; }

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

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ public MongoRepository(MongoDbContext dc, IServiceProvider services)
3232

3333
public void Add<TTableInterface>(object entity)
3434
{
35-
if (entity is Conversation conversation)
36-
{
37-
_conversations.Add(conversation);
38-
_changedTableNames.Add(nameof(Conversation));
39-
}
40-
else if (entity is Agent agent)
35+
if (entity is Agent agent)
4136
{
4237
_agents.Add(agent);
4338
_changedTableNames.Add(nameof(Agent));
@@ -61,33 +56,7 @@ public int Transaction<TTableInterface>(Action action)
6156

6257
foreach (var table in _changedTableNames)
6358
{
64-
if (table == nameof(Conversation))
65-
{
66-
var conversations = _conversations.Select(x => new ConversationCollection
67-
{
68-
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
69-
AgentId = x.AgentId,
70-
UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty,
71-
Title = x.Title,
72-
States = x.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
73-
CreatedTime = x.CreatedTime,
74-
UpdatedTime = x.UpdatedTime
75-
}).ToList();
76-
77-
foreach (var conversation in conversations)
78-
{
79-
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
80-
var update = Builders<ConversationCollection>.Update
81-
.Set(x => x.AgentId, conversation.AgentId)
82-
.Set(x => x.UserId, conversation.UserId)
83-
.Set(x => x.Title, conversation.Title)
84-
.Set(x => x.States, conversation.States)
85-
.Set(x => x.CreatedTime, conversation.CreatedTime)
86-
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
87-
_dc.Conversations.UpdateOne(filter, update, _options);
88-
}
89-
}
90-
else if (table == nameof(Agent))
59+
if (table == nameof(Agent))
9160
{
9261
var agents = _agents.Select(x => new AgentCollection
9362
{
@@ -610,6 +579,7 @@ public void CreateNewConversation(Conversation conversation)
610579
AgentId = conversation.AgentId,
611580
UserId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty,
612581
Title = conversation.Title,
582+
Channel = conversation.Channel,
613583
Status = conversation.Status,
614584
States = conversation.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
615585
CreatedTime = DateTime.UtcNow,
@@ -670,6 +640,7 @@ public void AppendConversationDialogs(string conversationId, List<DialogElement>
670640
_dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog);
671641
_dc.Conversations.UpdateOne(filterConv, updateConv);
672642
}
643+
673644
public void UpdateConversationTitle(string conversationId, string title)
674645
{
675646
if (string.IsNullOrEmpty(conversationId)) return;
@@ -684,6 +655,7 @@ public void UpdateConversationTitle(string conversationId, string title)
684655

685656
_dc.Conversations.UpdateOne(filterConv, updateConv);
686657
}
658+
687659
public List<StateKeyValue> GetConversationStates(string conversationId)
688660
{
689661
var states = new List<StateKeyValue>();
@@ -745,6 +717,7 @@ public Conversation GetConversation(string conversationId)
745717
AgentId = conv.AgentId.ToString(),
746718
UserId = conv.UserId.ToString(),
747719
Title = conv.Title,
720+
Channel = conv.Channel,
748721
Status = conv.Status,
749722
Dialogs = dialogElements,
750723
States = new ConversationState(conv.States ?? new List<StateKeyValue>()),
@@ -753,13 +726,20 @@ public Conversation GetConversation(string conversationId)
753726
};
754727
}
755728

756-
public List<Conversation> GetConversations(string userId)
729+
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
757730
{
758731
var records = new List<Conversation>();
759732
if (string.IsNullOrEmpty(userId)) return records;
760733

761-
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, userId);
762-
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
734+
var builder = Builders<ConversationCollection>.Filter;
735+
var filters = new List<FilterDefinition<ConversationCollection>>();
736+
737+
if (!string.IsNullOrEmpty(agentId)) filters.Add(builder.Eq(x => x.AgentId, agentId));
738+
if (!string.IsNullOrEmpty(status)) filters.Add(builder.Eq(x => x.Status, status));
739+
if (!string.IsNullOrEmpty(channel)) filters.Add(builder.Eq(x => x.Channel, channel));
740+
if (!string.IsNullOrEmpty(userId)) filters.Add(builder.Eq(x => x.UserId, userId));
741+
742+
var conversations = _dc.Conversations.Find(builder.And(filters)).ToList();
763743

764744
foreach (var conv in conversations)
765745
{
@@ -770,6 +750,7 @@ public List<Conversation> GetConversations(string userId)
770750
AgentId = conv.AgentId.ToString(),
771751
UserId = conv.UserId.ToString(),
772752
Title = conv.Title,
753+
Channel = conv.Channel,
773754
Status = conv.Status,
774755
CreatedTime = conv.CreatedTime,
775756
UpdatedTime = conv.UpdatedTime
@@ -791,6 +772,7 @@ public List<Conversation> GetLastConversations()
791772
AgentId = c.AgentId.ToString(),
792773
UserId = c.UserId.ToString(),
793774
Title = c.Title,
775+
Channel = c.Channel,
794776
Status = c.Status,
795777
CreatedTime = c.CreatedTime,
796778
UpdatedTime = c.UpdatedTime

0 commit comments

Comments
 (0)