This commit is contained in:
mmrbnjd
2025-08-07 22:41:22 +03:30
parent d153c4bde0
commit 0736632a59
5 changed files with 67 additions and 4 deletions

View File

@@ -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<string>();
//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<string>();
//foreach (var usn in usernames)
//{
// //await _hubContext.Clients.User(usn)
// // .SendAsync("ReceiveNewConversation", conv.Id, conv.Title);
//}
}
}
}

View File

@@ -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)

View File

@@ -18,6 +18,7 @@
<PackageReference Include="Blazor.Bootstrap" Version="3.3.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.8" />
</ItemGroup>
<ItemGroup>

View File

@@ -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<string>("U/key");
hubConnection = new HubConnectionBuilder()
.WithUrl("http://localhost:5089/chatNotificationHub", options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
})
.WithAutomaticReconnect()
.Build();
hubConnection.On<ChatItemResponseDto>("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()
{

View File

@@ -119,6 +119,8 @@
public int? SelectedGroup { get; set; }
public string InputMessage { get; set; }
public bool Sending { get; set; } = false;
#endregion
}
@functions {