From 202e09231bc2ab17745a9f74d68d54f0854a11c8 Mon Sep 17 00:00:00 2001 From: "tiago.kayaya" Date: Fri, 28 Jan 2022 17:34:27 +0100 Subject: [PATCH] save --- src/app/services/chat/room.service.ts | 21 ++++++++++++------- .../services/chat/ws-chat-methods.service.ts | 14 ++++++++----- src/app/services/functions/object.service.ts | 4 ++-- src/app/services/functions/sort.service.ts | 6 +++++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 868443e7c..0bebeeff5 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -48,7 +48,8 @@ export class RoomService { private platform: Platform, private sqlservice: SqliteService, private NativeNotificationService: NativeNotificationService, - ) { + private sortService: SortService, + ) { this.NativeNotificationService.askForPermission() } @@ -90,11 +91,11 @@ export class RoomService { if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { this.NativeNotificationService.sendNotificationChat({ - message: message.msg, + message: message.msg, title: this.name }); } - + // save to ionic storage this.storage.get('chatmsg' + this.id).then((messages: any) => { const newListMessages = messages.push(ChatMessage) @@ -110,7 +111,7 @@ export class RoomService { this.WsChatService.receiveStreamNotifyRoom((message) => { - + console.log(message.fields) if(message.fields.eventName == this.id+'/'+'typing') { @@ -167,7 +168,7 @@ export class RoomService { const lastIsTyping = this.isTyping if(text.length >= 1) { - this.isTyping = true + this.isTyping = true } else { this.isTyping = false } @@ -175,7 +176,7 @@ export class RoomService { if(lastIsTyping != this.isTyping) { this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping) } - + this.lastMessageTxt = this.message this.typingWatch() @@ -187,7 +188,7 @@ export class RoomService { const now = new Date().getTime() if((now - this.lastTimeType) >= 2888) { - + if(this.isTyping == true) { this.isTyping = false this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping) @@ -291,9 +292,10 @@ export class RoomService { this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => { console.log('loadHistory', chatHistory) - + let localMessages = [] + //const sortedRoomList = this.sortService.sortDate(chatHistory.result.messages, "_updatedAt.$date") chatHistory.result.messages.reverse().forEach(message => { message = this.fix_updatedAt(message) @@ -304,6 +306,9 @@ export class RoomService { this.messages = localMessages + console.log(chatHistory.result.messages); + + this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse()) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 153579fdc..1a16e17ad 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -45,9 +45,15 @@ export class WsChatMethodsService { const rooms = await this.WsChatService.getRooms(); - // console.log("ROOMS" + JSON.stringify(rooms)) + const sortedRoomList = this.sortService.sortDate(rooms.result.update, "_updatedAt.$date") - rooms.result.update.forEach((roomData: room) => { + console.log(rooms.result.update); + console.log(sortedRoomList); + + + //console.log("ROOMS" + JSON.stringify(rooms)) + + sortedRoomList.forEach((roomData: room) => { this.prepareRoom(roomData); }); @@ -106,7 +112,7 @@ export class WsChatMethodsService { prepareRoom(roomData){ let room:RoomService; - room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService) + room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService) room.setData({ customFields: roomData.customFields, @@ -123,8 +129,6 @@ export class WsChatMethodsService { let roomId = this.getRoomId(roomData) if(this.isIndividual(roomData)) { - console.log(room); - this.dm[roomId] = room this.dmCount++ } else { diff --git a/src/app/services/functions/object.service.ts b/src/app/services/functions/object.service.ts index 9b52713c8..562d28868 100644 --- a/src/app/services/functions/object.service.ts +++ b/src/app/services/functions/object.service.ts @@ -8,11 +8,11 @@ export class ObjectService { constructor() { } - deepFind(obj, path) { + deepFind(obj, path):any { var paths = path.split('.') , current = obj , i; - + for (i = 0; i < paths.length; ++i) { if (current[paths[i]] == undefined) { return undefined; diff --git a/src/app/services/functions/sort.service.ts b/src/app/services/functions/sort.service.ts index dc4c4b84f..5c35022f8 100644 --- a/src/app/services/functions/sort.service.ts +++ b/src/app/services/functions/sort.service.ts @@ -27,7 +27,11 @@ export class SortService { sortDate(array = [], path: string) { return array.sort( (a,b)=> { - return new Date(this.ObjectService.deepFind(b, path)).getTime() - new Date(this.ObjectService.deepFind(a, path)).getTime() + console.log("AAA"+new Date(this.ObjectService.deepFind(a, path))); + console.log("BB"+new Date(this.ObjectService.deepFind(b, path))); + //return (new Date(this.ObjectService.deepFind(a, path)) < new Date(this.ObjectService.deepFind(b, path))) ? -1 : ((new Date(this.ObjectService.deepFind(a, path)) > new Date(this.ObjectService.deepFind(b, path))) ? 1 : 0); + return new Date(this.ObjectService.deepFind(b, path)).getTime() - new Date(this.ObjectService.deepFind(a, path)).getTime(); + }) } }