diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 31c9647c1..1a4abe39b 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -26,7 +26,7 @@ import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx'; import { SqliteService } from 'src/app/services/sqlite.service'; import { Device } from '@capacitor/device'; import { RouteService } from 'src/app/services/route.service'; -import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service'; +import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { environment } from 'src/environments/environment'; import { v4 as uuidv4 } from 'uuid' @@ -100,19 +100,8 @@ export class HomePage implements OnInit { private screenOrientation: ScreenOrientation, private sqliteservice: SqliteService, private RouteService: RouteService, - private RocketChatClientService: RocketChatClientService) { - window['jj'] = ()=> { - //send message // roomId // Message - this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) - } + private WsChatService: WsChatService) { - - this.RocketChatClientService.registerCallback({type:'Onmessage',requestId:'asdfasdfasdf',funx:(message)=>{ - // console.log('message', message) - }}) - - - // this.RocketChatClientService.send() /* this.webNotificationPopupService.askNotificationPermission() */ this.router.events.subscribe((val) => { diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 5d61fdc88..aec57d9d4 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -4,7 +4,7 @@ - + @@ -45,7 +45,7 @@
@@ -86,7 +86,7 @@
- +
diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index b8f7cb9fd..07cfffcf0 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -12,7 +12,7 @@ import { import { ModalController, Platform } from '@ionic/angular'; import { AuthService } from 'src/app/services/auth.service'; import { ChatService } from 'src/app/services/chat.service'; -import { wsChatService as wsChatService} from 'src/app/services/chat/chat.service' +import { wsChatMethodsService as wsChatMethodsService} from 'src/app/services/chat/chat.service' import { GroupMessagesPage } from './group-messages/group-messages.page'; import { ContactsPage } from './messages/contacts/contacts.page'; import { MessagesPage } from './messages/messages.page'; @@ -125,7 +125,7 @@ export class ChatPage implements OnInit { private sqlservice: SqliteService, private platform: Platform, private storageservice: StorageService, - public wsChatService: wsChatService + public wsChatMethodsService: wsChatMethodsService ) { this.loggedUserChat = authService.ValidatedUserChat['data']; diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index 1cf59303b..f589e9f12 100644 --- a/src/app/pages/chat/messages/messages.page.html +++ b/src/app/pages/chat/messages/messages.page.html @@ -49,7 +49,7 @@ -->
-
diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index f240b6fe9..34bf21e39 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -28,7 +28,7 @@ import { SqliteService } from 'src/app/services/sqlite.service'; import { elementAt } from 'rxjs-compat/operator/elementAt'; import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page'; import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; -import { wsChatService as wsChatService} from 'src/app/services/chat/chat.service' +import { wsChatMethodsService } from 'src/app/services/chat/chat.service' const IMAGE_DIR = 'stored-images'; @@ -96,7 +96,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { private changeDetectorRef: ChangeDetectorRef, private platform: Platform, private sqlservice: SqliteService, - public wsChatService: wsChatService + public wsChatMethodsService: wsChatMethodsService ) { this.loggedUser = authService.ValidatedUserChat['data']; this.roomId = this.navParams.get('roomId'); @@ -108,7 +108,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } }; - this.wsChatService.getRoom(this.roomId).loadHistory() + this.wsChatMethodsService.getRoom(this.roomId).loadHistory() } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 27a64fa3a..1e42b2737 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -10,8 +10,7 @@ import { AlertController } from '@ionic/angular'; import { SessionStore } from '../store/session.service'; import { AESEncrypt } from '../services/aesencrypt.service'; import { CookieService } from 'ngx-cookie-service'; -import { RocketChatClientService } from '../services/socket/rocket-chat-client.service'; - +import { WsChatService } from 'src/app/services/chat/ws-chat.service'; @Injectable({ providedIn: 'root' }) @@ -30,7 +29,7 @@ export class AuthService { public alertController: AlertController, private aesencrypt: AESEncrypt, private cookieService: CookieService, - private RocketChatClientService: RocketChatClientService) { + private WsChatService: WsChatService) { this.headers = new HttpHeaders(); @@ -38,9 +37,9 @@ export class AuthService { this.ValidatedUser = SessionStore.user console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password) - this.RocketChatClientService.connect(()=>{ + this.WsChatService.connect(()=>{ - this.RocketChatClientService.login({ + this.WsChatService.login({ username: SessionStore.user.RochetChatUser, password: SessionStore.user.Password }).then((message) => { @@ -117,9 +116,9 @@ export class AuthService { } - this.RocketChatClientService.connect(()=>{ + this.WsChatService.connect(()=>{ - this.RocketChatClientService.login({ + this.WsChatService.login({ username: SessionStore.user.RochetChatUser, password: user.password }).then((message) => { diff --git a/src/app/services/chat/chat.service.spec.ts b/src/app/services/chat/chat.service.spec.ts index 8cb3b6ba3..760f446a5 100644 --- a/src/app/services/chat/chat.service.spec.ts +++ b/src/app/services/chat/chat.service.spec.ts @@ -1,13 +1,13 @@ import { TestBed } from '@angular/core/testing'; -import { wsChatService } from './chat.service'; +import { wsChatMethodsService } from './chat.service'; describe('ChatService', () => { - let service: wsChatService; + let service: wsChatMethodsService; beforeEach(() => { TestBed.configureTestingModule({}); - service = TestBed.inject(wsChatService); + service = TestBed.inject(wsChatMethodsService); }); it('should be created', () => { diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index 4b87fa4d7..521906bda 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -1,25 +1,25 @@ import { Injectable } from '@angular/core'; import { RoomService } from './room.service'; -import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service'; +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'; @Injectable({ providedIn: 'root' }) -export class wsChatService { +export class wsChatMethodsService { - individual: {[key: string]: RoomService} = {} + dm: {[key: string]: RoomService} = {} group: {[key: string]: RoomService} = {} loadingWholeList = false - individualCount = 0; + dmCount = 0; groupCount = 0; constructor( - private RocketChatClientService: RocketChatClientService + private WsChatService: WsChatService ) { (async()=>{ @@ -32,17 +32,17 @@ export class wsChatService { async getAllRooms () { this.loadingWholeList = true - const rooms: any = await this.RocketChatClientService.getRooms(); + const rooms: any = await this.WsChatService.getRooms(); console.log(rooms) rooms.result.update.forEach((roomData:any) => { let room:RoomService; - room = new RoomService(this.RocketChatClientService, new MessageService()) + room = new RoomService(this.WsChatService, new MessageService()) room.setData({ id: this.getRoomId(roomData), - name: this.getChatName(roomData), + name: this.getRoomName(roomData), lastMessage: this.getRoomLastMessage(roomData), _updatedAt: roomData._updatedAt['$date'] }) @@ -52,8 +52,8 @@ export class wsChatService { let roomId = roomData.lastMessage.rid if(this.isIndividual(roomData)) { - this.individual[roomId] = room - this.individualCount++ + this.dm[roomId] = room + this.dmCount++ } else { this.group[roomId] = room this.groupCount++ @@ -65,14 +65,14 @@ export class wsChatService { } subscribeToRoom() { - for (const id in this.individual) { - this.RocketChatClientService.subscribe(id).then((subscription)=>{ + for (const id in this.dm) { + this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{ console.log('subscription', subscription) }) } for (const id in this.group) { - this.RocketChatClientService.subscribe(id).then((subscription)=>{ + this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{ console.log('subscription', subscription) }) } @@ -80,13 +80,13 @@ export class wsChatService { getRoom(id): RoomService { try { - return this.individual[id] + return this.dm[id] } catch(e) { return this.group[id] } } - getChatName(roomData) { + getRoomName(roomData) { if(this.isIndividual(roomData)) { const names: String[] = roomData.usernames const roomName = names.filter((name)=>{ diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 2ce1f27b4..54fc6222d 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core' -import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service'; +import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { MessageService } from 'src/app/services/chat/message.service' import { ChatUserService } from 'src/app/services/chat/chat-user.service' import { showDateDuration } from 'src/plugin/showDateDuration' @@ -20,7 +20,7 @@ export class RoomService { constructor( - public RocketChatClientService: RocketChatClientService, + public WsChatService: WsChatService, private MessageService: MessageService, ) {} @@ -35,7 +35,7 @@ export class RoomService { } receiveMessage() { - this.RocketChatClientService.receiveLiveMessageFromRoom( + this.WsChatService.receiveLiveMessageFromRoom( this.id, this.constructor.name+this.id, (Chatmessage) => { @@ -51,7 +51,7 @@ export class RoomService { } send(msg) { - this.RocketChatClientService.send(this.id, msg) + this.WsChatService.send(this.id, msg) } // runs onces only @@ -59,7 +59,7 @@ export class RoomService { if(this.hasLoadHistory){ return false} - this.RocketChatClientService.loadHistory(this.id, limit).then((message:any) => { + this.WsChatService.loadHistory(this.id, limit).then((message:any) => { console.log('loadHistory', message) message.result.messages.reverse().forEach(element => { diff --git a/src/app/services/chat/ws-chat-methods.service.spec.ts b/src/app/services/chat/ws-chat-methods.service.spec.ts new file mode 100644 index 000000000..c10e87b7b --- /dev/null +++ b/src/app/services/chat/ws-chat-methods.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { WsChatMethodsService } from './ws-chat-methods.service'; + +describe('WsChatMethodsService', () => { + let service: WsChatMethodsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(WsChatMethodsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts new file mode 100644 index 000000000..670e78983 --- /dev/null +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -0,0 +1,116 @@ +import { Injectable } from '@angular/core'; +import { RoomService } from './room.service'; +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'; + + +@Injectable({ + providedIn: 'root' +}) +export class WsChatMethodsService { + + + + dm: {[key: string]: RoomService} = {} + group: {[key: string]: RoomService} = {} + + loadingWholeList = false + + dmCount = 0; + groupCount = 0; + + constructor( + private WsChatService: WsChatService + ) { + + (async()=>{ + await this.getAllRooms(); + this.subscribeToRoom() + })() + + } + + async getAllRooms () { + this.loadingWholeList = true + + const rooms: any = await this.WsChatService.getRooms(); + + console.log(rooms) + + rooms.result.update.forEach((roomData:any) => { + let room:RoomService; + + room = new RoomService(this.WsChatService, new MessageService()) + room.setData({ + id: this.getRoomId(roomData), + name: this.getRoomName(roomData), + lastMessage: this.getRoomLastMessage(roomData), + _updatedAt: roomData._updatedAt['$date'] + }) + + room.receiveMessage() + + let roomId = roomData.lastMessage.rid + + if(this.isIndividual(roomData)) { + this.dm[roomId] = room + this.dmCount++ + } else { + this.group[roomId] = room + this.groupCount++ + } + + }); + + this.loadingWholeList = false + } + + subscribeToRoom() { + for (const id in this.dm) { + this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{ + console.log('subscription', subscription) + }) + } + + for (const id in this.group) { + this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{ + console.log('subscription', subscription) + }) + } + } + + getRoom(id): RoomService { + try { + return this.dm[id] + } catch(e) { + return this.group[id] + } + } + + getRoomName(roomData) { + if(this.isIndividual(roomData)) { + const names: String[] = roomData.usernames + const roomName = names.filter((name)=>{ + return name != SessionStore.user.RochetChatUser + })[0] + + return roomName + } else { + return roomData.fName + } + } + + getRoomId(roomData) { + return roomData.lastMessage.rid + } + + getRoomLastMessage(roomData) { + return roomData.lastMessage + } + + private isIndividual(roomData) { + return !roomData.fname + } + +} diff --git a/src/app/services/socket/synchro.service.spec.ts b/src/app/services/chat/ws-chat.service.spec.ts similarity index 54% rename from src/app/services/socket/synchro.service.spec.ts rename to src/app/services/chat/ws-chat.service.spec.ts index 61f099398..c909a0da0 100644 --- a/src/app/services/socket/synchro.service.spec.ts +++ b/src/app/services/chat/ws-chat.service.spec.ts @@ -1,13 +1,13 @@ import { TestBed } from '@angular/core/testing'; -import { SynchroService } from './synchro.service'; +import { WsChatService } from './ws-chat.service'; -describe('SynchroService', () => { - let service: SynchroService; +describe('WsChatService', () => { + let service: WsChatService; beforeEach(() => { TestBed.configureTestingModule({}); - service = TestBed.inject(SynchroService); + service = TestBed.inject(WsChatService); }); it('should be created', () => { diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/chat/ws-chat.service.ts similarity index 98% rename from src/app/services/socket/rocket-chat-client.service.ts rename to src/app/services/chat/ws-chat.service.ts index 1e19fcba0..237862fe6 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -3,13 +3,11 @@ import { v4 as uuidv4 } from 'uuid' import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service' import { deepFind } from 'src/plugin/deep' import { environment } from 'src/environments/environment'; -/** - * Use this class to interact with rocketChat realtime API - */ - @Injectable({ +@Injectable({ providedIn: 'root' }) - export class RocketChatClientService { +export class WsChatService { + private firstPingFunx: Function isLogin = false; @@ -164,7 +162,7 @@ import { environment } from 'src/environments/environment'; }); } - subscribe(roomId : string) { + subscribeNotifyRoom(roomId : string) { const requestId = uuidv4() @@ -362,6 +360,4 @@ import { environment } from 'src/environments/environment'; console.log(`[error] ${event.message}`); } - } - -} \ No newline at end of file + }} diff --git a/src/app/services/socket/rocket-chat-client.service.spec.ts b/src/app/services/socket/rocket-chat-client.service.spec.ts deleted file mode 100644 index 474d7913d..000000000 --- a/src/app/services/socket/rocket-chat-client.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { RocketChatClientService } from './rocket-chat-client.service'; - -describe('RocketChatClientService', () => { - let service: RocketChatClientService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(RocketChatClientService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/src/app/services/socket/synchro.service.ts b/src/app/services/socket/synchro.service.ts deleted file mode 100644 index 7f5cd2994..000000000 --- a/src/app/services/socket/synchro.service.ts +++ /dev/null @@ -1,215 +0,0 @@ -import { Injectable } from '@angular/core'; -import { SessionStore } from 'src/app/store/session.service'; -import { v4 as uuidv4 } from 'uuid' -import { BackgroundService } from '../background.service'; -import { environment } from 'src/environments/environment'; -import { EventTrigger } from '../eventTrigger.service' - -export interface wss { - - url: string, - type: 'reflect' | 'emit' - header: { - id: string - bluePrint: string, - jwt: string - } - -} - -@Injectable({ - providedIn: 'root' -}) -class SynchroService { - /* [x: string]: any; - - private connection!: WebSocket; - private id: string = uuidv4(); - public conected = false - private url: string = '' - callback = function () { } - private _connected = false; - private eventtrigger: EventTrigger; - private BackgroundService = new BackgroundService() - callBacks: { - type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '', - object?: string - funx: Function - }[] = [] - private msgQueue = [] - - constructor() { - // alert(SessionStore.user.FullName) - } - - get connected() { - return this._connected - } - - setUrl() { - - let header = { - id: '1234', - bluePrint: '12312123', - jwt: uuidv4() - } - - let wss: wss = { - header, - url: 'wss://sychro-offline.herokuapp.com/ws/some_url/', - type: 'reflect' - } - - this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/` - } - - registerCallback(type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notifications' | 'Notification', funx: Function, object = '') { - this.callBacks.push({ - type, - funx, - object - }) - } - - connect() { - - this.connection = new WebSocket(this.url); - // bind function - this.connection.onopen = this.onopen; - this.connection.onmessage = this.onmessage; - this.connection.onclose = this.onclose; - this.connection.onerror = this.onerror; - } - - private onopen = () => { - - - - //if (this._connected === true) { - //this.BackgroundService.online() - console.log('Online', this._connected) - this.callBacks.forEach((e) => { - if (e.type == 'Online') { - e.funx() - } - }) - //} - - console.log('open ======================= welcome to socket server') - - this._connected = true - - // send all saved data due to internet connection - this.msgQueue.forEach((item, index, object) => { - this.$send(item); - object.splice(index, 1); - }) - - } - - public $send(object: any) { - - if (!this._connected) { // save data to send when back online - this.msgQueue.push(object) - } - - let payload = { - message: JSON.stringify(object) || '{"person.adress.country":"1Angola"}', - username: SessionStore.user.FullName, - idConnection: this.id - } - - - let sendData = JSON.stringify(payload); - console.log(sendData) - - this.connection.send(sendData); - } - - private onmessage = async (event: any) => { - - - let data = JSON.parse(event.data) - let payload = JSON.parse(data.message) - payload.message = JSON.parse(payload.message) - const idConnection = payload.idConnection - const username = payload.username - - if (idConnection != this.id) { - - if (window['platform'].is('desktop') || this.platform.is('mobileweb')) { } - else return false - - if (environment.production) return false - - this.callBacks.forEach((e) => { - - if (payload.message[0]) { - if (payload.message[0].Service && payload.message[0].Object && payload.message[0].IdObject) { - if (e.type == '' && !e.object) { - if (username == SessionStore.user.FullName) { - e.funx(payload.message, data) - } - } - - if (e.type == 'Notifications') { - e.funx(payload.message, data) - } - - } - } else if (payload.message.Service && payload.message.Object && payload.message.IdObject) { - if (e.type == 'Notification' && e.object == payload.message.Object || e.type == 'Notification' && e.object == 'any') { - e.funx(payload.message, data) - } - } - - }) - - } - this.callback() - } - - private onclose = (event: any) => { - console.log('Websocket close') - setTimeout(() => { - if (event.wasClean) { - console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); - } else { - // e.g. server process killed or network down - // event.code is usually 1006 in this case - console.log('[close] Connection died'); - console.log('Reconnect') - - - - // if (this._connected === false) { - // this.BackgroundService.offline(); - console.log('Offline', this._connected) - this.callBacks.forEach((e) => { - if (e.type == 'Offline') { - e.funx() - } - }) - //} - - - // status - this._connected = false - // reconnect - this.connect() - - } - }, 100); - } - - private onerror = (event: any) => { - console.log(`[error] ${event.message}`); - } */ -} - - -/* export const synchro = new SynchroService() -synchro.setUrl() -synchro.connect() - -window['synchro'] = synchro */ \ No newline at end of file diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index d48e1075b..df633ae84 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -3,7 +3,7 @@
- {{ wsChatService.getRoom(roomId).name }} + {{ wsChatMethodsService.getRoom(roomId).name }}