Skip to content

Commit

Permalink
moved chat consumers to chat service
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Oct 15, 2023
1 parent d5be4b0 commit c3cb9d7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 19 deletions.
64 changes: 55 additions & 9 deletions src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs
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

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'Server' does not exist in the namespace 'Jordnaer' (are you missing an assembly reference?)

Check failure on line 1 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'Server' does not exist in the namespace 'Jordnaer' (are you missing an assembly reference?)
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

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

public SendMessageConsumer(JordnaerDbContext context)

Check failure on line 12 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 12 in src/container_apps/Jordnaer.Chat/SendMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)
{
_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);
}
}
5 changes: 4 additions & 1 deletion src/container_apps/Jordnaer.Chat/SetChatNameConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ namespace Jordnaer.Chat;

public class SetChatNameConsumer : IConsumer<SetChatName>
{
public Task Consume(ConsumeContext<SetChatName> context) => throw new NotImplementedException();
public Task Consume(ConsumeContext<SetChatName> context)
{
throw new NotImplementedException();
}
}
61 changes: 52 additions & 9 deletions src/container_apps/Jordnaer.Chat/StartChatConsumer.cs
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

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'Server' does not exist in the namespace 'Jordnaer' (are you missing an assembly reference?)

Check failure on line 1 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'Server' does not exist in the namespace 'Jordnaer' (are you missing an assembly reference?)
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

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 9 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

public StartChatConsumer(JordnaerDbContext context)

Check failure on line 11 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/container_apps/Jordnaer.Chat/StartChatConsumer.cs

View workflow job for this annotation

GitHub Actions / test

The type or namespace name 'JordnaerDbContext' could not be found (are you missing a using directive or an assembly reference?)
{
_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);
}
}

0 comments on commit c3cb9d7

Please sign in to comment.