From e738bf08f39bf3f5948d4f44537613550e467a3a Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 4 Oct 2022 11:33:46 +0100 Subject: [PATCH] improve chat --- .../group-contacts/group-contacts.page.ts | 2 - .../group-messages/group-messages.page.ts | 3 - src/app/pages/chat/messages/messages.page.ts | 1 - src/app/pages/login/login.page.ts | 3 +- src/app/services/chat.service.ts | 4 +- src/app/services/chat/chat-system.service.ts | 61 +++++++++---- src/app/services/chat/room.service.ts | 13 +++ .../group-contacts/group-contacts.page.html | 2 +- .../group-contacts/group-contacts.page.ts | 9 +- .../group-messages/group-messages.page.ts | 1 - .../chat/messages/contacts/contacts.page.html | 2 +- .../chat/messages/contacts/contacts.page.ts | 25 +----- .../shared/chat/messages/messages.page.html | 4 +- .../chat/new-group/contacts/contacts.page.ts | 88 ++----------------- 14 files changed, 74 insertions(+), 144 deletions(-) diff --git a/src/app/pages/chat/group-messages/group-contacts/group-contacts.page.ts b/src/app/pages/chat/group-messages/group-contacts/group-contacts.page.ts index 047c160cb..8c73f3ede 100644 --- a/src/app/pages/chat/group-messages/group-contacts/group-contacts.page.ts +++ b/src/app/pages/chat/group-messages/group-contacts/group-contacts.page.ts @@ -93,14 +93,12 @@ export class GroupContactsPage implements OnInit { if(this.room.t == "p"){ this.chatService.getGroupMembers(this.room._id).subscribe(res=>{ this.members = res['members']; - this.ChatSystemService.getAllRooms() this.loadUsers(); }); } else if(this.room.t == "c"){ this.chatService.getChannelMembers(this.room._id).subscribe(res=>{ this.members = res['members']; - this.ChatSystemService.getAllRooms() this.loadUsers(); }); } 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 21791070d..dee43b41c 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -145,9 +145,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.getChatMembers(); this.getRoomMessageDB(this.roomId); - this.ChatSystemService.getUserOfRoom(this.roomId).then((value) => { - - }) } diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index f52fd4101..a67ea19d3 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -162,7 +162,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { ngOnInit() { this.createDirectoryImage() - this.ChatSystemService.getAllRooms(); this.chatService.refreshtoken(); this.ChatSystemService.getUserOfRoom(this.roomId).then((value) => { diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index ed3ed21fe..8cf9a15af 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -136,6 +136,7 @@ export class LoginPage implements OnInit { await this.authService.loginToChatWs(); this.ChatService.setheader() this.ChatService.refreshtoken(); + this.ChatSystemService.ReLoadChat(); } @@ -164,7 +165,7 @@ export class LoginPage implements OnInit { await this.authService.loginToChatWs(); this.ChatService.setheader(); this.ChatService.refreshtoken(); - this.ChatSystemService.ReLoadChat(); + this.ChatSystemService.loadChat(); } this.getToken(); diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index c88748960..13faac5cf 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -387,9 +387,7 @@ export class ChatService { } catch (error) { this.resetTimer(); - setTimeout(async () => { - await this.refreshtoken(); - }, 3000) + await this.refreshtoken(); } diff --git a/src/app/services/chat/chat-system.service.ts b/src/app/services/chat/chat-system.service.ts index d391bf9d0..1acbb18ad 100644 --- a/src/app/services/chat/chat-system.service.ts +++ b/src/app/services/chat/chat-system.service.ts @@ -39,6 +39,7 @@ export class ChatSystemService { users: chatUser[] = [] sessionStore = SessionStore delete = [] + loadingUsers = false constructor( private RochetChatConnectorService: RochetChatConnectorService, @@ -57,18 +58,20 @@ export class ChatSystemService { private NetworkServiceService: NetworkServiceService, ) { - - - this.loadChat() - this.RochetChatConnectorService.registerCallback({ type: 'reConnect', - funx: () => { + funx: async () => { /** * @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically, * when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish. */ - this.subscribeToRoom() + + await this.chatService.refreshtoken(); + await this.getUser(); + await this.getAllRooms(); + this.subscribeToRoom(); + // + this.getUserStatus(); if(this.currentRoom) { this.currentRoom.loadHistory({forceUpdate: true}) @@ -85,22 +88,25 @@ export class ChatSystemService { } }) + if(this.sessionStore.user.Inactivity) { + this.loadChat(); + } } - private loadChat() { + loadChat() { this.ReLoadChat() } async ReLoadChat() { + await this.chatService.refreshtoken(); + await this.getUser(); await this.restoreRooms(); await this.getAllRooms(); this.subscribeToRoom(); - // - await this.getUser(); this.getUserStatus(); } @@ -590,11 +596,37 @@ export class ChatSystemService { async getUser() { - let _res = await this.ChatService.getAllUsers().toPromise() + this.loadingUsers = true + let _res - let users = _res['users'].filter(data => data.username != SessionStore.user.UserName); + console.log('getuser') - users = users.sort((a,b) => { + try { + _res = await this.ChatService.getAllUsers().toPromise(); + } catch (error) { + await this.chatService.refreshtoken(); + _res = await this.ChatService.getAllUsers().toPromise(); + } + + let users: chatUser[] = _res['users'].filter(data => data.username != SessionStore.user.UserName); + + const userIds = this.users.map((user) => user._id) + + for(let UserUpdate of users) { + if(userIds.includes(UserUpdate._id)) { + console.log("found") + for (var index = 0; index < this.users.length; index++) { + if(UserUpdate._id == this.users[index]._id) { + this.users[index] = UserUpdate + } + } + } else { + this.users.push(UserUpdate) + console.log("push ") + } + } + + this.users = this.users.sort((a,b) => { if(a.name < b.name) { return -1; } @@ -604,11 +636,8 @@ export class ChatSystemService { return 0; }); - users.forEach((user, index) => { - // user[index].status = this.statusNumberToText(user[index].status) - }) + this.loadingUsers = false - this.users = users } getUserOfRoom(roomId){ diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index a0b5136fa..38e55d56d 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -202,6 +202,19 @@ export class RoomService { } + get online() { + + if(!this.isGroup) { + for(let user of this.ChatSystemService.users) { + if(this.membersExcludeMe[0]._id == user._id) { + return user.status + } + } + } + + return 'offline' + } + countDownDate(date) { let difference = new Date(date).getTime() - new Date().getTime(); let c_day = Math.floor(difference/(1000*60*60*24)); diff --git a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html index c5c126e57..fa3f46fe3 100644 --- a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html +++ b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html @@ -48,7 +48,7 @@ - +
{{header}} diff --git a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts index d1b369472..7932464d4 100644 --- a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts +++ b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts @@ -8,6 +8,7 @@ import { NewGroupPage } from '../../new-group/new-group.page'; import { GroupMessagesPage } from '../group-messages.page'; import { ThemeService } from 'src/app/services/theme.service' import { SessionStore } from 'src/app/store/session.service'; +import { ChatSystemService } from 'src/app/services/chat/chat-system.service'; @Component({ selector: 'app-group-contacts', @@ -42,18 +43,14 @@ export class GroupContactsPage implements OnInit { private http: HttpClient, private chatService: ChatService, private authService: AuthService, - public ThemeService: ThemeService - //private navParams: NavParams, + public ThemeService: ThemeService, + public ChatSystemService: ChatSystemService ) { this.loggedUser = authService.ValidatedUserChat['data']; this.textSearch=""; this.dm=null; this.room=null; - /* this.isGroupCreated = this.navParams.get('isCreated'); - this.groupName = this.navParams.get('name'); - this.room = this.navParams.get('room'); - this.members = this.navParams.get('members'); */ } ngOnInit() { 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 317a312ff..4dee1f1e9 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -163,7 +163,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } ngOnInit() { - this.chatService.refreshtoken(); // console.log(this.roomId) this.loggedUser = this.loggedUserChat; //setTimeout(() => { diff --git a/src/app/shared/chat/messages/contacts/contacts.page.html b/src/app/shared/chat/messages/contacts/contacts.page.html index 58b3bc8ad..bb59156f7 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.html +++ b/src/app/shared/chat/messages/contacts/contacts.page.html @@ -22,7 +22,7 @@ - + diff --git a/src/app/shared/chat/messages/contacts/contacts.page.ts b/src/app/shared/chat/messages/contacts/contacts.page.ts index aef23d1ef..15853dad6 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.ts +++ b/src/app/shared/chat/messages/contacts/contacts.page.ts @@ -14,13 +14,10 @@ import { SessionStore } from 'src/app/store/session.service'; styleUrls: ['./contacts.page.scss'], }) export class ContactsPage implements OnInit { - showLoader: boolean; loggedUser: any; - users = []; headers: HttpHeaders; options:any; - contacts:any; textSearch:string; room:any; dm:any; @@ -45,9 +42,6 @@ export class ContactsPage implements OnInit { async ngOnInit() { this.loadUsers(); - await this.chatService.refreshtoken(); - this.loadUsers(); - } onChange(event){ @@ -71,24 +65,7 @@ export class ContactsPage implements OnInit { } loadUsers() { - this.options = { - headers: this.headers, - }; - this.chatService.getAllUsers().subscribe((res:any)=> { - - //this.contacts = res.users.filter(data => data.username != this.sessionStore.user.UserName); - this.contacts = res.users.filter(data => data.username != this.sessionStore.user.UserName); - this.users = this.contacts.sort((a,b) => { - if(a.name < b.name){ - return -1; - } - if(a.name > b.name){ - return 1; - } - return 0; - }); - this.showLoader = false; - }); + this.ChatSystemService.getUser() } separateLetter(record, recordIndex, records){ diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 5568bc0da..dcf18c447 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -3,9 +3,9 @@
- {{ this.ChatSystemService.getDmRoom(this.roomId).name }} + {{ ChatSystemService.getDmRoom(this.roomId).name }} - +