diff --git a/Hushian.Application/Services/ChatService.cs b/Hushian.Application/Services/ChatService.cs index 2ec463e..9859071 100644 --- a/Hushian.Application/Services/ChatService.cs +++ b/Hushian.Application/Services/ChatService.cs @@ -237,7 +237,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)) @@ -276,6 +276,9 @@ namespace Hushian.Application.Services convModel.Status = ConversationStatus.InProgress; await _ConversationRepository.UPDATE(convModel); } + if (dto.Type != ConversationType.UE) + await WriteInHubFromCompany(Response.Value, convModel.UserID); + } else Response.Errors.Add("گفتگویی یافت نشد"); } @@ -448,5 +451,33 @@ namespace Hushian.Application.Services if (Response.Value != null) Response.Success = true; return Response; } + public async Task WriteInHubFromCompany(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); + //} + } + public async Task WriteInHubFromUser(ChatItemResponseDto item) + { + 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); + //} + } } } diff --git a/Hushian.Application/Services/ExMethod.cs b/Hushian.Application/Services/ExMethod.cs index 4af12e2..73b4b66 100644 --- a/Hushian.Application/Services/ExMethod.cs +++ b/Hushian.Application/Services/ExMethod.cs @@ -27,7 +27,7 @@ namespace Hushian.Application.Services { var claims = new[] { - new Claim(JwtRegisteredClaimNames.Sub,UserName), + new Claim(JwtRegisteredClaimNames.Sub,userId.ToString()), new Claim(ClaimTypes.NameIdentifier, UserName), new Claim(CustomClaimTypes.Uid,userId.ToString()), new Claim(ClaimTypes.Role, Role) diff --git a/Presentation/HushianWebApp/HushianWebApp.csproj b/Presentation/HushianWebApp/HushianWebApp.csproj index d396efa..99c46a2 100644 --- a/Presentation/HushianWebApp/HushianWebApp.csproj +++ b/Presentation/HushianWebApp/HushianWebApp.csproj @@ -18,6 +18,7 @@ + diff --git a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor index ed3e3c8..3995d70 100644 --- a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor +++ b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor @@ -5,6 +5,9 @@ @using Common.Dtos.Group @using HushianWebApp.Service @using HushianWebApp.Services +@using Microsoft.AspNetCore.SignalR.Client; + +@inject NavigationManager NavigationManager @inject ChatService ChatService @inject ILocalStorageService localStorageService; @inject AuthService authService; @@ -173,7 +176,7 @@ [Parameter] public int CompanyID { get; set; } [Parameter] public int? ChatID { get; set; } private ConfirmDialog dialog = default!; - + private HubConnection? hubConnection; private bool _shouldObserveVisibility = false; int? GroupID = null; ReadANDUpdate_CompanyDto? CompanyInfo = new(); @@ -259,7 +262,34 @@ } protected override async Task OnInitializedAsync() { + Console.WriteLine($"🔔 welcome"); + await IsOnline(); + //-------------hub + var token = await localStorageService.GetItem("U/key"); + + hubConnection = new HubConnectionBuilder() + .WithUrl("http://localhost:5089/chatNotificationHub", options => + { + options.AccessTokenProvider = () => Task.FromResult(token); + }) + .WithAutomaticReconnect() + .Build(); + + hubConnection.On("ReceiveNewChatItemFromCompany",async (chatitem) => + { + if (LastOpenChat.ID == chatitem.ChatItemID) + { + LastOpenChat.Responses.Add(chatitem); + StateHasChanged(); + await MarkAsRead(chatitem.ID); + // Scroll to target if exists, otherwise scroll to bottom + await JS.InvokeVoidAsync("scrollToTargetOrBottom"); + } + }); + + await hubConnection.StartAsync(); + //---------end hub await base.OnInitializedAsync(); } async Task IsOnline() @@ -336,7 +366,6 @@ } await Task.CompletedTask; } - // Method to handle new messages from other users public async Task HandleNewMessage() { diff --git a/Presentation/HushianWebApp/Pages/FromUserSide/ZUserPanel.razor b/Presentation/HushianWebApp/Pages/FromUserSide/ZUserPanel.razor index de8cadc..8b7a7c2 100644 --- a/Presentation/HushianWebApp/Pages/FromUserSide/ZUserPanel.razor +++ b/Presentation/HushianWebApp/Pages/FromUserSide/ZUserPanel.razor @@ -119,6 +119,8 @@ public int? SelectedGroup { get; set; } public string InputMessage { get; set; } public bool Sending { get; set; } = false; + + #endregion } @functions {