2021-01-20 13:17:54 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
2021-03-11 16:21:09 +01:00
|
|
|
import {
|
|
|
|
|
Component,
|
|
|
|
|
OnInit,
|
|
|
|
|
ViewChild,
|
|
|
|
|
ViewContainerRef,
|
|
|
|
|
ComponentFactoryResolver,
|
|
|
|
|
ComponentRef,
|
|
|
|
|
ComponentFactory,
|
|
|
|
|
Output
|
|
|
|
|
} from '@angular/core';
|
2020-11-03 11:52:21 +01:00
|
|
|
import { ModalController } from '@ionic/angular';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { AuthService } from 'src/app/services/auth.service';
|
|
|
|
|
import { ChatService } from 'src/app/services/chat.service';
|
2020-11-03 11:52:21 +01:00
|
|
|
import { ConversationPage } from './conversation/conversation.page';
|
2020-12-28 10:11:00 +01:00
|
|
|
import { GroupMessagesPage } from './group-messages/group-messages.page';
|
2021-01-04 12:00:04 +01:00
|
|
|
import { ContactsPage } from './messages/contacts/contacts.page';
|
2021-01-07 09:39:36 +01:00
|
|
|
import { MessagesPage } from './messages/messages.page';
|
2020-12-21 16:37:44 +01:00
|
|
|
import { NewGroupPage } from './new-group/new-group.page';
|
|
|
|
|
import { NewchatPage } from './newchat/newchat.page';
|
2021-02-26 08:38:33 +01:00
|
|
|
import { Storage } from '@ionic/storage';
|
2021-04-13 11:34:52 +01:00
|
|
|
import { AlertService } from 'src/app/services/alert.service';
|
2021-04-14 13:56:38 +01:00
|
|
|
import { EditGroupPage } from 'src/app/shared/chat/edit-group/edit-group.page';
|
2020-09-10 09:48:37 +01:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-chat',
|
|
|
|
|
templateUrl: './chat.page.html',
|
|
|
|
|
styleUrls: ['./chat.page.scss'],
|
|
|
|
|
})
|
|
|
|
|
export class ChatPage implements OnInit {
|
2021-01-13 10:02:30 +01:00
|
|
|
|
2021-01-27 16:01:49 +01:00
|
|
|
showLoader: boolean;
|
|
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
headers: HttpHeaders;
|
|
|
|
|
options:any;
|
|
|
|
|
X_User_Id:any;
|
|
|
|
|
X_Auth_Token:any;
|
|
|
|
|
|
2021-02-26 08:38:33 +01:00
|
|
|
private loggedUser: any;
|
2020-10-30 15:22:35 +01:00
|
|
|
/* Set segment variable */
|
|
|
|
|
segment:string;
|
2021-01-13 10:02:30 +01:00
|
|
|
allGroups: any[];
|
|
|
|
|
privateGroups: any[];
|
|
|
|
|
publicGroups: any[];
|
2020-11-03 11:52:21 +01:00
|
|
|
userConnectedList: any[];
|
2021-01-13 10:02:30 +01:00
|
|
|
userRooms: any[];
|
|
|
|
|
userChannels: any[];
|
|
|
|
|
userDirectMessages: any[];
|
2020-10-30 15:22:35 +01:00
|
|
|
result:any;
|
2021-01-13 10:02:30 +01:00
|
|
|
dmUsers:any;
|
2020-09-10 09:48:37 +01:00
|
|
|
|
2021-03-04 18:50:26 +01:00
|
|
|
desktopComponent: any = {
|
2021-03-05 11:58:59 +01:00
|
|
|
showMessages: false,
|
|
|
|
|
showGroupMessages: false,
|
2021-03-04 18:50:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 16:21:09 +01:00
|
|
|
@ViewChild('messagecontainer', { read: ViewContainerRef }) entry: ViewContainerRef;
|
|
|
|
|
//@ViewChild('groupMessages') child:GroupMessagesPage;
|
|
|
|
|
componentRef: any;
|
|
|
|
|
|
|
|
|
|
roomId:any;
|
2021-03-17 09:06:42 +01:00
|
|
|
groupRoomId:any;
|
2021-03-15 15:19:07 +01:00
|
|
|
showEmptyComponent=true;
|
2021-03-11 16:21:09 +01:00
|
|
|
showMessages=false;
|
2021-03-15 15:19:07 +01:00
|
|
|
showContacts=false;
|
2021-03-15 18:09:41 +01:00
|
|
|
showNewGroup=false;
|
2021-04-14 13:56:38 +01:00
|
|
|
showEditGroup=false;
|
2021-03-11 16:21:09 +01:00
|
|
|
showGroupMessages=false;
|
2021-03-16 14:33:36 +01:00
|
|
|
showGroupContacts=false;
|
2021-03-18 16:30:03 +01:00
|
|
|
emptyTextDescription = 'Sem conversa selecionada';
|
2021-03-11 16:21:09 +01:00
|
|
|
|
|
|
|
|
@Output() getRoomInfo;
|
|
|
|
|
|
2021-03-18 16:30:03 +01:00
|
|
|
|
2020-10-30 15:22:35 +01:00
|
|
|
constructor(
|
2021-01-13 10:02:30 +01:00
|
|
|
private http:HttpClient,
|
2020-10-30 15:22:35 +01:00
|
|
|
private chatService: ChatService,
|
2020-11-03 11:52:21 +01:00
|
|
|
private modalController: ModalController,
|
2021-01-13 10:02:30 +01:00
|
|
|
private authService: AuthService,
|
2021-02-26 08:38:33 +01:00
|
|
|
private storage:Storage,
|
2021-03-11 16:21:09 +01:00
|
|
|
private resolver: ComponentFactoryResolver,
|
2021-01-13 10:02:30 +01:00
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
this.headers = new HttpHeaders();
|
2021-02-26 08:38:33 +01:00
|
|
|
/* this.headers = this.headers.set('X-User-Id', 'GqjNWiLrGEHRna7Zn');
|
|
|
|
|
this.headers = this.headers.set('X-Auth-Token', 'SJwIgtlqfloPK696fpc2VBvyDluipuIHKB_0Q6-9ycJ'); */
|
2021-01-13 10:02:30 +01:00
|
|
|
}
|
2020-09-10 09:48:37 +01:00
|
|
|
ngOnInit() {
|
2020-10-30 15:22:35 +01:00
|
|
|
this.segment = "Contactos";
|
2021-02-26 08:38:33 +01:00
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
this.authService.userData$.subscribe((res:any)=>{
|
|
|
|
|
this.loggedUser=res;
|
|
|
|
|
console.log(this.loggedUser);
|
2021-03-15 10:42:19 +01:00
|
|
|
this.load();
|
2021-01-13 10:02:30 +01:00
|
|
|
});
|
2021-02-26 08:38:33 +01:00
|
|
|
this.storage.get('userDataKey').then(val=> {
|
|
|
|
|
let t = JSON.parse(unescape(atob(val)));
|
|
|
|
|
this.loggedUser=t;
|
2021-03-15 10:42:19 +01:00
|
|
|
//this.load();
|
2021-02-26 08:38:33 +01:00
|
|
|
})
|
|
|
|
|
|
2021-03-11 16:21:09 +01:00
|
|
|
console.log(this.roomId);
|
|
|
|
|
|
2020-09-10 09:48:37 +01:00
|
|
|
}
|
2021-04-13 14:14:55 +01:00
|
|
|
|
2021-03-12 11:56:54 +01:00
|
|
|
closeAllDesktopComponents() {
|
2021-03-11 16:21:09 +01:00
|
|
|
this.showMessages=false;
|
2021-03-15 15:19:07 +01:00
|
|
|
this.showContacts=false;
|
2021-03-15 18:09:41 +01:00
|
|
|
this.showNewGroup=false;
|
2021-04-14 13:56:38 +01:00
|
|
|
this.showEditGroup=false;
|
2021-03-11 16:21:09 +01:00
|
|
|
this.showGroupMessages=false;
|
2021-03-15 15:19:07 +01:00
|
|
|
this.showEmptyComponent=false;
|
2021-03-17 09:06:42 +01:00
|
|
|
this.showGroupContacts=false;
|
2021-03-16 14:33:36 +01:00
|
|
|
console.log('All components closed!');
|
|
|
|
|
}
|
2021-03-17 09:06:42 +01:00
|
|
|
openGroupContactsPage(data){
|
|
|
|
|
console.log(data);
|
|
|
|
|
this.groupRoomId = data;
|
|
|
|
|
console.log(this.groupRoomId);
|
2021-03-16 14:33:36 +01:00
|
|
|
this.closeAllDesktopComponents();
|
|
|
|
|
if(window.innerWidth <= 1024){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.showGroupContacts = true;
|
|
|
|
|
}
|
2021-03-11 16:21:09 +01:00
|
|
|
}
|
|
|
|
|
openMessagesPage(rid) {
|
2021-03-12 11:56:54 +01:00
|
|
|
if( window.innerWidth <= 1024){
|
|
|
|
|
this.openMessagesModal(rid);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.closeAllDesktopComponents();
|
2021-03-15 15:19:07 +01:00
|
|
|
this.showEmptyComponent = false;
|
2021-03-12 11:56:54 +01:00
|
|
|
this.roomId = rid;
|
|
|
|
|
this.showMessages=true;
|
|
|
|
|
}
|
2021-03-11 16:21:09 +01:00
|
|
|
}
|
2021-03-15 15:19:07 +01:00
|
|
|
openContactsPage() {
|
|
|
|
|
console.log('OK');
|
|
|
|
|
this.closeAllDesktopComponents();
|
|
|
|
|
|
|
|
|
|
if( window.innerWidth <= 1024){
|
2021-03-29 16:01:58 +01:00
|
|
|
this.selectContact();
|
2021-03-15 15:19:07 +01:00
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
console.log('here');
|
|
|
|
|
this.showContacts=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-15 18:09:41 +01:00
|
|
|
openNewGroupPage() {
|
|
|
|
|
if( window.innerWidth <= 1024){
|
|
|
|
|
this.newGroup();
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.closeAllDesktopComponents();
|
|
|
|
|
this.showNewGroup=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-14 13:56:38 +01:00
|
|
|
|
|
|
|
|
openEditGroupPage(rid) {
|
|
|
|
|
if( window.innerWidth <= 1024){
|
|
|
|
|
this.editGroup(rid);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.closeAllDesktopComponents();
|
|
|
|
|
this.showEditGroup=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 09:06:42 +01:00
|
|
|
openGroupMessagesPage(data) {
|
|
|
|
|
console.log('HERE');
|
|
|
|
|
|
2021-03-12 11:56:54 +01:00
|
|
|
if( window.innerWidth <= 1024){
|
2021-03-17 09:06:42 +01:00
|
|
|
this.openGroupMessagesModal(data);
|
2021-03-12 11:56:54 +01:00
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
this.closeAllDesktopComponents();
|
2021-03-15 15:19:07 +01:00
|
|
|
this.showEmptyComponent = false;
|
2021-03-17 09:06:42 +01:00
|
|
|
this.roomId = data;
|
2021-03-12 11:56:54 +01:00
|
|
|
this.showGroupMessages=true;
|
|
|
|
|
}
|
2021-03-11 16:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 15:22:35 +01:00
|
|
|
onSegmentChange(){
|
2021-01-27 16:01:49 +01:00
|
|
|
this.load();
|
|
|
|
|
}
|
|
|
|
|
doRefresh(ev:any){
|
|
|
|
|
this.load();
|
|
|
|
|
ev.target.complete();
|
2020-12-16 15:07:28 +01:00
|
|
|
}
|
2021-01-27 16:01:49 +01:00
|
|
|
load(){
|
|
|
|
|
switch (this.segment)
|
|
|
|
|
{
|
|
|
|
|
case "Contactos":
|
|
|
|
|
this.getDirectMessages();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "Grupos":
|
|
|
|
|
this.getGroups();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-09-10 09:48:37 +01:00
|
|
|
}
|
2021-01-20 13:17:54 +01:00
|
|
|
customRoom(){
|
|
|
|
|
let params = new HttpParams();
|
|
|
|
|
params = params.set("types", "c");
|
|
|
|
|
this.chatService.customsRooms(params).subscribe(res=>{
|
|
|
|
|
console.log(res);
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-01-19 16:10:40 +01:00
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
getDirectMessages(){
|
|
|
|
|
this.showLoader = true;
|
|
|
|
|
|
2021-01-19 09:26:33 +01:00
|
|
|
this.chatService.getAllDirectMessages().subscribe((res:any)=>{
|
|
|
|
|
console.log(res.ims);
|
|
|
|
|
|
|
|
|
|
this.userDirectMessages = res.ims.sort((a,b)=>{
|
2021-01-27 11:23:57 +01:00
|
|
|
var dateA = new Date(a._updatedAt).getTime();
|
|
|
|
|
var dateB = new Date(b._updatedAt).getTime();
|
2021-01-19 16:10:40 +01:00
|
|
|
return dateB - dateA;
|
2021-01-19 09:26:33 +01:00
|
|
|
});
|
|
|
|
|
console.log(this.userDirectMessages);
|
2021-02-11 10:40:12 +01:00
|
|
|
this.showLoader = false;
|
2021-01-13 10:02:30 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
getChatMembers(){
|
|
|
|
|
this.chatService.getMembers(this.userDirectMessages[0]._id).subscribe(res=> {
|
|
|
|
|
this.dmUsers = res['members'].filter(data => data.username != this.loggedUser.me.username)
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-01-19 16:10:40 +01:00
|
|
|
getGroups(){
|
|
|
|
|
this.showLoader = true;
|
|
|
|
|
this.result = this.chatService.getAllPrivateGroups().subscribe((res:any)=>{
|
|
|
|
|
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 = all.sort((a,b)=>{
|
2021-01-20 16:58:04 +01:00
|
|
|
var dateA = new Date(a._updatedAt).getTime();
|
|
|
|
|
var dateB = new Date(b._updatedAt).getTime();
|
2021-01-19 16:10:40 +01:00
|
|
|
return dateB - dateA;
|
|
|
|
|
});
|
|
|
|
|
console.log(this.allGroups);
|
2021-02-11 10:40:12 +01:00
|
|
|
this.showLoader = false;
|
2021-01-19 16:10:40 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-01-27 12:45:48 +01:00
|
|
|
|
2020-12-16 16:06:14 +01:00
|
|
|
async startConversation(selectedUser) {
|
2020-11-03 11:52:21 +01:00
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: ConversationPage,
|
|
|
|
|
cssClass: 'conversation',
|
|
|
|
|
backdropDismiss: false,
|
|
|
|
|
componentProps: {
|
|
|
|
|
user: selectedUser,
|
|
|
|
|
}
|
2020-10-30 15:22:35 +01:00
|
|
|
});
|
2020-11-03 11:52:21 +01:00
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
2020-12-21 16:37:44 +01:00
|
|
|
async selectContact(){
|
|
|
|
|
const modal = await this.modalController.create({
|
2021-01-04 12:00:04 +01:00
|
|
|
component: ContactsPage,
|
|
|
|
|
cssClass: 'contacts',
|
2020-12-21 16:37:44 +01:00
|
|
|
backdropDismiss: false,
|
|
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
|
|
|
|
async newGroup(){
|
|
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: NewGroupPage,
|
|
|
|
|
cssClass: 'new-group',
|
|
|
|
|
backdropDismiss: false,
|
|
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
2021-04-14 13:56:38 +01:00
|
|
|
|
|
|
|
|
async editGroup(roomId){
|
|
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: EditGroupPage,
|
|
|
|
|
cssClass: 'edit-group',
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: roomId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 11:56:54 +01:00
|
|
|
async openMessagesModal(roomId:any){
|
2021-03-18 16:30:03 +01:00
|
|
|
this.closeAllDesktopComponents();
|
2021-03-04 18:50:26 +01:00
|
|
|
|
|
|
|
|
let classs;
|
|
|
|
|
if( window.innerWidth <= 1024){
|
|
|
|
|
classs = 'modal'
|
|
|
|
|
} else {
|
|
|
|
|
classs = 'chat-mobile-modal-to-Desktop'
|
|
|
|
|
}
|
2021-03-12 11:56:54 +01:00
|
|
|
console.log(roomId);
|
2021-01-14 16:51:16 +01:00
|
|
|
|
2020-12-30 11:13:50 +01:00
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: MessagesPage,
|
2021-03-04 19:12:42 +01:00
|
|
|
cssClass: classs,
|
2020-12-30 11:13:50 +01:00
|
|
|
backdropDismiss: false,
|
2021-01-13 10:02:30 +01:00
|
|
|
componentProps: {
|
2021-03-12 11:56:54 +01:00
|
|
|
roomId: roomId,
|
2021-01-13 10:02:30 +01:00
|
|
|
},
|
2020-12-30 11:13:50 +01:00
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
2021-03-05 11:58:59 +01:00
|
|
|
|
2021-03-12 17:35:54 +01:00
|
|
|
async openGroupMessagesModal(roomId:any){
|
2021-03-04 19:12:42 +01:00
|
|
|
let classs;
|
|
|
|
|
if( window.innerWidth <= 1024){
|
|
|
|
|
classs = 'modal'
|
|
|
|
|
} else {
|
|
|
|
|
classs = 'chat-mobile-modal-to-Desktop'
|
|
|
|
|
}
|
2021-03-12 17:35:54 +01:00
|
|
|
console.log(roomId);
|
2021-01-19 16:10:40 +01:00
|
|
|
|
2020-12-28 10:11:00 +01:00
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: GroupMessagesPage,
|
2021-03-04 19:12:42 +01:00
|
|
|
cssClass: classs,
|
2020-12-28 10:11:00 +01:00
|
|
|
backdropDismiss: false,
|
2021-01-13 10:02:30 +01:00
|
|
|
componentProps: {
|
2021-03-12 17:35:54 +01:00
|
|
|
roomId: roomId,
|
2021-01-13 10:02:30 +01:00
|
|
|
},
|
2020-12-28 10:11:00 +01:00
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
2020-09-10 09:48:37 +01:00
|
|
|
}
|