diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index 879f56f13..d77448c51 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -9,7 +9,7 @@ import { ComponentFactory, Output } from '@angular/core'; -import { ModalController } from '@ionic/angular'; +import { ModalController, Platform } from '@ionic/angular'; import { AuthService } from 'src/app/services/auth.service'; import { ChatService } from 'src/app/services/chat.service'; import { GroupMessagesPage } from './group-messages/group-messages.page'; @@ -30,6 +30,7 @@ import { environment } from 'src/environments/environment'; import { TimeService } from 'src/app/services/functions/time.service'; import { ThemeService } from 'src/app/services/theme.service' import { DataService } from 'src/app/services/data.service'; +import { SqliteService } from 'src/app/services/sqlite.service'; @@ -43,13 +44,13 @@ export class ChatPage implements OnInit { showLoader: boolean; headers: HttpHeaders; - options:any; - X_User_Id:any; - X_Auth_Token:any; + options: any; + X_User_Id: any; + X_Auth_Token: any; loggedUser: any; /* Set segment variable */ - segment:string; + segment: string; allGroups: any[]; privateGroups: any[]; publicGroups: any[]; @@ -57,8 +58,8 @@ export class ChatPage implements OnInit { userRooms: any[]; userChannels: any[]; userDirectMessages: any[]; - result:any; - dmUsers:any[] = []; + result: any; + dmUsers: any[] = []; idSelected: string; desktopComponent: any = { @@ -70,17 +71,17 @@ export class ChatPage implements OnInit { //@ViewChild('groupMessages') child:GroupMessagesPage; componentRef: any; - roomId:any; - groupRoomId:any; - showEmptyComponent=true; - showMessages=false; - showContacts=false; - showNewGroup=false; - showEditGroup=false; - showGroupMessages=false; - showGroupContacts=false; - showNewEvent=false; - showAttendees=false; + roomId: any; + groupRoomId: any; + showEmptyComponent = true; + showMessages = false; + showContacts = false; + showNewGroup = false; + showEditGroup = false; + showGroupMessages = false; + showGroupContacts = false; + showNewEvent = false; + showAttendees = false; emptyTextDescription = 'Sem conversa selecionada'; @Output() getRoomInfo; @@ -97,169 +98,171 @@ export class ChatPage implements OnInit { /* Fim websockets variables*/ - loggedUserChat:any; + loggedUserChat: any; hideRefreshBtn = true; taskParticipants: any = []; taskParticipantsCc: any = []; adding: "intervenient" | "CC" = "intervenient"; - profile:'mdgpr' | 'pr'; + profile: 'mdgpr' | 'pr'; eventSelectedDate: Date = new Date(); - contacts: EventPerson[]; - showEventEditOrOpen: "edit" | "add" | "" | "eventoToApprove" = "" + contacts: EventPerson[]; + showEventEditOrOpen: "edit" | "add" | "" | "eventoToApprove" = "" constructor( - private http:HttpClient, + private http: HttpClient, private chatService: ChatService, private modalController: ModalController, private authService: AuthService, - private storage:Storage, + private storage: Storage, private resolver: ComponentFactoryResolver, private route: Router, private timeService: TimeService, public ThemeService: ThemeService, - private dataService:DataService, + private dataService: DataService, private router: Router, - - ){ - this.loggedUserChat = authService.ValidatedUserChat['data']; - this.headers = new HttpHeaders(); - window.onresize = (event) => { - if( window.innerWidth > 701){ - this.modalController.dismiss(); - } - }; + private sqlservice: SqliteService, + private platform: Platform - } + ) { + this.loggedUserChat = authService.ValidatedUserChat['data']; + this.headers = new HttpHeaders(); + window.onresize = (event) => { + if (window.innerWidth > 701) { + this.modalController.dismiss(); + } + }; + + } ngOnInit() { console.log(this.loggedUserChat); this.segment = "Contactos"; - this.authService.userData$.subscribe((res:any)=>{ - this.loggedUser=res; + this.authService.userData$.subscribe((res: any) => { + this.loggedUser = res; console.log(this.loggedUser); this.load(); }); - /* websocket functions */ - //this.sendMsg(); + /* websocket functions */ + //this.sendMsg(); - /* Fim websocket functions */ - this.hideRefreshButton(); - this.getChatMembers(); + /* Fim websocket functions */ + this.hideRefreshButton(); + this.getChatMembers(); - //Teste - let t = this.showDateDuration(new Date()); - console.log(t); + //Teste + let t = this.showDateDuration(new Date()); + console.log(t); - this.setStatus('away'); - if(this.dataService.get("newGroup")){ - this.openNewGroupPage(); + 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 (window.location.pathname.split('/').length >= 4 && window.location.pathname.startsWith('/home/chat')) { + alert('OIII') + } else { + if (this.dataService.get("newGroup")) { + this.openNewGroupPage(); + } + } + } + }); } - this.router.events.forEach((event) => { - if (event instanceof NavigationStart && event.url.startsWith('/home/chat')) { - if (window.location.pathname.split('/').length >= 4 && window.location.pathname.startsWith('/home/chat')) { - alert('OIII') - } else { - if(this.dataService.get("newGroup")){ - this.openNewGroupPage(); + ngOnDestroy() { + this.setStatus('offline'); + console.log('On Destroy') + } + + setStatus(status: string) { + let body = { + message: '', + status: status, + } + this.chatService.setUserStatus(body).subscribe(res => { + console.log(res); + }) + } + + hideRefreshButton() { + window.onresize = (event) => { + if (window.innerWidth < 701) { + this.idSelected = ''; + this.hideRefreshBtn = false; + } + else { + this.hideRefreshBtn = true; + if (this.idSelected == '') { + this.showEmptyComponent = true; } } } - }); -} - -ngOnDestroy(){ - this.setStatus('offline'); - console.log('On Destroy') -} - -setStatus(status:string){ - let body = { - message: '', - status: status, - } - this.chatService.setUserStatus(body).subscribe(res => { - console.log(res); - }) -} - -hideRefreshButton(){ - window.onresize = (event) => { - if( window.innerWidth < 701) { + if (window.innerWidth < 701) { this.idSelected = ''; this.hideRefreshBtn = false; } - else{ - this.hideRefreshBtn = true; - if(this.idSelected == ''){ - this.showEmptyComponent=true; - } - } } - if(window.innerWidth < 701){ - this.idSelected = ''; - this.hideRefreshBtn = false; - } -} -/* 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 = ""; -} */ + /* 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; - this.showNewGroup=false; - this.showEditGroup=false; - this.showGroupMessages=false; - this.showEmptyComponent=false; - this.showGroupContacts=false; - this.showNewEvent=false; - this.showAttendees=false; + this.showMessages = false; + this.showContacts = false; + this.showNewGroup = false; + this.showEditGroup = false; + this.showGroupMessages = false; + this.showEmptyComponent = false; + this.showGroupContacts = false; + this.showNewEvent = false; + this.showAttendees = false; console.log('All components closed!'); } - showEmptyContainer(){ + showEmptyContainer() { this.idSelected = ''; - this.showEmptyComponent=true; + this.showEmptyComponent = true; } - openGroupContactsPage(data){ + openGroupContactsPage(data) { this.idSelected = ''; this.groupRoomId = data; this.closeAllDesktopComponents(); - if(window.innerWidth < 801){ + if (window.innerWidth < 801) { } - else{ + else { this.showGroupContacts = true; } } openMessagesPage(rid) { - if( window.innerWidth < 701){ + if (window.innerWidth < 701) { this.openMessagesModal(rid); //this.router.navigate(['/home/chat/messages',rid,]); } - else{ + else { this.idSelected = rid; this.closeAllDesktopComponents(); this.showEmptyComponent = false; this.roomId = rid; - this.showMessages=true; + this.showMessages = true; } } openContactsPage() { @@ -267,63 +270,63 @@ hideRefreshButton(){ this.idSelected = ''; this.closeAllDesktopComponents(); - if( window.innerWidth < 701){ + if (window.innerWidth < 701) { this.selectContact(); } - else{ + else { console.log('here'); - this.showContacts=true; + this.showContacts = true; } } openNewGroupPage() { this.idSelected = ''; - if( window.innerWidth < 801){ + if (window.innerWidth < 801) { this.newGroup(); } - else{ + else { this.closeAllDesktopComponents(); - this.showNewGroup=true; + this.showNewGroup = true; } } openEditGroupPage(rid) { - if( window.innerWidth < 801){ + if (window.innerWidth < 801) { this.editGroup(rid); } - else{ + else { this.closeAllDesktopComponents(); - this.showEditGroup=true; + this.showEditGroup = true; } } openGroupMessagesPage(rid) { - if( window.innerWidth < 701){ + if (window.innerWidth < 701) { this.openGroupMessagesModal(rid); } - else{ + else { this.idSelected = rid; this.closeAllDesktopComponents(); this.showEmptyComponent = false; this.roomId = rid; console.log(this.roomId); - this.showGroupMessages=true; + this.showGroupMessages = true; } } - openNewEventPage(data:any){ - this.taskParticipants = data.members.map((val) =>{ + openNewEventPage(data: any) { + this.taskParticipants = data.members.map((val) => { return { Name: val.name, - EmailAddress: val.username+"@"+environment.domain, + EmailAddress: val.username + "@" + environment.domain, IsRequired: "true", } }); this.closeAllDesktopComponents(); - if(window.innerWidth < 701){ + if (window.innerWidth < 701) { console.log('Mobile'); } - else{ - this.showNewEvent=true; + else { + this.showNewEvent = true; } } @@ -337,7 +340,7 @@ hideRefreshButton(){ this.contacts = []; } - async setContact(data:EventPerson[]) { + async setContact(data: EventPerson[]) { this.contacts = data; } @@ -352,7 +355,7 @@ hideRefreshButton(){ async closeAttendeesComponent() { this.closeAllDesktopComponents(); - this.showNewEvent = true; + this.showNewEvent = true; } async closeNewEventComponent() { @@ -361,24 +364,23 @@ hideRefreshButton(){ this.idSelected = ""; } - onSegmentChange(){ + onSegmentChange() { this.load(); } - doRefresh(event){ + doRefresh(event) { setTimeout(() => { this.load(); event.target.complete(); }, 1000); } - refreshing(){ + refreshing() { this.load(); } - load(){ - switch (this.segment) - { + load() { + switch (this.segment) { case "Contactos": this.getDirectMessages(); break; @@ -388,106 +390,193 @@ hideRefreshButton(){ break; } } - customRoom(){ + customRoom() { let params = new HttpParams(); params = params.set("types", "c"); - this.chatService.customsRooms(params).subscribe(res=>{ + this.chatService.customsRooms(params).subscribe(res => { console.log(res); }); } - async getDirectMessages(event?){ + getDirectMessagesDB() { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + } else { + this.sqlservice.getAllChatRoom().then((rooms: any) => { + let roomsArray = []; + rooms.forEach(element => { + let roomListDB = { + _id: element.Id, + uids: JSON.parse(element.Uids), + usernames: JSON.parse(element.Usernames), + lastMessage: JSON.parse(element.LastMessage), + _updatedAt: element.UpdatedAt + } + roomsArray.push(roomListDB) + }); - this.chatService.getAllDirectMessages().subscribe(async (res:any)=>{ - //console.log(res.ims); - - if(res != 200){ - //console.log(res.ims); - this.userDirectMessages = res.ims.sort((a,b)=>{ + this.userDirectMessages = roomsArray.sort((a, b) => { var dateA = new Date(a._updatedAt).getTime(); var dateB = new Date(b._updatedAt).getTime(); return dateB - dateA; }); + console.log('DIRECTMESSAGE FROM DB', this.userDirectMessages); + + console.log('ROOMS FROM DB', rooms) + }) + + this.sqlservice.getAllChatUsers().then((userslist: any) => { + console.log('USERS FOM DB 1', userslist) + let chatusersArray = []; + userslist.forEach(element => { + console.log('USERS FOM DB 2', element) + let userListDB = { + _id: element.Id, + name: element.Name, + username: element.Username + } + + chatusersArray.push(userListDB); + }); + + this.dmUsers = chatusersArray.filter(data => data.username != this.loggedUserChat.me.username); + + }) + } + } + + transformDataRoomList(data) { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + } else { + data.forEach(element => { + let roomList = { + id: element._id, + uids: element.uids, + usernames: element.usernames, + lastMessage: element.lastMessage, + updatedat: element._updatedAt + } + + console.log('TRANSFORM ROOM LIST',roomList ) + this.sqlservice.addChatListRoom(roomList); + }); + } + } + + transformDataUserList(users) { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + } else { + users.forEach(element => { + console.log('TRANSFORM USER CHAT 1', element) + let chatusers = { + id: element._id, + name: element.name, + username: element.username + } + console.log('TRANSFORM USER CHAT 2', chatusers) + this.sqlservice.addChatListUsers(chatusers); + }); + } + } + + async getDirectMessages(event?) { + + this.chatService.getAllDirectMessages().subscribe(async (res: any) => { + + + this.transformDataRoomList(res.ims) + + this.getDirectMessagesDB() + + console.log('Chat list', res); + + if (res != 200) { + //console.log(res.ims); + /* this.userDirectMessages = res.ims.sort((a, b) => { + var dateA = new Date(a._updatedAt).getTime(); + var dateB = new Date(b._updatedAt).getTime(); + return dateB - dateA; + }); */ //console.log(this.userDirectMessages); - if(this.route.url != "/home/chat"){ + if (this.route.url != "/home/chat") { //console.log("Timer message stop") } else { //console.log('TIMER'); //Check if modal is opened - if(this.segment == "Contactos" && this.showMessages != true){ + if (this.segment == "Contactos" && this.showMessages != true) { await new Promise(resolve => setTimeout(resolve, 1000)); await this.getDirectMessages(); //console.log('Timer contactos list running') } - else{ + else { //console.log('No timer!'); } } } - else{ + else { await this.getDirectMessages(); } }); } - showDateDuration(start:any){ + showDateDuration(start: any) { return this.timeService.showDateDuration(start); } - countDownDate(date:any, roomId:string){ + countDownDate(date: any, roomId: string) { return this.timeService.countDownDate(date, roomId); } - async getChatMembers(){ + async getChatMembers() { //return await this.chatService.getMembers(roomId).toPromise(); - this.chatService.getAllUsers().subscribe(res=> { - console.log(res); + this.chatService.getAllUsers().subscribe(res => { + console.log('chatusers', res); + this.transformDataUserList(res['users']) - this.dmUsers = res['users'].filter(data => data.username != this.loggedUserChat.me.username); - console.log(this.dmUsers); + //this.dmUsers = res['users'].filter(data => data.username != this.loggedUserChat.me.username); + //console.log(this.dmUsers); }); } - async getGroups(event?){ - this.result = this.chatService.getAllPrivateGroups().subscribe(async (res:any)=>{ + async getGroups(event?) { + this.result = this.chatService.getAllPrivateGroups().subscribe(async (res: any) => { //console.log(res); - if(res.groups != 200){ + if (res.groups != 200) { - this.privateGroups = res.groups; - /* this.result = this.chatService.getAllUserChannels().subscribe((res:any)=>{ - this.publicGroups = res.channels; */ + this.privateGroups = res.groups; + /* this.result = this.chatService.getAllUserChannels().subscribe((res:any)=>{ + this.publicGroups = res.channels; */ //let all = this.privateGroups.concat(this.publicGroups); - this.allGroups = this.privateGroups.sort((a,b)=>{ + this.allGroups = this.privateGroups.sort((a, b) => { var dateA = new Date(a._updatedAt).getTime(); var dateB = new Date(b._updatedAt).getTime(); return dateB - dateA; }); //console.log(this.allGroups); - /* }); */ - if(this.route.url != "/home/chat"){ - console.log("Timer message stop") + /* }); */ + if (this.route.url != "/home/chat") { + console.log("Timer message stop") + } + else { + //Check if modal is opened + if (this.segment == "Grupos" && this.showGroupMessages != true) { + await new Promise(resolve => setTimeout(resolve, 1000)); + await this.getGroups(); + //console.log('Timer groups list running') + } + } + } else { - //Check if modal is opened - if(this.segment == "Grupos" && this.showGroupMessages != true){ - await new Promise(resolve => setTimeout(resolve, 1000)); - await this.getGroups(); - //console.log('Timer groups list running') - } + await this.getGroups(); } - } - else{ - await this.getGroups(); - } - }); } - async selectContact(){ + async selectContact() { const modal = await this.modalController.create({ component: ContactsPage, cssClass: 'modal modal-desktop', @@ -496,7 +585,7 @@ hideRefreshButton(){ modal.onDidDismiss(); } - async newGroup(){ + async newGroup() { const modal = await this.modalController.create({ component: NewGroupPage, cssClass: 'modal modal-desktop', @@ -505,7 +594,7 @@ hideRefreshButton(){ modal.onDidDismiss(); } - async editGroup(roomId){ + async editGroup(roomId) { const modal = await this.modalController.create({ component: EditGroupPage, cssClass: 'modal modal-desktop', @@ -514,13 +603,13 @@ hideRefreshButton(){ }, }); await modal.present(); - modal.onDidDismiss().then((res)=>{ + modal.onDidDismiss().then((res) => { console.log(res.data); this.modalController.dismiss(res.data); }); } - async openMessagesModal(roomId:any){ + async openMessagesModal(roomId: any) { this.closeAllDesktopComponents(); console.log(roomId); @@ -536,7 +625,7 @@ hideRefreshButton(){ modal.onDidDismiss(); } - async openGroupMessagesModal(roomId:any){ + async openGroupMessagesModal(roomId: any) { console.log(roomId); @@ -551,10 +640,10 @@ hideRefreshButton(){ modal.onDidDismiss(); } -// this.crop.crop('path/to/image.jpg', {quality: 75}) -// .then( -// newImage => console.log('new image path is: ' + newImage), -// error => console.error('Error cropping image', error) -// ); + // this.crop.crop('path/to/image.jpg', {quality: 75}) + // .then( + // newImage => console.log('new image path is: ' + newImage), + // error => console.error('Error cropping image', error) + // ); - } +} diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index cf14f59d9..14deccc04 100644 --- a/src/app/pages/chat/messages/messages.page.html +++ b/src/app/pages/chat/messages/messages.page.html @@ -49,7 +49,7 @@ -->