diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 048b2e17b..8b28279b0 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -99,28 +99,6 @@ export class HomePage implements OnInit { console.log('loadHistory', message) }) - - this.RocketChatClientService.getRooms().then((rooms: any)=>{ - console.log('rooms', rooms) - - rooms.result.update.forEach((room:any) => { - console.log('room', room) - this.RocketChatClientService.subscribe(room.lastMessage.rid).then((subscription)=>{ - console.log('subscription', subscription) - }) - }); - }); - - - this.RocketChatClientService.receiveLiveMessageFromRoom( - 'fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', - this.constructor.name, - (Chatmessage)=>{ - console.log('chat', Chatmessage) - } - ) - - window['jj'] = ()=>{ //send message // roomId // Message this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index 02df0983c..dd895bbfc 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 0e72b5634..aee83350b 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -28,6 +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 { ChatService as ChatServiceGPR} from 'src/app/services/chat/chat.service' const IMAGE_DIR = 'stored-images'; @@ -94,7 +95,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { public ThemeService: ThemeService, private changeDetectorRef: ChangeDetectorRef, private platform: Platform, - private sqlservice: SqliteService + private sqlservice: SqliteService, + public ChatServiceGPR: ChatServiceGPR ) { this.loggedUser = authService.ValidatedUserChat['data']; this.roomId = this.navParams.get('roomId'); @@ -105,10 +107,13 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.modalController.dismiss(); } }; + + this.ChatServiceGPR.getRoom(this.roomId).loadHistory() + } ngOnInit() { - this.load(); + //this.load(); this.setStatus('online'); //this.loadFiles(); diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index 89c2b440f..b8e1e92c4 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -21,33 +21,41 @@ export class ChatService { constructor( private RocketChatClientService: RocketChatClientService ) { - this.getAllRoomAndSubscribe() + + (async()=>{ + await this.getAllRoom(); + this.subscribeToRoom() + })() + } - async getAllRoomAndSubscribe () { + async getAllRoom () { this.loadingWholeList = true const rooms: any = await this.RocketChatClientService.getRooms(); rooms.result.update.forEach((roomData:any) => { - let room + let room:RoomService; + let roomId = roomData.lastMessage.rid if(this.isIndividual(roomData)) { - room = new RoomService(new RocketChatClientService(), new MessageService()) + room = new RoomService(this.RocketChatClientService, new MessageService()) room.setData({ id: this.getRoomId(roomData), name: this.getChatName(roomData), lastMessage: this.getRoomLastMessage(roomData) }) + room.receiveMessage() this.individual[roomId] = room this.individualCount++ } else { - room = new RoomService(new RocketChatClientService(), new MessageService()) + room = new RoomService(this.RocketChatClientService, new MessageService()) room.setData({ id: this.getRoomId(roomData), name: this.getChatName(roomData), lastMessage: this.getRoomLastMessage(roomData) }) + room.receiveMessage() this.group[roomId] = room this.groupCount++ } @@ -55,7 +63,28 @@ export class ChatService { }); this.loadingWholeList = false - + } + + subscribeToRoom() { + for (const id in this.individual) { + this.RocketChatClientService.subscribe(id).then((subscription)=>{ + console.log('subscription', subscription) + }) + } + + for (const id in this.group) { + this.RocketChatClientService.subscribe(id).then((subscription)=>{ + console.log('subscription', subscription) + }) + } + } + + getRoom(id): RoomService { + try { + return this.individual[id] + } catch(e) { + return this.group[id] + } } getChatName(roomData) { diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index b22bacf6c..0637c7516 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -5,5 +5,9 @@ import { Injectable } from '@angular/core'; }) export class MessageService { + channels = [] + mentions = [] + msg = '' + rid = '' constructor() { } } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 67a751fe9..873b3d240 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -13,21 +13,53 @@ export class RoomService { chatUser: ChatUserService[] = [] id = '' name = '' + mmm = [] + private hasLoadHistory = false constructor( - private RocketChatClientService: RocketChatClientService, + public RocketChatClientService: RocketChatClientService, private MessageService: MessageService - ) {} + ) { + + } setData({id, name, lastMessage}) { + this.id = id this.name = name this.lastMessage = lastMessage } + receiveMessage() { + this.RocketChatClientService.receiveLiveMessageFromRoom( + this.id, + this.constructor.name+this.id, + (Chatmessage)=>{ + this.hasLoadHistory = false + this.loadHistory() + } + ) + } + + send(msg) { + this.RocketChatClientService.send(this.id, msg) + } + + // runs onces only + loadHistory(limit= 100) { + + if(this.hasLoadHistory){ return false } + + this.RocketChatClientService.loadHistory(this.id, limit).then((message:any)=>{ + console.log('loadHistory', message) + this.mmm = message.result.messages.reverse() + }) + + this.hasLoadHistory = true + } + create() {} - sendMessage() {} - deleteMessage() {} + deleteMessage(msgId) {} ReactToMessage() {} } diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/socket/rocket-chat-client.service.ts index d6417db38..87f1c7930 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/socket/rocket-chat-client.service.ts @@ -209,7 +209,7 @@ import { deepFind } from 'src/plugin/deep' * @param key * @param funx */ - receiveLiveMessageFromRoom(roomId, key, funx: Function) { + receiveLiveMessageFromRoom(roomId =')(', key, funx: Function) { this.ws.registerCallback({ type:'Onmessage', @@ -302,7 +302,6 @@ import { deepFind } from 'src/plugin/deep' }, connect:(url)=> { - this.ws.connected = false this.wsUrl = url this.socket = new WebSocket(this.wsUrl); // bind function @@ -333,7 +332,7 @@ import { deepFind } from 'src/plugin/deep' send: (message: object, requestId = uuidv4(), loginRequired) => { if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online - console.log('save msgQueue') + console.log('save msgQueue this.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin) this.wsMsgQueue.push({message, requestId, loginRequired}) } else { let messageStr = JSON.stringify(message) diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 09f89ba4b..d5ac2116f 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -37,7 +37,7 @@
-
+
diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 59184d9b2..d676710f5 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -19,6 +19,7 @@ import { ThemeService } from 'src/app/services/theme.service' import { PreviewCameraPage } from 'src/app/modals/preview-camera/preview-camera.page'; import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page'; import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; +import { ChatService as ChatServiceGPR} from 'src/app/services/chat/chat.service' @Component({ selector: 'app-messages', @@ -77,27 +78,17 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy public ThemeService: ThemeService, private changeDetectorRef: ChangeDetectorRef, private router: Router, + public ChatServiceGPR: ChatServiceGPR ) { + this.loggedUser = authService.ValidatedUserChat['data']; - - /* this.dm = this.navParams.get('dm'); */ } ngOnChanges(changes: SimpleChanges): void { - this.load(); - - //throw new Error('Method not implemented.'); + this.ChatServiceGPR.getRoom(this.roomId).loadHistory() } ngOnInit() { - this.scrollToBottom(); - - /* setInterval(()=>{ */ - this.load(); - /* }, 9000); */ - console.log(this.roomId); - console.log("Chat route", this.route.url) - this.setStatus('online'); } @@ -223,19 +214,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } sendMessage() { - let body = { - "message": - { - "rid": this.roomId, "msg": this.message, - } - } - this.chatService.sendMessage(body).subscribe(res=> { - this.scrollingOnce = true; - }); + this.ChatServiceGPR.getRoom(this.roomId).send(this.message) this.message = ""; } - deleteMessage(msgId:string){ + deleteMessage(msgId:string) { let body = { "roomId": this.roomId, "msgId": msgId, @@ -247,23 +230,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy }); */ } - 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.serverLongPull(res) - /* this.chatService.subscribe(this.roomId).then(res => { - console.log("Real fake msg", res) - }); */ - //this.showLoader = false; - }) - } - async viewDocument(msg:any, url?:string){ if(msg.file.type == "application/img"){ let response:any = await this.fileService.getFile(msg.file.guid).toPromise(); @@ -539,7 +505,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.showLoader = false; //this.addDocGestaoDocumental(); } - this.loadMessages(); }); } @@ -555,7 +520,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy if (res['success'] == true) { // Show Error //showMessage(response.statusText); - this.loadMessages() this.messages = res['messages'].reverse(); this.chatMessageStore.add(roomId, this.messages) @@ -567,7 +531,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy else{ if(document.querySelector('app-messages')){ await new Promise(resolve => setTimeout(resolve, 5000)); - await this.serverLongPull(); + // await this.serverLongPull(); this.getDirectMessages.emit(); console.log('Timer message running') } @@ -580,10 +544,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } */ }, (error)=>{ console.log(error); - this.serverLongPull(); + // this.serverLongPull(); }); - }sliderOpts = { + } + sliderOpts = { zoom: false, slidesPerView: 1.5, spaceBetween: 20,