From a8a1307becf34cabf01b3d1705c6e082dda63a07 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 13:03:43 +0100 Subject: [PATCH 1/8] add user list --- src/app/pages/chat/chat.page.html | 104 +++++++++++++------------- src/app/pages/chat/chat.page.ts | 55 +++++++------- src/app/services/chat/chat.service.ts | 73 +++++++++++++++--- src/app/services/chat/room.service.ts | 13 +++- 4 files changed, 153 insertions(+), 92 deletions(-) diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index d65c8aa73..c6dcc827c 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -43,64 +43,64 @@
- -
-
- - - -
-
-
-
- - -
-
- {{t.name}} -
+ +
+
+ + + +
+
+
+
+ + +
+
+ {{room.value.name}}
- - +
+
+
+
+ +
+
+ {{room.value.lastMessage.msg}} + + + + {{room.value.lastMessage.file.name || room.value.lastMessage.file.subject }} + + +
+ + Fotografia
-
{{showDateDuration(dm._updatedAt)}}
-
-
- {{dm.lastMessage.msg}} - - - - {{dm.lastMessage.file.name || dm.lastMessage.file.subject }} - - -
- - Fotografia -
-
-
+
-
- -
-
-
-
-
-
-
-
- - -
+
+ + +
+
+
+
+
+
+
+
+ +
- +
+
diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index 247df586f..df13a2278 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -12,6 +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 { ChatService as ChatServiceGPR} 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'; @@ -123,9 +124,11 @@ export class ChatPage implements OnInit { private router: Router, private sqlservice: SqliteService, private platform: Platform, - private storageservice: StorageService + private storageservice: StorageService, + public ChatServiceGPR: ChatServiceGPR ) { + this.loggedUserChat = authService.ValidatedUserChat['data']; this.headers = new HttpHeaders(); window.onresize = (event) => { @@ -142,44 +145,44 @@ export class ChatPage implements OnInit { this.segment = "Contactos"; - this.authService.userData$.subscribe((res: any) => { - this.loggedUser = res; - console.log(this.loggedUser); - this.load(); + // this.authService.userData$.subscribe((res: any) => { + // this.loggedUser = res; + // console.log(this.loggedUser); + // this.load(); - this.getDirectMessagesDB(); - }); + // // this.getDirectMessagesDB(); + // }); /* websocket functions */ //this.sendMsg(); /* Fim websocket functions */ - this.hideRefreshButton(); - this.getChatMembers(); + // this.hideRefreshButton(); + // this.getChatMembers(); //Teste - let t = this.showDateDuration(new Date()); - console.log(t); + // let t = this.showDateDuration(new Date()); + // console.log(t); - this.setStatus('away'); + // this.setStatus('away'); /* if(this.dataService.get("newGroup")){ this.openNewGroupPage(); } */ - this.router.events.forEach((event) => { - if (event instanceof NavigationStart && event.url.startsWith('/home/chat')) { - if (this.dataService.get("newGroup")) { - this.openNewGroupPage(); - } - else{ - this.closeAllDesktopComponents(); - this.showEmptyComponent = true; - } - } - else{ - this.dataService.set("newGroup", false); - } - }); + // this.router.events.forEach((event) => { + // if (event instanceof NavigationStart && event.url.startsWith('/home/chat')) { + // if (this.dataService.get("newGroup")) { + // this.openNewGroupPage(); + // } + // else{ + // this.closeAllDesktopComponents(); + // this.showEmptyComponent = true; + // } + // } + // else{ + // this.dataService.set("newGroup", false); + // } + // }); } numSequence(n: number): Array { diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index 1686dd66a..89c2b440f 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -2,14 +2,21 @@ import { Injectable } from '@angular/core'; import { RoomService } from './room.service'; import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service'; import { MessageService } from 'src/app/services/chat/message.service' +import { SessionStore } from 'src/app/store/session.service'; @Injectable({ providedIn: 'root' }) export class ChatService { - rooms: RoomService[] = [] - group = [] + + individual: {[key: string]: RoomService} = {} + group: {[key: string]: RoomService} = {} + + loadingWholeList = false + + individualCount = 0; + groupCount = 0; constructor( private RocketChatClientService: RocketChatClientService @@ -17,17 +24,63 @@ export class ChatService { this.getAllRoomAndSubscribe() } - getAllRoomAndSubscribe() { - this.RocketChatClientService.getRooms().then((rooms: any) => { - rooms.result.update.forEach((roomData:any) => { - const room = new RoomService(new RocketChatClientService(), new MessageService()) - room.setData({id: roomData.lastMessage.rid}) - }); + async getAllRoomAndSubscribe () { + this.loadingWholeList = true + + const rooms: any = await this.RocketChatClientService.getRooms(); + + rooms.result.update.forEach((roomData:any) => { + let room + let roomId = roomData.lastMessage.rid + if(this.isIndividual(roomData)) { + room = new RoomService(new RocketChatClientService(), new MessageService()) + room.setData({ + id: this.getRoomId(roomData), + name: this.getChatName(roomData), + lastMessage: this.getRoomLastMessage(roomData) + }) + this.individual[roomId] = room + this.individualCount++ + } else { + room = new RoomService(new RocketChatClientService(), new MessageService()) + room.setData({ + id: this.getRoomId(roomData), + name: this.getChatName(roomData), + lastMessage: this.getRoomLastMessage(roomData) + }) + this.group[roomId] = room + this.groupCount++ + } + }); + + this.loadingWholeList = false + } - onJoinRoom() { - // live + getChatName(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/chat/room.service.ts b/src/app/services/chat/room.service.ts index c921a754c..67a751fe9 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -7,17 +7,22 @@ import { ChatUserService } from 'src/app/services/chat/chat-user.service' }) export class RoomService { - massages: MessageService[] = [] + massages: {[key: string]: MessageService} = {} + lastMessage: MessageService; + chatUser: ChatUserService[] = [] - id = [] + id = '' + name = '' constructor( private RocketChatClientService: RocketChatClientService, private MessageService: MessageService ) {} - setData({id}){ - this.id= id + setData({id, name, lastMessage}) { + this.id = id + this.name = name + this.lastMessage = lastMessage } create() {} From dcee5aa2f4d8839898f26dd0dc581bd3352a6153 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 15:43:09 +0100 Subject: [PATCH 2/8] improve chat remove loop --- src/app/home/home.page.ts | 22 -------- .../pages/chat/messages/messages.page.html | 2 +- src/app/pages/chat/messages/messages.page.ts | 9 ++- src/app/services/chat/chat.service.ts | 41 ++++++++++++-- src/app/services/chat/message.service.ts | 4 ++ src/app/services/chat/room.service.ts | 40 ++++++++++++-- .../socket/rocket-chat-client.service.ts | 5 +- .../shared/chat/messages/messages.page.html | 2 +- src/app/shared/chat/messages/messages.page.ts | 55 ++++--------------- 9 files changed, 96 insertions(+), 84 deletions(-) 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, From ab2198a4120ccfd1cec385e93fd7d4b30fee15cd Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 16:07:54 +0100 Subject: [PATCH 3/8] improve chat --- src/app/pages/chat/chat.page.ts | 64 ++----------------- .../pages/chat/messages/messages.page.html | 2 +- src/app/services/chat/message.service.ts | 19 ++++++ src/app/services/chat/room.service.ts | 15 +++-- .../shared/chat/messages/messages.page.html | 2 +- 5 files changed, 37 insertions(+), 65 deletions(-) diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index df13a2278..712b4bddc 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -136,7 +136,9 @@ export class ChatPage implements OnInit { this.modalController.dismiss(); } }; + this.showLoader = true; + this.load() } ngOnInit() { @@ -144,45 +146,6 @@ export class ChatPage implements OnInit { console.log(this.loggedUserChat); this.segment = "Contactos"; - - // this.authService.userData$.subscribe((res: any) => { - // this.loggedUser = res; - // console.log(this.loggedUser); - // this.load(); - - // // this.getDirectMessagesDB(); - // }); - - /* websocket functions */ - //this.sendMsg(); - - /* Fim websocket functions */ - // this.hideRefreshButton(); - // this.getChatMembers(); - - //Teste - // let t = this.showDateDuration(new Date()); - // console.log(t); - - // this.setStatus('away'); - /* if(this.dataService.get("newGroup")){ - this.openNewGroupPage(); - } */ - - // this.router.events.forEach((event) => { - // if (event instanceof NavigationStart && event.url.startsWith('/home/chat')) { - // if (this.dataService.get("newGroup")) { - // this.openNewGroupPage(); - // } - // else{ - // this.closeAllDesktopComponents(); - // this.showEmptyComponent = true; - // } - // } - // else{ - // this.dataService.set("newGroup", false); - // } - // }); } numSequence(n: number): Array { @@ -224,21 +187,6 @@ export class ChatPage implements OnInit { } } - - /* loadMessage(){ - this.chatService.messages.subscribe(msg => { - console.log("Response from websocket: " + msg); - }); - } */ - /* sendMsg() { - console.log("new message from client to websocket: ", this.message); - this.chatService.messages.next(this.message); - this.message.msg = ""; - } */ - - - /* Fim websockets functions */ - closeAllDesktopComponents() { this.showMessages = false; this.showContacts = false; @@ -570,7 +518,7 @@ export class ChatPage implements OnInit { //Check if modal is opened if (this.segment == "Contactos" && this.showMessages != true) { await new Promise(resolve => setTimeout(resolve, 1000)); - await this.getDirectMessages(); + //await this.getDirectMessages(); //console.log('Timer contactos list running') } else { @@ -579,7 +527,7 @@ export class ChatPage implements OnInit { } } else { - await this.getDirectMessages(); + //await this.getDirectMessages(); } }); } @@ -734,12 +682,12 @@ export class ChatPage implements OnInit { //Check if modal is opened if (this.segment == "Grupos" && this.showGroupMessages != true) { await new Promise(resolve => setTimeout(resolve, 1000)); - await this.getGroups(); + //await this.getGroups(); } } } else { - await this.getGroups(); + //await this.getGroups(); } }); } diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index dd895bbfc..836d6ff58 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/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 0637c7516..66e228556 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -9,5 +9,24 @@ export class MessageService { mentions = [] msg = '' rid = '' + ts = {} + u = {} + _id ='' + _updatedAt = {} + constructor() { } + + setData({channels, mentions, msg ,rid ,ts, u, _id, _updatedAt}) { + this.channels = channels + this.mentions = mentions + this.msg = msg + this.rid = rid + this.ts = ts + this.u = u + this._id = _id + this._updatedAt = _updatedAt + } + + delete() {} + } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 873b3d240..2c483140d 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -7,7 +7,7 @@ import { ChatUserService } from 'src/app/services/chat/chat-user.service' }) export class RoomService { - massages: {[key: string]: MessageService} = {} + massages: MessageService[] = [] lastMessage: MessageService; chatUser: ChatUserService[] = [] @@ -35,8 +35,7 @@ export class RoomService { this.id, this.constructor.name+this.id, (Chatmessage)=>{ - this.hasLoadHistory = false - this.loadHistory() + this.massages.push(Chatmessage.result) } ) } @@ -50,9 +49,15 @@ export class RoomService { if(this.hasLoadHistory){ return false } - this.RocketChatClientService.loadHistory(this.id, limit).then((message:any)=>{ + this.RocketChatClientService.loadHistory(this.id, limit).then((message:any) => { console.log('loadHistory', message) - this.mmm = message.result.messages.reverse() + + message.result.messages.reverse().forEach(element => { + const message = new MessageService() + message.setData(element) + this.massages.push(message) + }); + }) this.hasLoadHistory = true diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index d5ac2116f..cf0913ab6 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -37,7 +37,7 @@
-
+
From e8a13f08d93707be0152be5a9afe40fc606ff2c8 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 16:09:14 +0100 Subject: [PATCH 4/8] improve --- src/app/pages/chat/chat.page.html | 2 -- src/app/services/chat/room.service.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index c6dcc827c..27f9acffc 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -259,5 +259,3 @@
- - diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 2c483140d..482e33dce 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -13,7 +13,6 @@ export class RoomService { chatUser: ChatUserService[] = [] id = '' name = '' - mmm = [] private hasLoadHistory = false constructor( From 5ffcadbd40335fc31441f0176597385d5f8519b1 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 16:28:59 +0100 Subject: [PATCH 5/8] add showDateDuration method --- src/app/services/chat/message.service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 66e228556..47c0c09cc 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -29,4 +29,6 @@ export class MessageService { delete() {} + showDateDuration() {} + } From e79501e83cf56ddf6c8ee29d987c5e8948a69e0c Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 19:49:43 +0100 Subject: [PATCH 6/8] improve message --- src/app/home/home.page.ts | 5 ----- src/app/pages/chat/chat.page.html | 2 +- src/app/services/chat/room.service.ts | 6 ++++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 8b28279b0..c39f473f3 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -94,11 +94,6 @@ export class HomePage implements OnInit { private sqliteservice: SqliteService, private RouteService: RouteService, private RocketChatClientService: RocketChatClientService) { - - this.RocketChatClientService.loadHistory('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 10).then((message)=>{ - console.log('loadHistory', message) - }) - window['jj'] = ()=>{ //send message // roomId // Message this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 27f9acffc..09b092845 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -86,7 +86,7 @@
- +
diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 482e33dce..d0af942f4 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -33,8 +33,10 @@ export class RoomService { this.RocketChatClientService.receiveLiveMessageFromRoom( this.id, this.constructor.name+this.id, - (Chatmessage)=>{ - this.massages.push(Chatmessage.result) + (Chatmessage) => { + const message = new MessageService() + message.setData(Chatmessage.result) + this.massages.push(message) } ) } From 890048dc8731519410b1a06ad8ff8b98c508dd5a Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 20:33:46 +0100 Subject: [PATCH 7/8] improve create room --- src/plugin/showDateDuration.js | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/plugin/showDateDuration.js diff --git a/src/plugin/showDateDuration.js b/src/plugin/showDateDuration.js new file mode 100644 index 000000000..4e8301a3b --- /dev/null +++ b/src/plugin/showDateDuration.js @@ -0,0 +1,42 @@ + +function addZero(i) { + if (i < 10) { + i = "0" + i; + } + return i; +} + +function showDateDuration(start) { + let end; + end = new Date(); + start = new Date(start); + let customizedDate; + + + const totalSeconds = Math.floor((end - (start))/1000);; + const totalMinutes = Math.floor(totalSeconds/60); + const totalHours = Math.floor(totalMinutes/60); + const totalDays = Math.floor(totalHours/24); + + const hours = totalHours - ( totalDays * 24 ); + const minutes = totalMinutes - ( totalDays * 24 * 60 ) - ( hours * 60 ); + const seconds = totalSeconds - ( totalDays * 24 * 60 * 60 ) - ( hours * 60 * 60 ) - ( minutes * 60 ); + + if(totalDays == 0){ + if(start.getDate() == new Date().getDate()){ + let time = start.getHours() + ":" + addZero(start.getUTCMinutes()); + return time; + } + else{ + return 'Ontem'; + } + } + else{ + let date = addZero(start.getDate()) + "/" + addZero(start.getMonth()+1) + "/" + start.getFullYear(); + return date; + } +} + +module.exports = { + showDateDuration: showDateDuration +}; \ No newline at end of file From b8b4ba6a05ade56dc2c74b80d7641551ce41c15e Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jan 2022 20:56:21 +0100 Subject: [PATCH 8/8] improve chat --- src/app/home/home.page.ts | 2 +- src/app/pages/chat/chat.page.html | 2 +- src/app/services/chat/chat.service.ts | 25 ++++++++----------- src/app/services/chat/room.service.ts | 36 +++++++++++++++++++++++---- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index c39f473f3..656e38716 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -94,7 +94,7 @@ export class HomePage implements OnInit { private sqliteservice: SqliteService, private RouteService: RouteService, private RocketChatClientService: RocketChatClientService) { - window['jj'] = ()=>{ + window['jj'] = ()=> { //send message // roomId // Message this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) } diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 09b092845..141e2871c 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -67,7 +67,7 @@
- +
{{room.value.duration}}
{{room.value.lastMessage.msg}} diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index b8e1e92c4..501c26195 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -36,26 +36,23 @@ export class ChatService { rooms.result.update.forEach((roomData:any) => { let room:RoomService; + + room = new RoomService(this.RocketChatClientService, new MessageService()) + room.setData({ + id: this.getRoomId(roomData), + name: this.getChatName(roomData), + lastMessage: this.getRoomLastMessage(roomData), + _updatedAt: roomData._updatedAt['$date'] + }) + + room.receiveMessage() let roomId = roomData.lastMessage.rid + if(this.isIndividual(roomData)) { - 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(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++ } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index d0af942f4..ef85e84a0 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -2,6 +2,9 @@ import { Injectable } from '@angular/core' import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service'; import { MessageService } from 'src/app/services/chat/message.service' import { ChatUserService } from 'src/app/services/chat/chat-user.service' +import { TimeService } from 'src/app/services/functions/time.service'; +import { ChatService } from '../chat.service'; +import { showDateDuration } from 'src/plugin/showDateDuration' @Injectable({ providedIn: 'root' }) @@ -13,20 +16,24 @@ export class RoomService { chatUser: ChatUserService[] = [] id = '' name = '' + _updatedAt = {} private hasLoadHistory = false + duration = '' + constructor( public RocketChatClientService: RocketChatClientService, - private MessageService: MessageService - ) { - - } + private MessageService: MessageService, + ) {} - setData({id, name, lastMessage}) { + setData({id, name, lastMessage, _updatedAt}) { this.id = id this.name = name this.lastMessage = lastMessage + this._updatedAt = _updatedAt + + this.calDateDuration() } receiveMessage() { @@ -34,9 +41,12 @@ export class RoomService { this.id, this.constructor.name+this.id, (Chatmessage) => { + + Chatmessage = this.fix_updatedAt(Chatmessage) const message = new MessageService() message.setData(Chatmessage.result) this.massages.push(message) + this.calDateDuration(Chatmessage.result._updatedAt) } ) } @@ -54,6 +64,8 @@ export class RoomService { console.log('loadHistory', message) message.result.messages.reverse().forEach(element => { + console.log('element', element) + element = this.fix_updatedAt(element) const message = new MessageService() message.setData(element) this.massages.push(message) @@ -68,4 +80,18 @@ export class RoomService { deleteMessage(msgId) {} ReactToMessage() {} + private calDateDuration(date = null) { + this.duration = showDateDuration(date || this._updatedAt); + } + + + private fix_updatedAt(message) { + if(message.result) { + message.result._updatedAt = message.result._updatedAt['$date'] + } else{ + message._updatedAt = message._updatedAt['$date'] + } + return message + } + }