improve chat

This commit is contained in:
Peter Maquiran
2022-10-04 11:33:46 +01:00
parent b3dfb396f3
commit e738bf08f3
14 changed files with 74 additions and 144 deletions
@@ -93,14 +93,12 @@ export class GroupContactsPage implements OnInit {
if(this.room.t == "p"){ if(this.room.t == "p"){
this.chatService.getGroupMembers(this.room._id).subscribe(res=>{ this.chatService.getGroupMembers(this.room._id).subscribe(res=>{
this.members = res['members']; this.members = res['members'];
this.ChatSystemService.getAllRooms()
this.loadUsers(); this.loadUsers();
}); });
} }
else if(this.room.t == "c"){ else if(this.room.t == "c"){
this.chatService.getChannelMembers(this.room._id).subscribe(res=>{ this.chatService.getChannelMembers(this.room._id).subscribe(res=>{
this.members = res['members']; this.members = res['members'];
this.ChatSystemService.getAllRooms()
this.loadUsers(); this.loadUsers();
}); });
} }
@@ -145,9 +145,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
this.getChatMembers(); this.getChatMembers();
this.getRoomMessageDB(this.roomId); this.getRoomMessageDB(this.roomId);
this.ChatSystemService.getUserOfRoom(this.roomId).then((value) => {
})
} }
@@ -162,7 +162,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
ngOnInit() { ngOnInit() {
this.createDirectoryImage() this.createDirectoryImage()
this.ChatSystemService.getAllRooms();
this.chatService.refreshtoken(); this.chatService.refreshtoken();
this.ChatSystemService.getUserOfRoom(this.roomId).then((value) => { this.ChatSystemService.getUserOfRoom(this.roomId).then((value) => {
+2 -1
View File
@@ -136,6 +136,7 @@ export class LoginPage implements OnInit {
await this.authService.loginToChatWs(); await this.authService.loginToChatWs();
this.ChatService.setheader() this.ChatService.setheader()
this.ChatService.refreshtoken(); this.ChatService.refreshtoken();
this.ChatSystemService.ReLoadChat();
} }
@@ -164,7 +165,7 @@ export class LoginPage implements OnInit {
await this.authService.loginToChatWs(); await this.authService.loginToChatWs();
this.ChatService.setheader(); this.ChatService.setheader();
this.ChatService.refreshtoken(); this.ChatService.refreshtoken();
this.ChatSystemService.ReLoadChat(); this.ChatSystemService.loadChat();
} }
this.getToken(); this.getToken();
+1 -3
View File
@@ -387,9 +387,7 @@ export class ChatService {
} catch (error) { } catch (error) {
this.resetTimer(); this.resetTimer();
setTimeout(async () => { await this.refreshtoken();
await this.refreshtoken();
}, 3000)
} }
+45 -16
View File
@@ -39,6 +39,7 @@ export class ChatSystemService {
users: chatUser[] = [] users: chatUser[] = []
sessionStore = SessionStore sessionStore = SessionStore
delete = [] delete = []
loadingUsers = false
constructor( constructor(
private RochetChatConnectorService: RochetChatConnectorService, private RochetChatConnectorService: RochetChatConnectorService,
@@ -57,18 +58,20 @@ export class ChatSystemService {
private NetworkServiceService: NetworkServiceService, private NetworkServiceService: NetworkServiceService,
) { ) {
this.loadChat()
this.RochetChatConnectorService.registerCallback({ this.RochetChatConnectorService.registerCallback({
type: 'reConnect', 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, * @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. * 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) { if(this.currentRoom) {
this.currentRoom.loadHistory({forceUpdate: true}) this.currentRoom.loadHistory({forceUpdate: true})
@@ -85,22 +88,25 @@ export class ChatSystemService {
} }
}) })
if(this.sessionStore.user.Inactivity) {
this.loadChat();
}
} }
private loadChat() { loadChat() {
this.ReLoadChat() this.ReLoadChat()
} }
async ReLoadChat() { async ReLoadChat() {
await this.chatService.refreshtoken(); await this.chatService.refreshtoken();
await this.getUser();
await this.restoreRooms(); await this.restoreRooms();
await this.getAllRooms(); await this.getAllRooms();
this.subscribeToRoom(); this.subscribeToRoom();
// //
await this.getUser();
this.getUserStatus(); this.getUserStatus();
} }
@@ -590,11 +596,37 @@ export class ChatSystemService {
async getUser() { 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) { if(a.name < b.name) {
return -1; return -1;
} }
@@ -604,11 +636,8 @@ export class ChatSystemService {
return 0; return 0;
}); });
users.forEach((user, index) => { this.loadingUsers = false
// user[index].status = this.statusNumberToText(user[index].status)
})
this.users = users
} }
getUserOfRoom(roomId){ getUserOfRoom(roomId){
+13
View File
@@ -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) { countDownDate(date) {
let difference = new Date(date).getTime() - new Date().getTime(); let difference = new Date(date).getTime() - new Date().getTime();
let c_day = Math.floor(difference/(1000*60*60*24)); let c_day = Math.floor(difference/(1000*60*60*24));
@@ -48,7 +48,7 @@
</ion-list> </ion-list>
</div> </div>
<ion-virtual-scroll [items]="users | filter:textSearch: 'name'" approxItemHeight="70px" [headerFn]="separateLetter"> <ion-virtual-scroll [items]="ChatSystemService.users | filter:textSearch: 'name'" approxItemHeight="70px" [headerFn]="separateLetter">
<div class="item-divider" *virtualHeader="let header"> <div class="item-divider" *virtualHeader="let header">
<ion-label>{{header}}</ion-label> <ion-label>{{header}}</ion-label>
@@ -8,6 +8,7 @@ import { NewGroupPage } from '../../new-group/new-group.page';
import { GroupMessagesPage } from '../group-messages.page'; import { GroupMessagesPage } from '../group-messages.page';
import { ThemeService } from 'src/app/services/theme.service' import { ThemeService } from 'src/app/services/theme.service'
import { SessionStore } from 'src/app/store/session.service'; import { SessionStore } from 'src/app/store/session.service';
import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
@Component({ @Component({
selector: 'app-group-contacts', selector: 'app-group-contacts',
@@ -42,18 +43,14 @@ export class GroupContactsPage implements OnInit {
private http: HttpClient, private http: HttpClient,
private chatService: ChatService, private chatService: ChatService,
private authService: AuthService, private authService: AuthService,
public ThemeService: ThemeService public ThemeService: ThemeService,
//private navParams: NavParams, public ChatSystemService: ChatSystemService
) )
{ {
this.loggedUser = authService.ValidatedUserChat['data']; this.loggedUser = authService.ValidatedUserChat['data'];
this.textSearch=""; this.textSearch="";
this.dm=null; this.dm=null;
this.room=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() { ngOnInit() {
@@ -163,7 +163,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
} }
ngOnInit() { ngOnInit() {
this.chatService.refreshtoken();
// console.log(this.roomId) // console.log(this.roomId)
this.loggedUser = this.loggedUserChat; this.loggedUser = this.loggedUserChat;
//setTimeout(() => { //setTimeout(() => {
@@ -22,7 +22,7 @@
<ion-content> <ion-content>
<ion-refresher name="refresher" slot="fixed" (ionRefresh)="doRefresh($event)"> <ion-refresher name="refresher" slot="fixed" (ionRefresh)="doRefresh($event)">
<ion-progress-bar type="indeterminate" *ngIf="showLoader"></ion-progress-bar> <!-- <ion-progress-bar type="indeterminate" *ngIf="showLoader"></ion-progress-bar> -->
<ion-refresher-content> <ion-refresher-content>
</ion-refresher-content> </ion-refresher-content>
</ion-refresher> </ion-refresher>
@@ -14,13 +14,10 @@ import { SessionStore } from 'src/app/store/session.service';
styleUrls: ['./contacts.page.scss'], styleUrls: ['./contacts.page.scss'],
}) })
export class ContactsPage implements OnInit { export class ContactsPage implements OnInit {
showLoader: boolean;
loggedUser: any; loggedUser: any;
users = [];
headers: HttpHeaders; headers: HttpHeaders;
options:any; options:any;
contacts:any;
textSearch:string; textSearch:string;
room:any; room:any;
dm:any; dm:any;
@@ -45,9 +42,6 @@ export class ContactsPage implements OnInit {
async ngOnInit() { async ngOnInit() {
this.loadUsers(); this.loadUsers();
await this.chatService.refreshtoken();
this.loadUsers();
} }
onChange(event){ onChange(event){
@@ -71,24 +65,7 @@ export class ContactsPage implements OnInit {
} }
loadUsers() { loadUsers() {
this.options = { this.ChatSystemService.getUser()
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;
});
} }
separateLetter(record, recordIndex, records){ separateLetter(record, recordIndex, records){
@@ -3,9 +3,9 @@
<div class="main-header"> <div class="main-header">
<div class="header-top"> <div class="header-top">
<div class="middle"> <div class="middle">
<ion-label class="title">{{ this.ChatSystemService.getDmRoom(this.roomId).name }}</ion-label> <ion-label class="title">{{ ChatSystemService.getDmRoom(this.roomId).name }}</ion-label>
<button (click)="ChatMessageDebuggingPage()">Dev</button> <button (click)="ChatMessageDebuggingPage()">Dev</button>
<!-- <span><ion-icon class="{{users.status}}" name="ellipse"></ion-icon></span> --> <span><ion-icon class="{{ ChatSystemService.getDmRoom(this.roomId).online }}" name="ellipse"></ion-icon></span>
</div> </div>
<div hidden class="right"> <div hidden class="right">
<button title="Menu" class="btn-no-color" (click)="_openMessagesOptions()"> <button title="Menu" class="btn-no-color" (click)="_openMessagesOptions()">
@@ -1,8 +1,8 @@
import { HttpHeaders } from '@angular/common/http';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular'; import { ModalController } from '@ionic/angular';
import { GroupMessagesPage } from '../../group-messages/group-messages.page'; import { GroupMessagesPage } from '../../group-messages/group-messages.page';
import { ThemeService } from 'src/app/services/theme.service' import { ThemeService } from 'src/app/services/theme.service'
import { ChatSystemService } from 'src/app/services/chat/chat-system.service'
@Component({ @Component({
selector: 'app-contacts', selector: 'app-contacts',
@@ -10,58 +10,15 @@ import { ThemeService } from 'src/app/services/theme.service'
styleUrls: ['./contacts.page.scss'], styleUrls: ['./contacts.page.scss'],
}) })
export class ContactsPage implements OnInit { export class ContactsPage implements OnInit {
showLoader: boolean;
users = [];
contact: string[] = [" Ana M.", "Andre F.", "Bruno G.", "Catarina T", "Tiago"];
headers: HttpHeaders;
options:any; options:any;
contacts = [
{
first: 'Ana',
last: 'Manuel',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
},
{
first: 'Abdullah',
last: 'Hill',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
},
{
first: 'Batur',
last: 'Oymen',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
},
{
first: 'Bianca',
last: 'Costa',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
},
{
first: 'Zaya',
last: 'Mary',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
},
{
first: 'Tiago',
last: 'Kayaya',
url: 'https://randomuser.me/api/portraits/med/women/54.jpg',
}
];
constructor( constructor(
private modalController: ModalController, private modalController: ModalController,
public ThemeService: ThemeService public ThemeService: ThemeService,
public ChatSystemService: ChatSystemService
) )
{ {
this.headers = new HttpHeaders();
this.headers = this.headers.set('Access-Control-Allow-Origin' , '*');
this.headers = this.headers.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
this.headers = this.headers.set('Accept','application/json');
this.headers = this.headers.set('content-type','application/json');
} }
ngOnInit() { ngOnInit() {
@@ -70,46 +27,11 @@ export class ContactsPage implements OnInit {
} }
loadUsers(){ loadUsers(){
this.options = { this.ChatSystemService.getUser()
headers: this.headers,
};
/* this.http.get('https://randomuser.me/api/?results=100', this.options)
.subscribe(res => {
this.users = res['results'].sort((a,b) => {
if(a.name.first < b.name.first){
return -1;
}
if(a.name.first > b.name.first){
return 1;
}
return 0;
});
}); */
this.users = this.contacts.sort((a,b) => {
if(a.first < b.first){
return -1;
}
if(a.first > b.first){
return 1;
}
return 0;
});
} }
separateLetter(record, recordIndex, records){ separateLetter(record, recordIndex, records){
/* if(recordIndex == 0){
return record.name.first[0];
}
let first_prev = records[recordIndex - 1].name.first[0];
let first_current = record.name.first[0];
if(first_prev != first_current){
return first_current;
}
return null; */
if(recordIndex == 0){ if(recordIndex == 0){
return record.first[0]; return record.first[0];
} }