Skip to content

Commit 8c510d6

Browse files
authored
Merge pull request SciSharp#1258 from yileicn/master
optimize User,InstructionLog use async method
2 parents 8ed8525 + b874f99 commit 8c510d6

16 files changed

Lines changed: 228 additions & 225 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public interface IFileStorageService
5252

5353

5454
#region User
55-
string GetUserAvatar();
56-
bool SaveUserAvatar(FileDataModel file);
55+
Task<string> GetUserAvatar();
56+
Task<bool> SaveUserAvatar(FileDataModel file);
5757
#endregion
5858

5959
#region Speech

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ bool UpdateRole(Role role, bool updateRoleAgents = false)
3939
#endregion
4040

4141
#region User
42-
User? GetUserByEmail(string email) => throw new NotImplementedException();
43-
User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException();
44-
User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") => throw new NotImplementedException();
45-
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
46-
User? GetUserById(string id) => throw new NotImplementedException();
47-
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
48-
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
49-
User? GetUserByUserName(string userName) => throw new NotImplementedException();
50-
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
51-
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
52-
void CreateUser(User user) => throw new NotImplementedException();
53-
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
54-
void UpdateUserVerified(string userId) => throw new NotImplementedException();
55-
void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
56-
void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
57-
void UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException();
58-
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
59-
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
60-
void UpdateUserEmail(string userId, string email) => throw new NotImplementedException();
61-
void UpdateUserPhone(string userId, string Iphone, string regionCode) => throw new NotImplementedException();
62-
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
63-
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
42+
Task<User?> GetUserByEmail(string email) => throw new NotImplementedException();
43+
Task<User?> GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException();
44+
Task<User?> GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") => throw new NotImplementedException();
45+
Task<User?> GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
46+
Task<User?> GetUserById(string id) => throw new NotImplementedException();
47+
Task<List<User>> GetUserByIds(List<string> ids) => throw new NotImplementedException();
48+
Task<List<User>> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
49+
Task<User?> GetUserByUserName(string userName) => throw new NotImplementedException();
50+
Task UpdateUserName(string userId, string userName) => throw new NotImplementedException();
51+
Task<Dashboard?> GetDashboard(string id = null) => throw new NotImplementedException();
52+
Task CreateUser(User user) => throw new NotImplementedException();
53+
Task UpdateExistUser(string userId, User user) => throw new NotImplementedException();
54+
Task UpdateUserVerified(string userId) => throw new NotImplementedException();
55+
Task AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
56+
Task RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
57+
Task UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException();
58+
Task UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
59+
Task UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
60+
Task UpdateUserEmail(string userId, string email) => throw new NotImplementedException();
61+
Task UpdateUserPhone(string userId, string Iphone, string regionCode) => throw new NotImplementedException();
62+
Task UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
63+
Task UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
6464
ValueTask<PagedItems<User>> GetUsers(UserFilter filter) => throw new NotImplementedException();
65-
List<User> SearchLoginUsers(User filter, string source = UserSource.Internal) =>throw new NotImplementedException();
66-
User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
67-
bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();
65+
Task<List<User>> SearchLoginUsers(User filter, string source = UserSource.Internal) =>throw new NotImplementedException();
66+
Task<User?> GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
67+
Task<bool> UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();
6868
#endregion
6969

7070
#region Agent
@@ -193,13 +193,13 @@ DateTimePagination<ConversationStateLogModel> GetConversationStateLogs(string co
193193
#endregion
194194

195195
#region Instruction Log
196-
bool SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
196+
Task<bool> SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
197197
=> throw new NotImplementedException();
198198

199199
ValueTask<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
200200
=> throw new NotImplementedException();
201201

202-
List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
202+
Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
203203
=> throw new NotImplementedException();
204204

205205
Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates)

src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public class User
3232
public IEnumerable<UserAgentAction> AgentActions { get; set; } = [];
3333
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
3434
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
35+
36+
public Dashboard? Dashboard { get; set; }
3537
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<Agent> CreateAgent(Agent agent)
2626
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
2727
var agentSettings = _services.GetRequiredService<AgentSettings>();
2828

29-
var user = _db.GetUserById(userIdentity.Id);
29+
var user = await _db.GetUserById(userIdentity.Id);
3030
var userService = _services.GetRequiredService<IUserService>();
3131
var auth = await userService.GetUserAuthorizations();
3232

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public async Task<List<string>> GetIdleConversations(int batchSize, int messageL
103103
public async Task<Conversation> NewConversation(Conversation sess)
104104
{
105105
var db = _services.GetRequiredService<IBotSharpRepository>();
106-
var user = db.GetUserById(_user.Id);
106+
var user = await db.GetUserById(_user.Id);
107107
var foundUserId = user?.Id ?? string.Empty;
108108

109109
var record = sess;

src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.User.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ namespace BotSharp.Core.Files.Services;
44

55
public partial class LocalFileStorageService
66
{
7-
public string GetUserAvatar()
7+
public async Task<string> GetUserAvatar()
88
{
99
var db = _services.GetRequiredService<IBotSharpRepository>();
10-
var user = db.GetUserById(_user.Id);
10+
var user = await db.GetUserById(_user.Id);
1111
var dir = GetUserAvatarDir(user?.Id);
1212

1313
if (!ExistDirectory(dir))
@@ -19,7 +19,7 @@ public string GetUserAvatar()
1919
return found;
2020
}
2121

22-
public bool SaveUserAvatar(FileDataModel file)
22+
public async Task<bool> SaveUserAvatar(FileDataModel file)
2323
{
2424
if (file == null || string.IsNullOrEmpty(file.FileData))
2525
{
@@ -29,7 +29,7 @@ public bool SaveUserAvatar(FileDataModel file)
2929
try
3030
{
3131
var db = _services.GetRequiredService<IBotSharpRepository>();
32-
var user = db.GetUserById(_user.Id);
32+
var user = await db.GetUserById(_user.Id);
3333
var dir = GetUserAvatarDir(user?.Id);
3434

3535
if (string.IsNullOrEmpty(dir))

src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public async Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilte
7474

7575
var (isAdmin, user) = await userService.IsAdminUser(_user.Id);
7676
filter.UserIds = !isAdmin && user?.Id != null ? [user.Id] : null;
77-
keys = db.GetInstructionLogSearchKeys(filter);
77+
keys = await db.GetInstructionLogSearchKeys(filter);
7878
keys = filter.PreLoad ? keys : keys.Where(x => x.Contains(filter.Query ?? string.Empty, StringComparison.OrdinalIgnoreCase)).ToList();
7979
return keys.OrderBy(x => x).Take(filter.KeyLimit).ToList();
8080
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public DateTimePagination<ConversationStateLogModel> GetConversationStateLogs(st
179179
#endregion
180180

181181
#region Instruction Log
182-
public bool SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
182+
public async Task<bool> SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
183183
{
184184
if (logs.IsNullOrEmpty())
185185
{
@@ -199,7 +199,7 @@ public bool SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
199199
var innerLog = InstructionFileLogModel.From(log);
200200
innerLog.States = BuildLogStates(log.States);
201201
var text = JsonSerializer.Serialize(innerLog, _options);
202-
File.WriteAllText(file, text);
202+
await File.WriteAllTextAsync(file, text);
203203
}
204204
return true;
205205
}
@@ -259,7 +259,7 @@ public async Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesMod
259259
}
260260
}
261261

262-
File.WriteAllText(logFile, JsonSerializer.Serialize(log, _options));
262+
await File.WriteAllTextAsync(logFile, JsonSerializer.Serialize(log, _options));
263263
return true;
264264
}
265265

@@ -279,7 +279,7 @@ public async ValueTask<PagedItems<InstructionLogModel>> GetInstructionLogs(Instr
279279
var innerLogs = new List<InstructionFileLogModel>();
280280
foreach (var file in Directory.EnumerateFiles(baseDir))
281281
{
282-
var json = File.ReadAllText(file);
282+
var json = await File.ReadAllTextAsync(file);
283283
var log = JsonSerializer.Deserialize<InstructionFileLogModel>(json, _options);
284284
if (log == null)
285285
{
@@ -415,7 +415,7 @@ public async ValueTask<PagedItems<InstructionLogModel>> GetInstructionLogs(Instr
415415
};
416416
}
417417

418-
public List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
418+
public async Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
419419
{
420420
var keys = new List<string>();
421421
var baseDir = Path.Combine(_dbSettings.FileRepository, INSTRUCTION_LOG_FOLDER);
@@ -427,7 +427,7 @@ public List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
427427
var count = 0;
428428
foreach (var file in Directory.EnumerateFiles(baseDir))
429429
{
430-
var json = File.ReadAllText(file);
430+
var json = await File.ReadAllTextAsync(file);
431431
var log = JsonSerializer.Deserialize<InstructionFileLogModel>(json, _options);
432432
if (log == null)
433433
{

0 commit comments

Comments
 (0)