Skip to content

Commit

Permalink
(#394) communication: udpate udpateStatus command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Aug 29, 2024
1 parent 5489c28 commit c936cf3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,32 @@
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;

namespace MiniSpace.Services.Communication.Application.Commands.Handlers
{
public class UpdateMessageStatusHandler : ICommandHandler<UpdateMessageStatus>
{
private readonly IUserChatsRepository _userChatsRepository;
private readonly IMessageBroker _messageBroker;
private readonly ILogger<UpdateMessageStatusHandler> _logger;
private readonly ICommunicationService _communicationService;
private readonly IHubContext<ChatHub> _hubContext;
private readonly ILogger<UpdateMessageStatusHandler> _logger;

public UpdateMessageStatusHandler(
IUserChatsRepository userChatsRepository,
IMessageBroker messageBroker,
ILogger<UpdateMessageStatusHandler> logger,
IHubContext<ChatHub> hubContext)
ICommunicationService communicationService,
IHubContext<ChatHub> hubContext,
ILogger<UpdateMessageStatusHandler> 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);


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static async Task SendMessageStatusUpdate(IHubContext<ChatHub> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,65 @@ public async Task<Guid> CreateChatAsync(Guid chatId, List<Guid> 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));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace MiniSpace.Services.Communication.Application.Services
public interface ICommunicationService
{
Task<Guid> CreateChatAsync(Guid chatId, List<Guid> participantIds, string chatName = null);
Task UpdateMessageStatusAsync(Guid chatId, Guid messageId, string status);
// Task<IEnumerable<ChatDto>> GetUserChatsAsync(Guid userId);
// Task<ChatDto> GetChatByIdAsync(Guid chatId);
// Task SendMessageAsync(SendMessage command);
Expand Down

0 comments on commit c936cf3

Please sign in to comment.