From af37d0b85d9de6ed5119569e434555996159d60e Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Tue, 29 Jul 2025 17:07:37 +0330 Subject: [PATCH] ... --- Hushian.Application/Services/ChatService.cs | 42 ++++++++ Hushian.Application/Services/ExperService.cs | 3 +- .../Controllers/v1/ChatController.cs | 34 +++++++ Presentation/HushianWebApp/Pages/Chat.razor | 96 +++++++++++++------ .../HushianWebApp/Service/ChatService.cs | 11 +++ 5 files changed, 158 insertions(+), 28 deletions(-) diff --git a/Hushian.Application/Services/ChatService.cs b/Hushian.Application/Services/ChatService.cs index 1ce2d9e..e8d0952 100644 --- a/Hushian.Application/Services/ChatService.cs +++ b/Hushian.Application/Services/ChatService.cs @@ -23,6 +23,18 @@ namespace Hushian.Application.Services private readonly GroupService _groupService; private readonly ExperService _experService; private readonly IHubContext _hubContext; + + public ChatService(IGenericRepository conversationRepository, IGenericRepository conversationResponseRepository, CompanyService companyService, UserService userService, GroupService groupService, ExperService experService, IHubContext hubContext) + { + _ConversationRepository = conversationRepository; + _ConversationResponseRepository = conversationResponseRepository; + _companyService = companyService; + _userService = userService; + _groupService = groupService; + _experService = experService; + _hubContext = hubContext; + } + public async Task> GeChatsByExperID(int ExperID, ConversationStatus status) => await _ConversationRepository.Get() .Include(inc => inc.Group) @@ -257,5 +269,35 @@ namespace Hushian.Application.Services return true; } + + public async Task MarkAsReadChatItem(int ID, ConversationType Type, int? ExperID) + { + var item = await _ConversationResponseRepository.Get() + .Include(inc => inc.conversation).FirstOrDefaultAsync(w => w.ID == ID && !w.ExperID.HasValue + && w.conversation.Status != ConversationStatus.Finished); + + if (item != null) + { + if (Type != item.Type) + { + if (Type != ConversationType.UE && item.conversation.Status == ConversationStatus.Recorded) + { + item.conversation.Status = ConversationStatus.InProgress; + await _ConversationRepository.UPDATE(item.conversation); + } + + if (!item.IsRead) + { + item.IsRead = true; + item.ReadDateTime = DateTime.Now; + item.ExperID = ExperID; + return (await _ConversationResponseRepository.UPDATE(item)) != null; + } + } + + + } + return false; + } } } diff --git a/Hushian.Application/Services/ExperService.cs b/Hushian.Application/Services/ExperService.cs index 356c764..8dee085 100644 --- a/Hushian.Application/Services/ExperService.cs +++ b/Hushian.Application/Services/ExperService.cs @@ -16,11 +16,12 @@ namespace Hushian.Application.Services private readonly VerificationService _VerificationService; private readonly IMapper _mapper; - public ExperService(IGenericRepository experRepository, VerificationService verificationService, IMapper mapper) + public ExperService(IGenericRepository experRepository, VerificationService verificationService, IMapper mapper, IGenericRepository companyRepository) { _ExperRepository = experRepository; _VerificationService = verificationService; _mapper = mapper; + _CompanyRepository = companyRepository; } public async Task> ADDExper(ADD_ExperDto dto, int CompanyID) diff --git a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs index 9756e44..c9e969f 100644 --- a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs +++ b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs @@ -13,6 +13,13 @@ namespace Hushian.WebApi.Controllers.v1 public class ChatController : ControllerBase { private readonly ChatService _chatService; private readonly ExperService _experService; + + public ChatController(ChatService chatService, ExperService experService) + { + _chatService = chatService; + _experService = experService; + } + [HttpPost("MyChats")] [Authorize(Roles = "Company,Exper")] public async Task MyChats([FromBody] ConversationStatus status) @@ -100,5 +107,32 @@ namespace Hushian.WebApi.Controllers.v1 return await _chatService.FinishChat(ChatID) ? NoContent() : BadRequest(new List { "خطا در بروزرسانی وضعیت" }); } + [HttpPut("MarkAsReadChatItem/{ConversationItemID}")] + [Authorize(Roles = "Company,User,Exper")] + public async Task MarkAsReadChatItem(int ConversationItemID) + { + int? ExperID = 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); + Type = ConversationType.EU; + } + + else if (User.IsInRole("User")) + { + Type = ConversationType.UE; + } + else if (User.IsInRole("Company")) + { + Type = ConversationType.CU; + } + else return Unauthorized(); + + return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ExperID) ? NoContent() + : BadRequest(new List() { "خطا در بروزرسانی گفتگو" }); + + } } } diff --git a/Presentation/HushianWebApp/Pages/Chat.razor b/Presentation/HushianWebApp/Pages/Chat.razor index 24c762e..65cdb62 100644 --- a/Presentation/HushianWebApp/Pages/Chat.razor +++ b/Presentation/HushianWebApp/Pages/Chat.razor @@ -1,4 +1,4 @@ -@page "/Chant" +@page "/Chat" @using Common.Dtos.Conversation @using Common.Dtos.Group @using Common.Enums @@ -158,7 +158,44 @@
@if (ChatCurrent != null && ChatCurrent.Responses != null) - { @B1Content } + { +
+ @{ + bool target = false; + } + @foreach (var msg in ChatCurrent?.Responses) + { + @if (!target && ((!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) || ChatCurrent.Responses.Last() == msg)) + { + target = true; +
+ @if (!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) + { +

ـــــــــــــــــــــــــ

+ } +
+ } + +
+
@msg.text
+ @if (msg.Type == Common.Enums.ConversationType.EU) + { + if (msg.IsRead) + { + + } + else + { + + } + } +
+ } + @{ + target = false; + } +
+ } else {
@@ -195,7 +232,6 @@ @code { - public RenderFragment B1Content { get; set; } public Common.Dtos.CurrentUserInfo CurrentUser { get; set; } List _Group = new List(); //------------------------------------- @@ -210,6 +246,8 @@ public string MsgInput { get; set; } bool chatloading = false; string SelectedChatUserName = "مهدی ربیع نژاد"; + private bool _shouldObserveVisibility = false; + } @functions { @@ -220,8 +258,19 @@ Inbox1Items = await chatService.ChatAwaitingOurResponse(); Inbox2Items = await chatService.MyChatsIsInProgress(); Inbox3Items = await chatService.MyChatsIsFinished(); + await base.OnInitializedAsync(); } + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (_shouldObserveVisibility) + { + _shouldObserveVisibility = false; + await JS.InvokeVoidAsync("observeVisibility", DotNetObjectReference.Create(this)); + await JS.InvokeVoidAsync("scrollToTarget"); + } + } + async Task OnclickInbox(int ID) { switch (ID) @@ -262,37 +311,29 @@ } async Task onClickSelectedChat(int InboxID, ChatItemDto chatItem) { + chatloading = true; SelectedChatUserName = "در حال گفتگو با " + chatItem.UserFullName; ChatCurrent = chatItem; + + _shouldObserveVisibility = true; // فعال کن تا در OnAfterRenderAsync صدا زده بشه - bool target = false; - B1Content =@
- @foreach (var msg in ChatCurrent.Responses) - { - @if (!target && ((!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) || ChatCurrent.Responses.Last() == msg)) - { - target = true; -
- @if (!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) {

ـــــــــــــــــــــــــ

} -
- } - -
-
@msg.text
- @if (msg.Type == Common.Enums.ConversationType.EU) - { - if (msg.IsRead) { } - else { } - } -
- } -
; - await JS.InvokeVoidAsync("observeVisibility", DotNetObjectReference.Create(this)); - await JS.InvokeVoidAsync("scrollToTarget"); + StateHasChanged(); // مجبور کن Blazor رندر کنه chatloading = false; } + [JSInvokable] + public async Task MarkAsRead(int id) + { + var msg = ChatCurrent.Responses.FirstOrDefault(m => m.ID == id); + if (msg != null && !msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) + { + msg.IsRead = true; + await chatService.MarkAsReadChatItemAsync(id); + } + StateHasChanged(); + await Task.CompletedTask; + } }