This commit is contained in:
Peter Maquiran
2024-08-14 15:29:16 +01:00
parent d7eb6a552b
commit ea4ca5c34c
19 changed files with 270 additions and 80 deletions
+62 -17
View File
@@ -167,17 +167,19 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
ngOnChanges(changes: SimpleChanges): void {
this.roomData$ = this.roomRepositoryService.getItemByIdLive(this.roomId)
this.getMessages()
this.listenToIncomingMessage()
this.listenToDeleteMessage()
this.getMessages();
this.listenToIncomingMessage();
this.listenToDeleteMessage();
this.listenToUpdateMessage();
this.listenToSendMessage()
this.listenToSendMessage();
this.roomMessage$ = this.messageRepositoryService.getItemsLive(this.roomId)
this.roomMembers$ = this.roomRepositoryService.getRoomMemberByIdLive(this.roomId) as any
this.roomStatus$ = this.roomRepositoryService.getRoomStatus(this.roomId)
this.roomRepositoryService.getRoomById(this.roomId)
this.messageRepositoryService.listAllMessagesByRoomId(this.roomId)
this.messageRepositoryService.listAllMessagesByRoomId(this.roomId).then(()=> {
// this.getMessages();
})
this.userTypingServiceRepository.getUserTypingLive().subscribe((e) => {
const arrayNames = e.map(e => e.userName)
@@ -190,44 +192,79 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
async getMessages() {
this.messages1[this.roomId] = []
let messages = await this.messageRepositoryService.getItems(this.roomId)
this.messages1[this.roomId].unshift(...messages)
this.messages1[this.roomId] = messages
this.messages1[this.roomId] = this.sortBySentAt(this.messages1[this.roomId])
this.loadAttachment()
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
}
async loadAttachment() {
for(const message of this.messages1[this.roomId]) {
if(message.hasAttachment) {
const result = await this.chatServiceService.getMessageAttachmentByMessageId({
$messageId: message.$id,
id: message.attachments[0].id
id: message.attachments[0].id
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
}
}
// Sorting function
sortBySentAt (arr, order = 'asc') {
return arr.sort((a, b) => {
// Handle null or undefined sentAt
const dateA = a.sentAt ? new Date(a.sentAt).getTime() : Number.MAX_VALUE;
const dateB = b.sentAt ? new Date(b.sentAt).getTime() : Number.MAX_VALUE;
if (order === 'asc') {
return dateA - dateB;
} else {
return dateB - dateA;
}
});
};
listenToIncomingMessage() {
this.messageReceiveSubject?.unsubscribe();
this.messageReceiveSubject = this.chatServiceService.listenToIncomingMessage(this.roomId).subscribe((message) => {
this.messageReceiveSubject = this.chatServiceService.listenToIncomingMessage(this.roomId).subscribe(async (message) => {
this.messages1[this.roomId].push(message as MessageEntity)
this.scrollToBottomClicked()
if(message.hasAttachment) {
const result = await this.chatServiceService.getMessageAttachmentByMessageId({
$messageId: message.$id,
id: message.attachments[0].id
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
});
}
@@ -531,6 +568,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const message = new MessageEntity();
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
@@ -601,6 +640,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.sentAt = new Date().toISOString()
this.textField = ''
this.messages1[this.roomId].push(message)
@@ -608,9 +648,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.scrollToBottomClicked()
}, 100)
const data = await this.chatServiceService.sendMessage(message)
console.log({data})
}
@@ -778,6 +815,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
wxUserId: SessionStore.user.UserId
}
message.sentAt = new Date().toISOString()
message.attachments = [{
file: compressedImage.value,
fileName: "foto",
@@ -826,6 +865,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
message.message = this.textField
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
@@ -876,6 +917,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const message = new MessageEntity();
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
@@ -931,6 +974,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const message = new MessageEntity();
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,