Skip to content

Commit ce965f4

Browse files
authored
Merge pull request #454 from Ketteiteki/update_delete_message_flow
Update delete message flow
2 parents 0bcba8e + c508f82 commit ce965f4

6 files changed

Lines changed: 65 additions & 29 deletions

File tree

MangoAPI.BusinessLogic/ApiCommands/Messages/DeleteMessageCommandHandler.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ public async Task<Result<DeleteMessageResponse>> Handle(
3737
{
3838
var checkMessage = await dbContext.Messages
3939
.Include(x => x.User)
40-
.Select(x=> new
41-
{
42-
MessageId = x.Id,
43-
UserId = x.User.Id
44-
})
40+
.Select(x => new { MessageId = x.Id, UserId = x.User.Id })
4541
.FirstOrDefaultAsync(t => t.MessageId == request.MessageId, cancellationToken);
4642

4743
if (checkMessage == null)
@@ -85,7 +81,7 @@ public async Task<Result<DeleteMessageResponse>> Handle(
8581

8682
var deleteNotification = isMessageLast
8783
? UpdateChatLastMessageAndReturnNotification(chat, deletedMessage)
88-
: CreateNotLastMessageNotification(chat.Id, deletedMessage.Id);
84+
: CreateNotLastMessageNotification(chat.Id, deletedMessage.Id, deletedMessage.UserId);
8985

9086
dbContext.Messages.Remove(deletedMessage);
9187
dbContext.Chats.Update(chat);
@@ -97,7 +93,7 @@ public async Task<Result<DeleteMessageResponse>> Handle(
9793
return responseFactory.SuccessResponse(DeleteMessageResponse.FromSuccess(deletedMessage));
9894
}
9995

100-
private static MessageDeleteNotification UpdateChatLastMessageAndReturnNotification(
96+
private static DeleteMessageNotification UpdateChatLastMessageAndReturnNotification(
10197
ChatEntity chat,
10298
MessageEntity deletedMessage)
10399
{
@@ -110,7 +106,8 @@ private static MessageDeleteNotification UpdateChatLastMessageAndReturnNotificat
110106
newLastMessage?.CreatedAt,
111107
newLastMessage?.Id);
112108

113-
var deleteNotification = new MessageDeleteNotification(
109+
var deleteNotification = new DeleteMessageNotification(
110+
deletedMessage.UserId,
114111
chat.Id,
115112
deletedMessage.Id,
116113
newLastMessage?.Text,
@@ -122,9 +119,13 @@ private static MessageDeleteNotification UpdateChatLastMessageAndReturnNotificat
122119
return deleteNotification;
123120
}
124121

125-
private static MessageDeleteNotification CreateNotLastMessageNotification(Guid chatId, Guid deletedMessageId)
122+
private static DeleteMessageNotification CreateNotLastMessageNotification(
123+
Guid chatId,
124+
Guid deletedMessageId,
125+
Guid userId)
126126
{
127-
var deleteNotification = new MessageDeleteNotification(
127+
var deleteNotification = new DeleteMessageNotification(
128+
userId,
128129
chatId,
129130
deletedMessageId,
130131
NewLastMessageText: string.Empty,

MangoAPI.BusinessLogic/HubConfig/IHubClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public interface IHubClient
2323
/// <summary>
2424
/// Notifies chat subscribers on the message delete via SignalR.
2525
/// </summary>
26-
Task MessageDeletedAsync(MessageDeleteNotification notification);
26+
Task MessageDeletedAsync(DeleteMessageNotification notification);
2727
}

MangoAPI.BusinessLogic/Notifications/MessageDeleteNotification.cs renamed to MangoAPI.BusinessLogic/Notifications/DeleteMessageNotification.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace MangoAPI.BusinessLogic.Notifications;
44

5-
public record MessageDeleteNotification(
5+
public record DeleteMessageNotification(
6+
Guid UserId,
67
Guid ChatId,
78
Guid DeletedMessageId,
89
string NewLastMessageText,

MangoAPI.Client/src/app/components/chats/chats.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ <h5>Chats</h5>
270270
data-target="#createGroup"
271271
data-toggle="modal"
272272
role="button"
273-
(click)="deleteMessage(message)"
273+
(click)="onDeleteMessageClick(message)"
274274
>
275275
<img
276276
alt="Delete Message Icon"

MangoAPI.Client/src/app/components/chats/chats.component.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,46 @@ export class ChatsComponent implements OnInit {
348348
await this.onSendMessageClick().then((r) => r);
349349
}
350350

351-
async deleteMessage(message: Message) {
351+
async onDeleteMessageClick(message: Message) {
352352
const deleteMessageCommand = new DeleteMessageCommand(message.messageId, message.chatId);
353353

354354
const messageIndex = this.messages.findIndex((x) => x.messageId === message.messageId);
355355

356356
if (messageIndex === -1) return;
357357

358-
this.messages.splice(messageIndex, 1);
358+
const lastMessage = this.messages[this.messages.length - 1];
359+
360+
const deletedMessage = this.messages.splice(messageIndex, 1)[0];
361+
362+
const newLastMessage = this.messages[this.messages.length - 1];
363+
364+
const lastMessageDeleted = lastMessage.messageId === deletedMessage.messageId;
365+
366+
const chatIndex = this.chats.findIndex((x) => x.chatId === message.chatId);
367+
368+
const chat = this.chats[chatIndex];
369+
370+
if (lastMessageDeleted && newLastMessage) {
371+
chat.lastMessageId = newLastMessage.messageId;
372+
chat.lastMessageText = newLastMessage.text;
373+
chat.lastMessageAuthor = newLastMessage.displayName;
374+
chat.lastMessageTime = newLastMessage.createdAt;
375+
376+
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
377+
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
378+
return;
379+
}
380+
381+
if (lastMessageDeleted && !newLastMessage) {
382+
chat.lastMessageId = '';
383+
chat.lastMessageText = '';
384+
chat.lastMessageAuthor = '';
385+
chat.lastMessageTime = '';
386+
387+
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
388+
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
389+
return;
390+
}
359391

360392
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
361393
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
@@ -490,25 +522,26 @@ export class ChatsComponent implements OnInit {
490522
}
491523

492524
private onMessageDeleteHandler(notification: DeleteMessageNotification) {
493-
const chatIndex = this.chats.findIndex((x) => x.chatId === notification.chatId);
525+
const selfMessage = notification.userId === this.userId;
494526

495-
if (notification.isLastMessage && chatIndex !== -1) {
496-
const updatedChat = this.chats[chatIndex];
527+
if (selfMessage) return;
497528

498-
updatedChat.lastMessageAuthor = notification.newLastMessageDisplayName;
499-
updatedChat.lastMessageText = notification.newLastMessageText;
500-
updatedChat.lastMessageTime = notification.newLastMessageTime;
501-
updatedChat.lastMessageId = notification.newLastMessageId;
529+
if (this.activeChatId === notification.chatId) {
530+
const messageIndex = this.messages.findIndex(
531+
(x) => x.messageId === notification.deletedMessageId
532+
);
533+
this.messages.splice(messageIndex, 1);
502534
}
503535

504-
if (this.activeChatId !== notification.chatId) return;
505-
506-
const messageIndex = this.messages.findIndex(
507-
(x) => x.messageId === notification.deletedMessageId
508-
);
536+
const chatIndex = this.chats.findIndex((x) => x.chatId === notification.chatId);
537+
const chat = this.chats[chatIndex];
509538

510-
if (messageIndex !== -1) {
511-
this.messages.splice(messageIndex, 1);
539+
if (notification.isLastMessage) {
540+
chat.lastMessageId = notification.newLastMessageId;
541+
chat.lastMessageText = notification.newLastMessageText;
542+
chat.lastMessageAuthor = notification.newLastMessageDisplayName;
543+
chat.lastMessageTime = notification.newLastMessageTime;
544+
return;
512545
}
513546
}
514547

MangoAPI.Client/src/app/types/notifications/DeleteMessageNotification.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface DeleteMessageNotification {
2+
userId: string;
23
chatId: string;
34
deletedMessageId: string;
45
newLastMessageText: string;

0 commit comments

Comments
 (0)