diff --git a/src/web/Client/Features/Chat/ChatClientHub.razor b/src/web/Client/Features/Chat/ChatClientHub.razor index e8582104..f111e01c 100644 --- a/src/web/Client/Features/Chat/ChatClientHub.razor +++ b/src/web/Client/Features/Chat/ChatClientHub.razor @@ -11,17 +11,24 @@ .WithUrl(Navigation.ToAbsoluteUri("/hubs/chat")) .WithAutomaticReconnect() .Build(); - - await _hubConnection.StartAsync(); - } + _hubConnection.On(nameof(IChatHub.StartChat), (StartChat startChat) => + { + if (startChat.InitiatorId == CurrentUserId) + { - private async Task Send() - { - if (_hubConnection is not null) + } + }); + + _hubConnection.On(nameof(IChatHub.ReceiveChatMessage), (ChatMessageDto chatMessage) => { - await _hubConnection.SendAsync("SendMessage"); - } + if (chatMessage.Sender.Id == CurrentUserId) + { + + } + }); + + await _hubConnection.StartAsync(); } public bool IsConnected => _hubConnection?.State == HubConnectionState.Connected; diff --git a/src/web/Server/Features/Chat/ChatApi.cs b/src/web/Server/Features/Chat/ChatApi.cs index 29de19e0..c3c63859 100644 --- a/src/web/Server/Features/Chat/ChatApi.cs +++ b/src/web/Server/Features/Chat/ChatApi.cs @@ -6,6 +6,7 @@ using MassTransit; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; namespace Jordnaer.Server.Features.Chat; @@ -129,6 +130,7 @@ async Task> ( [FromServices] CurrentUser currentUser, [FromServices] JordnaerDbContext context, [FromServices] IPublishEndpoint publishEndpoint, + [FromServices] IHubContext chatHub, CancellationToken cancellationToken) => { if (await context.Chats.AsNoTracking().AnyAsync(existingChat => existingChat.Id == chat.Id, cancellationToken)) @@ -171,6 +173,8 @@ async Task> ( await context.SaveChangesAsync(cancellationToken); + await chatHub.Clients.Users(chat.Recipients.Select(recipient => recipient.Id)).StartChat(chat); + // TODO: This should send a message through an exchange to an Azure Function, which does the heavy lifting await publishEndpoint.Publish(chat, cancellationToken); @@ -178,10 +182,11 @@ async Task> ( }); group.MapPost(MessagingConstants.SendMessage, async Task> ( - [FromBody] SendMessage chatMessage, + [FromBody] ChatMessageDto chatMessage, [FromServices] CurrentUser currentUser, [FromServices] JordnaerDbContext context, [FromServices] IPublishEndpoint publishEndpoint, + [FromServices] IHubContext chatHub, CancellationToken cancellationToken) => { if (await context.ChatMessages.AnyAsync(message => message.Id == chatMessage.Id, cancellationToken)) @@ -230,6 +235,11 @@ await context.Chats await context.SaveChangesAsync(cancellationToken); await transaction.CommitAsync(cancellationToken); + foreach (string recipientId in recipientIds) + { + await chatHub.Clients.User(recipientId).ReceiveChatMessage(chatMessage); + } + // TODO: This should send a message through an exchange to an Azure Function, which does the heavy lifting await publishEndpoint.Publish(chatMessage, cancellationToken); diff --git a/src/web/Server/Features/Chat/ChatHub.cs b/src/web/Server/Features/Chat/ChatHub.cs index 6d707eb8..840eabc9 100644 --- a/src/web/Server/Features/Chat/ChatHub.cs +++ b/src/web/Server/Features/Chat/ChatHub.cs @@ -5,17 +5,18 @@ namespace Jordnaer.Server.Features.Chat; [Authorize] -public class ChatHub : Hub +public class ChatHub : Hub { - public async Task SendMessage(string user, string message) + public async Task SendChatMessageAsync(ChatMessageDto chatMessage, string userId) { - //TODO: Add azure signalR - await Clients.All.SendAsync("ReceiveMessage", user, message); + await Clients.User(userId).ReceiveChatMessage(chatMessage); } - - public override Task OnConnectedAsync() + public async Task StartChatAsync(StartChat startChat) { - Context.User.GetId(); - return base.OnConnectedAsync(); + await Clients + .Users(startChat + .Recipients + .Select(user => user.Id)) + .StartChat(startChat); } } diff --git a/src/web/Shared/Chat/IChatHub.cs b/src/web/Shared/Chat/IChatHub.cs new file mode 100644 index 00000000..99d93144 --- /dev/null +++ b/src/web/Shared/Chat/IChatHub.cs @@ -0,0 +1,7 @@ +namespace Jordnaer.Shared; + +public interface IChatHub +{ + Task ReceiveChatMessage(ChatMessageDto message); + Task StartChat(StartChat startChat); +}