diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 1deff1038..34e4d4d54 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -5,6 +5,7 @@ import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text' import { NfService } from 'src/app/services/chat/nf.service' import { WsChatService } from 'src/app/services/chat/ws-chat.service'; +import { environment } from 'src/environments/environment'; @Injectable({ providedIn: 'root' }) @@ -30,6 +31,7 @@ export class MessageService { sendAttempt = 0 uploadingFile = false errorUploadingAttachment = false + loadHistory = false constructor(private storage: Storage, private NfService: NfService, @@ -98,14 +100,15 @@ export class MessageService { this.sendAttempt++; if(!this.hasFile) { - console.log('simple send') this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => { - let ChatMessage = data.result - this.redefinedMessage(this, ChatMessage) - this.offline = false + if (environment.chatOffline) { + let ChatMessage = data.result + this.redefinedMessage(this, ChatMessage) + this.offline = false + } + }) } else { - console.log('complex send') this.uploadingFile = true @@ -122,10 +125,12 @@ export class MessageService { this.temporaryData = {} this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => { - console.log('send sucees', data.result) - let ChatMessage = data.result - this.redefinedMessage(this, ChatMessage) - this.offline = false + if (environment.chatOffline) { + // console.log('send sucees', data.result) + let ChatMessage = data.result + this.redefinedMessage(this, ChatMessage) + this.offline = false + } }) } else if(this.WsChatService.isLogin == false) { @@ -133,7 +138,7 @@ export class MessageService { this.WsChatService.registerCallback({ type: 'reConnect', funx:()=> { - // this.send() + this.send() return true } }) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 6d50b2854..1b350b112 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -15,7 +15,7 @@ import { chatUser } from 'src/app/models/chatMethod'; import { environment } from 'src/environments/environment'; import { ChatService } from 'src/app/services/chat.service'; import { NfService } from 'src/app/services/chat/nf.service' -import alasql from 'alasql' + @Injectable({ providedIn: 'root' }) @@ -30,7 +30,7 @@ export class RoomService { t = '' name = '' _updatedAt = {} - private hasLoadHistory = false + hasLoadHistory = false duration = '' isTyping = false otherUserType = false @@ -67,15 +67,6 @@ export class RoomService { private NfService: NfService ) { this.NativeNotificationService.askForPermission() - - - this.WsChatService.registerCallback({ - type: 'reConnect', - funx: ()=>{ - this.hasLoadHistory = false - } - }) - } setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) { @@ -99,41 +90,30 @@ export class RoomService { this.WsChatService.updateRoomEventss( this.id, "stream-room-messages", - (ChatMessage) => { + (_ChatMessage) => { - setTimeout(() => { - ChatMessage = ChatMessage.fields.args[0] + let ChatMessage = _ChatMessage.fields.args[0] - ChatMessage = this.fix_updatedAt(ChatMessage) - // console.log('recivemessage', ChatMessage) + ChatMessage = this.fix_updatedAt(ChatMessage) + console.log('recivemessage', JSON.stringify(_ChatMessage)) - const messageIsFound = this.messages.find((message) => { - return message._id == ChatMessage._id - }) + const message = this.prepareMessage(ChatMessage) + this.lastMessage = message + this.calDateDuration(ChatMessage._updatedAt) - if(!messageIsFound) { - const message = this.prepareMessage(ChatMessage) + if (message.t == 'r') { + this.name = message.msg + } - this.lastMessage = message - if (message.t == 'r') { this.name = message.msg } - this.calDateDuration(ChatMessage._updatedAt) + if(this.isSenderIsNotMe(ChatMessage)) { + this.NativeNotificationService.sendNotificationChat({ + message: message.msg, + title: this.name + }); + } - setTimeout(() => { - this.scrollDown() - }, 100) - - this.NativeNotificationService.sendNotificationChat({ - message: message.msg, - title: this.name - }); - - - if(this.isSenderIsNotMe(ChatMessage)) { - this.addMessageDB(ChatMessage) - } - - } - }, 150) + this.addMessageDB(ChatMessage) + this.scrollDown() } ) @@ -154,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 = [] + } - delete ChatMessage.temporaryData - messages.push(ChatMessage) + 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) + } + + } + }) + } - this.storage.set('chatmsg' + this.id, messages) - }) } async receiveMessageDelete() { @@ -242,19 +239,19 @@ export class RoomService { temporaryData } - this.addMessageDB(offlineChatMessage) - const message: MessageService = this.prepareMessage(offlineChatMessage) - - setTimeout(() => { - this.scrollDown() - }, 150) - - this.lastMessage = message - + const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline) message.send() - this.calDateDuration(message._updatedAt) - this.sortRoomList() + if (environment.chatOffline) { + this.addMessageDB(offlineChatMessage) + setTimeout(() => { + this.scrollDown() + }, 150) + + this.lastMessage = message + this.calDateDuration(message._updatedAt) + this.sortRoomList() + } this.message= '' } @@ -340,49 +337,49 @@ export class RoomService { async restoreMessageFromDB() { - await this.storage.get('chatmsg' + this.id).then( async (messages = []) => { - if(!Array.isArray(messages)) { - messages = [] - } + if(environment.chatOffline) { - await messages.forEach( async (ChatMessage, index) => { - const wewMessage = this.prepareMessage(ChatMessage, false) - - if(wewMessage.offline == false) { - this.prepareMessage(ChatMessage) + await this.storage.get('chatmsg' + this.id).then( async (messages = []) => { + if(!Array.isArray(messages)) { + messages = [] } - }); + await messages.forEach( async (ChatMessage, index) => { + const wewMessage = this.prepareMessage(ChatMessage, false) - setTimeout(()=> { - this.scrollDown() - }, 50) + if(wewMessage.offline == false) { + this.prepareMessage(ChatMessage) + } - }) + }); + + setTimeout(()=> { + this.scrollDown() + }, 50) + + }) + + } } // runs onces only async loadHistory({limit = 50, forceUpdate = false }) { - if (this.hasLoadHistory || forceUpdate) { - return false + if(forceUpdate == false) { + if (this.hasLoadHistory) { + return false + } } - await this.restoreMessageFromDB() await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => { console.log('loadHistory', chatHistory) - this.messages = [] await chatHistory.result.messages.reverse().forEach( async (message) => { + this.prepareMessage(message) - - /* const wewMessage = this.prepareMessage(message, false) - - if(wewMessage.offline == false) { - this.prepareMessage(message) - } */ + this.messages = this.sortService.sortDate(this.messages, '_updatedAt') }) @@ -402,22 +399,24 @@ export class RoomService { message = this.fix_updatedAt(message) const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService) wewMessage.setData(message) + wewMessage.loadHistory = this.hasLoadHistory - if (save) { + if(!message._id && environment.chatOffline) { + this.messages.push(wewMessage) + return wewMessage + } + + const found = this.messages.find((MessageService) => { + return MessageService._id == message._id + }) + + if (save && !found) { this.messages.push(wewMessage) } return wewMessage } - async returnData(res) { - - return res; - } - - - ReactToMessage() { } - 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 a6940d40f..caecf5ccc 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -19,9 +19,8 @@ import { NfService } from 'src/app/services/chat/nf.service' }) export class WsChatMethodsService { - dm = {} - group = {} - + dm: {[key: string]: RoomService} = {} + group: {[key: string]: RoomService} = {} _dm = [] _group = [] @@ -31,8 +30,7 @@ export class WsChatMethodsService { dmCount = 0; groupCount = 0; - - currentRoom = null + currentRoom: RoomService = null users: chatUser[] = [] constructor( @@ -57,20 +55,23 @@ export class WsChatMethodsService { })() - - /** - * @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically, - * when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish. - */ this.WsChatService.registerCallback({ type: 'reConnect', funx: ()=>{ + /** + * @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically, + * when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish. + */ this.subscribeToRoom() - if(this.getDmRoom(this.currentRoom)) { - this.getDmRoom(this.currentRoom).loadHistory({forceUpdate: true}) - } else if(this.getGroupRoom(this.currentRoom)) { - this.getGroupRoom(this.currentRoom).loadHistory({forceUpdate: true}) + this.currentRoom.loadHistory({forceUpdate: true}) + + for (const id in this.dm) { + this.dm[id].hasLoadHistory = false + } + + for (const id in this.group) { + this.group[id].hasLoadHistory = false } } @@ -117,27 +118,22 @@ export class WsChatMethodsService { this.prepareRoom(roomData); }); }) - }; + } + openRoom(roomId) { if(this.currentRoom) { - if(this.getDmRoom(this.currentRoom)) { - this.getDmRoom(this.currentRoom).roomLeave() - } else if(this.getGroupRoom(this.currentRoom)) { - this.getGroupRoom(this.currentRoom).roomLeave() - } + this.currentRoom.roomLeave() } - this.currentRoom = roomId - - if(this.currentRoom) { - if(this.getDmRoom(this.currentRoom)) { - this.getDmRoom(this.currentRoom).open() - } else if(this.getGroupRoom(this.currentRoom)) { - this.getGroupRoom(this.currentRoom).open() - } + if(this.getDmRoom(roomId)) { + this.currentRoom = this.getDmRoom(roomId) + } else if(this.getGroupRoom(roomId)) { + this.currentRoom = this.getGroupRoom(roomId) } + this.currentRoom.open() + } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 2dc140a95..c50d480f0 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -677,6 +677,9 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { this.wsMsgQueue[requestId] = {message, requestId, loginRequired} } else { let messageStr = JSON.stringify(message) + + console.log('send', messageStr) + this.socket.send(messageStr) } return requestId @@ -685,6 +688,8 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { onmessage: async (event: any)=> { const data = JSON.parse(event.data) + console.log('onmessage', data) + for (const [key, value] of Object.entries(this.wsCallbacks)) { if(value.type== 'Onmessage') { const dontRepeat = await value.funx(data) diff --git a/src/environments/environment.ts b/src/environments/environment.ts index cd809f519..6bf93dabc 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: true + chatOffline: false }; /*