-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
using Jordnaer.Shared; | ||
using MassTransit; | ||
|
||
namespace Jordnaer.Chat; | ||
|
||
public class SendMessageConsumer : IConsumer<SendMessage> | ||
{ | ||
public Task Consume(ConsumeContext<SendMessage> context) => throw new NotImplementedException(); | ||
} | ||
using Jordnaer.Server.Database; | ||
Check failure on line 1 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs GitHub Actions / test
|
||
using Jordnaer.Shared; | ||
using MassTransit; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Jordnaer.Chat; | ||
|
||
public class SendMessageConsumer : IConsumer<SendMessage> | ||
{ | ||
private readonly JordnaerDbContext _context; | ||
Check failure on line 10 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs GitHub Actions / test
|
||
|
||
public SendMessageConsumer(JordnaerDbContext context) | ||
Check failure on line 12 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs GitHub Actions / test
|
||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Consume(ConsumeContext<SendMessage> consumeContext) | ||
{ | ||
var chatMessage = consumeContext.Message; | ||
|
||
_context.ChatMessages.Add( | ||
new ChatMessage | ||
{ | ||
ChatId = chatMessage.ChatId, | ||
Id = chatMessage.Id, | ||
SenderId = chatMessage.SenderId, | ||
Text = chatMessage.Text, | ||
AttachmentUrl = chatMessage.AttachmentUrl, | ||
SentUtc = chatMessage.SentUtc | ||
}); | ||
|
||
var recipientIds = await _context.UserChats | ||
.Where(userChat => userChat.ChatId == chatMessage.ChatId) | ||
.Select(userChat => userChat.UserProfileId) | ||
.ToListAsync(consumeContext.CancellationToken); | ||
|
||
foreach (string recipientId in recipientIds.Where(recipientId => recipientId != chatMessage.SenderId)) | ||
{ | ||
_context.UnreadMessages.Add(new UnreadMessage | ||
{ | ||
RecipientId = recipientId, | ||
ChatId = chatMessage.ChatId, | ||
MessageSentUtc = chatMessage.SentUtc | ||
}); | ||
} | ||
|
||
await _context.SaveChangesAsync(consumeContext.CancellationToken); | ||
|
||
await _context.Chats | ||
.Where(chat => chat.Id == chatMessage.ChatId) | ||
.ExecuteUpdateAsync(call => | ||
call.SetProperty(chat => chat.LastMessageSentUtc, DateTime.UtcNow), | ||
consumeContext.CancellationToken); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,52 @@ | ||
using Jordnaer.Shared; | ||
using MassTransit; | ||
|
||
namespace Jordnaer.Chat; | ||
|
||
public class StartChatConsumer : IConsumer<StartChat> | ||
{ | ||
public Task Consume(ConsumeContext<StartChat> context) => throw new NotImplementedException(); | ||
} | ||
using Jordnaer.Server.Database; | ||
Check failure on line 1 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs GitHub Actions / test
|
||
using Jordnaer.Shared; | ||
using MassTransit; | ||
|
||
namespace Jordnaer.Chat; | ||
|
||
public class StartChatConsumer : IConsumer<StartChat> | ||
{ | ||
private readonly JordnaerDbContext _context; | ||
Check failure on line 9 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs GitHub Actions / test
|
||
|
||
public StartChatConsumer(JordnaerDbContext context) | ||
Check failure on line 11 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs GitHub Actions / test
|
||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Consume(ConsumeContext<StartChat> consumeContext) | ||
{ | ||
var chat = consumeContext.Message; | ||
|
||
_context.Chats.Add(new Shared.Chat | ||
{ | ||
LastMessageSentUtc = chat.LastMessageSentUtc, | ||
Id = chat.Id, | ||
StartedUtc = chat.StartedUtc, | ||
Messages = chat.Messages.Select(static message => message.ToChatMessage()).ToList() | ||
}); | ||
|
||
foreach (var message in chat.Messages) | ||
{ | ||
foreach (var recipient in chat.Recipients.Where(recipient => recipient.Id != chat.InitiatorId)) | ||
{ | ||
_context.UnreadMessages.Add(new UnreadMessage | ||
{ | ||
RecipientId = recipient.Id, | ||
ChatId = chat.Id, | ||
MessageSentUtc = message.SentUtc | ||
}); | ||
} | ||
} | ||
|
||
foreach (var recipient in chat.Recipients) | ||
{ | ||
_context.UserChats.Add(new UserChat | ||
{ | ||
ChatId = chat.Id, | ||
UserProfileId = recipient.Id | ||
}); | ||
} | ||
|
||
await _context.SaveChangesAsync(consumeContext.CancellationToken); | ||
} | ||
} |