From c936cf354d9f5faaff054797f58aad10f77a4bbf Mon Sep 17 00:00:00 2001 From: Andrii Voznesenskyi Date: Thu, 29 Aug 2024 18:02:09 +0200 Subject: [PATCH] (#394) communication: udpate udpateStatus command handler --- .../Handlers/UpdateMessageStatusHandler.cs | 62 +++---------------- .../Hubs/ChatHub.cs | 1 - .../Services/CommunicationService.cs | 60 ++++++++++++++++++ .../Services/ICommunicationService.cs | 1 + 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Commands/Handlers/UpdateMessageStatusHandler.cs b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Commands/Handlers/UpdateMessageStatusHandler.cs index ad2fe3a10..c18402d17 100644 --- a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Commands/Handlers/UpdateMessageStatusHandler.cs +++ b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Commands/Handlers/UpdateMessageStatusHandler.cs @@ -5,7 +5,6 @@ using MiniSpace.Services.Communication.Application.Events; using MiniSpace.Services.Communication.Application.Hubs; using MiniSpace.Services.Communication.Application.Services; -using MiniSpace.Services.Communication.Core.Repositories; using System.Threading; using System.Threading.Tasks; @@ -13,70 +12,25 @@ namespace MiniSpace.Services.Communication.Application.Commands.Handlers { public class UpdateMessageStatusHandler : ICommandHandler { - private readonly IUserChatsRepository _userChatsRepository; - private readonly IMessageBroker _messageBroker; - private readonly ILogger _logger; + private readonly ICommunicationService _communicationService; private readonly IHubContext _hubContext; + private readonly ILogger _logger; public UpdateMessageStatusHandler( - IUserChatsRepository userChatsRepository, - IMessageBroker messageBroker, - ILogger logger, - IHubContext hubContext) + ICommunicationService communicationService, + IHubContext hubContext, + ILogger logger) { - _userChatsRepository = userChatsRepository; - _messageBroker = messageBroker; - _logger = logger; + _communicationService = communicationService; _hubContext = hubContext; + _logger = logger; } public async Task HandleAsync(UpdateMessageStatus command, CancellationToken cancellationToken) { - // Retrieve the chat using the GetByChatIdAsync method - var chat = await _userChatsRepository.GetByChatIdAsync(command.ChatId); - - if (chat == null) - { - _logger.LogWarning($"Chat with ID {command.ChatId} not found."); - return; // Exit if the chat is not found - } - - // Find the message by its ID within the chat - var message = chat.Messages.Find(m => m.Id == command.MessageId); - if (message == null) - { - _logger.LogWarning($"Message with ID {command.MessageId} not found in chat with ID {command.ChatId}."); - return; // Exit if the message is not found - } - - // Update the message status based on the command - switch (command.Status) - { - case "Read": - message.MarkAsRead(); - break; - case "Unread": - message.MarkAsUnread(); - break; - case "Deleted": - message.MarkAsDeleted(); - break; - } - - // Update the user chats to reflect the status change - var userChats = await _userChatsRepository.GetByUserIdAsync(chat.ParticipantIds.FirstOrDefault()); - await _userChatsRepository.UpdateAsync(userChats); - - // Publish the event to notify other systems - await _messageBroker.PublishAsync(new MessageStatusUpdated(command.ChatId, command.MessageId, command.Status)); - - // Log the status update + await _communicationService.UpdateMessageStatusAsync(command.ChatId, command.MessageId, command.Status); _logger.LogInformation($"Message status updated: ChatId={command.ChatId}, MessageId={command.MessageId}, Status={command.Status}"); - - // Notify the chat participants of the status change via SignalR await ChatHub.SendMessageStatusUpdate(_hubContext, command.ChatId.ToString(), command.MessageId, command.Status, _logger); - - } } } diff --git a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Hubs/ChatHub.cs b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Hubs/ChatHub.cs index ec45ee015..d570aec26 100644 --- a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Hubs/ChatHub.cs +++ b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Hubs/ChatHub.cs @@ -93,7 +93,6 @@ public static async Task SendMessageStatusUpdate(IHubContext hubContext logger.LogInformation($"Sending message status update to chat {chatId} for message {messageId} with status {status}"); - // Serialize the statusUpdate object to JSON var jsonStatusUpdate = JsonSerializer.Serialize(statusUpdate); await hubContext.Clients.All.SendAsync("ReceiveMessageStatusUpdate", jsonStatusUpdate); diff --git a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/CommunicationService.cs b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/CommunicationService.cs index 99d8c01f6..96f67ad1d 100644 --- a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/CommunicationService.cs +++ b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/CommunicationService.cs @@ -60,5 +60,65 @@ public async Task CreateChatAsync(Guid chatId, List participantIds, _logger.LogInformation($"New chat created with ID: {newChat.Id}"); return newChat.Id; } + + + public async Task UpdateMessageStatusAsync(Guid chatId, Guid messageId, string status) + { + // Retrieve the chat using the GetByChatIdAsync method + var chat = await _userChatsRepository.GetByChatIdAsync(chatId); + if (chat == null) + { + _logger.LogWarning($"Chat with ID {chatId} not found."); + return; + } + + var message = chat.Messages.FirstOrDefault(m => m.Id == messageId); + if (message == null) + { + _logger.LogWarning($"Message with ID {messageId} not found in chat with ID {chatId}."); + return; + } + + switch (status) + { + case "Read": + message.MarkAsRead(); + break; + case "Unread": + message.MarkAsUnread(); + break; + case "Deleted": + message.MarkAsDeleted(); + break; + default: + _logger.LogWarning($"Unsupported status '{status}' for message ID {messageId} in chat ID {chatId}."); + return; + } + + foreach (var participantId in chat.ParticipantIds) + { + var userChats = await _userChatsRepository.GetByUserIdAsync(participantId); + if (userChats != null) + { + var existingChat = userChats.GetChatById(chatId); + if (existingChat != null) + { + var messageToUpdate = existingChat.Messages.FirstOrDefault(m => m.Id == messageId); + if (messageToUpdate != null) + { + _logger.LogInformation($"Updating message status for chat ID {chatId} and message ID {messageId} to {status}"); + messageToUpdate.MarkAsRead(); + } + _logger.LogInformation($"Updating chat for participant {participantId} with chat ID {chatId}"); + await _userChatsRepository.UpdateAsync(userChats); + } + } + } + + _logger.LogInformation($"Message status updated: ChatId={chatId}, MessageId={messageId}, Status={status}"); + + await _messageBroker.PublishAsync(new MessageStatusUpdated(chatId, messageId, status)); + } + } } diff --git a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/ICommunicationService.cs b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/ICommunicationService.cs index d766c7a97..da0dd02cd 100644 --- a/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/ICommunicationService.cs +++ b/MiniSpace.Services.Communication/src/MiniSpace.Services.Communication.Application/Services/ICommunicationService.cs @@ -9,6 +9,7 @@ namespace MiniSpace.Services.Communication.Application.Services public interface ICommunicationService { Task CreateChatAsync(Guid chatId, List participantIds, string chatName = null); + Task UpdateMessageStatusAsync(Guid chatId, Guid messageId, string status); // Task> GetUserChatsAsync(Guid userId); // Task GetChatByIdAsync(Guid chatId); // Task SendMessageAsync(SendMessage command);