diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 9ac1017ca..f53a86590 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -193,9 +193,10 @@ export class MessageService { } if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) { - + console.log('online send') this.WsChatService.send(params).then( (ChatMessage: any) => { + console.log('response') ChatMessage = ChatMessage.message.result this.messageSend = true @@ -204,6 +205,7 @@ export class MessageService { ) } else { + console.log("reconnect") this.WsChatService.registerCallback({ type: 'reConnect', funx: async ()=> { diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 1969b7d7f..d72ac551c 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -57,6 +57,15 @@ export class RoomService { chatOpen = false messageUnread = false + status = { + receive: { + message: false, + typing: false, + readMessage: false, + deleteMessage: false + } + } + scrollDown = () => { } /** @@ -128,6 +137,17 @@ export class RoomService { } }) + this.WsChatService.registerCallback({ + type: 'Offline', + 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.resetStatus(); + } + }) + } /** @@ -150,6 +170,18 @@ export class RoomService { } } + + resetStatus() { + this.status = { + receive: { + message: false, + typing: false, + readMessage: false, + deleteMessage: false + } + } + } + setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), _updatedAt }) { this.customFields = customFields this.id = id @@ -454,9 +486,14 @@ export class RoomService { //Get previous last message from room const previousLastMessage = this.messages.slice(-1)[0]; - this.lastMessage = previousLastMessage; - this.calDateDuration(previousLastMessage._updatedAt) - this.sortRoomList() + + if(previousLastMessage) { + this.lastMessage = previousLastMessage; + console.log("last message"+ previousLastMessage) + this.calDateDuration(previousLastMessage._updatedAt) + this.sortRoomList() + } + return true diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 246c85557..75af9067e 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -4,7 +4,7 @@ import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { MessageService } from 'src/app/services/chat/message.service'; import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text'; -import { Rooms, Update as room } from 'src/app/models/chatMethod'; +import { Update as room } from 'src/app/models/chatMethod'; import { Storage } from '@ionic/storage'; import { Platform } from '@ionic/angular'; import { SqliteService } from 'src/app/services/sqlite.service'; @@ -139,6 +139,14 @@ export class WsChatMethodsService { } + getRoomById(roomId) { + if(this.getDmRoom(roomId)) { + return this.getDmRoom(roomId) + } else if(this.getGroupRoom(roomId)) { + return this.getGroupRoom(roomId) + } + } + async restoreRooms() { try { @@ -167,10 +175,13 @@ export class WsChatMethodsService { try { await this.storage.remove('Rooms'); } catch(e) {} - - try { - await rooms.result.update.forEach( async (roomData: room, index) => { + console.log(rooms,'=========!+!+!++!+=====') + let index = 0 + + if(rooms?.result?.update) { + for (let roomData of rooms.result.update) { + const roomId = this.getRoomId(roomData); if(roomData.t == 'd') { @@ -203,16 +214,15 @@ export class WsChatMethodsService { } } - }); - - - await this.storage.set('Rooms', rooms); - - - } catch (e) { - console.log(e, "error"); + index++; + } } - + + + await this.storage.set('Rooms', rooms); + console.log('done get rooms') + + setTimeout(() => { this.sortRoomList() }, 1000) @@ -223,13 +233,12 @@ export class WsChatMethodsService { }, 10000) this.loadingWholeList = false - } /** * @description sort room list by last message date */ - sortRoomList =() => { + sortRoomList = () => { this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() @@ -240,6 +249,8 @@ export class WsChatMethodsService { */ subscribeToRoom() { + console.log('defaultSubtribe', this.dm) + for (const id in this.dm) { this.defaultSubtribe(id) } @@ -248,6 +259,8 @@ export class WsChatMethodsService { this.defaultSubtribe(id) } + console.log("Done subcribe"); + this.WsChatService.streamNotifyLogged().then((subscription=>{ })) @@ -274,14 +287,33 @@ export class WsChatMethodsService { * @param id room id */ private defaultSubtribe(id: any) { - this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + + console.log(id, this._dm); + + this.WsChatService.streamRoomMessages(id).then((subscription) => { + const room = this.getRoomById(id); + room.status.receive.message = true; + console.log(subscription, "subscription"); + }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false).then((subscription) => { + const room = this.getRoomById(id); + room.status.receive.typing = true; + console.log(subscription, "subStreamNotifyRoom"); // }) - this.WsChatService.subStreamNotifyRoom(id, 'typing', false) - this.WsChatService.subStreamNotifyRoom(id, 'readMessage', false) + this.WsChatService.subStreamNotifyRoom(id, 'readMessage', false).then((subscription) => { + const room = this.getRoomById(id); + room.status.receive.readMessage = true; + console.log(subscription, "subStreamNotifyRoom"); + // + }) - this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ + this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription) => { + const room = this.getRoomById(id); + room.status.receive.deleteMessage = true; + console.log(subscription); // }) } @@ -323,43 +355,46 @@ export class WsChatMethodsService { let roomId = this.getRoomId(roomData); - // create room - if(!this.roomExist(roomId)) { - let room:RoomService = new RoomService(this.WsChatService, new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService , this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this) - room.setData(setData) - room.receiveMessage() - room.getAllUsers = this.getUsers - room.receiveMessageDelete(); - room.sortRoomList = this.sortRoomList + if (setData.name != 'Rocket Cat') { + // create room + if(!this.roomExist(roomId)) { + let room:RoomService = new RoomService(this.WsChatService, new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService , this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this) + room.setData(setData) + room.receiveMessage() + room.getAllUsers = this.getUsers + room.receiveMessageDelete(); + room.sortRoomList = this.sortRoomList - // create individual room + // create individual room - if(this.isIndividual(roomData)) { + if(this.isIndividual(roomData)) { - this.dm[roomId] = room - this._dm.push(room) - // console.log(this._dm) - this.dmCount++ + this.dm[roomId] = room + this._dm.push(room) + // console.log(this._dm) + this.dmCount++ + } else { + // create group room + + this.group[roomId] = room + this._group.push(room) + this.groupCount++ + + } } else { - // create group room + // in this case room is already present, therefor it will only be necessary, + // to redefine - this.group[roomId] = room - this._group.push(room) - this.groupCount++ + if(this.dm[roomId]) { + this.dm[roomId].setData(setData) + } else if(this.group[roomId]) { + this.group[roomId].setData(setData) + } } - } else { - // in this case room is already present, therefor it will only be necessary, - // to redefine - - if(this.dm[roomId]) { - this.dm[roomId].setData(setData) - } else if(this.group[roomId]) { - this.group[roomId].setData(setData) - } - } + } @@ -486,7 +521,7 @@ export class WsChatMethodsService { } catch(e) {} } - getRoomName(roomData: room) { + getRoomName(roomData: room): string { if(this.isIndividual(roomData)) { const names: String[] = roomData.usernames const roomName = names.filter((name, index)=>{ diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 2bc758e80..82409ca57 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -38,6 +38,7 @@ export class WsChatService { key:'ping-pong', funx:(message: any) => { if(message.msg == "ping") { + // console.log(message) this.ws.send({message:{msg:"pong"}, loginRequired: false}) } } @@ -552,7 +553,7 @@ export class WsChatService { streamRoomMessages(roomId : string) { - const requestId = uuidv4() + const requestId = "streamRoomMessages"+uuidv4() const message = { "msg": "sub", @@ -567,8 +568,10 @@ export class WsChatService { this.ws.send({message, requestId}); return new Promise((resolve, reject) => { - this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId) { // same request send + this.ws.registerCallback({type:'Onmessage', funx:(message) => { + + // console.log(); + if(message.id == requestId || message?.subs?.[0] == requestId) { // same request send resolve(message) return true } @@ -690,7 +693,7 @@ export class WsChatService { onopen: async ()=> { this.ws.connected = true - + console.log("Open connection =====================================================") setTimeout(()=>{ @@ -739,7 +742,7 @@ export class WsChatService { } else { let messageStr = JSON.stringify(message) // - + console.log(messageStr) this.socket.send(messageStr) } return requestId @@ -748,8 +751,12 @@ export class WsChatService { onmessage: async (event: any)=> { const data = JSON.parse(event.data) - // - // console.log(data) + console.log(data); + + if(data?.fields?.args?.[0]?.msg?.includes("hello")) { + console.log(data); + alert("message"); + } for (const [key, value] of Object.entries(this.wsCallbacks)) { if(value.type== 'Onmessage') { @@ -766,12 +773,26 @@ export class WsChatService { }, - onclose:(event: any)=> { + onclose: async (event: any)=> { this.ws.connected = false this.isLogin = false this.connect() this.login() + console.log("close connection ==============================="); + + for (const [key, value] of Object.entries(this.wsCallbacks)) { + if(value.type == 'Offline') { + const dontRepeat = await value.funx() + + if(dontRepeat) { + delete this.wsCallbacks[key] + this.n-- + } + + } + + } }, diff --git a/src/app/shared/popover/chat-debugging/chat-debugging.page.html b/src/app/shared/popover/chat-debugging/chat-debugging.page.html index 4154694b2..99ec48fd1 100644 --- a/src/app/shared/popover/chat-debugging/chat-debugging.page.html +++ b/src/app/shared/popover/chat-debugging/chat-debugging.page.html @@ -4,6 +4,6 @@
  • Chat user list