Skip to content

Commit 00f84e2

Browse files
author
Jicheng Lu
committed
Merge branch 'master' of https://github.com/SciSharp/BotSharp into features/add-message-queue-2
2 parents 717ec7a + d7eae94 commit 00f84e2

5 files changed

Lines changed: 34 additions & 43 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public interface IConversationService
1111
Task SetConversationId(string conversationId, List<MessageState> states, bool isReadOnly = false);
1212
Task<Conversation> GetConversation(string id, bool isLoadStates = false);
1313
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
14-
Task<Conversation> UpdateConversationTitle(string id, string title);
15-
Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias);
14+
Task<bool> UpdateConversationTitle(string id, string title);
15+
Task<bool> UpdateConversationTitleAlias(string id, string titleAlias);
1616
Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags);
1717
Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
1818
Task<List<Conversation>> GetLastConversations();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ response.RichContent is RichContent<IRichMessage> template &&
153153
if (response.Instruction != null)
154154
{
155155
var conversation = _services.GetRequiredService<IConversationService>();
156-
var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.NextActionReason);
156+
await conversation.UpdateConversationTitle(_conversationId, response.Instruction.NextActionReason);
157157

158158
// Emit conversation ending hook
159159
if (response.Instruction.ConversationEnd)

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,26 @@ public async Task<bool> DeleteConversations(IEnumerable<string> ids)
4343
return isDeleted;
4444
}
4545

46-
public async Task<Conversation> UpdateConversationTitle(string id, string title)
46+
public async Task<bool> UpdateConversationTitle(string id, string title)
4747
{
48-
var db = _services.GetRequiredService<IBotSharpRepository>();
49-
await db.UpdateConversationTitle(id, title);
50-
var conversation = await db.GetConversation(id);
51-
return conversation;
48+
if (!string.IsNullOrEmpty(title))
49+
{
50+
var db = _services.GetRequiredService<IBotSharpRepository>();
51+
await db.UpdateConversationTitle(id, title);
52+
}
53+
54+
return true;
5255
}
5356

54-
public async Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias)
57+
public async Task<bool> UpdateConversationTitleAlias(string id, string titleAlias)
5558
{
56-
var db = _services.GetRequiredService<IBotSharpRepository>();
57-
await db.UpdateConversationTitleAlias(id, titleAlias);
58-
var conversation = await db.GetConversation(id);
59-
return conversation;
59+
if (!string.IsNullOrEmpty(titleAlias))
60+
{
61+
var db = _services.GetRequiredService<IBotSharpRepository>();
62+
await db.UpdateConversationTitleAlias(id, titleAlias);
63+
}
64+
65+
return true;
6066
}
6167

6268
public async Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ public async Task<bool> UpdateConversationTitle([FromRoute] string conversationI
213213
return false;
214214
}
215215

216-
var response = await conv.UpdateConversationTitle(conversationId, newTile.NewTitle);
217-
return response != null;
216+
await conv.UpdateConversationTitle(conversationId, newTile.NewTitle);
217+
return true;
218218
}
219219

220220
[HttpPut("/conversation/{conversationId}/update-title-alias")]

src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,39 @@
11
using BotSharp.Abstraction.Repositories.Settings;
2+
using BotSharp.Abstraction.Utilities;
23
using System.Threading;
34

45
namespace BotSharp.Plugin.MongoStorage;
56

67
public class MongoDbContext
78
{
9+
private const string DefaultTablePrefix = "BotSharp";
810
private readonly MongoClient _mongoClient;
911
private readonly string _mongoDbDatabaseName;
1012
private readonly string _collectionPrefix;
1113
private static int _indexesInitialized = 0;
1214

13-
private const string DB_NAME_INDEX = "authSource";
14-
1515
public MongoDbContext(BotSharpDatabaseSettings dbSettings)
1616
{
17-
var mongoDbConnectionString = dbSettings.BotSharpMongoDb;
17+
var mongoDbConnectionString = dbSettings?.BotSharpMongoDb;
18+
if (string.IsNullOrWhiteSpace(mongoDbConnectionString))
19+
throw new InvalidOperationException("MongoDB is enabled but BotSharpMongoDb is not configured.");
20+
1821
_mongoClient = new MongoClient(mongoDbConnectionString);
1922
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
20-
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp")!;
23+
_collectionPrefix = dbSettings?.TablePrefix.IfNullOrEmptyAs(DefaultTablePrefix)!;
2124
CreateIndexes();
2225
}
2326

2427
private string GetDatabaseName(string mongoDbConnectionString)
2528
{
26-
var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
27-
28-
var symbol = "?";
29-
if (databaseName.Contains(symbol))
29+
var mongoUrl = new MongoUrl(mongoDbConnectionString);
30+
var databaseName = mongoUrl.DatabaseName.IfNullOrEmptyAs(mongoUrl.AuthenticationSource);
31+
if (string.IsNullOrWhiteSpace(databaseName))
3032
{
31-
var markIdx = databaseName.IndexOf(symbol, StringComparison.InvariantCultureIgnoreCase);
32-
var db = databaseName.Substring(0, markIdx);
33-
if (!string.IsNullOrWhiteSpace(db))
34-
{
35-
return db;
36-
}
37-
38-
var queryStr = databaseName.Substring(markIdx + 1);
39-
var queries = queryStr.Split("&", StringSplitOptions.RemoveEmptyEntries).Select(x => new
40-
{
41-
Key = x.Split("=")[0],
42-
Value = x.Split("=")[1]
43-
}).ToList();
44-
45-
var source = queries.FirstOrDefault(x => x.Key.IsEqualTo(DB_NAME_INDEX));
46-
if (source != null)
47-
{
48-
databaseName = source.Value;
49-
}
33+
throw new InvalidOperationException(
34+
"MongoDB connection string must specify a database: set the database in the path (e.g. mongodb://host/dbname) or set authSource in the query string (e.g. ?authSource=dbname). ");
5035
}
51-
return databaseName;
36+
return databaseName!;
5237
}
5338

5439
private IMongoDatabase Database => _mongoClient.GetDatabase(_mongoDbDatabaseName);

0 commit comments

Comments
 (0)