diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index db5d0df49..34e4d4d54 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -39,7 +39,7 @@ export class MessageService { } setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData}:Message) { - this.customFields = customFields + this.customFields = customFields this.channels = channels || [] this.mentions = mentions || [] this.msg = msg || "" @@ -80,7 +80,7 @@ export class MessageService { const firstName = capitalizeTxt(username.split('.')[0]) const lastName = capitalizeTxt(username.split('.')[1]) return firstName + ' ' + lastName - } + } getFileFromDb() { @@ -116,14 +116,14 @@ export class MessageService { if(this.hasSendAttachment == false) { uploadSuccessfully = await this.NfService.beforeSendAttachment(this) } - + this.uploadingFile = false if(uploadSuccessfully || this.hasSendAttachment == false) { this.hasSendAttachment = true this.errorUploadingAttachment = false this.temporaryData = {} - + this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => { if (environment.chatOffline) { // console.log('send sucees', data.result) @@ -147,11 +147,11 @@ export class MessageService { this.errorUploadingAttachment = true } - + } } - redefinedMessage(messagem, ChatMessage) { + redefinedMessage(messagem, ChatMessage) { this.setData(ChatMessage) } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 11e1600c9..b19247335 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -24,7 +24,7 @@ export class RoomService { messages: MessageService[] = [] storageMessage: any[] = []; lastMessage: MessageService; - + customFields: any; id = '' t = '' @@ -112,10 +112,7 @@ export class RoomService { }); } - if(this.isSenderIsNotMe(ChatMessage)) { - // this.addMessageDB(ChatMessage) - } - + this.addMessageDB(ChatMessage) this.scrollDown() } @@ -137,17 +134,34 @@ export class RoomService { } - addMessageDB(ChatMessage) { - this.storage.get('chatmsg' + this.id).then((messages: any = []) => { - if(!Array.isArray(messages)) { - messages = [] - } + async addMessageDB(ChatMessage) { + if (environment.chatOffline) { + this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + if(!Array.isArray(messages)) { + messages = [] + } + + if(!ChatMessage._id && environment.chatOffline) { + + delete ChatMessage.temporaryData + messages.push(ChatMessage) + this.storage.set('chatmsg' + this.id, messages) + + } else { + const find = messages.find((message)=> { + return message._id == ChatMessage._id + }) + + if(!find) { + delete ChatMessage.temporaryData + messages.push(ChatMessage) + this.storage.set('chatmsg' + this.id, messages) + } + + } + }) + } - delete ChatMessage.temporaryData - messages.push(ChatMessage) - - this.storage.set('chatmsg' + this.id, messages) - }) } async receiveMessageDelete() { @@ -349,23 +363,22 @@ export class RoomService { } // runs onces only - async loadHistory({limit = 100, forceUpdate = false }) { + async loadHistory({limit = 50, forceUpdate = false }) { if(forceUpdate == false) { if (this.hasLoadHistory) { return false } } - - this.restoreMessageFromDB() + await this.restoreMessageFromDB() await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => { console.log('loadHistory', chatHistory) + await chatHistory.result.messages.reverse().forEach( async (message) => { this.prepareMessage(message) - this.messages = this.sortService.sortDate(this.messages, '_updatedAt') })