|
6 | 6 | using MassTransit;
|
7 | 7 | using Microsoft.AspNetCore.Http.HttpResults;
|
8 | 8 | using Microsoft.AspNetCore.Mvc;
|
| 9 | +using Microsoft.AspNetCore.SignalR; |
9 | 10 | using Microsoft.EntityFrameworkCore;
|
10 | 11 |
|
11 | 12 | namespace Jordnaer.Server.Features.Chat;
|
@@ -129,6 +130,7 @@ async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
|
129 | 130 | [FromServices] CurrentUser currentUser,
|
130 | 131 | [FromServices] JordnaerDbContext context,
|
131 | 132 | [FromServices] IPublishEndpoint publishEndpoint,
|
| 133 | + [FromServices] IHubContext<ChatHub, IChatHub> chatHub, |
132 | 134 | CancellationToken cancellationToken) =>
|
133 | 135 | {
|
134 | 136 | if (await context.Chats.AsNoTracking().AnyAsync(existingChat => existingChat.Id == chat.Id, cancellationToken))
|
@@ -171,17 +173,20 @@ async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
|
171 | 173 |
|
172 | 174 | await context.SaveChangesAsync(cancellationToken);
|
173 | 175 |
|
| 176 | + await chatHub.Clients.Users(chat.Recipients.Select(recipient => recipient.Id)).StartChat(chat); |
| 177 | + |
174 | 178 | // TODO: This should send a message through an exchange to an Azure Function, which does the heavy lifting
|
175 | 179 | await publishEndpoint.Publish(chat, cancellationToken);
|
176 | 180 |
|
177 | 181 | return TypedResults.NoContent();
|
178 | 182 | });
|
179 | 183 |
|
180 | 184 | group.MapPost(MessagingConstants.SendMessage, async Task<Results<NoContent, BadRequest, UnauthorizedHttpResult>> (
|
181 |
| - [FromBody] SendMessage chatMessage, |
| 185 | + [FromBody] ChatMessageDto chatMessage, |
182 | 186 | [FromServices] CurrentUser currentUser,
|
183 | 187 | [FromServices] JordnaerDbContext context,
|
184 | 188 | [FromServices] IPublishEndpoint publishEndpoint,
|
| 189 | + [FromServices] IHubContext<ChatHub, IChatHub> chatHub, |
185 | 190 | CancellationToken cancellationToken) =>
|
186 | 191 | {
|
187 | 192 | if (await context.ChatMessages.AnyAsync(message => message.Id == chatMessage.Id, cancellationToken))
|
@@ -230,6 +235,11 @@ await context.Chats
|
230 | 235 | await context.SaveChangesAsync(cancellationToken);
|
231 | 236 | await transaction.CommitAsync(cancellationToken);
|
232 | 237 |
|
| 238 | + foreach (string recipientId in recipientIds) |
| 239 | + { |
| 240 | + await chatHub.Clients.User(recipientId).ReceiveChatMessage(chatMessage); |
| 241 | + } |
| 242 | + |
233 | 243 | // TODO: This should send a message through an exchange to an Azure Function, which does the heavy lifting
|
234 | 244 | await publishEndpoint.Publish(chatMessage, cancellationToken);
|
235 | 245 |
|
|
0 commit comments