Skip to content

Commit

Permalink
working on the chathub
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Aug 30, 2023
1 parent 0693e83 commit 4abecde
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
23 changes: 15 additions & 8 deletions src/web/Client/Features/Chat/ChatClientHub.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 17 in src/web/Client/Features/Chat/ChatClientHub.razor

View workflow job for this annotation

GitHub Actions / test

The name 'CurrentUserId' does not exist in the current context
{

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)

Check failure on line 25 in src/web/Client/Features/Chat/ChatClientHub.razor

View workflow job for this annotation

GitHub Actions / test

The name 'CurrentUserId' does not exist in the current context
{

}
});

await _hubConnection.StartAsync();
}

public bool IsConnected => _hubConnection?.State == HubConnectionState.Connected;
Expand Down
12 changes: 11 additions & 1 deletion src/web/Server/Features/Chat/ChatApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +130,7 @@ async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
[FromServices] CurrentUser currentUser,
[FromServices] JordnaerDbContext context,
[FromServices] IPublishEndpoint publishEndpoint,
[FromServices] IHubContext<ChatHub, IChatHub> chatHub,
CancellationToken cancellationToken) =>
{
if (await context.Chats.AsNoTracking().AnyAsync(existingChat => existingChat.Id == chat.Id, cancellationToken))
Expand Down Expand Up @@ -171,17 +173,20 @@ async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
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);
return TypedResults.NoContent();
});

group.MapPost(MessagingConstants.SendMessage, async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
[FromBody] SendMessage chatMessage,
[FromBody] ChatMessageDto chatMessage,
[FromServices] CurrentUser currentUser,
[FromServices] JordnaerDbContext context,
[FromServices] IPublishEndpoint publishEndpoint,
[FromServices] IHubContext<ChatHub, IChatHub> chatHub,
CancellationToken cancellationToken) =>
{
if (await context.ChatMessages.AnyAsync(message => message.Id == chatMessage.Id, cancellationToken))
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions src/web/Server/Features/Chat/ChatHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
namespace Jordnaer.Server.Features.Chat;

[Authorize]
public class ChatHub : Hub
public class ChatHub : Hub<IChatHub>
{
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);
}
}
7 changes: 7 additions & 0 deletions src/web/Shared/Chat/IChatHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Jordnaer.Shared;

public interface IChatHub
{
Task ReceiveChatMessage(ChatMessageDto message);
Task StartChat(StartChat startChat);
}

0 comments on commit 4abecde

Please sign in to comment.