From be0bd474560dce831c0834307178a752bd8ac0ca Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Thu, 10 Feb 2022 15:23:37 +0100 Subject: [PATCH] improve --- src/app/services/chat/room.service.ts | 49 +++++++++++++++------------ src/environments/environment.ts | 2 +- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index a3908258b..6ed13e20e 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -174,15 +174,17 @@ export class RoomService { let index; const find = messages.find((message, _index)=> { - if(message.localReference == ChatMessage.localReference) { - index = _index - return true + if(message.localReference) { + if(message.localReference == ChatMessage.localReference) { + index = _index + return true + } } return false }) - if(!find) { + if(find) { messages[index] = ChatMessage this.storage.set('chatmsg' + this.id, messages) } @@ -197,23 +199,25 @@ export class RoomService { * @param id message ID */ private deleteMessageFromDb(id) { - this.storage.get('chatmsg' + this.id).then((messages: any = []) => { - if(!Array.isArray(messages)) { - messages = [] - } - - messages.forEach((message, index) => { - - if(message._id == id) { - messages.splice(index, 1) + if (environment.chatOffline) { + this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + if(!Array.isArray(messages)) { + messages = [] } - + + messages.forEach((message, index) => { + + if(message._id == id) { + messages.splice(index, 1) + } + + }) + + this.storage.set('chatmsg' + this.id, messages).then((value) => { + console.log('MSG SAVED ON STORAGE', value) + }); }) - - this.storage.set('chatmsg' + this.id, messages).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - }); - }) + } } async receiveMessageDelete() { @@ -384,7 +388,10 @@ export class RoomService { if(wewMessage.offline == false) { this.prepareMessage(ChatMessage) } else { - + const offlineMessage = this.prepareMessage(ChatMessage) + offlineMessage.send().then(()=>{ + this.updateMessageDB(ChatMessage, ChatMessage.localReference) + }) } }); @@ -461,11 +468,9 @@ export class RoomService { private fix_updatedAt(message) { if (message.result) { - //console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] } else if(message._updatedAt) { if(message._updatedAt.hasOwnProperty('$date')) { - // console.log('FIX UPDATE 11', message) message._updatedAt = message._updatedAt['$date'] } } diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 6bf93dabc..cd809f519 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -13,7 +13,7 @@ export const environment = { domain: 'gabinetedigital.local', //gabinetedigital.local defaultuser: 'paulo.pinto@gabinetedigital.local',//paulo.pinto paulo.pinto@gabinetedigital.local defaultuserpwd: 'tabteste@006', //tabteste@006, - chatOffline: false + chatOffline: true }; /*