diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index c475d6a5..cc19a3d4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Repositories.Filters; + namespace BotSharp.Abstraction.Conversations; public interface IConversationService @@ -7,7 +9,7 @@ public interface IConversationService Task NewConversation(Conversation conversation); void SetConversationId(string conversationId, List states); Task GetConversation(string id); - Task> GetConversations(); + Task> GetConversations(ConversationFilter filter); Task UpdateConversationTitle(string id, string title); Task> GetLastConversations(); Task DeleteConversation(string id); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs index 9550125e..de0ca68b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs @@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Repositories.Filters; public class ConversationFilter { + public Pagination Pager { get; set; } = new Pagination(); public string? AgentId { get; set; } public string? Status { get; set; } public string? Channel { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Using.cs b/src/Infrastructure/BotSharp.Abstraction/Using.cs index 39ececfe..639d6fcc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Using.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Using.cs @@ -6,6 +6,7 @@ global using System.Threading.Tasks; global using System.ComponentModel.DataAnnotations; global using System.Text.Json.Serialization; global using BotSharp.Abstraction.Agents.Models; +global using BotSharp.Abstraction.Utilities; global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Agents.Enums; global using BotSharp.Abstraction.Models; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/Pagination.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/Pagination.cs new file mode 100644 index 00000000..72e4e884 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/Pagination.cs @@ -0,0 +1,17 @@ +namespace BotSharp.Abstraction.Utilities; + +public class Pagination +{ + public int Page { get; set; } = 0; + /// + /// Use -1 for all records + /// + public int Size { get; set; } = 20; + public int Offset => (Page + 1) * Size; +} + +public class PagedItems +{ + public int Count { get; set; } + public IEnumerable Items { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 65436ccb..a9d8067c 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -54,16 +54,17 @@ public partial class ConversationService : IConversationService return conversation; } - public async Task> GetConversations() + public async Task> GetConversations(ConversationFilter filter) { var db = _services.GetRequiredService(); var user = db.GetUserById(_user.Id); - var filter = new ConversationFilter - { - UserId = user.Role == UserRole.CSR ? string.Empty : user?.Id - }; var conversations = db.GetConversations(filter); - return conversations.OrderByDescending(x => x.CreatedTime).ToList(); + var result = new PagedItems + { + Count = conversations.Count(), + Items = conversations.OrderByDescending(x => x.CreatedTime) + }; + return result; } public async Task> GetLastConversations() diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 99949ce2..eb84453c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,7 +1,3 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.ApiAdapters; -using BotSharp.OpenAPI.ViewModels.Agents; - namespace BotSharp.OpenAPI.Controllers; [Authorize] diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index dde37482..d5605b25 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,14 +1,3 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Conversations.Enums; -using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.Models; -using BotSharp.OpenAPI.ViewModels.Conversations; -using BotSharp.OpenAPI.ViewModels.Users; -using Microsoft.AspNetCore.Http; -using Microsoft.VisualBasic; -using System.Net.Http.Headers; - namespace BotSharp.OpenAPI.Controllers; [Authorize] @@ -41,13 +30,16 @@ public class ConversationController : ControllerBase, IApiAdapter return ConversationViewModel.FromSession(conv); } - [HttpGet("/conversations/{agentId}")] - public async Task> GetConversations() + [HttpGet("/conversations")] + public async Task> GetConversations([FromQuery] ConversationFilter filter) { var service = _services.GetRequiredService(); - var conversations = await service.GetConversations(); + var conversations = await service.GetConversations(filter); + var userService = _services.GetRequiredService(); - var list = conversations.Select(x => ConversationViewModel.FromSession(x)).ToList(); + var list = conversations.Items + .Select(x => ConversationViewModel.FromSession(x)) + .ToList(); foreach (var item in list) { @@ -55,7 +47,11 @@ public class ConversationController : ControllerBase, IApiAdapter item.User = UserViewModel.FromUser(user); } - return list; + return new PagedItems + { + Count = conversations.Count, + Items = list + }; } [HttpGet("/conversation/{conversationId}/dialogs")] diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/EvaluationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/EvaluationController.cs index e9e7fc1d..1bba62a3 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/EvaluationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/EvaluationController.cs @@ -1,5 +1,3 @@ -using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Evaluations; using BotSharp.Abstraction.Evaluations.Models; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs index d30a59eb..31b78e09 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs @@ -1,6 +1,4 @@ -using BotSharp.Abstraction.ApiAdapters; using BotSharp.Abstraction.Knowledges.Models; -using Microsoft.AspNetCore.Http; using BotSharp.Abstraction.Knowledges.Settings; namespace BotSharp.OpenAPI.Controllers; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index 1952c9c1..28ad91a6 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -1,7 +1,3 @@ -using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Users.Models; -using BotSharp.OpenAPI.ViewModels.Users; - namespace BotSharp.OpenAPI.Controllers; [Authorize] diff --git a/src/Infrastructure/BotSharp.OpenAPI/Using.cs b/src/Infrastructure/BotSharp.OpenAPI/Using.cs index 75f7274b..7a7936a8 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Using.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Using.cs @@ -4,6 +4,8 @@ global using System.Text; global using System.Threading.Tasks; global using System.Linq; global using System.Text.Json; +global using System.Net.Http.Headers; +global using Microsoft.AspNetCore.Http; global using Microsoft.AspNetCore.Authorization; global using Microsoft.AspNetCore.Mvc; global using Microsoft.Extensions.DependencyInjection; @@ -13,6 +15,16 @@ global using BotSharp.Abstraction.Agents; global using BotSharp.Abstraction.Conversations; global using BotSharp.Abstraction.Knowledges; global using BotSharp.Abstraction.Users; +global using BotSharp.Abstraction.Users.Models; global using BotSharp.Abstraction.Utilities; global using BotSharp.Abstraction.Agents.Settings; -global using BotSharp.Abstraction.Conversations.Settings; \ No newline at end of file +global using BotSharp.Abstraction.Conversations.Settings; +global using BotSharp.Abstraction.Agents.Enums; +global using BotSharp.Abstraction.ApiAdapters; +global using BotSharp.Abstraction.Conversations.Enums; +global using BotSharp.Abstraction.Conversations.Models; +global using BotSharp.Abstraction.Models; +global using BotSharp.Abstraction.Repositories.Filters; +global using BotSharp.OpenAPI.ViewModels.Conversations; +global using BotSharp.OpenAPI.ViewModels.Users; +global using BotSharp.OpenAPI.ViewModels.Agents; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs index 7b5fdd2e..d5912d14 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs @@ -1,7 +1,5 @@ -using BotSharp.Abstraction.Conversations.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SignalR; -using Microsoft.EntityFrameworkCore; namespace BotSharp.Plugin.ChatHub; @@ -31,14 +29,18 @@ public class SignalRHub : Hub var hooks = _services.GetServices(); var convService = _services.GetRequiredService(); _context.HttpContext.Request.Query.TryGetValue("conversationId", out var conversationId); - var conv = await convService.GetConversation(conversationId); - foreach (var hook in hooks) + if (!string.IsNullOrEmpty(conversationId)) { - // Check if user connected with agent is the first time. - if (!conv.Dialogs.Any()) + var conv = await convService.GetConversation(conversationId); + + foreach (var hook in hooks) { - await hook.OnUserAgentConnectedInitially(conv); + // Check if user connected with agent is the first time. + if (!conv.Dialogs.Any()) + { + await hook.OnUserAgentConnectedInitially(conv); + } } } diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs index b3bc1e1f..5013100e 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; +using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Users.Models; using BotSharp.Plugin.WeChat.Users; using Microsoft.AspNetCore.Http; @@ -43,9 +44,12 @@ namespace BotSharp.Plugin.WeChat var conversationService = scoped.GetRequiredService(); - var latestConversationId = (await conversationService.GetConversations()) - .OrderByDescending(_ => _.CreatedTime) - .FirstOrDefault()?.Id; + var latestConversationId = (await conversationService.GetConversations(new ConversationFilter + { + })).Items + .OrderByDescending(_ => _.CreatedTime) + .FirstOrDefault()?.Id; + conversationService.SetConversationId(latestConversationId, new List { "channel=wechat" diff --git a/src/web-live-chat/package.json b/src/web-live-chat/package.json index 4fb985e1..47c5cf1f 100644 --- a/src/web-live-chat/package.json +++ b/src/web-live-chat/package.json @@ -20,7 +20,7 @@ "prettier": "^3.0.0", "prettier-plugin-svelte": "^3.0.0", "sass": "^1.69.5", - "svelte": "^4.2.7", + "svelte": "^4.2.8", "svelte-check": "^3.6.0", "typescript": "^5.0.0", "vite": "^4.4.2"