Skip to content

Commit c508f82

Browse files
committed
realtime flow updated | minor cleanups and edits
1 parent 00e0f52 commit c508f82

5 files changed

Lines changed: 50 additions & 53 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.ts

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UserChatsService } from '../../services/api/user-chats.service';
1313
import { ValidationService } from '../../services/messenger/validation.service';
1414
import * as signalR from '@microsoft/signalr';
1515
import { DeleteMessageNotification } from '../../types/notifications/DeleteMessageNotification';
16-
import { BehaviorSubject, firstValueFrom, distinctUntilKeyChanged, last } from 'rxjs';
16+
import { BehaviorSubject, firstValueFrom, distinctUntilKeyChanged } from 'rxjs';
1717
import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours';
1818
import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand';
1919
import ApiBaseService from 'src/app/services/api/api-base.service';
@@ -356,30 +356,36 @@ export class ChatsComponent implements OnInit {
356356
if (messageIndex === -1) return;
357357

358358
const lastMessage = this.messages[this.messages.length - 1];
359+
359360
const deletedMessage = this.messages.splice(messageIndex, 1)[0];
360-
const lastMessageAfterDeleting = this.messages[this.messages.length - 1];
361-
const isDeleteMessageLast = lastMessage.messageId === deletedMessage.messageId;
362-
const chatIndex = this.chats.findIndex((x) => x.chatId === message.chatId)
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+
363368
const chat = this.chats[chatIndex];
364369

365-
if (isDeleteMessageLast && lastMessageAfterDeleting) {
366-
chat.lastMessageId = lastMessageAfterDeleting.messageId;
367-
chat.lastMessageText = lastMessageAfterDeleting.text;
368-
chat.lastMessageAuthor = lastMessageAfterDeleting.displayName;
369-
chat.lastMessageTime = lastMessageAfterDeleting.createdAt;
370+
if (lastMessageDeleted && newLastMessage) {
371+
chat.lastMessageId = newLastMessage.messageId;
372+
chat.lastMessageText = newLastMessage.text;
373+
chat.lastMessageAuthor = newLastMessage.displayName;
374+
chat.lastMessageTime = newLastMessage.createdAt;
370375

371376
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
372377
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
373-
return
378+
return;
374379
}
375-
else if (isDeleteMessageLast && !lastMessageAfterDeleting) {
376-
chat.lastMessageId = "";
377-
chat.lastMessageText = "";
378-
chat.lastMessageAuthor = "";
379-
chat.lastMessageTime = "";
380+
381+
if (lastMessageDeleted && !newLastMessage) {
382+
chat.lastMessageId = '';
383+
chat.lastMessageText = '';
384+
chat.lastMessageAuthor = '';
385+
chat.lastMessageTime = '';
380386

381387
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
382-
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$)
388+
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
383389
return;
384390
}
385391

@@ -516,37 +522,25 @@ export class ChatsComponent implements OnInit {
516522
}
517523

518524
private onMessageDeleteHandler(notification: DeleteMessageNotification) {
519-
const message = this.messages.find(x => x.messageId === notification.deletedMessageId);
520-
521-
if (message === undefined) return;
525+
const selfMessage = notification.userId === this.userId;
522526

523-
const isDeletedMessageSelf = message.userId === this.userId;
527+
if (selfMessage) return;
524528

525-
if (isDeletedMessageSelf) return;
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);
534+
}
526535

527536
const chatIndex = this.chats.findIndex((x) => x.chatId === notification.chatId);
528-
529-
const lastMessage = this.messages[this.messages.length - 1];
530-
const messageIndex = this.messages.findIndex(x => x.messageId === notification.deletedMessageId);
531-
const deletedMessage = this.messages.splice(messageIndex, 1)[0];
532-
const lastMessageAfterDeleting = this.messages[this.messages.length - 1];
533-
const isDeleteMessageLast = lastMessage.messageId === deletedMessage.messageId;
534537
const chat = this.chats[chatIndex];
535538

536-
if (isDeleteMessageLast && lastMessageAfterDeleting) {
537-
chat.lastMessageId = lastMessageAfterDeleting.messageId;
538-
chat.lastMessageText = lastMessageAfterDeleting.text;
539-
chat.lastMessageAuthor = lastMessageAfterDeleting.displayName;
540-
chat.lastMessageTime = lastMessageAfterDeleting.createdAt;
541-
542-
return
543-
}
544-
else if (isDeleteMessageLast && !lastMessageAfterDeleting) {
545-
chat.lastMessageId = "";
546-
chat.lastMessageText = "";
547-
chat.lastMessageAuthor = "";
548-
chat.lastMessageTime = "";
549-
539+
if (notification.isLastMessage) {
540+
chat.lastMessageId = notification.newLastMessageId;
541+
chat.lastMessageText = notification.newLastMessageText;
542+
chat.lastMessageAuthor = notification.newLastMessageDisplayName;
543+
chat.lastMessageTime = notification.newLastMessageTime;
550544
return;
551545
}
552546
}

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)