From ed77e1aea76eaee1517ddb13e0e671d7f0b628f7 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:21:46 +0100 Subject: [PATCH 01/14] merge --- src/app/models/chatMethod.ts | 206 ++++++++++++++++++ src/app/models/rochet-chat-cliente-service.ts | 1 + .../group-messages/group-messages.page.ts | 3 +- src/app/pages/chat/messages/messages.page.ts | 3 +- .../app-process-status.service.spec.ts | 16 ++ .../services/app-process-status.service.ts | 25 +++ src/app/services/chat/message.service.ts | 2 +- src/app/services/chat/room.service.ts | 20 +- .../services/chat/ws-chat-methods.service.ts | 103 +++++++++ src/app/services/chat/ws-chat.service.ts | 15 +- .../services/native-notification.service.ts | 4 +- .../group-messages/group-messages.page.ts | 3 +- src/app/shared/chat/messages/messages.page.ts | 14 +- src/plugin/array.js | 6 + 14 files changed, 402 insertions(+), 19 deletions(-) create mode 100644 src/app/services/app-process-status.service.spec.ts create mode 100644 src/app/services/app-process-status.service.ts create mode 100644 src/plugin/array.js diff --git a/src/app/models/chatMethod.ts b/src/app/models/chatMethod.ts index 3a090d813..99d41f39d 100644 --- a/src/app/models/chatMethod.ts +++ b/src/app/models/chatMethod.ts @@ -167,3 +167,209 @@ export interface chatHistory { unreadNotLoaded: number; }; } + + + + +interface Ts { + $date: any; +} + +interface U { + _id: string; + username: string; + name: string; +} + +interface UpdatedAt { + $date: any; +} + +interface Attachment { + ts: Date; + title_link_download: boolean; +} + +export interface File { + type: string; + guid: string; + image_url: string; + subject: string; + start_date?: Date; + end_date?: Date; + venue: string; + id: string; +} + +interface EditedAt { + $date: number; +} + +interface EditedBy { + _id: string; + username: string; +} + + + +interface Ts2 { + $date: number; +} + +interface U2 { + _id: string; + username: string; + name: string; +} + +interface UpdatedAt2 { + $date: number; +} + +interface FirstUnread { + _id: string; + rid: string; + msg: string; + ts: Ts2; + u: U2; + _updatedAt: UpdatedAt2; + mentions: any[]; + channels: any[]; +} + +export interface Message { + customFields:any; + _id: string; + rid: string; + msg: string; + ts: Ts; + u: U; + t: string; + _updatedAt: UpdatedAt; + mentions: any[]; + channels: any[]; + attachments: Attachment[]; + file: File; + editedAt: EditedAt; + editedBy: EditedBy; + urls: any[]; +} + + + + +export interface Lm { + $date: any; +} + + + + +export interface LastMessage { + _id: string; + rid: string; + msg: string; + ts: Ts; + u: U; + _updatedAt: UpdatedAt2; + mentions: any[]; + channels: any[]; + file: File; + attachments: Attachment[]; +} + +export interface CustomFields { +} + +export interface Update { + _id: string; + t: string; + usernames: string[]; + usersCount: number; + uids: string[]; + default: boolean; + ro: boolean; + sysMes: boolean; + _updatedAt: UpdatedAt; + lm: Lm; + lastMessage: LastMessage; + name: string; + fname: string; + u: U2; + customFields: CustomFields; +} + +export interface DeletedAt { + $date: any; +} + +export interface Remove { + _id: string; + _deletedAt: DeletedAt; +} + +export interface Result { + update: Update[]; + remove: Remove[]; +} + +export interface Rooms { + msg: string; + id: string; + result: Result; +} + + + +export interface ChatMessage { + msg: string; + id: string; + result: Message +} + + + + + +export interface chatHistory { + msg: string; + id: string; + result: { + messages: Message[]; + firstUnread: FirstUnread; + unreadNotLoaded: number; + }; +} + +export interface chatUser { + _id: string; + createdAt: Date; + emails: { + address: string; + verified: boolean; + } + type: string; + status: string; + active: boolean; + _updatedAt: Date; + roles: string[]; + name: string; + lastLogin: Date; + statusConnection: string; + utcOffset: number; + username: string; + __rooms: string[]; + requirePasswordChange?: boolean; + settings: { + preferences: { + language: string; + }; + }; + nickname: string; + statusText: string; + banners: any; + statusDefault: string; + language: string; + avatarOrigin: string; + avatarETag?: any; +} \ No newline at end of file diff --git a/src/app/models/rochet-chat-cliente-service.ts b/src/app/models/rochet-chat-cliente-service.ts index 36b10321b..46698ca58 100644 --- a/src/app/models/rochet-chat-cliente-service.ts +++ b/src/app/models/rochet-chat-cliente-service.ts @@ -4,6 +4,7 @@ export interface wsCallbacksParams { runOnces?: boolean requestId?: string key?: string + order?: number } diff --git a/src/app/pages/chat/group-messages/group-messages.page.ts b/src/app/pages/chat/group-messages/group-messages.page.ts index 0a9b8d3a7..dcbbc9423 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -101,7 +101,8 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked; - + this.wsChatMethodsService.openRoom(this.roomId) + setTimeout(()=>{ this.scrollToBottomClicked() }, 50) diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index d177f5bc2..78e4f6f51 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -114,7 +114,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory() this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked - + this.wsChatMethodsService.openRoom(this.roomId) + setTimeout(()=>{ this.scrollToBottomClicked() }, 150) diff --git a/src/app/services/app-process-status.service.spec.ts b/src/app/services/app-process-status.service.spec.ts new file mode 100644 index 000000000..7c629ccb0 --- /dev/null +++ b/src/app/services/app-process-status.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AppProcessStatusService } from './app-process-status.service'; + +describe('AppProcessStatusService', () => { + let service: AppProcessStatusService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AppProcessStatusService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/app-process-status.service.ts b/src/app/services/app-process-status.service.ts new file mode 100644 index 000000000..8f66b538b --- /dev/null +++ b/src/app/services/app-process-status.service.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class AppProcessStatusService { + + status: 'resume' | 'pause' = 'resume' + + constructor() { + this.events() + } + + private events() { + document.addEventListener('pause', () => { + // console.log('App going to background'); + this.status = 'pause' + }); + + document.addEventListener('resume', () => { + // console.log('App going to background'); + this.status = 'resume' + }); + } +} diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 02ea997be..bde3ef601 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -42,7 +42,7 @@ export class MessageService { if (this.file) { if (this.file.guid) { this.storage.get(this.file.guid).then((image) => { - console.log('IMAGE FROM STORAGE', image) + // console.log('IMAGE FROM STORAGE', image) this.file.image_url = image }); } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index b39259603..4e85074ae 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -12,6 +12,8 @@ import { NativeNotificationService } from 'src/app/services/native-notification. import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text' import { SortService } from '../functions/sort.service'; +import { chatUser } from 'src/app/models/chatMethod'; + @Injectable({ providedIn: 'root' }) @@ -40,6 +42,9 @@ export class RoomService { mgsArray = []; scrollDown = () => { } + getAllUsers = (): chatUser[] => { + return [] + } constructor( public WsChatService: WsChatService, @@ -198,6 +203,19 @@ export class RoomService { }, 3000) } + private setTypingOff() { + this.typing('') + } + + roomLeave() { + this.setTypingOff() + } + + open() { + // this.typing(this.message) + } + + leave(rid?) { this.WsChatService.leaveRoom(this.id) } @@ -437,7 +455,7 @@ export class RoomService { console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] } else { - console.log('FIX UPDATE 11', message) + // console.log('FIX UPDATE 11', message) message._updatedAt = message._updatedAt['$date'] } return message diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 1950bb692..d2e806d88 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -10,6 +10,7 @@ import { Platform } from '@ionic/angular'; import { SqliteService } from 'src/app/services/sqlite.service'; import { NativeNotificationService } from 'src/app/services/native-notification.service'; import { SortService } from '../functions/sort.service'; +import { chatUser } from 'src/app/models/chatMethod'; @Injectable({ providedIn: 'root' @@ -28,6 +29,10 @@ export class WsChatMethodsService { dmCount = 0; groupCount = 0; + + currentRoom = null + users: chatUser[] = [] + constructor( private WsChatService: WsChatService, private storage: Storage, @@ -40,10 +45,37 @@ export class WsChatMethodsService { await this.getAllRooms(); this.subscribeToRoom() + + // + await this.getUser() + this.getUserStatus() + })() } + 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 = 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() + } + } + + } + async getAllRooms () { this.loadingWholeList = true @@ -140,6 +172,7 @@ export class WsChatMethodsService { } + prepareRoom(roomData) { let room:RoomService; @@ -155,6 +188,7 @@ export class WsChatMethodsService { }) room.receiveMessage() + room.getAllUsers = this.getUsers room.receiveMessageDelete(); let roomId = this.getRoomId(roomData) @@ -170,6 +204,51 @@ export class WsChatMethodsService { } } + + + getReceptorName(roomData) { + try { + return roomData.usernames.find((e)=> e != SessionStore.user.RochetChatUser) + } catch(e) { + return '*' + } + } + + getUserStatus(id?:string) { + + this.WsChatService.getUserStatus((d) => { + + const username = d.fields.args[0][1] + const statusNum = d.fields.args[0][2] + + console.log('d', d) + + const statusText = this.statusNumberToText(statusNum) + this.getUserByName(username).status = statusText + }) + + } + + getUserByName(username) { + return this.users.find((user)=> user.username == username) + } + + statusNumberToText(text) { + if(text == '0') { + return "offline" + } + else if(text == '1') { + return "online" + } + else if(text == '2') { + return "away" + } + else if(text == '3') { + return "busy" + } + } + + deleteMessage(id?) { return this.WsChatService.deleteMessage(id); } @@ -229,4 +308,28 @@ export class WsChatMethodsService { return !roomData.fname } + getUsers = () =>{ + return this.users + } + + async getUser() { + + // let _res = await this.chatService.getAllUsers().toPromise() + + // let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser); + + // user = user.sort((a,b) => { + // if(a.name < b.name) { + // return -1; + // } + // if(a.name > b.name) { + // return 1; + // } + // return 0; + // }); + + // this.users = user + } + + } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 724b9cce1..586035907 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -204,7 +204,7 @@ export class WsChatService { }); } - hidingRoom(roomId){ + hidingRoom(roomId) { const requestId = uuidv4() @@ -350,6 +350,19 @@ export class WsChatService { }}) } + + getUserStatus(funx:Function){ + this.ws.registerCallback({ + type:'Onmessage', + funx:(message)=>{ + if(message.msg == 'changed' && message.collection == "stream-notify-logged") { + funx(message) + } + } + }) + } + + loadHistory(roomId, limit: number = 50) { const requestId = uuidv4() diff --git a/src/app/services/native-notification.service.ts b/src/app/services/native-notification.service.ts index dea860b50..4cf54ba31 100644 --- a/src/app/services/native-notification.service.ts +++ b/src/app/services/native-notification.service.ts @@ -18,9 +18,9 @@ export class NativeNotificationService { LocalNotifications.requestPermissions() LocalNotifications.checkPermissions().then((data)=>{ - console.log('success', data) + // console.log('success', data) }).catch((data)=>{ - console.log('error', data) + // console.log('error', data) }) } diff --git a/src/app/shared/chat/group-messages/group-messages.page.ts b/src/app/shared/chat/group-messages/group-messages.page.ts index b14ec33ec..52558f953 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -112,7 +112,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe //this.scrollToBottom(); this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); this.wsChatMethodsService.getGroupRoom(this.roomId).getMsgFromDB(); - console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); + // console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); + this.wsChatMethodsService.openRoom(this.roomId) this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked setTimeout(()=>{ diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index dd5037409..f799c6d64 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -100,21 +100,13 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } ngOnChanges(changes: SimpleChanges): void { this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory() - //this.wsChatMethodsService.getDmRoom(this.roomId).getMsgFromDB() + this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked - /* console.log('DATATATA', this.wsChatMethodsService.getDmRoom(this.roomId).massages) - this.wsChatMethodsService.getDmRoom(this.roomId).massages.forEach((element) => { - console.log('DATATATA 11', element) - }) */ - - - //this.transformData(this.wsChatMethodsService.getDmRoom(this.roomId).massages) - //this.getMessageDB() - + this.wsChatMethodsService.openRoom(this.roomId) setTimeout(() => { this.scrollToBottomClicked() - }, 50) + }, 150) } diff --git a/src/plugin/array.js b/src/plugin/array.js new file mode 100644 index 000000000..a972f8dda --- /dev/null +++ b/src/plugin/array.js @@ -0,0 +1,6 @@ +// https://stackoverflow.com/a/68628927/14115342 + +const ArrayToObject = (array = [], getKey) => array.reduce((obj, cur) => { + const key = getKey(cur); + return (Object.assign(Object.assign({}, obj), { [key]: cur })); +}, {}); \ No newline at end of file From 2dfb1d6b979e004ccebfefeaf8a53beaa78034e4 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:24:46 +0100 Subject: [PATCH 02/14] add --- .../services/chat/ws-chat-methods.service.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index d2e806d88..cd96d15e9 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -8,6 +8,7 @@ import { Rooms, 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'; +import { ChatService } from 'src/app/services/chat.service'; import { NativeNotificationService } from 'src/app/services/native-notification.service'; import { SortService } from '../functions/sort.service'; import { chatUser } from 'src/app/models/chatMethod'; @@ -39,7 +40,8 @@ export class WsChatMethodsService { private platform: Platform, private sqlservice: SqliteService, private NativeNotificationService: NativeNotificationService, - private sortService: SortService + private sortService: SortService, + private ChatService: ChatService ) { (async()=>{ await this.getAllRooms(); @@ -314,21 +316,21 @@ export class WsChatMethodsService { async getUser() { - // let _res = await this.chatService.getAllUsers().toPromise() + let _res = await this.ChatService.getAllUsers().toPromise() - // let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser); + let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser); - // user = user.sort((a,b) => { - // if(a.name < b.name) { - // return -1; - // } - // if(a.name > b.name) { - // return 1; - // } - // return 0; - // }); + user = user.sort((a,b) => { + if(a.name < b.name) { + return -1; + } + if(a.name > b.name) { + return 1; + } + return 0; + }); - // this.users = user + this.users = user } From 009c1f3747b40820eb9f570e0c394eaf5a6232aa Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:33:32 +0100 Subject: [PATCH 03/14] user contact --- .../pages/chat/messages/contacts/contacts.page.html | 2 +- src/app/pages/chat/messages/contacts/contacts.page.ts | 4 +++- src/app/services/chat/ws-chat-methods.service.ts | 10 +++++++--- .../shared/chat/messages/contacts/contacts.page.html | 2 +- src/app/shared/chat/messages/contacts/contacts.page.ts | 4 +++- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/app/pages/chat/messages/contacts/contacts.page.html b/src/app/pages/chat/messages/contacts/contacts.page.html index 5dc48c889..3ac1d542e 100644 --- a/src/app/pages/chat/messages/contacts/contacts.page.html +++ b/src/app/pages/chat/messages/contacts/contacts.page.html @@ -29,7 +29,7 @@
- +
{{header}} diff --git a/src/app/pages/chat/messages/contacts/contacts.page.ts b/src/app/pages/chat/messages/contacts/contacts.page.ts index a8dc89df4..8c23c93dd 100644 --- a/src/app/pages/chat/messages/contacts/contacts.page.ts +++ b/src/app/pages/chat/messages/contacts/contacts.page.ts @@ -6,6 +6,7 @@ import { ChatService } from 'src/app/services/chat.service'; import { GroupMessagesPage } from '../../group-messages/group-messages.page'; import { MessagesPage } from '../messages.page'; import { ThemeService } from 'src/app/services/theme.service' +import { WsChatMethodsService} from 'src/app/services/chat/ws-chat-methods.service' @Component({ selector: 'app-contacts', @@ -29,7 +30,8 @@ export class ContactsPage implements OnInit { private http: HttpClient, private chatService: ChatService, private authService: AuthService, - public ThemeService: ThemeService + public ThemeService: ThemeService, + public WsChatMethodsService: WsChatMethodsService ) { this.loggedUser = authService.ValidatedUserChat['data']; diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index cd96d15e9..fae2adf08 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -222,11 +222,15 @@ export class WsChatMethodsService { const username = d.fields.args[0][1] const statusNum = d.fields.args[0][2] - - console.log('d', d) const statusText = this.statusNumberToText(statusNum) - this.getUserByName(username).status = statusText + + const user = this.getUserByName(username) + + if(user) { + user.status = statusText + } + }) } diff --git a/src/app/shared/chat/messages/contacts/contacts.page.html b/src/app/shared/chat/messages/contacts/contacts.page.html index be0ccfea1..ba60cce1e 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.html +++ b/src/app/shared/chat/messages/contacts/contacts.page.html @@ -29,7 +29,7 @@
- +
{{header}} diff --git a/src/app/shared/chat/messages/contacts/contacts.page.ts b/src/app/shared/chat/messages/contacts/contacts.page.ts index fdfbefb5a..2ac2537ca 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.ts +++ b/src/app/shared/chat/messages/contacts/contacts.page.ts @@ -6,6 +6,7 @@ import { ChatService } from 'src/app/services/chat.service'; import { GroupMessagesPage } from '../../group-messages/group-messages.page'; import { MessagesPage } from '../messages.page'; import { ThemeService } from 'src/app/services/theme.service' +import { WsChatMethodsService} from 'src/app/services/chat/ws-chat-methods.service' @Component({ selector: 'app-contacts', @@ -31,7 +32,8 @@ export class ContactsPage implements OnInit { private http: HttpClient, private chatService: ChatService, private authService: AuthService, - public ThemeService: ThemeService + public ThemeService: ThemeService, + public WsChatMethodsService: WsChatMethodsService ) { this.loggedUser = authService.ValidatedUserChat['data']; From 64cebb66915f185da747b75cf413cbf6ba056c0b Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:39:22 +0100 Subject: [PATCH 04/14] add typing --- src/app/services/chat/ws-chat-methods.service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index fae2adf08..ba9068d47 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -168,6 +168,8 @@ export class WsChatMethodsService { console.log('streamNotifyRoomDeleteMessage', subscription); }) + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) + this.prepareRoom(roomData); this.getGroupRoom(id).loadHistory(); From c00b6e444c11ae492a99c424eea1b02dd2fe50da Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:44:20 +0100 Subject: [PATCH 05/14] improve code --- .../services/chat/ws-chat-methods.service.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index ba9068d47..ebb3792a3 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -91,39 +91,35 @@ export class WsChatMethodsService { if(message.fields.args[0].rid) { setTimeout(()=>{ - console.log('sort this._dm', this._dm) - this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() - this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + this.sortRoomList() + }, 100) - } - } if(message.msg =='changed' && message.collection == "stream-notify-room") { if(message.fields.eventName.includes('deleteMessage')){ setTimeout(()=>{ - console.log('sort this._dm', this._dm) - this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() - this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + + this.sortRoomList() }, 100) } } } }) - await rooms.result.update.forEach( async (roomData: room) => { await this.prepareRoom(roomData); }); - this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() - this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() - + this.sortRoomList() this.loadingWholeList = false } - + sortRoomList() { + this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() + this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + } subscribeToRoom() { From a2b435b492582f0dcf5fa7044594f906df8c0a1d Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 19:54:38 +0100 Subject: [PATCH 06/14] improve --- .../services/chat/ws-chat-methods.service.ts | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index ebb3792a3..eca8ae55b 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -124,26 +124,13 @@ export class WsChatMethodsService { subscribeToRoom() { for (const id in this.dm) { - this.WsChatService.streamRoomMessages(id).then((subscription)=>{ - console.log('streamRoomMessages', subscription) - }) - - this.WsChatService.subStreamNotifyRoom(id, 'typing', false) - this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ - console.log('streamNotifyRoomDeleteMessage', subscription); - }) + this.defaultSubtribe(id) } for (const id in this.group) { - this.WsChatService.streamRoomMessages(id).then((subscription)=>{ - console.log('streamRoomMessages', subscription) - }) - - this.WsChatService.subStreamNotifyRoom(id, 'typing', false) - this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ - console.log('streamNotifyRoomDeleteMessage', subscription); - }) + this.defaultSubtribe(id) } + this.WsChatService.streamNotifyLogged().then((subscription=>{ console.log('streamRoomMessages', subscription) })) @@ -151,20 +138,8 @@ export class WsChatMethodsService { subscribeToRoomUpdate(id, roomData) { - this.WsChatService.streamRoomMessages(id).then((subscription)=> { - console.log('streamRoomMessages', subscription) - }) - this.WsChatService.streamRoomMessages(id).then((subscription) => { - console.log('streamRoomMessages', subscription) - }) - this.WsChatService.streamNotifyLogged().then((subscription=> { - console.log('streamRoomMessages', subscription) - })) - this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ - console.log('streamNotifyRoomDeleteMessage', subscription); - }) - - this.WsChatService.subStreamNotifyRoom(id, 'typing', false) + + this.defaultSubtribe(id) this.prepareRoom(roomData); @@ -172,6 +147,22 @@ export class WsChatMethodsService { } + + /** + * @deprecated things a room need to subscribe on + * @param id + */ + private defaultSubtribe(id) { + this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + console.log('streamRoomMessages', subscription) + }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) + this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ + console.log('streamNotifyRoomDeleteMessage', subscription); + }) + } + prepareRoom(roomData) { let room:RoomService; @@ -214,6 +205,10 @@ export class WsChatMethodsService { } } + /** + * @description update user status + * @param id user ID + */ getUserStatus(id?:string) { this.WsChatService.getUserStatus((d) => { From 34208d80405c5d6932cb9515c21580f399347071 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 20:01:16 +0100 Subject: [PATCH 07/14] add commnets --- src/app/services/chat/ws-chat-methods.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index eca8ae55b..6914bde47 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -150,9 +150,9 @@ export class WsChatMethodsService { /** * @deprecated things a room need to subscribe on - * @param id + * @param id room id */ - private defaultSubtribe(id) { + private defaultSubtribe(id: any) { this.WsChatService.streamRoomMessages(id).then((subscription)=>{ console.log('streamRoomMessages', subscription) }) @@ -206,10 +206,10 @@ export class WsChatMethodsService { } /** - * @description update user status + * @description update user status. this method is called once only * @param id user ID */ - getUserStatus(id?:string) { + private getUserStatus(id?:string) { this.WsChatService.getUserStatus((d) => { From acacb58d0d27b87dac8d16e5e3f2d5739abf3d45 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 20:16:39 +0100 Subject: [PATCH 08/14] fix rocket chat session --- src/app/services/auth.service.ts | 9 ++++++--- src/app/services/chat.service.ts | 9 ++++++--- src/app/services/chat/room.service.ts | 5 +++++ src/app/services/chat/ws-chat-methods.service.ts | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 4e87e26ed..219eb2900 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -11,6 +11,7 @@ import { SessionStore } from '../store/session.service'; import { AESEncrypt } from '../services/aesencrypt.service'; import { CookieService } from 'ngx-cookie-service'; import { WsChatService } from 'src/app/services/chat/ws-chat.service'; +import { ChatService } from './chat.service'; @Injectable({ providedIn: 'root' }) @@ -31,7 +32,8 @@ export class AuthService { public alertController: AlertController, private aesencrypt: AESEncrypt, private cookieService: CookieService, - private WsChatService: WsChatService) { + private WsChatService: WsChatService, + private ChatService: ChatService) { this.headers = new HttpHeaders(); @@ -116,8 +118,9 @@ export class AuthService { if(responseChat) { + + setTimeout(()=>{ - console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password) this.WsChatService.connect(); this.WsChatService.login().then((message) => { @@ -133,8 +136,8 @@ export class AuthService { this.ValidatedUserChat = responseChat; localStorage.setItem('userChat', JSON.stringify(responseChat)); - this.storageService.store(AuthConnstants.AUTH, responseChat); + this.ChatService.setHeader() return true; } diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index 78a481246..9d786fe02 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -27,19 +27,22 @@ export class ChatService { constructor( private http:HttpClient, private httpService: HttpService, - private authService: AuthService, + public authService: AuthService, private storage: Storage, private storageService:StorageService, ) { - this.loggedUserChat = authService.ValidatedUserChat; + this.setHeader() + } + + setHeader() { + this.loggedUserChat = this.authService.ValidatedUserChat; this.headers = new HttpHeaders(); this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId); this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); this.options = { headers: this.headers, }; - } getDocumentDetails(url:string){ diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 4e85074ae..a785e5b12 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -42,6 +42,11 @@ export class RoomService { mgsArray = []; scrollDown = () => { } + + /** + * @description get user list from ws-chat-methods.service + * @returns chatUser[] + */ getAllUsers = (): chatUser[] => { return [] } diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 6914bde47..fa9b44579 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -136,7 +136,11 @@ export class WsChatMethodsService { })) } - + /** + * @description when a new room is create, needs to subtribe in order to receive updates + * @param id + * @param roomData + */ subscribeToRoomUpdate(id, roomData) { this.defaultSubtribe(id) @@ -164,6 +168,10 @@ export class WsChatMethodsService { } + /** + * @description create a representation of an room in these instance this.dm, this.group ... + * @param roomData + */ prepareRoom(roomData) { let room:RoomService; @@ -232,6 +240,11 @@ export class WsChatMethodsService { return this.users.find((user)=> user.username == username) } + /** + * @description convert rocketchat statues num to readable string + * @param text + * @returns + */ statusNumberToText(text) { if(text == '0') { return "offline" From b06e155d20edab61bee14673df5c65dda4304ef6 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 20:21:51 +0100 Subject: [PATCH 09/14] add commnets --- src/app/services/auth.service.ts | 12 ++++++------ src/app/services/chat.service.ts | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 219eb2900..76d77f1d0 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -11,7 +11,6 @@ import { SessionStore } from '../store/session.service'; import { AESEncrypt } from '../services/aesencrypt.service'; import { CookieService } from 'ngx-cookie-service'; import { WsChatService } from 'src/app/services/chat/ws-chat.service'; -import { ChatService } from './chat.service'; @Injectable({ providedIn: 'root' }) @@ -32,8 +31,7 @@ export class AuthService { public alertController: AlertController, private aesencrypt: AESEncrypt, private cookieService: CookieService, - private WsChatService: WsChatService, - private ChatService: ChatService) { + private WsChatService: WsChatService) { this.headers = new HttpHeaders(); @@ -118,8 +116,6 @@ export class AuthService { if(responseChat) { - - setTimeout(()=>{ this.WsChatService.connect(); @@ -137,8 +133,12 @@ export class AuthService { localStorage.setItem('userChat', JSON.stringify(responseChat)); this.storageService.store(AuthConnstants.AUTH, responseChat); - this.ChatService.setHeader() + + if(window['setChatHeader']){ + window['setChatHeader']() + } + return true; } else{ diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index 9d786fe02..666decc63 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -33,9 +33,10 @@ export class ChatService { ) { this.setHeader() + window['setChatHeader'] = this.setHeader } - setHeader() { + setHeader = () => { this.loggedUserChat = this.authService.ValidatedUserChat; this.headers = new HttpHeaders(); this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId); From eb504f53422651c917fc94c8944d4831da8a14a7 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 20:34:36 +0100 Subject: [PATCH 10/14] fix rocket chat session --- src/app/services/auth.service.ts | 5 ----- src/app/services/chat.service.ts | 26 ++++++++++++++---------- src/app/services/chat/ws-chat.service.ts | 6 ++++-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 76d77f1d0..a508f72d2 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -133,11 +133,6 @@ export class AuthService { localStorage.setItem('userChat', JSON.stringify(responseChat)); this.storageService.store(AuthConnstants.AUTH, responseChat); - - - if(window['setChatHeader']){ - window['setChatHeader']() - } return true; } diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index 666decc63..bb772b459 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -13,7 +13,7 @@ import { Observable, Subject } from "rxjs/Rx"; providedIn: 'root' }) export class ChatService { - headers: HttpHeaders; + _headers: HttpHeaders; options:any; options1:any; X_User_Id:any; @@ -31,19 +31,23 @@ export class ChatService { private storage: Storage, private storageService:StorageService, ) - { - this.setHeader() - window['setChatHeader'] = this.setHeader + {} + + + get headers() { + this.loggedUserChat = this.authService.ValidatedUserChat; + this._headers = new HttpHeaders(); + this._headers = this._headers.set('X-User-Id', this.loggedUserChat['data'].userId); + this._headers = this._headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); + this.options = { + headers: this._headers, + }; + + return this._headers } setHeader = () => { - this.loggedUserChat = this.authService.ValidatedUserChat; - this.headers = new HttpHeaders(); - this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId); - this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); - this.options = { - headers: this.headers, - }; + } getDocumentDetails(url:string){ diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 586035907..a2c252654 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -602,11 +602,11 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { for (const [key, item] of Object.entries(this.wsMsgQueue)) { if(item.loginRequired == true && this.isLogin == true) { - // console.log('run msgQueue ',index) + console.log('run msgQueue ', key) this.ws.send(item); delete this.wsMsgQueue[key] } else if(item.loginRequired == false) { - // console.log('run msgQueue ',index) + console.log('run msgQueue ', key) this.ws.send(item); delete this.wsMsgQueue[key] } @@ -616,6 +616,8 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online // console.log('save msgQueue this.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin) + console.log('save msgQueue', requestId) + this.wsMsgQueue[requestId] = {message, requestId, loginRequired} } else { let messageStr = JSON.stringify(message) From d64065d08224336e853ff416fdbd9bcfc14c15ec Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sat, 29 Jan 2022 20:39:35 +0100 Subject: [PATCH 11/14] fix mobile --- .../group-messages/group-messages.page.ts | 5 ++- src/app/pages/chat/messages/messages.page.ts | 32 +++---------------- src/app/services/chat.service.ts | 1 - 3 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/app/pages/chat/group-messages/group-messages.page.ts b/src/app/pages/chat/group-messages/group-messages.page.ts index dcbbc9423..385b64a06 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -39,7 +39,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { showLoader: boolean; isGroupCreated:boolean; loggedUser: any; - messages:any; room:any; roomName:any; @@ -614,7 +613,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { chatmsgArray.push(msgChat) }); - this.messages = chatmsgArray; + // this.messages = chatmsgArray; console.log('GROUP CHAT MSG FROM DB', chatmsgArray) }) } @@ -673,7 +672,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { //this.loadMessages() let msgOnly = res['messages'].filter(data => data.t != 'au'); //this.messages = msgOnly.reverse(); - console.log('MOBILE MSG ROCKET',this.messages); + // console.log('MOBILE MSG ROCKET',this.messages); this.transformDataMSG(msgOnly.reverse()); this.getRoomMessageDB(this.roomId); // Reconnect in one second diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 78e4f6f51..605f83532 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -49,8 +49,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { canvas: any ctx: any loggedUser: any; - message = ''; - messages: any; userPresence = ''; dmUsers: any; roomId: string; @@ -349,31 +347,10 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } sendMessage() { - let body = { - "message": - { - "rid": this.roomId, "msg": this.message - } - } - this.chatService.sendMessage(body).subscribe(res => { - //this.loadMessages(); - this.scrollingOnce = true; - }); - this.message = ""; + this.wsChatMethodsService.getDmRoom(this.roomId).send() } - loadMessages() { - this.showLoader = true; - const roomId = this.roomId; - this.chatService.getRoomMessages(this.roomId).subscribe(res => { - console.log(res); - this.messages = res['messages'].reverse(); - this.chatMessageStore.add(roomId, this.messages) - console.log(this.messages); - this.showLoader = false; - }) - } viewDocument(file: any, url?: string) { @@ -576,7 +553,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.fileService.addDocGestaoDocumentalToChat(this.roomId); //this.addDocGestaoDocumental(); } - this.loadMessages(); + }); } @@ -642,7 +619,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { chatmsgArray.push(msgChat) }); - this.messages = chatmsgArray.reverse(); + console.log('CHAT MSG FROM DB', chatmsgArray) }) } @@ -717,8 +694,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { //showMessage(response.statusText); //this.loadMessages() if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - this.messages = res['messages'].reverse(); - this.chatMessageStore.add(roomId, this.messages) + } diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index bb772b459..2b3096e06 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -33,7 +33,6 @@ export class ChatService { ) {} - get headers() { this.loggedUserChat = this.authService.ValidatedUserChat; this._headers = new HttpHeaders(); From b734112eb6b414db534b1e1537c5d3b28a904b1b Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sun, 30 Jan 2022 08:36:43 +0100 Subject: [PATCH 12/14] fix user chat list --- src/app/services/chat.service.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index 2b3096e06..78a481246 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -13,7 +13,7 @@ import { Observable, Subject } from "rxjs/Rx"; providedIn: 'root' }) export class ChatService { - _headers: HttpHeaders; + headers: HttpHeaders; options:any; options1:any; X_User_Id:any; @@ -27,26 +27,19 @@ export class ChatService { constructor( private http:HttpClient, private httpService: HttpService, - public authService: AuthService, + private authService: AuthService, private storage: Storage, private storageService:StorageService, ) - {} - - get headers() { - this.loggedUserChat = this.authService.ValidatedUserChat; - this._headers = new HttpHeaders(); - this._headers = this._headers.set('X-User-Id', this.loggedUserChat['data'].userId); - this._headers = this._headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); + { + this.loggedUserChat = authService.ValidatedUserChat; + this.headers = new HttpHeaders(); + this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId); + this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); this.options = { - headers: this._headers, + headers: this.headers, }; - return this._headers - } - - setHeader = () => { - } getDocumentDetails(url:string){ From 00bff455852ae82ec569ac802140b61de17e27eb Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sun, 30 Jan 2022 09:34:56 +0100 Subject: [PATCH 13/14] improve --- .../chat/chat-storage.service.spec.ts | 16 ++ src/app/services/chat/chat-storage.service.ts | 170 ++++++++++++ src/app/services/chat/room.service.ts | 244 +++++------------- .../group-messages/group-messages.page.ts | 1 - 4 files changed, 250 insertions(+), 181 deletions(-) create mode 100644 src/app/services/chat/chat-storage.service.spec.ts create mode 100644 src/app/services/chat/chat-storage.service.ts diff --git a/src/app/services/chat/chat-storage.service.spec.ts b/src/app/services/chat/chat-storage.service.spec.ts new file mode 100644 index 000000000..34a01e3a9 --- /dev/null +++ b/src/app/services/chat/chat-storage.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ChatStorageService } from './chat-storage.service'; + +describe('ChatStorageService', () => { + let service: ChatStorageService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ChatStorageService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/chat/chat-storage.service.ts b/src/app/services/chat/chat-storage.service.ts new file mode 100644 index 000000000..ae0262824 --- /dev/null +++ b/src/app/services/chat/chat-storage.service.ts @@ -0,0 +1,170 @@ +import { Injectable } from '@angular/core'; +import { Storage } from '@ionic/storage'; + +@Injectable({ + providedIn: 'root' +}) +export class ChatStorageService { + + constructor( + private storage: Storage, + ) { } + + + /** + * @description delete message in the DB. get all messages, delete then corresponding message and update the store + * @param id message ID + */ + private deleteMessageFromDb(messageId, roomId) { + this.storage.get('chatmsg' + roomId).then((messages: any = []) => { + + messages.forEach((message, index) => { + + if(message._id == messageId) { + messages.splice(index, 1) + } + + }) + + this.storage.set('chatmsg' + roomId, messages).then((value) => { + console.log('MSG SAVED ON STORAGE', value) + }); + }) + } + + + getMsgFromDB() { + + /* this.storage.get('chatmsg' + this.id).then((message) => { + console.log('ALL MESSAGE WEB', message) + message.forEach(message => { + + if (message.file) { + if (message.file.guid) { + this.storage.get(message.file.guid).then((image) => { + //console.log('IMAGE FROM STORAGE', image) + message.file.image_url = image + }); + } + } + + let mmessage = this.fix_updatedAt(message) + console.log('FROM DB WEB', mmessage) + const wewMessage = new MessageService(this.storage) + wewMessage.setData(mmessage) + this.messages.push(wewMessage) + console.log('loadHistory 222', this.messages) + }); + }) */ + } + + async transformData(res) { + + // this.mgsArray = []; + // res.forEach(async element => { + + // if (element.file) { + // if (element.file.guid) { + // await this.storage.get(element.file.guid).then((image) => { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // file: { + // guid: element.file.guid, + // image_url: image, + // type: element.file.type + // }, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg); + + // }) + // } else { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // file: element.file, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg) + // } + // } else { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg) + // } + + // }); + // await this.storage.remove('chatmsg').then(() => { + // console.log('MSG REMOVE FROM STORAGE') + // }); + // await this.storage.set('chatmsg', this.mgsArray).then((value) => { + // console.log('MSG SAVED ON STORAGE', value) + // }); + + } + + + + getMsgFromDBMobile() { + // console.log('ALL MSG DBBB', this.id) + // this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => { + // let ad = []; + // ad = msg + // console.log('ALL MSG DBBB', ad.length) + // msg.map(element => { + // console.log('CHANNEL ELEMENT', element) + // let msgChat = { + // _id: element.Id, + // attachments: this.isJson(element.Attachments), + // channels: this.isJson(element.Channels), + // file: { + // guid: this.isJson(element.File).guid, + // image_url: this.isJson(element.image_url), + // type: this.isJson(element.File).type + + // }, + // mentions: this.isJson(element.Mentions), + // msg: element.Msg, + // rid: element.Rid, + // ts: element.Ts, + // u: this.isJson(element.U), + // _updatedAt: this.isJson(element.UpdatedAt), + // } + + // let mmessage = this.fix_updatedAt(msgChat) + // console.log('FROM DB WEB', mmessage) + // const wewMessage = new MessageService(this.storage) + // wewMessage.setData(mmessage) + // this.messages.push(wewMessage) + // console.log('loadHistory 222', this.messages) + // }); + // }); + } + + +} diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index a785e5b12..fb89ea961 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -84,9 +84,8 @@ export class RoomService { ChatMessage = this.fix_updatedAt(ChatMessage) console.log('recivemessage', ChatMessage) - /* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */ - const message = new MessageService(this.storage) - message.setData(ChatMessage) + + const message = this.prepareMessage(ChatMessage) this.lastMessage = message if (message.t == 'r') { this.name = message.msg } @@ -140,29 +139,61 @@ export class RoomService { this.id, "stream-notify-room", async (ChatMessage) => { - console.log(ChatMessage.fields.args[0]._id); - const messageId = ChatMessage.fields.args[0]._id; - this.messages.forEach((message, index)=>{ - if(message._id == messageId){ - this.messages.splice(index, 1) - this.storage.set('chatmsg' + this.id, this.messages).then((value) => { - //console.log('MSG DELETE ON STORAGE', value) - }); - //Get previous last message from room - const previousLastMessage = this.messages.slice(-1)[0]; - - this.lastMessage = previousLastMessage; - this.calDateDuration(previousLastMessage._updatedAt) - - } - }) + const DeletedMessageId = ChatMessage.fields.args[0]._id; + console.log(DeletedMessageId); + this.deleteMessage(DeletedMessageId) } ) this.WsChatService.registerCallback } + /** + * @description delete message in the view + * @param id message ID + */ + deleteMessage(id) { + this.messages.forEach((message, index) => { + if(message._id == id) { + this.messages.splice(index, 1) + + this.deleteMessageFromDb(id) + //Get previous last message from room + const previousLastMessage = this.messages.slice(-1)[0]; + + this.lastMessage = previousLastMessage; + this.calDateDuration(previousLastMessage._updatedAt) + + } + }) + } + + + /** + * @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 = []) => { + + 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 + */ send() { this.WsChatService.send(this.id, this.message) this.message= '' @@ -224,43 +255,7 @@ export class RoomService { leave(rid?) { this.WsChatService.leaveRoom(this.id) } - - getMsgFromDBMobile() { - console.log('ALL MSG DBBB', this.id) - this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => { - let ad = []; - ad = msg - console.log('ALL MSG DBBB', ad.length) - msg.map(element => { - console.log('CHANNEL ELEMENT', element) - let msgChat = { - _id: element.Id, - attachments: this.isJson(element.Attachments), - channels: this.isJson(element.Channels), - file: { - guid: this.isJson(element.File).guid, - image_url: this.isJson(element.image_url), - type: this.isJson(element.File).type - - }, - mentions: this.isJson(element.Mentions), - msg: element.Msg, - rid: element.Rid, - ts: element.Ts, - u: this.isJson(element.U), - _updatedAt: this.isJson(element.UpdatedAt), - } - - let mmessage = this.fix_updatedAt(msgChat) - console.log('FROM DB WEB', mmessage) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(mmessage) - this.messages.push(wewMessage) - console.log('loadHistory 222', this.messages) - }); - }); - } - + isJson(str) { try { JSON.parse(str); @@ -270,32 +265,6 @@ export class RoomService { return JSON.parse(str); } - getMsgFromDB() { - - /* this.storage.get('chatmsg' + this.id).then((message) => { - console.log('ALL MESSAGE WEB', message) - message.forEach(message => { - - if (message.file) { - if (message.file.guid) { - this.storage.get(message.file.guid).then((image) => { - //console.log('IMAGE FROM STORAGE', image) - message.file.image_url = image - }); - } - } - - let mmessage = this.fix_updatedAt(message) - console.log('FROM DB WEB', mmessage) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(mmessage) - this.messages.push(wewMessage) - console.log('loadHistory 222', this.messages) - }); - }) */ - } - - // runs onces only loadHistory(limit = 100) { @@ -307,9 +276,7 @@ export class RoomService { let localMessages = [] messages.forEach(message => { - message = this.fix_updatedAt(message) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(message) + const wewMessage = this.prepareMessage(message) localMessages.push(wewMessage) }); @@ -320,12 +287,11 @@ export class RoomService { 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) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(message) + const wewMessage = this.prepareMessage(message) + localMessages.push(wewMessage) }); @@ -338,28 +304,6 @@ export class RoomService { }) - /* this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => { - - const mgsArray = chatHistory.result.messages.reverse(); - if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - await this.storage.remove('chatmsg' + this.id).then(() => { - console.log('MSG REMOVE ON STORAGE') - }) - await this.storage.set('chatmsg' + this.id, mgsArray).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - this.getMsgFromDB() - }); - } else { - mgsArray.forEach((element) => { - console.log('SQLITE WEBSOCKET', element) - this.sqlservice.addChatMSG(element) - }) - this.getMsgFromDBMobile() - } - - - }) */ - setTimeout(() => { this.scrollDown() }, 50) @@ -368,6 +312,15 @@ export class RoomService { } + + prepareMessage(message) { + message = this.fix_updatedAt(message) + const wewMessage = new MessageService(this.storage) + wewMessage.setData(message) + + return wewMessage + } + updateMeessage(messageID, imgbase64) { } @@ -377,75 +330,6 @@ export class RoomService { return res; } - async transformData(res) { - - this.mgsArray = []; - res.forEach(async element => { - - if (element.file) { - if (element.file.guid) { - await this.storage.get(element.file.guid).then((image) => { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - file: { - guid: element.file.guid, - image_url: image, - type: element.file.type - }, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg); - - }) - } else { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - file: element.file, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg) - } - } else { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg) - } - - }); - await this.storage.remove('chatmsg').then(() => { - console.log('MSG REMOVE FROM STORAGE') - }); - await this.storage.set('chatmsg', this.mgsArray).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - }); - - } ReactToMessage() { } diff --git a/src/app/shared/chat/group-messages/group-messages.page.ts b/src/app/shared/chat/group-messages/group-messages.page.ts index 52558f953..5747db9e1 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -111,7 +111,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.getRoomInfo(); //this.scrollToBottom(); this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); - this.wsChatMethodsService.getGroupRoom(this.roomId).getMsgFromDB(); // console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); this.wsChatMethodsService.openRoom(this.roomId) this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked From 88e2a0878168637cac91a037d813496ae890a823 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Sun, 30 Jan 2022 09:44:12 +0100 Subject: [PATCH 14/14] improve --- src/app/services/chat/ws-chat.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index a2c252654..bf89d11eb 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -35,7 +35,6 @@ export class WsChatService { this.ws.registerCallback({ type:'Onmessage', - key: this.constructor.name+'ping/pong', funx:(message: any) => { if(message.msg == "ping") { this.ws.send({message:{msg:"pong"}, loginRequired: false})