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/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 42f80a4a2..a3c58eaed 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, @@ -74,47 +77,41 @@ export class RoomService { this.calDateDuration() } + + isSenderIsNotMe(ChatMessage) { + return SessionStore.user.RochetChatUser != ChatMessage.u.username + } + receiveMessage() { this.WsChatService.updateRoomEventss( this.id, "stream-room-messages", (ChatMessage) => { - ChatMessage = ChatMessage.fields.args[0] - ChatMessage = this.fix_updatedAt(ChatMessage) - console.log('recivemessage', ChatMessage) - const message = this.prepareMessage(ChatMessage) + if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) { + ChatMessage = ChatMessage.fields.args[0] + ChatMessage = this.fix_updatedAt(ChatMessage) + console.log('recivemessage', ChatMessage) + + const message = this.prepareMessage(ChatMessage) - if(message._updatedAt == undefined){ - message._updatedAt = new Date().getTime(); - } - - this.lastMessage = message - if (message.t == 'r') { this.name = message.msg } - this.calDateDuration(message._updatedAt) - this.messages.push(message) - - setTimeout(() => { - this.scrollDown() - }, 100) - - if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { + this.lastMessage = message + if (message.t == 'r') { this.name = message.msg } + this.calDateDuration(ChatMessage._updatedAt) + + setTimeout(() => { + this.scrollDown() + }, 100) + 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) - }); - }) - } ) @@ -134,6 +131,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( @@ -196,8 +202,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() + }) } @@ -267,22 +325,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) @@ -318,7 +385,8 @@ export class RoomService { message = this.fix_updatedAt(message) const wewMessage = new MessageService(this.storage) wewMessage.setData(message) - + this.messages.push(wewMessage) + return wewMessage } @@ -344,8 +412,8 @@ export class RoomService { if (message.result) { //console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] - } else { - //console.log('FIX UPDATE 11', message) + } else if(message._updatedAt.hasOwnProperty('$date')) { + // console.log('FIX UPDATE 11', message) message._updatedAt = message._updatedAt['$date'] } return message diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 63f37c4d7..b8879a467 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -141,7 +141,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() } @@ -217,6 +217,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 54a508f0e..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 @@ -30,8 +38,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 +73,7 @@ export class WsChatService { } ] } - this.ws.send({message, requestId, loginRequired: false}) + this.ws.send({message, requestId: 'login', loginRequired: false}) return new Promise((resolve, reject) => { @@ -662,7 +670,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/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)}}
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 }; /*