Files
doneit-web/src/app/pages/chat/chat.page.ts
T

535 lines
13 KiB
TypeScript
Raw Normal View History

2021-01-20 13:17:54 +01:00
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
2021-07-23 14:43:51 +01:00
import {
Component,
2021-03-11 16:21:09 +01:00
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-12-28 10:11:00 +01:00
import { GroupMessagesPage } from './group-messages/group-messages.page';
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';
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';
2021-05-18 14:37:43 +01:00
import * as Rx from "rxjs/Rx";
import { Message } from 'src/app/models/message.model';
import { Observable, Subject } from "rxjs/Rx";
2021-08-23 16:31:06 +01:00
import { NavigationStart, NavigationEnd, Router } from '@angular/router';
2021-08-18 16:40:58 +01:00
import { EventPerson } from 'src/app/models/eventperson.model';
import { removeDuplicate } from 'src/plugin/removeDuplicate.js'
import { environment } from 'src/environments/environment';
2021-08-25 11:51:02 +01:00
import { NotificationsService } from 'src/app/services/notifications.service';
2021-09-06 17:02:45 +01:00
import { TimeService } from 'src/app/services/functions/time.service';
2021-10-21 15:55:54 +01:00
import { ThemeService } from 'src/app/services/theme.service'
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-07-23 14:43:51 +01:00
2021-06-03 17:07:29 +01:00
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-08-19 18:54:50 +01:00
dmUsers:any[] = [];
2021-07-27 00:01:38 +01:00
idSelected: string;
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-08-18 16:40:58 +01:00
showNewEvent=false;
showAttendees=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
2021-05-18 14:37:43 +01:00
subject: any;
public messages: Subject<any>;
message = {
"msg": "connect",
"version": "1",
"support": ["1"]
};
2021-05-18 09:37:23 +01:00
/* Fim websockets variables*/
2021-06-04 11:37:56 +01:00
loggedUserChat:any;
2021-06-17 16:43:18 +01:00
hideRefreshBtn = true;
2021-06-04 11:37:56 +01:00
2021-08-18 16:40:58 +01:00
taskParticipants: any = [];
taskParticipantsCc: any = [];
adding: "intervenient" | "CC" = "intervenient";
profile:'mdgpr' | 'pr';
eventSelectedDate: Date = new Date();
contacts: EventPerson[];
showEventEditOrOpen: "edit" | "add" | "" | "eventoToApprove" = ""
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-08-20 19:10:13 +01:00
private route: Router,
2021-09-06 17:02:45 +01:00
private timeService: TimeService,
2021-10-21 15:55:54 +01:00
public ThemeService: ThemeService
2021-07-23 14:43:51 +01:00
){
2021-06-04 11:37:56 +01:00
this.loggedUserChat = authService.ValidatedUserChat['data'];
2021-01-13 10:02:30 +01:00
this.headers = new HttpHeaders();
2021-09-03 17:02:42 +01:00
window.onresize = (event) => {
if( window.innerWidth > 701){
this.modalController.dismiss();
}
};
2021-08-23 16:31:06 +01:00
2021-01-13 10:02:30 +01:00
}
2021-05-18 14:37:43 +01:00
2020-09-10 09:48:37 +01:00
ngOnInit() {
2021-06-04 11:37:56 +01:00
console.log(this.loggedUserChat);
2021-07-23 14:43:51 +01:00
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-07-23 14:43:51 +01:00
2021-05-18 09:37:23 +01:00
/* websocket functions */
2021-05-18 14:37:43 +01:00
//this.sendMsg();
/* Fim websocket functions */
2021-07-23 14:43:51 +01:00
this.hideRefreshButton();
2021-08-19 18:54:50 +01:00
this.getChatMembers();
2021-08-20 16:38:58 +01:00
//Teste
let t = this.showDateDuration(new Date());
console.log(t);
2021-08-23 16:31:06 +01:00
this.setStatus('away');
}
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);
})
2021-05-18 14:37:43 +01:00
}
2021-06-17 16:43:18 +01:00
hideRefreshButton(){
window.onresize = (event) => {
2021-08-19 18:54:50 +01:00
if( window.innerWidth < 701) {
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-06-17 16:43:18 +01:00
this.hideRefreshBtn = false;
}
else{
this.hideRefreshBtn = true;
2021-08-20 11:24:42 +01:00
if(this.idSelected == ''){
this.showEmptyComponent=true;
}
2021-06-17 16:43:18 +01:00
}
}
2021-08-19 18:54:50 +01:00
if(window.innerWidth < 701){
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-06-17 16:43:18 +01:00
this.hideRefreshBtn = false;
}
}
2021-07-26 09:44:11 +01:00
/* loadMessage(){
2021-05-19 09:37:43 +01:00
this.chatService.messages.subscribe(msg => {
console.log("Response from websocket: " + msg);
});
2021-07-26 09:44:11 +01:00
} */
/* sendMsg() {
2021-05-18 14:37:43 +01:00
console.log("new message from client to websocket: ", this.message);
this.chatService.messages.next(this.message);
this.message.msg = "";
2021-07-26 09:44:11 +01:00
} */
2021-05-18 09:37:23 +01:00
/* Fim websockets functions */
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-08-18 16:40:58 +01:00
this.showNewEvent=false;
this.showAttendees=false;
2021-03-16 14:33:36 +01:00
console.log('All components closed!');
}
2021-08-18 16:40:58 +01:00
2021-04-15 23:57:14 +01:00
showEmptyContainer(){
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-04-15 23:57:14 +01:00
this.showEmptyComponent=true;
}
2021-03-17 09:06:42 +01:00
openGroupContactsPage(data){
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-03-17 09:06:42 +01:00
this.groupRoomId = data;
2021-03-16 14:33:36 +01:00
this.closeAllDesktopComponents();
2021-07-26 10:09:33 +01:00
if(window.innerWidth < 801){
2021-03-16 14:33:36 +01:00
}
else{
this.showGroupContacts = true;
}
2021-03-11 16:21:09 +01:00
}
openMessagesPage(rid) {
2021-07-27 09:14:35 +01:00
if( window.innerWidth < 701){
2021-07-26 09:55:57 +01:00
this.openMessagesModal(rid);
2021-07-27 00:21:30 +01:00
//this.router.navigate(['/home/chat/messages',rid,]);
2021-03-12 11:56:54 +01:00
}
else{
2021-08-20 11:24:42 +01:00
this.idSelected = rid;
2021-03-12 11:56:54 +01:00
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');
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-03-15 15:19:07 +01:00
this.closeAllDesktopComponents();
2021-07-23 14:43:51 +01:00
2021-07-27 09:14:35 +01:00
if( window.innerWidth < 701){
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() {
2021-08-20 11:24:42 +01:00
this.idSelected = '';
2021-07-26 10:09:33 +01:00
if( window.innerWidth < 801){
2021-03-15 18:09:41 +01:00
this.newGroup();
}
else{
this.closeAllDesktopComponents();
this.showNewGroup=true;
}
}
2021-04-14 13:56:38 +01:00
openEditGroupPage(rid) {
2021-07-26 10:09:33 +01:00
if( window.innerWidth < 801){
2021-04-14 13:56:38 +01:00
this.editGroup(rid);
}
else{
this.closeAllDesktopComponents();
this.showEditGroup=true;
}
}
2021-07-23 14:43:51 +01:00
openGroupMessagesPage(rid) {
2021-08-19 18:54:50 +01:00
if( window.innerWidth < 701){
2021-07-23 14:43:51 +01:00
this.openGroupMessagesModal(rid);
2021-03-12 11:56:54 +01:00
}
else{
2021-08-20 11:24:42 +01:00
this.idSelected = rid;
2021-03-12 11:56:54 +01:00
this.closeAllDesktopComponents();
2021-03-15 15:19:07 +01:00
this.showEmptyComponent = false;
2021-07-23 14:43:51 +01:00
this.roomId = rid;
console.log(this.roomId);
2021-03-12 11:56:54 +01:00
this.showGroupMessages=true;
}
2021-03-11 16:21:09 +01:00
}
2021-08-18 16:40:58 +01:00
openNewEventPage(data:any){
this.taskParticipants = data.members.map((val) =>{
return {
Name: val.name,
EmailAddress: val.username+"@"+environment.domain,
IsRequired: "true",
}
});
this.closeAllDesktopComponents();
2021-08-19 18:54:50 +01:00
if(window.innerWidth < 701){
2021-08-18 16:40:58 +01:00
console.log('Mobile');
}
else{
this.showNewEvent=true;
}
}
async openAttendeesComponent(data) {
this.adding = data.type
this.closeAllDesktopComponents();
this.showAttendees = true;
}
async clearContact() {
this.contacts = [];
}
async setContact(data:EventPerson[]) {
this.contacts = data;
}
async setIntervenient(data) {
this.taskParticipants = removeDuplicate(data)
}
async setIntervenientCC(data) {
this.taskParticipantsCc = removeDuplicate(data)
}
async closeAttendeesComponent() {
this.closeAllDesktopComponents();
this.showNewEvent = true;
}
async closeNewEventComponent() {
this.closeAllDesktopComponents();
this.showEmptyComponent = true;
this.idSelected = "";
}
2020-10-30 15:22:35 +01:00
onSegmentChange(){
2021-01-27 16:01:49 +01:00
this.load();
}
2021-08-19 18:54:50 +01:00
doRefresh(event){
setTimeout(() => {
this.load();
event.target.complete();
}, 1000);
}
refreshing(){
2021-01-27 16:01:49 +01:00
this.load();
}
2021-08-19 18:54:50 +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-07-23 14:43:51 +01:00
2021-08-30 18:56:37 +01:00
async getDirectMessages(event?){
2021-07-23 14:43:51 +01:00
2021-08-20 19:10:13 +01:00
this.chatService.getAllDirectMessages().subscribe(async (res:any)=>{
2021-10-07 15:30:36 +01:00
console.log(res.ims);
2021-08-19 18:54:50 +01:00
2021-08-30 18:56:37 +01:00
if(res != 200){
2021-09-28 15:23:51 +01:00
//console.log(res.ims);
2021-08-20 19:10:13 +01:00
this.userDirectMessages = res.ims.sort((a,b)=>{
var dateA = new Date(a._updatedAt).getTime();
var dateB = new Date(b._updatedAt).getTime();
return dateB - dateA;
});
2021-09-21 14:05:59 +01:00
//console.log(this.userDirectMessages);
2021-08-20 19:10:13 +01:00
if(this.route.url != "/home/chat"){
2021-09-21 14:05:59 +01:00
//console.log("Timer message stop")
2021-08-20 19:10:13 +01:00
}
else {
2021-09-21 14:05:59 +01:00
//console.log('TIMER');
2021-08-20 19:10:13 +01:00
//Check if modal is opened
2021-08-20 19:20:48 +01:00
if(this.segment == "Contactos" && this.showMessages != true){
2021-08-30 18:56:37 +01:00
await new Promise(resolve => setTimeout(resolve, 2000));
2021-08-20 19:10:13 +01:00
await this.getDirectMessages();
2021-09-21 14:05:59 +01:00
//console.log('Timer contactos list running')
2021-08-20 19:10:13 +01:00
}
2021-08-30 18:56:37 +01:00
else{
2021-09-21 14:05:59 +01:00
//console.log('No timer!');
2021-08-30 18:56:37 +01:00
}
2021-08-20 19:10:13 +01:00
}
}
else{
await this.getDirectMessages();
}
2021-01-13 10:02:30 +01:00
});
}
2021-08-19 18:54:50 +01:00
2021-08-20 16:38:58 +01:00
showDateDuration(start:any){
2021-09-06 17:02:45 +01:00
return this.timeService.showDateDuration(start);
2021-08-20 16:38:58 +01:00
}
2021-10-27 08:45:37 +01:00
countDownDate(date:any, roomId:string){
return this.timeService.countDownDate(date, roomId);
}
2021-08-19 18:54:50 +01:00
async getChatMembers(){
//return await this.chatService.getMembers(roomId).toPromise();
this.chatService.getAllUsers().subscribe(res=> {
console.log(res);
this.dmUsers = res['users'].filter(data => data.username != this.loggedUserChat.me.username);
console.log(this.dmUsers);
2021-01-13 10:02:30 +01:00
});
}
2021-08-20 19:10:13 +01:00
2021-08-30 18:56:37 +01:00
async getGroups(event?){
2021-08-20 19:10:13 +01:00
this.result = this.chatService.getAllPrivateGroups().subscribe(async (res:any)=>{
2021-10-27 08:45:37 +01:00
console.log(res);
2021-08-20 19:10:13 +01:00
if(res.groups != 200){
2021-01-19 16:10:40 +01:00
this.privateGroups = res.groups;
2021-08-17 14:17:19 +01:00
/* 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)=>{
var dateA = new Date(a._updatedAt).getTime();
var dateB = new Date(b._updatedAt).getTime();
return dateB - dateA;
});
2021-10-27 08:45:37 +01:00
console.log(this.allGroups);
2021-08-17 14:17:19 +01:00
/* }); */
2021-08-20 19:10:13 +01:00
if(this.route.url != "/home/chat"){
console.log("Timer message stop")
}
else {
//Check if modal is opened
2021-08-20 19:20:48 +01:00
if(this.segment == "Grupos" && this.showGroupMessages != true){
2021-08-30 18:56:37 +01:00
await new Promise(resolve => setTimeout(resolve, 2000));
2021-08-20 19:10:13 +01:00
await this.getGroups();
2021-09-21 14:05:59 +01:00
//console.log('Timer groups list running')
2021-08-20 19:10:13 +01:00
}
}
}
else{
await this.getGroups();
}
2021-07-23 14:43:51 +01:00
});
2021-01-19 16:10:40 +01:00
}
2021-05-18 14:37:43 +01:00
2020-12-21 16:37:44 +01:00
async selectContact(){
const modal = await this.modalController.create({
component: ContactsPage,
2021-05-21 12:03:50 +01:00
cssClass: 'modal modal-desktop',
2020-12-21 16:37:44 +01:00
});
await modal.present();
modal.onDidDismiss();
}
2021-05-18 14:37:43 +01:00
2020-12-21 16:37:44 +01:00
async newGroup(){
const modal = await this.modalController.create({
component: NewGroupPage,
2021-05-21 12:03:50 +01:00
cssClass: 'modal modal-desktop',
2020-12-21 16:37:44 +01:00
});
await modal.present();
modal.onDidDismiss();
}
2021-04-14 13:56:38 +01:00
async editGroup(roomId){
const modal = await this.modalController.create({
component: EditGroupPage,
2021-05-21 12:03:50 +01:00
cssClass: 'modal modal-desktop',
2021-04-14 13:56:38 +01:00
componentProps: {
roomId: roomId,
},
});
await modal.present();
2021-04-14 14:10:17 +01:00
modal.onDidDismiss().then((res)=>{
console.log(res.data);
this.modalController.dismiss(res.data);
});
2021-04-14 13:56:38 +01:00
}
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
2021-03-12 11:56:54 +01:00
console.log(roomId);
2021-07-23 14:43:51 +01:00
2020-12-30 11:13:50 +01:00
const modal = await this.modalController.create({
component: MessagesPage,
2021-08-20 11:24:42 +01:00
cssClass: 'modal modal-desktop isMessagesChatOpened',
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
async openGroupMessagesModal(roomId:any){
2021-05-21 11:57:16 +01:00
console.log(roomId);
2021-07-23 14:43:51 +01:00
2020-12-28 10:11:00 +01:00
const modal = await this.modalController.create({
component: GroupMessagesPage,
2021-08-20 11:24:42 +01:00
cssClass: 'modal modal-desktop isGroupChatOpened',
2021-01-13 10:02:30 +01:00
componentProps: {
roomId: roomId,
2021-01-13 10:02:30 +01:00
},
2020-12-28 10:11:00 +01:00
});
await modal.present();
modal.onDidDismiss();
}
2021-05-18 14:37:43 +01:00
2020-09-10 09:48:37 +01:00
}