From 33cce0fd449e4a66ff13fdcd6f14662876a68c0a Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Wed, 2 Feb 2022 20:16:12 +0100 Subject: [PATCH 1/3] add chat offline --- src/app/models/chatMethod.ts | 8 +- src/app/services/chat/message.service.ts | 16 +- src/app/services/chat/room.service.ts | 146 +++++++++++++----- .../services/chat/ws-chat-methods.service.ts | 3 +- src/app/services/chat/ws-chat.service.ts | 8 +- src/environments/environment.prod.ts | 1 + src/environments/environment.ts | 1 + 7 files changed, 134 insertions(+), 49 deletions(-) diff --git a/src/app/models/chatMethod.ts b/src/app/models/chatMethod.ts index 99d41f39d..1b5c6bd9c 100644 --- a/src/app/models/chatMethod.ts +++ b/src/app/models/chatMethod.ts @@ -59,7 +59,7 @@ interface FirstUnread { msg: string; ts: Ts2; u: U2; - _updatedAt: UpdatedAt2; + _updatedAt: string; mentions: any[]; channels: any[]; } @@ -72,7 +72,7 @@ export interface Message { ts: Ts; u: U; t: string; - _updatedAt: UpdatedAt; + _updatedAt: ''; mentions: any[]; channels: any[]; attachments: Attachment[]; @@ -232,7 +232,7 @@ interface FirstUnread { msg: string; ts: Ts2; u: U2; - _updatedAt: UpdatedAt2; + _updatedAt: string; mentions: any[]; channels: any[]; } @@ -245,7 +245,7 @@ export interface Message { ts: Ts; u: U; t: string; - _updatedAt: UpdatedAt; + _updatedAt: ''; mentions: any[]; channels: any[]; attachments: Attachment[]; diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index bde3ef601..43c8f74be 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -17,9 +17,10 @@ export class MessageService { u = {} t = '' _id ='' - _updatedAt = {} + _updatedAt = '' file attachments + offline = false constructor(private storage: Storage) { } @@ -47,10 +48,23 @@ export class MessageService { }); } } + + + if(this.rid == 'offline') { + this.offline = true + } else { + this.offline = false + } + } delete() {} showDateDuration() {} + + resend() { + + } + } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index fb89ea961..623d6043b 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -13,6 +13,7 @@ import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text' import { SortService } from '../functions/sort.service'; import { chatUser } from 'src/app/models/chatMethod'; +import { environment } from 'src/environments/environment'; @Injectable({ providedIn: 'root' @@ -51,6 +52,8 @@ export class RoomService { return [] } + sortRoomList = () => {} + constructor( public WsChatService: WsChatService, private MessageService: MessageService, @@ -80,40 +83,34 @@ export class RoomService { this.id, "stream-room-messages", (ChatMessage) => { - ChatMessage = ChatMessage.fields.args[0] - ChatMessage = this.fix_updatedAt(ChatMessage) - console.log('recivemessage', ChatMessage) + if(environment.chatOffline == false) { + ChatMessage = ChatMessage.fields.args[0] + ChatMessage = this.fix_updatedAt(ChatMessage) + console.log('recivemessage', ChatMessage) + + const message = this.prepareMessage(ChatMessage) - const message = this.prepareMessage(ChatMessage) + this.lastMessage = message + if (message.t == 'r') { this.name = message.msg } + this.calDateDuration(ChatMessage._updatedAt) + + setTimeout(() => { + this.scrollDown() + }, 100) + + //this.sortService.sortDate(this.messages, '') + + if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { + this.NativeNotificationService.sendNotificationChat({ + message: message.msg, + title: this.name + }); + } - this.lastMessage = message - if (message.t == 'r') { this.name = message.msg } - this.calDateDuration(ChatMessage._updatedAt) - this.messages.push(message) - - setTimeout(() => { - this.scrollDown() - }, 100) - - //this.sortService.sortDate(this.messages, '') - - if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { - this.NativeNotificationService.sendNotificationChat({ - message: message.msg, - title: this.name - }); + this.addMessageDB(ChatMessage) } - // save to ionic storage - this.storage.get('chatmsg' + this.id).then((messages: any) => { - const newListMessages = messages.push(ChatMessage) - - this.storage.set('chatmsg' + this.id, newListMessages).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - }); - }) - } ) @@ -133,6 +130,15 @@ export class RoomService { this.WsChatService.registerCallback } + + addMessageDB(ChatMessage) { + this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + messages.push(ChatMessage) + + this.storage.set('chatmsg' + this.id, messages) + }) + } + async receiveMessageDelete() { this.WsChatService.updateRoomEventss( @@ -195,8 +201,60 @@ export class RoomService { * @description sen text message */ send() { - this.WsChatService.send(this.id, this.message) - this.message= '' + + if(environment.chatOffline == false) { + this.WsChatService.send(this.id, this.message) + this.message= '' + } else { + + const offlineChatMessage = { + channels: [], + mentions: [], + rid: 'offline', + msg: this.message, + ts: { $date: new Date().getTime() }, + u: { + name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), + username: SessionStore.user.RochetChatUser, + _id: "" + }, + _updatedAt: new Date().getTime() + } + + this.addMessageDB(offlineChatMessage) + const message: MessageService = this.prepareMessage(offlineChatMessage) + + setTimeout(() => { + this.scrollDown() + }, 150) + + this.lastMessage = message + + this.redefinedMessage(message) + + this.calDateDuration(message._updatedAt) + this.sortRoomList() + + + this.message= '' + } + + } + + redefinedMessage = (message: MessageService) => { + this.WsChatService.send(this.id, message.msg).then((data: any) => { + let ChatMessage = data.result + + ChatMessage = this.fix_updatedAt(ChatMessage) + + message.setData(ChatMessage) + + if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) { + this.lastMessage = message + this.calDateDuration(message._updatedAt) + } + this.sortRoomList() + }) } @@ -266,22 +324,31 @@ export class RoomService { } - // runs onces only - loadHistory(limit = 100) { - - if (this.hasLoadHistory) { return false } - + restoreMessageFromDB() { this.storage.get('chatmsg' + this.id).then((messages = []) => { let localMessages = [] - messages.forEach(message => { + messages.forEach((message = []) => { const wewMessage = this.prepareMessage(message) + + if(wewMessage.rid == 'offline') { + this.redefinedMessage(wewMessage) + } + localMessages.push(wewMessage) }); this.messages = localMessages }) + } + + // runs onces only + loadHistory(limit = 100) { + + if (this.hasLoadHistory) { return false } + + this.restoreMessageFromDB() this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => { console.log('loadHistory', chatHistory) @@ -317,7 +384,8 @@ export class RoomService { message = this.fix_updatedAt(message) const wewMessage = new MessageService(this.storage) wewMessage.setData(message) - + this.messages.push(wewMessage) + return wewMessage } @@ -343,7 +411,7 @@ export class RoomService { if (message.result) { console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] - } else { + } else if(message._updatedAt.hasOwnProperty('$date')) { // console.log('FIX UPDATE 11', message) message._updatedAt = message._updatedAt['$date'] } diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 4208e6ca7..4d86b8ef6 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -131,7 +131,7 @@ export class WsChatMethodsService { /** * @description sort room list by last message date */ - sortRoomList() { + sortRoomList =() => { this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() } @@ -207,6 +207,7 @@ export class WsChatMethodsService { room.receiveMessage() room.getAllUsers = this.getUsers room.receiveMessageDelete(); + room.sortRoomList = this.sortRoomList let roomId = this.getRoomId(roomData) diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 845a02ba1..af2d28267 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -30,8 +30,8 @@ export class WsChatService { support: ["1"] } - this.ws.send({message, loginRequired: false}) - this.ws.send({message:{msg:"pong"}, loginRequired: false}) + this.ws.send({message, loginRequired: false, requestId: 'connectMessage'}) + this.ws.send({message:{msg:"pong"}, loginRequired: false, requestId: 'connectPong'}) this.ws.registerCallback({ type:'Onmessage', @@ -65,7 +65,7 @@ export class WsChatService { } ] } - this.ws.send({message, requestId, loginRequired: false}) + this.ws.send({message, requestId: 'login', loginRequired: false}) return new Promise((resolve, reject) => { @@ -633,7 +633,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online // console.log('save msgQueue this.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin) - console.log('save msgQueue', requestId) + console.log('save msgQueue', requestId, message) this.wsMsgQueue[requestId] = {message, requestId, loginRequired} } else { diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index d839c7ee7..e5f72e4c6 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -6,4 +6,5 @@ export const environment = { domain: 'gabinetedigital.local', defaultuser: '',//paulo.pinto paulo.pinto@gabinetedigital.local defaultuserpwd: '', //tabteste@006, + chatOffline: true }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 37e513724..cd809f519 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -13,6 +13,7 @@ export const environment = { domain: 'gabinetedigital.local', //gabinetedigital.local defaultuser: 'paulo.pinto@gabinetedigital.local',//paulo.pinto paulo.pinto@gabinetedigital.local defaultuserpwd: 'tabteste@006', //tabteste@006, + chatOffline: true }; /* From 47fd5e3895d9a48b8d28078de92de53e7bcda7ce Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Thu, 3 Feb 2022 15:52:21 +0100 Subject: [PATCH 2/3] receive message --- nice.html | 49 +++++++++++++++++++++++++++ src/app/services/auth.service.ts | 2 +- src/app/services/chat/room.service.ts | 19 ++++++----- 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 nice.html diff --git a/nice.html b/nice.html new file mode 100644 index 000000000..9e964f49f --- /dev/null +++ b/nice.html @@ -0,0 +1,49 @@ + + + + + + + Document + + + + + + + \ No newline at end of file diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index f26ca9f5e..9167fa75d 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -131,7 +131,7 @@ export class AuthService { localStorage.setItem('userChat', JSON.stringify(responseChat)); this.storageService.store(AuthConnstants.AUTH, responseChat); - this.autologout(10000); + // this.autologout(10000); return true; diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 2f2e4645d..a3c58eaed 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -77,6 +77,11 @@ export class RoomService { this.calDateDuration() } + + isSenderIsNotMe(ChatMessage) { + return SessionStore.user.RochetChatUser != ChatMessage.u.username + } + receiveMessage() { this.WsChatService.updateRoomEventss( @@ -84,7 +89,7 @@ export class RoomService { "stream-room-messages", (ChatMessage) => { - if(environment.chatOffline == false) { + if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) { ChatMessage = ChatMessage.fields.args[0] ChatMessage = this.fix_updatedAt(ChatMessage) console.log('recivemessage', ChatMessage) @@ -99,14 +104,10 @@ export class RoomService { this.scrollDown() }, 100) - //this.sortService.sortDate(this.messages, '') - - if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { - this.NativeNotificationService.sendNotificationChat({ - message: message.msg, - title: this.name - }); - } + this.NativeNotificationService.sendNotificationChat({ + message: message.msg, + title: this.name + }); this.addMessageDB(ChatMessage) From 33a2982805548f5a9345b65aec51d6dbb4bd8453 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Thu, 3 Feb 2022 16:14:21 +0100 Subject: [PATCH 3/3] send message offline --- src/app/services/chat/ws-chat.service.ts | 10 +++++++++- src/app/shared/chat/messages/messages.page.html | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 0c97a91a8..4b94ad019 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -14,7 +14,15 @@ export class WsChatService { isLogin = false; loginResponse = {} - constructor() {} + constructor() { + + + window.addEventListener('online', ()=>{ + this.connect() + this.login() + }); + + } connect() { // dont connect if is already connected diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 37c723586..b132f662f 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -48,11 +48,11 @@
- {{msg.u.name}} + {{msg.u.name}} {{msg.offline}} {{showDateDuration(msg._updatedAt)}}
- {{msg.msg}} + {{msg.msg}} {{msg.offline}} {{last ? scrollToBottom() : ''}}
@@ -67,7 +67,7 @@
- {{msg.u.name}} + {{msg.u.name}} {{showDateDuration(msg.duration)}}