diff --git a/src/app/modals/set-room-owner/set-room-owner.page.html b/src/app/modals/set-room-owner/set-room-owner.page.html index a492ffd4f..a6d13bec1 100644 --- a/src/app/modals/set-room-owner/set-room-owner.page.html +++ b/src/app/modals/set-room-owner/set-room-owner.page.html @@ -9,7 +9,7 @@
- Promover para Administrador + Nomear novo administrador
diff --git a/src/app/modals/set-room-owner/set-room-owner.page.ts b/src/app/modals/set-room-owner/set-room-owner.page.ts index d332b3296..ac9939bf2 100644 --- a/src/app/modals/set-room-owner/set-room-owner.page.ts +++ b/src/app/modals/set-room-owner/set-room-owner.page.ts @@ -3,6 +3,7 @@ import { Component, OnInit } from '@angular/core'; import { ModalController, NavParams } from '@ionic/angular'; import { AuthService } from 'src/app/services/auth.service'; import { ChatService } from 'src/app/services/chat.service'; +import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; import { ThemeService } from 'src/app/services/theme.service' import { ToastService } from 'src/app/services/toast.service'; @@ -14,7 +15,7 @@ import { ToastService } from 'src/app/services/toast.service'; export class SetRoomOwnerPage implements OnInit { textSearch:string = ""; - room:any; + roomId:any; members:any; constructor( @@ -25,13 +26,14 @@ export class SetRoomOwnerPage implements OnInit { private navParams: NavParams, public ThemeService: ThemeService, private toastService: ToastService, + public wsChatMethodsService: WsChatMethodsService, ) { - this.room = this.navParams.get('room'); + this.roomId = this.navParams.get('roomId'); this.members = this.navParams.get('members'); } ngOnInit() { - console.log(this.room); + console.log(this.members); } @@ -57,23 +59,43 @@ export class SetRoomOwnerPage implements OnInit { return null; } - setRoomOwner(user:any){ + async setRoomOwner(user:any){ - console.log(user); - console.log(this.room); + let res:any = await this.wsChatMethodsService.addRoomOwner(this.roomId, user._id); + console.log(res); + + if(res.error){ + if(res.error.error == 'error-user-already-owner'){ + this.toastService._badRequest('Este utilizador já é administrador'); + } + else{ + this.toastService._badRequest('Não foi possível completar a ação, por favor tente novamente.'); + } + } + else{ + this.modalController.dismiss('success'); + } + + /* let body = { - "roomId": this.room._id, + "roomId": this.roomId, "userId": user._id } this.chatService.addGroupOwner(body).subscribe((res)=>{ + alert('here'); console.log(res); - this.close(); - }, (error) => { - this.toastService._badRequest('Não foi possível completar a ação, por favor tente novamente.'); - }); + this.toastService._successMessage('Operação realizada com sucesso'); + }, (e) => { + if(e.error.errorType == 'error-user-already-owner'){ + this.toastService._badRequest('Este utilizador já é administrador'); + } + else{ + this.toastService._badRequest('Não foi possível completar a ação, por favor tente novamente.'); + } + }); */ } diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 770139c39..aae5650aa 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -121,7 +121,7 @@
{{countDownDate(group.value.customFields.countDownDate, group.value.id)}}
-
{{group.value.lastMessage.u.name}}: {{group.value.lastMessage.msg}}
+
{{group.value.lastMessage.u.name}}: {{group.value.lastMessage.msg}}
diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index b0c731cdb..870541ab5 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -548,7 +548,7 @@ export class ChatPage implements OnInit { return dateB - dateA; }); - + //console.log('GROUPS FROM DB', this.allGroups) }) 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 406c41827..c927ed141 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -333,12 +333,14 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { cssClass: 'chat-popover', componentProps: { roomId: this.roomId, + members: this.members, }, }); await modal.present(); modal.onDidDismiss().then(res=>{ if(res.data == 'leave'){ console.log('saiu do grupo'); + //this.wsChatMethodsService.subscribeToRoomUpdate(this.roomId, this.room); } else if(res.data == 'cancel'){ console.log('cancel'); diff --git a/src/app/pages/chat/new-group/new-group.page.ts b/src/app/pages/chat/new-group/new-group.page.ts index d1006c0e2..5ca806462 100644 --- a/src/app/pages/chat/new-group/new-group.page.ts +++ b/src/app/pages/chat/new-group/new-group.page.ts @@ -6,6 +6,8 @@ import { GroupContactsPage } from '../group-messages/group-contacts/group-contac import { ThemeService } from 'src/app/services/theme.service' import { ChatService } from 'src/app/services/chat.service'; import { ProcessesService } from 'src/app/services/processes.service'; +import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; +import { AuthService } from 'src/app/services/auth.service'; @Component({ selector: 'app-new-group', @@ -21,6 +23,7 @@ export class NewGroupPage implements OnInit { thedate:any; groupName:string; documents:any; + loggedUserChat: any; constructor( private pickerController: PickerController, @@ -30,8 +33,11 @@ export class NewGroupPage implements OnInit { public ThemeService: ThemeService, private chatService: ChatService, private processesService: ProcessesService, + public wsChatMethodsService: WsChatMethodsService, + private authService: AuthService, ) { + this.loggedUserChat = authService.ValidatedUserChat['data']; this.isGroupCreated = false; this.groupName = this.navParams.get('name'); this.documents = this.navParams.get('documents'); @@ -55,10 +61,34 @@ export class NewGroupPage implements OnInit { this.modalController.dismiss(); } - createGroup(){ + async createGroup(){ let name = this.groupName.split(' ').join('-'); //Take out all special characters in string name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); + + let customFields = {} + let res:any; + + if(this.thedate){ + let customFields = { + "countDownDate":this.thedate + } + res = await this.wsChatMethodsService.createPrivateRoom(name, this.loggedUserChat.me.username, customFields); + console.log(res); + } + else{ + res = await this.wsChatMethodsService.createPrivateRoom(name, this.loggedUserChat.me.username, customFields); + console.log(res); + } + + this.isGroupCreated = true; + this.addContacts(res.result); + + this.wsChatMethodsService.subscribeToRoomUpdate(res.result.rid, res.result); + + //this.addGroupMessage.emit(res.result.rid); + + /* let body = { "name":name, } this.chatService.addGroup(body).subscribe(res=>{ console.log('group created'); @@ -112,7 +142,7 @@ export class NewGroupPage implements OnInit { this.chatService.sendMessage(body).toPromise(); }); } - }); + }); */ } async addContacts(room){ diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 5ff6b23be..8d9c3a482 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -39,7 +39,7 @@ export class RoomService { private sqlservice: SqliteService, ) { } - setData({ customFields, id, name, t, lastMessage, _updatedAt }) { + setData({ customFields, id, name, t, lastMessage = new MessageService(), _updatedAt }) { this.customFields = customFields this.id = id this.name = name @@ -62,6 +62,7 @@ export class RoomService { /* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */ const message = new MessageService() message.setData(ChatMessage) + this.lastMessage.msg = message.msg if (message.t == 'r') { this.name = message.msg } this.calDateDuration(ChatMessage._updatedAt) @@ -89,6 +90,10 @@ export class RoomService { this.WsChatService.send(this.id, msg) } + leave(rid?) { + this.WsChatService.leaveRoom(this.id) + } + getMsgFromDBMobile() { console.log('ALL MSG DBBB', this.id) this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => { @@ -159,7 +164,7 @@ export class RoomService { }) } - + // runs onces only loadHistory(limit = 100) { diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 70625d0af..1db0198c6 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -46,35 +46,7 @@ export class WsChatMethodsService { // console.log("ROOMS" + JSON.stringify(rooms)) rooms.result.update.forEach((roomData: room) => { - let room:RoomService; - - //console.log(roomData); - - - room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice) - room.setData({ - customFields: roomData.customFields, - id: this.getRoomId(roomData), - name: this.getRoomName(roomData), - t: roomData.t, - lastMessage: this.getRoomLastMessage(roomData), - _updatedAt: new Date(roomData._updatedAt['$date']) - }) - - room.receiveMessage() - - let roomId = this.getRoomId(roomData) - - if(this.isIndividual(roomData)) { - this.dm[roomId] = room - this.dmCount++ - } else { - this.group[roomId] = room - this.groupCount++ - } - - - + this.prepareRoom(roomData); }); this.loadingWholeList = false @@ -99,6 +71,68 @@ export class WsChatMethodsService { })) } + + subscribeToRoomUpdate(id, roomData) { + this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + console.log('streamRoomMessages', subscription) + }) + this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + console.log('streamRoomMessages', subscription) + }) + this.WsChatService.streamNotifyLogged().then((subscription=>{ + console.log('streamRoomMessages', subscription) + })) + + this.prepareRoom(roomData); + + this.getGroupRoom(id).loadHistory(); + + } + + prepareRoom(roomData){ + let room:RoomService; + + room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice) + room.setData({ + customFields: roomData.customFields, + id: this.getRoomId(roomData), + name: this.getRoomName(roomData), + t: roomData.t, + lastMessage: this.getRoomLastMessage(roomData), + _updatedAt: new Date(roomData._updatedAt['$date']) + }) + + room.receiveMessage() + + let roomId = this.getRoomId(roomData) + + if(this.isIndividual(roomData)) { + this.dm[roomId] = room + this.dmCount++ + } else { + this.group[roomId] = room + this.groupCount++ + } + } + + + + leaveRoom(id?) { + return this.WsChatService.leaveRoom(id); + } + + hidingRoom(id?) { + return this.WsChatService.hidingRoom(id); + } + + addRoomOwner(roomid, userId){ + return this.WsChatService.addRoomOwner(roomid, userId); + } + + createPrivateRoom(groupName, username, customFields){ + return this.WsChatService.createPrivateRoom(groupName, username, customFields); + } + getDmRoom(id): RoomService { try { return this.dm[id] @@ -130,7 +164,7 @@ export class WsChatMethodsService { return roomData._id } - getRoomLastMessage(roomData: room) { + getRoomLastMessage(roomData: room):any { return roomData.lastMessage } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 052a8c78e..e4415d5fc 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -153,9 +153,112 @@ export class WsChatService { }); } + leaveRoom(roomId) { + + const requestId = uuidv4() + + var message = { + msg: "method", + method: "leaveRoom", + id: requestId, + params: [ + roomId, + ] + } + + this.ws.send({message, requestId}); + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + resolve(message) + return true + } + }}) + }); + } + + addRoomOwner(roomId, userId) { + + const requestId = uuidv4() + + var message = { + msg: "method", + method: "addRoomOwner", + id: requestId, + params: [ + roomId, + userId + ] + } + + this.ws.send({message, requestId}); + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + resolve(message) + return true + } + }}) + }); + } + + hidingRoom(roomId){ + + const requestId = uuidv4() + + var message = { + msg: "method", + method: "hideRoom", + id: requestId, + params: [roomId] + } + + this.ws.send({message, requestId}); + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + resolve(message) + return true + } + }}) + }); + } + joinRoom(){} deleteMessage() {} - createRoom() {} + createPrivateRoom(groupName, username, customFields) { + const requestId = uuidv4() + + var message = { + msg: "method", + method: "createPrivateGroup", + id: requestId, + params: [ + groupName, + [username], + false, + customFields, + { + "broadcast":false, + "encrypted":false + } + ] + } + + this.ws.send({message, requestId}); + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + resolve(message) + return true + } + }}) + }); + } 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 63173ed2f..53a2394cb 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 @@ -134,12 +134,9 @@ export class GroupContactsPage implements OnInit { updateGroup(){ this.showLoader = true; - this.chatService.getRoomInfo(this.roomId).subscribe(room=>{ - this.room = room['room']; - this.addContacts(this.room); - this.showLoader = false; - this.openGroupMessage.emit(this.room._id); - }); + this.addContacts(this.roomId); + this.openGroupMessage.emit(this.roomId); + this.showLoader = false; } openGroupMessagesPage(){ this.showLoader = true; @@ -259,15 +256,15 @@ export class GroupContactsPage implements OnInit { user.isChecked = !user.isChecked; } - addContacts(room:any){ - console.log(room); + addContacts(roomId:any){ + console.log(roomId); this.selectedUserList = this.users.filter(function(contact) { return contact.isChecked == true; }); console.log( this.selectedUserList); this.selectedUserList.forEach(user=>{ let body ={ - "roomId":room._id, + "roomId":roomId, "userId":user._id, } this.chatService.addUserToGroup(body).subscribe(res=>{ diff --git a/src/app/shared/chat/group-messages/group-messages.page.html b/src/app/shared/chat/group-messages/group-messages.page.html index 7b1ee94b9..fd98e664c 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -92,55 +92,62 @@
- -
-
- - - - -
-
- {{msg.u.name}} - {{showDateDuration(msg._updatedAt)}} -
-
- {{msg.msg}} -
-
-
- image -
-
-
+
+
+
+ AQUI +
+ + + + +
+
+ {{msg.u.name}} + {{showDateDuration(msg._updatedAt)}} +
+
+
+
+
+ image +
+
- -
- - - - - - - {{file.title}} + +
+ + + + + + + {{file.title}} +
-
-
- - {{file.description}} - - {{msg.file.type.replace('application/','').toUpperCase()}} - +
+ + {{file.description}} + + {{msg.file.type.replace('application/','').toUpperCase()}} +
+ {{last ? scrollToBottom() : ''}}
- {{last ? scrollToBottom() : ''}}
+
+ {{msg.u.name}} criou esta reunião
+
+ De {{showDateDuration(msg.file.start_date)}} a {{showDateDuration(msg.file.end_date)}}
+ {{msg.file.venue}}
+
+
-
+ image
@@ -167,7 +173,7 @@
- + -->
@@ -198,7 +204,7 @@
{{last ? scrollToBottom() : ''}}
-
+
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 ef186df24..df6e4fc46 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -80,9 +80,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe downloadFile: any; constructor( - private menu: MenuController, + public wsChatMethodsService: WsChatMethodsService, private modalController: ModalController, - private actionSheetController: ActionSheetController, public popoverController: PopoverController, private chatService: ChatService, private authService: AuthService, @@ -97,8 +96,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe public ThemeService: ThemeService, private changeDetectorRef: ChangeDetectorRef, private storage: Storage, - public wsChatMethodsService: WsChatMethodsService + ) { + console.log('OnCONSTRUCTOR'); this.loggedUserChat = authService.ValidatedUserChat['data']; this.isGroupCreated = true; this.roomCountDownDate = ""; @@ -106,11 +106,13 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } ngOnChanges(changes: SimpleChanges): void { + console.log('OnCHANGES'); + this.getRoomInfo(); //this.scrollToBottom(); this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); this.wsChatMethodsService.getGroupRoom(this.roomId).getMsgFromDB(); - console.log('MESSAGES'+this.wsChatMethodsService.getGroupRoom(this.roomId).massages); + console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).massages); this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked setTimeout(()=>{ @@ -120,11 +122,13 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } ngOnInit() { + console.log(this.roomId); this.loggedUser = this.loggedUserChat; - //console.log(this.wsChatMethodsService.getRoom(this.roomId).massages); this.loggedUser=this.loggedUserChat; - this.getRoomInfo(); + setTimeout(() => { + this.getRoomInfo(); + }, 1000); this.setStatus('online'); this.getChatMembers(); //this.getMessageDB(); @@ -254,6 +258,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } async getRoomInfo() { + this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); let room = await this.chatService.getRoomInfo(this.roomId).toPromise(); this.room = room['room']; if (this.room.name) { @@ -288,29 +293,12 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } loadGroupMessages(roomId) { - //console.log('here'+room.t); - this.showLoader = true; - //If group is private call getGroupMembers - /* if(room.t === 'p'){ - console.log('private'); */ - this.chatService.getPrivateGroupMessages(this.roomId).subscribe(res => { console.log(res); let msgOnly = res['messages'].filter(data => data.t != 'au'); - //this.messages = msgOnly.reverse(); - //console.log(res); - this.showLoader = false; }); - /* } */ - //Otherwise call getChannelMembers for públic groups - /* else{ - this.chatService.getPublicGroupMessages(this.roomId).subscribe(res=>{ - console.log(res); - this.messages = res['messages'].reverse(); - }); - } */ } sendMessage() { @@ -364,6 +352,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe }); return await popover.present(); */ + console.log(this.roomId); + + const modal = await this.modalController.create({ enterAnimation, leaveAnimation, @@ -379,6 +370,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe console.log(res); if (res.data == 'leave') { this.getRoomInfo(); + this.wsChatMethodsService.hidingRoom(this.roomId); this.closeAllDesktopComponents.emit(); this.showEmptyContainer.emit(); } diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 5ceb5633e..c8c6f57c6 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -56,63 +56,73 @@ {{last ? scrollToBottom() : ''}}
-
-
- - - - - -
-
- {{msg.u.name}} - {{showDateDuration(msg._updatedAt)}} -
-
- -
-
-
- - image - -
-
-
-
- - - - - - - - {{file.title}} -
+
+
+
+ + + + + +
+
+ {{msg.u.name}} + {{showDateDuration(msg._updatedAt)}} +
+
+
+
+
+ image
-
- - {{file.description}} - - {{msg.file.type.replace('application/','').toUpperCase()}} - +
+
+
+ + + + + + + + {{file.title}} +
+
+
+ + {{file.description}} + + {{msg.file.type.replace('application/','').toUpperCase()}} + +
+ {{last ? scrollToBottom() : ''}}
- {{last ? scrollToBottom() : ''}}
- +
+ {{msg.u.name}} criou esta reunião
+
+ + De {{showDateDuration(msg.file.start_date)}} a + {{showDateDuration(msg.file.end_date)}} +
+ + + {{msg.file.venue}} +
+
-
+ File image
@@ -141,10 +150,10 @@
-
+
--> -
+
diff --git a/src/app/shared/chat/new-group/new-group.page.ts b/src/app/shared/chat/new-group/new-group.page.ts index a8fb7a4c5..9bcc2eddc 100644 --- a/src/app/shared/chat/new-group/new-group.page.ts +++ b/src/app/shared/chat/new-group/new-group.page.ts @@ -2,7 +2,9 @@ import { analyzeAndValidateNgModules } from '@angular/compiler'; import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; import { NavigationStart, Router } from '@angular/router'; import { ModalController, NavParams, PickerController, PopoverController } from '@ionic/angular'; +import { AuthService } from 'src/app/services/auth.service'; import { ChatService } from 'src/app/services/chat.service'; +import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; import { DataService } from 'src/app/services/data.service'; import { ProcessesService } from 'src/app/services/processes.service'; import { GroupDurationPage } from 'src/app/shared/popover/group-duration/group-duration.page'; @@ -25,6 +27,7 @@ export class NewGroupPage implements OnInit{ //groupName:string; task:any; documents: any; + loggedUserChat: any; @Input() groupName:string; @Output() addGroupMessage:EventEmitter = new EventEmitter(); @@ -37,8 +40,11 @@ export class NewGroupPage implements OnInit{ private dataService:DataService, private processesService: ProcessesService, private router: Router, + public wsChatMethodsService: WsChatMethodsService, + private authService: AuthService, ) { + this.loggedUserChat = authService.ValidatedUserChat['data']; this.isGroupCreated = false; //this.groupName = this.navParams.get('name'); } @@ -86,16 +92,38 @@ export class NewGroupPage implements OnInit{ this.addGroupMessage.emit(); } - createGroup(){ + async createGroup(){ let name = this.groupName.split(' ').join('-'); //Take out all special characters in string name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); + + let customFields = {} + let res:any; + + if(this.thedate){ + let customFields = { + "countDownDate":this.thedate + } + res = await this.wsChatMethodsService.createPrivateRoom(name, this.loggedUserChat.me.username, customFields); + console.log(res); + } + else{ + res = await this.wsChatMethodsService.createPrivateRoom(name, this.loggedUserChat.me.username, customFields); + console.log(res); + } + + this.wsChatMethodsService.subscribeToRoomUpdate(res.result.rid, res.result); + this.addGroupMessage.emit(res.result.rid); + + /* let body = { "name":name, } this.chatService.addGroup(body).subscribe(res=>{ console.log('group created'); console.log(res['group']); - this.addGroupMessage.emit(res['group']._id); + this.addGroupMessage.emit(res['group']._id); */ + + /* this.addGroupMessage.emit(res['group']._id); if(this.thedate){ let countDownBody = { @@ -139,11 +167,11 @@ export class NewGroupPage implements OnInit{ } this.chatService.sendMessage(body).toPromise(); }); - } + } */ - }); + //}); } async addContacts(){ diff --git a/src/app/shared/popover/chat-popover/chat-popover.page.ts b/src/app/shared/popover/chat-popover/chat-popover.page.ts index 3dc0b9088..d15e5abdb 100644 --- a/src/app/shared/popover/chat-popover/chat-popover.page.ts +++ b/src/app/shared/popover/chat-popover/chat-popover.page.ts @@ -4,6 +4,7 @@ import { ChatService } from 'src/app/services/chat.service'; import { ToastService } from 'src/app/services/toast.service'; import { ThemeService } from 'src/app/services/theme.service' import { SetRoomOwnerPage } from 'src/app/modals/set-room-owner/set-room-owner.page'; +import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; @Component({ @@ -22,7 +23,8 @@ export class ChatPopoverPage implements OnInit { private navParams: NavParams, private chatService: ChatService, private toastService: ToastService, - public ThemeService: ThemeService + public ThemeService: ThemeService, + public wsChatMethodsService: WsChatMethodsService, ) { this.roomId = this.navParams.get('roomId'); this.members = this.navParams.get('members'); @@ -58,21 +60,46 @@ export class ChatPopoverPage implements OnInit { cssClass: classs, backdropDismiss: true, componentProps: { - room: this.room, + roomId: this.roomId, members: this.members, } }); await modal.present(); modal.onDidDismiss().then((res)=>{ + this.leaveGroup(); }); } - leaveGroup(){ - console.log('leave'); + async leaveGroup(){ let body = { "roomId":this.roomId, } - this.chatService.getRoomInfo(this.roomId).subscribe(room=>{ + let res:any = await this.wsChatMethodsService.leaveRoom(this.roomId); + + console.log(res.error.error); + + if(res.error){ + if(res.error.error = "error-you-are-last-owner"){ + this.toastService._badRequest("Você é o último administrador do grupo. Por favor, defina o novo administrador antes de sair da grupo."); + + this.setRoomOwner(); + } + else if(res.error.error == 'error-user-not-in-room'){ + this.toastService._badRequest("Você já não esta nesta conversa"); + } + else{ + this.toastService._badRequest("Não foi possível sair do grupo"); + } + } + else{ + this.close('leave'); + } + + this.close('leave'); + //this.wsChatMethodsService.subscribeToRoomUpdate(this.roomId, res.result); + + + /* this.chatService.getRoomInfo(this.roomId).subscribe(room=>{ this.room = room['room']; if(this.room.t === 'p'){ @@ -117,7 +144,7 @@ export class ChatPopoverPage implements OnInit { //loader.remove() }); } - }); + }); */ } //Delete