Skip to content

Commit 934aa24

Browse files
authored
Merge pull request #942 from iceljc/master
refine conv tags
2 parents 2a54764 + eec704c commit 934aa24

File tree

7 files changed

+22
-10
lines changed

7 files changed

+22
-10
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IConversationService
1313
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
1414
Task<Conversation> UpdateConversationTitle(string id, string title);
1515
Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias);
16-
Task<bool> UpdateConversationTags(string conversationId, List<string> tags);
16+
Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags);
1717
Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
1818
Task<List<Conversation>> GetLastConversations();
1919
Task<List<string>> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable<string> excludeAgentIds);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void UpdateConversationTitle(string conversationId, string title)
132132
=> throw new NotImplementedException();
133133
void UpdateConversationTitleAlias(string conversationId, string titleAlias)
134134
=> throw new NotImplementedException();
135-
bool UpdateConversationTags(string conversationId, List<string> tags)
135+
bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
136136
=> throw new NotImplementedException();
137137
bool AppendConversationTags(string conversationId, List<string> tags)
138138
=> throw new NotImplementedException();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public async Task<Conversation> UpdateConversationTitleAlias(string id, string t
5959
return conversation;
6060
}
6161

62-
public async Task<bool> UpdateConversationTags(string conversationId, List<string> tags)
62+
public async Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
6363
{
6464
var db = _services.GetRequiredService<IBotSharpRepository>();
65-
return db.UpdateConversationTags(conversationId, tags);
65+
return db.UpdateConversationTags(conversationId, toAddTags, toDeleteTags);
6666
}
6767

6868
public async Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia
158158
}
159159
}
160160

161-
public bool UpdateConversationTags(string conversationId, List<string> tags)
161+
public bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
162162
{
163163
if (string.IsNullOrEmpty(conversationId)) return false;
164164

@@ -170,7 +170,11 @@ public bool UpdateConversationTags(string conversationId, List<string> tags)
170170

171171
var json = File.ReadAllText(convFile);
172172
var conv = JsonSerializer.Deserialize<Conversation>(json, _options);
173-
conv.Tags = tags ?? new();
173+
174+
var tags = conv.Tags ?? [];
175+
tags = tags.Concat(toAddTags).Distinct().ToList();
176+
conv.Tags = tags.Where(x => !toDeleteTags.Contains(x, StringComparer.OrdinalIgnoreCase)).ToList();
177+
174178
conv.UpdatedTime = DateTime.UtcNow;
175179
File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options));
176180
return true;

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public async Task<bool> UpdateConversationTitleAlias([FromRoute] string conversa
255255
public async Task<bool> UpdateConversationTags([FromRoute] string conversationId, [FromBody] UpdateConversationRequest request)
256256
{
257257
var conv = _services.GetRequiredService<IConversationService>();
258-
return await conv.UpdateConversationTags(conversationId, request.Tags);
258+
return await conv.UpdateConversationTags(conversationId, request.ToAddTags, request.ToDeleteTags);
259259
}
260260

261261
[HttpPut("/conversation/{conversationId}/update-message")]

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/Request/UpdateConversationRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ namespace BotSharp.OpenAPI.ViewModels.Conversations;
22

33
public class UpdateConversationRequest
44
{
5-
public List<string> Tags { get; set; } = [];
5+
public List<string> ToAddTags { get; set; } = [];
6+
public List<string> ToDeleteTags { get; set; } = [];
67
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,20 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia
134134
_dc.Conversations.UpdateOne(filterConv, updateConv);
135135
}
136136

137-
public bool UpdateConversationTags(string conversationId, List<string> tags)
137+
public bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
138138
{
139139
if (string.IsNullOrEmpty(conversationId)) return false;
140140

141141
var filter = Builders<ConversationDocument>.Filter.Eq(x => x.Id, conversationId);
142+
var conv = _dc.Conversations.Find(filter).FirstOrDefault();
143+
if (conv == null) return false;
144+
145+
var tags = conv.Tags ?? [];
146+
tags = tags.Concat(toAddTags).Distinct().ToList();
147+
tags = tags.Where(x => !toDeleteTags.Contains(x, StringComparer.OrdinalIgnoreCase)).ToList();
148+
142149
var update = Builders<ConversationDocument>.Update
143-
.Set(x => x.Tags, tags ?? new())
150+
.Set(x => x.Tags, tags)
144151
.Set(x => x.UpdatedTime, DateTime.UtcNow);
145152

146153
var res = _dc.Conversations.UpdateOne(filter, update);

0 commit comments

Comments
 (0)