diff --git a/src/app/models/chatMethod.ts b/src/app/models/chatMethod.ts index 1adabf590..6b455b237 100644 --- a/src/app/models/chatMethod.ts +++ b/src/app/models/chatMethod.ts @@ -81,6 +81,7 @@ export interface Message { editedBy: EditedBy; urls: any[]; temporaryData: object + localReference?: string } diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index de8e9d8f8..9f2092bdd 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -103,6 +103,7 @@ export class LoginPage implements OnInit { if (attempt.UserId == SessionStore.user.UserId) { await this.authService.SetSession(attempt, this.userattempt); await this.authService.loginChat(); + await this.authService.loginToChatWs() this.getToken(); SessionStore.setInativity(true); @@ -117,6 +118,7 @@ export class LoginPage implements OnInit { this.changeProfileService.run() await this.authService.loginChat(); + await this.authService.loginToChatWs() this.getToken(); this.router.navigateByUrl('/pin', { replaceUrl: true }); } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index f7cef3f8f..aee17f236 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -125,8 +125,6 @@ export class AuthService { if(responseChat) { - this.loginToChatWs() - console.log('Login to Rocket chat OK', responseChat); this.ValidatedUserChat = responseChat; localStorage.setItem('userChat', JSON.stringify(responseChat)); @@ -146,7 +144,7 @@ export class AuthService { }, expirationDate) } - private loginToChatWs() { + loginToChatWs() { setTimeout(()=>{ this.WsChatService.connect(); diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index ceafb850a..f92c5b48e 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -34,13 +34,14 @@ export class MessageService { errorUploadingAttachment = false loadHistory = false duration = '' + localReference = null constructor(private storage: Storage, private NfService: NfService, private WsChatService: WsChatService) { } - setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData}:Message) { + setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData, localReference}:Message) { this.customFields = customFields this.channels = channels || [] this.mentions = mentions || [] @@ -54,6 +55,7 @@ export class MessageService { this.file = file this.attachments = attachments this.temporaryData = temporaryData + this.localReference = localReference || null if(!this.ts) { this.offline = true @@ -104,8 +106,8 @@ export class MessageService { this.sendAttempt++; if(!this.hasFile) { - this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => { - let ChatMessage = data.result + this.WsChatService.send({roomId:this.rid, msg:this.msg, requestId: this.localReference}).then(({message, requestId}) => { + let ChatMessage = message.result if (environment.chatOffline) { this.redefinedMessage(this, ChatMessage) @@ -132,8 +134,8 @@ export class MessageService { this.errorUploadingAttachment = false this.temporaryData = {} - this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => { - let ChatMessage = data.result + this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, requestId: this.localReference}).then(({message, requestId}) => { + let ChatMessage = message.result if (environment.chatOffline) { this.redefinedMessage(this, ChatMessage) @@ -154,14 +156,19 @@ export class MessageService { }) } else if(uploadSuccessfully == false) { - this.errorUploadingAttachment = true + + return new Promise((resolve, reject)=>{ + reject(false) + }) + } } } redefinedMessage(message, ChatMessage) { + ChatMessage = this.NfService.fix_updatedAt(ChatMessage) this.setData(ChatMessage) } @@ -178,4 +185,5 @@ export class MessageService { this.duration = showDateDuration(date || this._updatedAt); } + } diff --git a/src/app/services/chat/nf.service.ts b/src/app/services/chat/nf.service.ts index 72c533cc5..f0441720c 100644 --- a/src/app/services/chat/nf.service.ts +++ b/src/app/services/chat/nf.service.ts @@ -10,5 +10,16 @@ export class NfService { beforeSendAttachment = async (message: MessageService, room?: RoomService): Promise => new Promise ((resolve, reject)=> (resolve(true))); downloadFileMsg = async (message: MessageService, room?: RoomService): Promise => new Promise ((resolve, reject)=> (resolve(true))); + fix_updatedAt(message) { + if (message.result) { + message.result._updatedAt = message.result._updatedAt['$date'] + } else if(message._updatedAt) { + if(message._updatedAt.hasOwnProperty('$date')) { + message._updatedAt = message._updatedAt['$date'] + } + } + return message + } + constructor() { } } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index e39006567..0fc332fb6 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -39,10 +39,11 @@ export class RoomService { message = '' lastMessageTxt = '' userThatIsTyping = '' - private ToastService = ToastsService mgsArray = []; + messagesLocalReference = [] + scrollDown = () => { } /** @@ -92,12 +93,11 @@ export class RoomService { this.id, "stream-room-messages", (_ChatMessage) => { + console.log('recivemessage', _ChatMessage) let ChatMessage = _ChatMessage.fields.args[0] - ChatMessage = this.fix_updatedAt(ChatMessage) - console.log('recivemessage', ChatMessage) - + alert('receive') const message = this.prepareMessage(ChatMessage) this.lastMessage = message this.calDateDuration(ChatMessage._updatedAt) @@ -117,7 +117,7 @@ export class RoomService { setTimeout(()=>{ this.scrollDown() - }, 50) + }, 50) } ) @@ -266,7 +266,7 @@ export class RoomService { */ async send({file = null, attachments = null, temporaryData = {}}) { - const localReference = uuidv4() + const localReference = uuidv4() + 'peter' let offlineChatMessage = { rid: this.id, @@ -278,14 +278,15 @@ export class RoomService { } const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline) + + message.send().then((ChatMessage) => { + this.updateMessageDB(ChatMessage, localReference) + }) if (environment.chatOffline) { - - message.send().then((ChatMessage) => { - this.updateMessageDB(ChatMessage, localReference) - }) - + this.messagesLocalReference.push(localReference) this.addMessageDB(offlineChatMessage) + setTimeout(() => { this.scrollDown() }, 150) @@ -392,6 +393,7 @@ export class RoomService { if(wewMessage.offline == false) { this.prepareMessage(ChatMessage) } else { + alert('create offline') const offlineMessage = this.prepareMessage(ChatMessage) offlineMessage.send().then(()=>{ this.updateMessageDB(ChatMessage, ChatMessage.localReference) @@ -449,15 +451,23 @@ export class RoomService { wewMessage.loadHistory = this.hasLoadHistory if(!message._id && environment.chatOffline) { + if(this.hasLoadHistory) alert('create offline') + this.messages.push(wewMessage) return wewMessage } const found = this.messages.find((MessageService) => { - return MessageService._id == message._id + if (MessageService._id == message._id) { + if(this.hasLoadHistory) console.log(`${MessageService._id} == ${message._id}`) + return true + } else { + return false + } }) if (save && !found) { + if(this.hasLoadHistory) alert('not found') this.messages.push(wewMessage) } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index ddf514cf1..ab0d28f30 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -36,6 +36,7 @@ export class WsChatService { this.ws.registerCallback({ type:'Onmessage', + key:'ping-pong', funx:(message: any) => { if(message.msg == "ping") { this.ws.send({message:{msg:"pong"}, loginRequired: false}) @@ -157,9 +158,7 @@ export class WsChatService { } // send message to room - send({roomId, msg, attachments = null, file = null}) { - - const requestId = uuidv4() + send({roomId, msg, attachments = null, file = null, requestId = uuidv4()}) { var message = { msg: "method", @@ -178,7 +177,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ if(message.id == requestId ) { // same request send - resolve(message) + resolve({message, requestId}) return true } }}) @@ -677,7 +676,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { this.wsMsgQueue[requestId] = {message, requestId, loginRequired} } else { let messageStr = JSON.stringify(message) - + console.log('messageStr', messageStr) this.socket.send(messageStr) } return requestId @@ -686,6 +685,8 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { onmessage: async (event: any)=> { const data = JSON.parse(event.data) + console.log('onmessage', data) + for (const [key, value] of Object.entries(this.wsCallbacks)) { if(value.type== 'Onmessage') { const dontRepeat = await value.funx(data) 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 147a166c0..cc817b8b6 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -49,7 +49,7 @@
- +
@@ -68,7 +68,7 @@
- +
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 2c1083e5b..021643fad 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -307,7 +307,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.wsChatMethodsService.getGroupRoom(this.roomId).send({}) } - deleteMessage(msgId: string, room:any) { + deleteMessage(msgId: string) { + const room = this.wsChatMethodsService.getGroupRoom(this.roomId) this.alertService.confirmDeleteMessage(msgId, room); } diff --git a/src/app/shared/chat/messages/contacts/contacts.page.ts b/src/app/shared/chat/messages/contacts/contacts.page.ts index 2ac2537ca..8ca65d560 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.ts +++ b/src/app/shared/chat/messages/contacts/contacts.page.ts @@ -34,8 +34,7 @@ export class ContactsPage implements OnInit { private authService: AuthService, public ThemeService: ThemeService, public WsChatMethodsService: WsChatMethodsService - ) - { + ) { this.loggedUser = authService.ValidatedUserChat['data']; this.textSearch=""; this.dm=null; diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index d106d725f..65c1e02fb 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -44,7 +44,7 @@ - +
@@ -63,7 +63,7 @@ - +
diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index c4e648c4a..f66ff5670 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -249,7 +249,8 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.wsChatMethodsService.getDmRoom(this.roomId).send({}) } - deleteMessage(msgId: string, room:any) { + deleteMessage(msgId: string) { + const room = this.wsChatMethodsService.getDmRoom(this.roomId) this.alertService.confirmDeleteMessage(msgId, room); }