diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 34e4d4d54..9d6cc59ab 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -95,18 +95,22 @@ export class MessageService { } } - async send() { + async send(): Promise { this.sendAttempt++; if(!this.hasFile) { this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => { - if (environment.chatOffline) { - let ChatMessage = data.result + let ChatMessage = data.result + + if (environment.chatOffline) { this.redefinedMessage(this, ChatMessage) this.offline = false } + return new Promise((resolve, reject)=>{ + resolve(ChatMessage) + }) }) } else { @@ -125,21 +129,23 @@ export class MessageService { 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) - let ChatMessage = data.result + let ChatMessage = data.result + + if (environment.chatOffline) { this.redefinedMessage(this, ChatMessage) this.offline = false } + + return new Promise((resolve, reject)=>{ + resolve(ChatMessage) + }) }) } else if(this.WsChatService.isLogin == false) { - this.WsChatService.registerCallback({ type: 'reConnect', - funx:()=> { - this.send() - return true + funx: async ()=> { + return await this.send() } }) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 1b350b112..a4cc1e8f3 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -14,7 +14,8 @@ import { SortService } from '../functions/sort.service'; 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 { NfService } from 'src/app/services/chat/nf.service'; +import { v4 as uuidv4 } from 'uuid' @Injectable({ providedIn: 'root' @@ -113,7 +114,10 @@ export class RoomService { } this.addMessageDB(ChatMessage) - this.scrollDown() + + setTimeout(()=>{ + this.scrollDown() + }, 50) } ) @@ -164,6 +168,61 @@ export class RoomService { } + async updateMessageDB(ChatMessage, localReference) { + if (environment.chatOffline) { + this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + if(!Array.isArray(messages)) { + messages = [] + } + + let index; + const find = messages.find((message, _index)=> { + if(message.localReference) { + if(message.localReference == ChatMessage.localReference) { + index = _index + return true + } + } + + return false + }) + + if(find) { + messages[index] = ChatMessage + this.storage.set('chatmsg' + this.id, messages) + } + + }) + } + + } + + /** + * @description delete message in the DB. get all messages, delete then corresponding message and update the store + * @param id message ID + */ + private deleteMessageFromDb(id) { + 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) + }); + }) + } + } + async receiveMessageDelete() { this.WsChatService.updateRoomEventss( @@ -201,48 +260,30 @@ export class RoomService { } - /** - * @description delete message in the DB. get all messages, delete then corresponding message and update the store - * @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) - } - - }) - - this.storage.set('chatmsg' + this.id, messages).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - }); - }) - } - /** * @description sen text message */ async send({file = null, attachments = null, temporaryData = {}}) { + const localReference = uuidv4() let offlineChatMessage = { rid: this.id, msg: this.message, attachments, file, - temporaryData + temporaryData, + localReference } const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline) - message.send() + message.send().then((ChatMessage) => { + this.updateMessageDB(ChatMessage, localReference) + }) + if (environment.chatOffline) { + this.addMessageDB(offlineChatMessage) setTimeout(() => { this.scrollDown() @@ -349,6 +390,11 @@ export class RoomService { if(wewMessage.offline == false) { this.prepareMessage(ChatMessage) + } else { + const offlineMessage = this.prepareMessage(ChatMessage) + offlineMessage.send().then(()=>{ + this.updateMessageDB(ChatMessage, ChatMessage.localReference) + }) } }); @@ -425,11 +471,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/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index caecf5ccc..7aef0cae3 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -13,7 +13,7 @@ import { NativeNotificationService } from 'src/app/services/native-notification. import { SortService } from '../functions/sort.service'; import { chatUser } from 'src/app/models/chatMethod'; import { NfService } from 'src/app/services/chat/nf.service' - +import { ChangeProfileService } from '../change-profile.service'; @Injectable({ providedIn: 'root' }) @@ -42,18 +42,10 @@ export class WsChatMethodsService { private sortService: SortService, private ChatService: ChatService, private NfService: NfService, + private changeProfileService: ChangeProfileService, ) { - (async()=>{ - await this.restoreRooms() - await this.getAllRooms(); - this.subscribeToRoom() - - - // - await this.getUser() - this.getUserStatus() - - })() + + this.loadChat() this.WsChatService.registerCallback({ type: 'reConnect', @@ -64,7 +56,9 @@ export class WsChatMethodsService { */ this.subscribeToRoom() - this.currentRoom.loadHistory({forceUpdate: true}) + if(this.currentRoom) { + this.currentRoom.loadHistory({forceUpdate: true}) + } for (const id in this.dm) { this.dm[id].hasLoadHistory = false @@ -77,6 +71,12 @@ export class WsChatMethodsService { } }) + this.changeProfileService.registerCallback(()=>{ + this.clearChat() + this.ReLoadChat() + this.storage.remove('Rooms'); + }) + // this.WsChatService.registerCallback({ // type:'Onmessage', @@ -112,6 +112,36 @@ export class WsChatMethodsService { } + private loadChat() { + this.ReLoadChat() + } + + async ReLoadChat() { + await this.restoreRooms() + await this.getAllRooms(); + this.subscribeToRoom() + + + // + await this.getUser() + this.getUserStatus() + } + + clearChat() { + this.dm = {} + this.group = {} + this._dm = [] + this._group = [] + + this.loadingWholeList = false + + this.dmCount = 0; + this.groupCount = 0; + + this.currentRoom = null + this.users = [] + } + getRoomFromDb() { this.storage.get('Rooms').then((rooms) => { rooms.result.update.forEach((roomData: room) => { diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index c50d480f0..ddf514cf1 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -678,8 +678,6 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { } else { let messageStr = JSON.stringify(message) - console.log('send', messageStr) - this.socket.send(messageStr) } return requestId @@ -688,8 +686,6 @@ 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)