From 2ab70edd9d546677a2734d69d6ea08b2bb86d315 Mon Sep 17 00:00:00 2001 From: "tiago.kayaya" Date: Wed, 26 Jan 2022 16:37:59 +0100 Subject: [PATCH] fix bug in create new room --- src/app/pages/chat/chat.page.html | 2 +- src/app/pages/chat/chat.page.ts | 1 - src/app/services/chat/room.service.ts | 3 +- .../services/chat/ws-chat-methods.service.ts | 82 ++++++++++++------- src/app/services/chat/ws-chat.service.ts | 31 ++++++- .../shared/chat/new-group/new-group.page.ts | 37 +++++++-- 6 files changed, 117 insertions(+), 39 deletions(-) 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 cadd6c5fb..870541ab5 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -298,7 +298,6 @@ export class ChatPage implements OnInit { this.openGroupMessagesModal(rid); } else { - alert('HERE OPEN') this.idSelected = rid; this.closeAllDesktopComponents(); this.showEmptyComponent = false; diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 5ef5762f0..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) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index b43c3b155..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,52 @@ 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); } @@ -111,6 +129,10 @@ export class WsChatMethodsService { 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] @@ -142,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 872970ece..e4415d5fc 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -229,7 +229,36 @@ export class WsChatService { 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/new-group/new-group.page.ts b/src/app/shared/chat/new-group/new-group.page.ts index a8fb7a4c5..a63b55ec4 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,37 @@ 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 body = { "name":name, } - this.chatService.addGroup(body).subscribe(res=>{ + + 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); + + /* 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 +166,11 @@ export class NewGroupPage implements OnInit{ } this.chatService.sendMessage(body).toPromise(); }); - } + } */ - }); + //}); } async addContacts(){