diff --git a/src/app/services/chat/chat-methods.service.ts b/src/app/services/chat/chat-methods.service.ts index d92cb2198..bcb927321 100644 --- a/src/app/services/chat/chat-methods.service.ts +++ b/src/app/services/chat/chat-methods.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { ChatService } from '../chat.service'; +import { v4 as uuidv4 } from 'uuid' @Injectable({ providedIn: 'root' @@ -17,7 +18,7 @@ export class ChatMethodsService { { "rid": roomId, "msg":"", - "file":{ + "file": { "type": "application/meeting", "subject": data.subject, "start_date": data.start, @@ -29,4 +30,21 @@ export class ChatMethodsService { } this.chatService.sendMessage(body).subscribe(res=> {}); } -} + + send({roomId, msg, attachments = null, file = null, localReference = null}) { + + let body = { + "message": + { + rid: roomId, + localReference: localReference, + msg: msg, + attachments, + file + } + } + + return this.chatService.sendMessage(body) + } + +} \ No newline at end of file diff --git a/src/app/services/chat/chat-storage.service.ts b/src/app/services/chat/chat-storage.service.ts index b0227f5f9..6044fd8af 100644 --- a/src/app/services/chat/chat-storage.service.ts +++ b/src/app/services/chat/chat-storage.service.ts @@ -11,11 +11,11 @@ export class ChatStorageService { private storage: Storage, ) { } - - /** + /** * @description delete message in the DB. get all messages, delete then corresponding message and update the store * @param id message ID */ + async deleteMessageFromDb(messageId, roomId) { if (environment.chatOffline) { await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => { @@ -118,7 +118,6 @@ export class ChatStorageService { } } - async addMessageDB(ChatMessage, roomId) { if (environment.chatOffline) { await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => { @@ -133,7 +132,7 @@ export class ChatStorageService { messages.push(ChatMessage) await this.storage.set('chatmsg' + roomId, messages) - console.log('add to DB', ChatMessage) + // console.log('add to DB', ChatMessage) } else { const find = messages.find((message)=> { @@ -144,12 +143,50 @@ export class ChatStorageService { delete ChatMessage.temporaryData messages.push(ChatMessage) await this.storage.set('chatmsg' + roomId, messages) - console.log('add to DB', ChatMessage) + // console.log('add to DB', ChatMessage) + } else { + console.log('duplicate') } } }) } } + + async addManyMessageDB(_ChatMessage: any[], roomId) { + if (environment.chatOffline) { + await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => { + if(!Array.isArray(messages)) { + messages = [] + } + + await _ChatMessage.forEach(async(ChatMessage)=>{ + if(!ChatMessage._id && environment.chatOffline) { + + delete ChatMessage.temporaryData + messages.push(ChatMessage) + + } else { + const find = messages.find((message)=> { + return message._id == ChatMessage._id + }) + + if(!find) { + delete ChatMessage.temporaryData + messages.push(ChatMessage) + + + } + + } + }) + + console.log('add to DB') + await this.storage.set('chatmsg' + roomId, messages) + + + }) + } + } } diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index c478ef8db..bccdbde0a 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -8,6 +8,8 @@ import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { environment } from 'src/environments/environment'; import { showDateDuration } from 'src/plugin/showDateDuration'; import { ChatStorageService } from './chat-storage.service' +import { ChatMethodsService } from './chat-methods.service' + @Injectable({ providedIn: 'root' @@ -51,7 +53,8 @@ export class MessageService { constructor(private storage: Storage, private NfService: NfService, private WsChatService: WsChatService, - private ChatStorageService: ChatStorageService) { + private ChatStorageService: ChatStorageService, + private ChatMethodsService: ChatMethodsService) { } setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData, localReference , viewed = [], received = []}:Message) { @@ -70,17 +73,9 @@ export class MessageService { this.temporaryData = temporaryData this.localReference = localReference || null - - if(this.msg.includes('***********')) { - console.log('-=-=--=-=-=',JSON.stringify(viewed), JSON.stringify(this.viewed)) - } - this.viewed = [...new Set([...viewed,...this.viewed])]; this.received = [...new Set([...received,...this.received])]; - if(this.msg.includes('***********')) { - console.log('-=-=--=-=-=',JSON.stringify(viewed), JSON.stringify(this.viewed)) - } if(!this.ts) { this.offline = true @@ -133,20 +128,37 @@ export class MessageService { this.sendAttempt++; if(!this.hasFile) { - this.WsChatService.send({roomId:this.rid, msg:this.msg, localReference: this.localReference}).then(({message, requestId}) => { - let ChatMessage = message.result - this.messageSend = true - this.redefinedMessage(ChatMessage) + const params = {roomId:this.rid, msg:this.msg, localReference: this.localReference} + + this.ChatMethodsService.send(params).subscribe( + (response: any) => { + const ChatMessage = response.message + this.messageSend = true + this.redefinedMessage(ChatMessage) + + }, + (error) => { + this.WsChatService.registerCallback({ + type: 'reConnect', + funx: async ()=> { + + this.WsChatService.send(params).then(({message, requestId}) => { + let ChatMessage = message.result + this.messageSend = true + this.redefinedMessage(ChatMessage) + + }) + + return true + } + }) + - if (environment.chatOffline) { - this.offline = false } + ) + - return new Promise((resolve, reject)=>{ - resolve(ChatMessage) - }) - }) } else { this.uploadingFile = true @@ -164,30 +176,43 @@ export class MessageService { this.temporaryData = {} - this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, localReference: this.localReference}).then(({message, requestId}) => { + const params = {roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, localReference: this.localReference} + this.ChatMethodsService.send(params).subscribe( + (response: any) => { + const ChatMessage = response.message + this.messageSend = true + this.redefinedMessage(ChatMessage) + + }, + (error) => { - let ChatMessage = message.result - this.messageSend = true - - this.redefinedMessage(ChatMessage) - - if (environment.chatOffline) { - this.offline = false + this.WsChatService.registerCallback({ + type: 'reConnect', + funx: async ()=> { + this.WsChatService.send(params).then(({message, requestId}) => { + let ChatMessage = message.result + this.messageSend = true + this.redefinedMessage(ChatMessage) + + }) + + return true + } + }) + } + ) - - return new Promise((resolve, reject)=>{ - resolve(ChatMessage) - }) - }) } else if(this.WsChatService.isLogin == false) { this.WsChatService.registerCallback({ type: 'reConnect', funx: async ()=> { - return await this.send() + this.send() + + return true } }) @@ -214,19 +239,8 @@ export class MessageService { reference = 'localReference' } - const message = this.getChatObj() - // const viewed = [...new Set([...ChatMessage.viewed,...this.viewed])]; - // const received = [...new Set([...ChatMessage.received,...this.received])]; - // if(ChatMessage.msg.includes('***********')) { - // console.log('redefinedMessage') - // console.log(JSON.stringify(ChatMessage)) - // console.log(JSON.stringify(message)) - // console.log(JSON.stringify(Object.assign(message, ChatMessage))) - - // } - ChatMessage = Object.assign(message, ChatMessage) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 10adaa6ec..0254b2432 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -17,7 +17,7 @@ import { ChatService } from 'src/app/services/chat.service'; import { NfService } from 'src/app/services/chat/nf.service'; import { v4 as uuidv4 } from 'uuid' import { ChatStorageService } from './chat-storage.service' - +import { ChatMethodsService } from './chat-methods.service' @Injectable({ providedIn: 'root' }) @@ -69,7 +69,8 @@ export class RoomService { private sortService: SortService, private chatService: ChatService, private NfService: NfService, - private ChatStorageService: ChatStorageService + private ChatStorageService: ChatStorageService, + private ChatMethodsService: ChatMethodsService ) { this.NativeNotificationService.askForPermission() @@ -104,14 +105,11 @@ export class RoomService { }) } } - - - }) } - setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService), _updatedAt }) { + setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService), _updatedAt }) { this.customFields = customFields this.id = id this.name = name @@ -368,7 +366,6 @@ export class RoomService { return JSON.parse(str); } - async restoreMessageFromDB() { if(environment.chatOffline) { @@ -377,25 +374,18 @@ export class RoomService { messages = [] } - // console.log('offline messages', messages) - await messages.forEach( async (ChatMessage, index) => { - - // if(ChatMessage.msg.includes('***********')) { - // console.log('restore ========') - // console.log(JSON.stringify(ChatMessage)) - // } - - const wewMessage = await this.prepareMessage({message:ChatMessage, save: false}) + const wewMessage = await this.simplePrepareMessage(ChatMessage) if(wewMessage.offline == false) { - this.prepareMessage(ChatMessage) + this.prepareMessage({message:ChatMessage}) } else { - const offlineMessage = await this.prepareMessage({message:ChatMessage, save: true}) + const offlineMessage = await this.prepareMessageCreateIfNotExist({message:ChatMessage}) this.messagesLocalReference.push(offlineMessage.localReference) - // offlineMessage.send() + offlineMessage.send() + } }); @@ -425,15 +415,39 @@ export class RoomService { await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => { - await chatHistory.result.messages.reverse().forEach( async (message) => { + // await chatHistory.result.messages.reverse().forEach( async (message) => {}) - // await this.prepareMessage({message}) + const messagesId = this.messages.map((message)=> message._id) + const createdMsgIds = [] + chatHistory.result.messages.reverse().forEach(async(message) => { + + if (!messagesId.includes(message._id)) { + createdMsgIds.push(message._id) + await this.prepareMessageCreateIfNotExist({message: message}); + } + + const messagesToSave = this.messages.filter((message)=> createdMsgIds.includes(message._id)).map((message)=>{ + return { + channels: message.channels, + mentions: message.mentions, + msg: message.msg, + rid: message.rid, + ts: message.ts, + u: message.u, + _id: message._id, + _updatedAt: message._updatedAt, + messageSend: message.messageSend, + offline: message.offline, + viewed: message.viewed, + received: message.received + } + }) + + await this.ChatStorageService.addManyMessageDB(messagesToSave, this.id) }) - // console.log('load history ',chatHistory) await this.readMessage(chatHistory) - //await this.updateAllMessages() this.storage.set('chatmsg' + this.id, chatHistory.result.messages) }) @@ -490,7 +504,6 @@ export class RoomService { this.updateAllMessages() } - updateAllMessages () { const newHistory = this.messages.map((message) => { return { @@ -522,11 +535,13 @@ export class RoomService { message = this.fix_updatedAt(message) - const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService) + const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService) wewMessage.setData(message) wewMessage.loadHistory = this.hasLoadHistory if(!message?._id && environment.chatOffline && save) { + + console.log('wewMessage.localReference ::', wewMessage.localReference) this.messages.push(wewMessage) return wewMessage } @@ -542,18 +557,51 @@ export class RoomService { } }) - if (save || !found) { - this.messages.push(wewMessage) - return wewMessage - } else{ - - if(redefined) { - } + if(save) { + if (!found) { + this.messages.push(wewMessage) + return wewMessage + } + } else if(foundIndex) { return this.messages[foundIndex] + } else { + return wewMessage } } + simplePrepareMessage(message) { + message = this.fix_updatedAt(message) + const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService) + wewMessage.setData(message) + wewMessage.loadHistory = this.hasLoadHistory + + return wewMessage + } + + async prepareMessageCreateIfNotExist({message}) { + message = this.fix_updatedAt(message) + + let foundIndex; + const found = this.messages.find((MessageService, index) => { + if (MessageService._id == message._id || + MessageService.localReference == message.localReference ) { + foundIndex = index + return true + } else { + return false + } + }) + + if (!found) { + const wewMessage = this.simplePrepareMessage(message) + this.messages.push(wewMessage) + return wewMessage + } else { + return this.messages[foundIndex] + } + } + private calDateDuration(date = null) { this.duration = showDateDuration(date || this._updatedAt); this._updatedAt = date || this._updatedAt diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 0e9075abc..f6107f7f8 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -17,6 +17,8 @@ import { ChangeProfileService } from '../change-profile.service'; import { UserSession } from 'src/app/models/user.model'; import { AuthService } from '../auth.service'; import { ChatStorageService } from './chat-storage.service' +import { ChatMethodsService } from './chat-methods.service' + @Injectable({ providedIn: 'root' @@ -51,7 +53,8 @@ export class WsChatMethodsService { private changeProfileService: ChangeProfileService, private chatService: ChatService, private authService: AuthService, - private ChatStorageService: ChatStorageService + private ChatStorageService: ChatStorageService, + private ChatMethodsService:ChatMethodsService ) { this.loggedUser = authService.ValidatedUserChat['data']; @@ -288,7 +291,7 @@ export class WsChatMethodsService { // create room if(!this.roomExist(roomId)) { - let room:RoomService = new RoomService(this.WsChatService, new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService , this.ChatStorageService) + let room:RoomService = new RoomService(this.WsChatService, new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService , this.ChatStorageService, this.ChatMethodsService) room.setData(setData) room.receiveMessage() room.getAllUsers = this.getUsers diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 5ebbd7551..83ac08c6f 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -54,10 +54,6 @@
{{msg.msg}} - - {{ msg.messageSend }} - {{ msg.received.length }} - {{ msg.viewed.length }}