From f6013e482b420e48eb6c0f512cbce24225252523 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Fri, 8 Aug 2025 18:10:25 +0330 Subject: [PATCH] ... --- Hushian.Application/Services/ChatService.cs | 66 ++-- .../Controllers/v1/ChatController.cs | 6 +- Presentation/HushianWebApp/Pages/Chat.razor | 300 ++++++++++-------- .../Pages/FromUserSide/UserCP.razor | 16 +- .../HushianWebApp/Service/BaseController.cs | 2 + 5 files changed, 242 insertions(+), 148 deletions(-) diff --git a/Hushian.Application/Services/ChatService.cs b/Hushian.Application/Services/ChatService.cs index 9859071..dcc03df 100644 --- a/Hushian.Application/Services/ChatService.cs +++ b/Hushian.Application/Services/ChatService.cs @@ -4,6 +4,7 @@ using Hushian.Application.Contracts.Persistence; using Hushian.Application.Models; using Hushian.Domain.Entites; using Hushian.WebApi; +using Microsoft.AspNetCore.Routing.Constraints; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using System; @@ -77,7 +78,7 @@ namespace Hushian.Application.Services .Where(w => w.CompanyID == CompanyID && w.Status == status) .Select(s => new ChatItemDto() { - ID = s.ID, + ID = s.ID, ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last(l => l.ExperID.HasValue).ExperID, ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last(l => l.ExperID.HasValue).Exper.FullName, GroupID = s.GroupID, @@ -117,7 +118,7 @@ namespace Hushian.Application.Services .Include(inc => inc.Group) .Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper) .Where(w => w.Status == ConversationStatus.Recorded && w.CompanyID == CompanyID); - if (groupallow != null) request = request.Where(w=>!w.GroupID.HasValue || groupallow.Contains(w.GroupID.Value)); + if (groupallow != null) request = request.Where(w => !w.GroupID.HasValue || groupallow.Contains(w.GroupID.Value)); return await request.Select(s => new ChatItemDto() { ID = s.ID, @@ -237,7 +238,7 @@ namespace Hushian.Application.Services var convModel = await _ConversationRepository.Get() .Include(inc => inc.ConversationResponses) .FirstOrDefaultAsync(w => w.ID == dto.ConversationID); - + if (convModel != null) { if (ExperID.HasValue && !await _experService.CheckExperInCompany(convModel.CompanyID, ExperID.Value)) @@ -277,7 +278,24 @@ namespace Hushian.Application.Services await _ConversationRepository.UPDATE(convModel); } if (dto.Type != ConversationType.UE) - await WriteInHubFromCompany(Response.Value, convModel.UserID); + await WriteInHubFromCompany(Response.Value, convModel.UserID); + else + { + var modelA = convModel.ConversationResponses.OrderBy(o => o.ID) + .LastOrDefault(l => l.Type == ConversationType.EU || l.Type == ConversationType.CU); + if (modelA != null) + { + int userid = 0; + + if (modelA.Type == ConversationType.EU) userid = modelA.ExperID.Value; + + else if (modelA.Type == ConversationType.CU) userid = modelA.conversation.CompanyID; + + await WriteInHubFromUser(Response.Value, userid); + + } + + } } else Response.Errors.Add("گفتگویی یافت نشد"); @@ -360,8 +378,23 @@ namespace Hushian.Application.Services { item.IsRead = true; item.ReadDateTime = DateTime.Now; + if (!item.ExperID.HasValue) item.ExperID = ExperID; - return (await _ConversationResponseRepository.UPDATE(item)) != null; + + + if( (await _ConversationResponseRepository.UPDATE(item)) != null) + { + int userID = 0; + if (item.Type == ConversationType.EU) userID = item.ExperID.Value; + + else if (item.Type == ConversationType.CU) userID = item.conversation.CompanyID; + + else if (item.Type == ConversationType.UE) userID = item.conversation.UserID; + + await CheckMarkAsReadInHub(item.ID, userID); + + return true; + } } } @@ -380,7 +413,7 @@ namespace Hushian.Application.Services .Select(s => new ChatItemDto() { ID = s.ID, - ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last(l=>l.ExperID.HasValue).ExperID, + ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last(l => l.ExperID.HasValue).ExperID, ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last(l => l.ExperID.HasValue).Exper.FullName, GroupID = s.GroupID, GroupName = s.Group.Name, @@ -451,7 +484,7 @@ namespace Hushian.Application.Services if (Response.Value != null) Response.Success = true; return Response; } - public async Task WriteInHubFromCompany(ChatItemResponseDto item,int UserID) + public async Task WriteInHubFromCompany(ChatItemResponseDto item, int UserID) { await _hubContext.Clients.User(UserID.ToString()) .SendAsync("ReceiveNewChatItemFromCompany", item); @@ -465,19 +498,16 @@ namespace Hushian.Application.Services // // .SendAsync("ReceiveNewConversation", conv.Id, conv.Title); //} } - public async Task WriteInHubFromUser(ChatItemResponseDto item) + public async Task WriteInHubFromUser(ChatItemResponseDto item, int UserID) { await _hubContext.Clients.User(UserID.ToString()) - .SendAsync("ReceiveNewChatItemFromCompany", item); - - //// فرض: لیستی از کاربرانی که به گفتگو دسترسی دارند - //var usernames = new List(); - - //foreach (var usn in usernames) - //{ - // //await _hubContext.Clients.User(usn) - // // .SendAsync("ReceiveNewConversation", conv.Id, conv.Title); - //} + .SendAsync("ReceiveNewChatItemFromUser", item); } + public async Task CheckMarkAsReadInHub(int item, int UserID) + { + await _hubContext.Clients.User(UserID.ToString()) + .SendAsync("CheckMarkAsRead", item); + } + } } diff --git a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs index 386101b..fd7c712 100644 --- a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs +++ b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs @@ -206,12 +206,12 @@ namespace Hushian.WebApi.Controllers.v1 [Authorize(Roles = "Company,User,Exper")] public async Task MarkAsReadChatItem(int ConversationItemID) { - int? ExperID = null; + int? ID = null; ConversationType Type = ConversationType.UE; if (User.IsInRole("Exper")) { string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); - ExperID = Convert.ToInt32(strExperID); + ID = Convert.ToInt32(strExperID); Type = ConversationType.EU; } @@ -225,7 +225,7 @@ namespace Hushian.WebApi.Controllers.v1 } else return Unauthorized(); - return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ExperID) ? NoContent() + return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ID) ? NoContent() : BadRequest(new List() { "خطا در بروزرسانی گفتگو" }); } diff --git a/Presentation/HushianWebApp/Pages/Chat.razor b/Presentation/HushianWebApp/Pages/Chat.razor index 7feca37..a864c9e 100644 --- a/Presentation/HushianWebApp/Pages/Chat.razor +++ b/Presentation/HushianWebApp/Pages/Chat.razor @@ -5,11 +5,15 @@ @using Common.Enums @using HushianWebApp.Components @using HushianWebApp.Service +@using HushianWebApp.Services +@using Microsoft.AspNetCore.SignalR.Client @inject ChatService chatService @inject GroupService groupService @inject UserService userService @inject IJSRuntime JS @inject ToastService toastService +@inject ILocalStorageService localStorageService; +@inject HttpClient _Http; گفتمان @@ -29,7 +33,7 @@