Skip to content

Commit

Permalink
VM-1506: update last message in conversation when delete
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-buravlev committed Nov 27, 2024
1 parent 5046d04 commit 148de80
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public virtual async Task UpdateMessage(Message message)

public virtual async Task DeleteMessage(IList<string> messageIds, bool withReplies)
{
var messages = await _messageCrudService.GetAsync(messageIds);
var conversationIds = messages.Select(x => x.ConversationId).Distinct().ToList();

if (withReplies)
{
var idsToDelete = await GetChildMessageIdsRecursively(messageIds);
Expand All @@ -147,6 +150,19 @@ public virtual async Task DeleteMessage(IList<string> messageIds, bool withRepli

await _messageCrudService.DeleteAsync(messageIds);
}

var conversations = await _conversationCrudService.GetAsync(conversationIds);
foreach (var conversation in conversations)
{
if (conversation != null && messageIds.Contains(conversation.LastMessageId))
{
var newLastMessage = (await GetMessagesByConversation(conversation.Id)).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
conversation.LastMessageId = newLastMessage?.Id;
conversation.LastMessageTimestamp = newLastMessage?.CreatedDate ?? DateTime.MinValue;
}
}

await _conversationCrudService.SaveChangesAsync(conversations);
}

public virtual async Task<Message> SetMessageReadStatus(string messageId, string recipientId, bool notRead = false)
Expand Down

0 comments on commit 148de80

Please sign in to comment.