diff --git a/src/app/services/chat/chat-storage.service.spec.ts b/src/app/services/chat/chat-storage.service.spec.ts new file mode 100644 index 000000000..34a01e3a9 --- /dev/null +++ b/src/app/services/chat/chat-storage.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ChatStorageService } from './chat-storage.service'; + +describe('ChatStorageService', () => { + let service: ChatStorageService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ChatStorageService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/chat/chat-storage.service.ts b/src/app/services/chat/chat-storage.service.ts new file mode 100644 index 000000000..ae0262824 --- /dev/null +++ b/src/app/services/chat/chat-storage.service.ts @@ -0,0 +1,170 @@ +import { Injectable } from '@angular/core'; +import { Storage } from '@ionic/storage'; + +@Injectable({ + providedIn: 'root' +}) +export class ChatStorageService { + + constructor( + private storage: Storage, + ) { } + + + /** + * @description delete message in the DB. get all messages, delete then corresponding message and update the store + * @param id message ID + */ + private deleteMessageFromDb(messageId, roomId) { + this.storage.get('chatmsg' + roomId).then((messages: any = []) => { + + messages.forEach((message, index) => { + + if(message._id == messageId) { + messages.splice(index, 1) + } + + }) + + this.storage.set('chatmsg' + roomId, messages).then((value) => { + console.log('MSG SAVED ON STORAGE', value) + }); + }) + } + + + getMsgFromDB() { + + /* this.storage.get('chatmsg' + this.id).then((message) => { + console.log('ALL MESSAGE WEB', message) + message.forEach(message => { + + if (message.file) { + if (message.file.guid) { + this.storage.get(message.file.guid).then((image) => { + //console.log('IMAGE FROM STORAGE', image) + message.file.image_url = image + }); + } + } + + let mmessage = this.fix_updatedAt(message) + console.log('FROM DB WEB', mmessage) + const wewMessage = new MessageService(this.storage) + wewMessage.setData(mmessage) + this.messages.push(wewMessage) + console.log('loadHistory 222', this.messages) + }); + }) */ + } + + async transformData(res) { + + // this.mgsArray = []; + // res.forEach(async element => { + + // if (element.file) { + // if (element.file.guid) { + // await this.storage.get(element.file.guid).then((image) => { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // file: { + // guid: element.file.guid, + // image_url: image, + // type: element.file.type + // }, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg); + + // }) + // } else { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // file: element.file, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg) + // } + // } else { + // let chatmsg = { + // _id: element._id, + // attachments: element.attachments, + // channels: element.channels, + // mentions: element.mentions, + // msg: element.msg, + // rid: element.rid, + // ts: element.ts, + // u: element.u, + // _updatedAt: element._updatedAt, + // } + + // this.mgsArray.push(chatmsg) + // } + + // }); + // await this.storage.remove('chatmsg').then(() => { + // console.log('MSG REMOVE FROM STORAGE') + // }); + // await this.storage.set('chatmsg', this.mgsArray).then((value) => { + // console.log('MSG SAVED ON STORAGE', value) + // }); + + } + + + + getMsgFromDBMobile() { + // console.log('ALL MSG DBBB', this.id) + // this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => { + // let ad = []; + // ad = msg + // console.log('ALL MSG DBBB', ad.length) + // msg.map(element => { + // console.log('CHANNEL ELEMENT', element) + // let msgChat = { + // _id: element.Id, + // attachments: this.isJson(element.Attachments), + // channels: this.isJson(element.Channels), + // file: { + // guid: this.isJson(element.File).guid, + // image_url: this.isJson(element.image_url), + // type: this.isJson(element.File).type + + // }, + // mentions: this.isJson(element.Mentions), + // msg: element.Msg, + // rid: element.Rid, + // ts: element.Ts, + // u: this.isJson(element.U), + // _updatedAt: this.isJson(element.UpdatedAt), + // } + + // let mmessage = this.fix_updatedAt(msgChat) + // console.log('FROM DB WEB', mmessage) + // const wewMessage = new MessageService(this.storage) + // wewMessage.setData(mmessage) + // this.messages.push(wewMessage) + // console.log('loadHistory 222', this.messages) + // }); + // }); + } + + +} diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index a785e5b12..fb89ea961 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -84,9 +84,8 @@ export class RoomService { ChatMessage = this.fix_updatedAt(ChatMessage) console.log('recivemessage', ChatMessage) - /* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */ - const message = new MessageService(this.storage) - message.setData(ChatMessage) + + const message = this.prepareMessage(ChatMessage) this.lastMessage = message if (message.t == 'r') { this.name = message.msg } @@ -140,29 +139,61 @@ export class RoomService { this.id, "stream-notify-room", async (ChatMessage) => { - console.log(ChatMessage.fields.args[0]._id); - const messageId = ChatMessage.fields.args[0]._id; - this.messages.forEach((message, index)=>{ - if(message._id == messageId){ - this.messages.splice(index, 1) - this.storage.set('chatmsg' + this.id, this.messages).then((value) => { - //console.log('MSG DELETE ON STORAGE', value) - }); - //Get previous last message from room - const previousLastMessage = this.messages.slice(-1)[0]; - - this.lastMessage = previousLastMessage; - this.calDateDuration(previousLastMessage._updatedAt) - - } - }) + const DeletedMessageId = ChatMessage.fields.args[0]._id; + console.log(DeletedMessageId); + this.deleteMessage(DeletedMessageId) } ) this.WsChatService.registerCallback } + /** + * @description delete message in the view + * @param id message ID + */ + deleteMessage(id) { + this.messages.forEach((message, index) => { + if(message._id == id) { + this.messages.splice(index, 1) + + this.deleteMessageFromDb(id) + //Get previous last message from room + const previousLastMessage = this.messages.slice(-1)[0]; + + this.lastMessage = previousLastMessage; + this.calDateDuration(previousLastMessage._updatedAt) + + } + }) + } + + + /** + * @description delete message in the DB. get all messages, delete then corresponding message and update the store + * @param id message ID + */ + private deleteMessageFromDb(id) { + this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + + messages.forEach((message, index) => { + + if(message._id == id) { + messages.splice(index, 1) + } + + }) + + this.storage.set('chatmsg' + this.id, messages).then((value) => { + console.log('MSG SAVED ON STORAGE', value) + }); + }) + } + + /** + * @description sen text message + */ send() { this.WsChatService.send(this.id, this.message) this.message= '' @@ -224,43 +255,7 @@ export class RoomService { leave(rid?) { this.WsChatService.leaveRoom(this.id) } - - getMsgFromDBMobile() { - console.log('ALL MSG DBBB', this.id) - this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => { - let ad = []; - ad = msg - console.log('ALL MSG DBBB', ad.length) - msg.map(element => { - console.log('CHANNEL ELEMENT', element) - let msgChat = { - _id: element.Id, - attachments: this.isJson(element.Attachments), - channels: this.isJson(element.Channels), - file: { - guid: this.isJson(element.File).guid, - image_url: this.isJson(element.image_url), - type: this.isJson(element.File).type - - }, - mentions: this.isJson(element.Mentions), - msg: element.Msg, - rid: element.Rid, - ts: element.Ts, - u: this.isJson(element.U), - _updatedAt: this.isJson(element.UpdatedAt), - } - - let mmessage = this.fix_updatedAt(msgChat) - console.log('FROM DB WEB', mmessage) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(mmessage) - this.messages.push(wewMessage) - console.log('loadHistory 222', this.messages) - }); - }); - } - + isJson(str) { try { JSON.parse(str); @@ -270,32 +265,6 @@ export class RoomService { return JSON.parse(str); } - getMsgFromDB() { - - /* this.storage.get('chatmsg' + this.id).then((message) => { - console.log('ALL MESSAGE WEB', message) - message.forEach(message => { - - if (message.file) { - if (message.file.guid) { - this.storage.get(message.file.guid).then((image) => { - //console.log('IMAGE FROM STORAGE', image) - message.file.image_url = image - }); - } - } - - let mmessage = this.fix_updatedAt(message) - console.log('FROM DB WEB', mmessage) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(mmessage) - this.messages.push(wewMessage) - console.log('loadHistory 222', this.messages) - }); - }) */ - } - - // runs onces only loadHistory(limit = 100) { @@ -307,9 +276,7 @@ export class RoomService { let localMessages = [] messages.forEach(message => { - message = this.fix_updatedAt(message) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(message) + const wewMessage = this.prepareMessage(message) localMessages.push(wewMessage) }); @@ -320,12 +287,11 @@ export class RoomService { console.log('loadHistory', chatHistory) let localMessages = [] - //const sortedRoomList = this.sortService.sortDate(chatHistory.result.messages, "_updatedAt.$date") + chatHistory.result.messages.reverse().forEach(message => { - message = this.fix_updatedAt(message) - const wewMessage = new MessageService(this.storage) - wewMessage.setData(message) + const wewMessage = this.prepareMessage(message) + localMessages.push(wewMessage) }); @@ -338,28 +304,6 @@ export class RoomService { }) - /* this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => { - - const mgsArray = chatHistory.result.messages.reverse(); - if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - await this.storage.remove('chatmsg' + this.id).then(() => { - console.log('MSG REMOVE ON STORAGE') - }) - await this.storage.set('chatmsg' + this.id, mgsArray).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - this.getMsgFromDB() - }); - } else { - mgsArray.forEach((element) => { - console.log('SQLITE WEBSOCKET', element) - this.sqlservice.addChatMSG(element) - }) - this.getMsgFromDBMobile() - } - - - }) */ - setTimeout(() => { this.scrollDown() }, 50) @@ -368,6 +312,15 @@ export class RoomService { } + + prepareMessage(message) { + message = this.fix_updatedAt(message) + const wewMessage = new MessageService(this.storage) + wewMessage.setData(message) + + return wewMessage + } + updateMeessage(messageID, imgbase64) { } @@ -377,75 +330,6 @@ export class RoomService { return res; } - async transformData(res) { - - this.mgsArray = []; - res.forEach(async element => { - - if (element.file) { - if (element.file.guid) { - await this.storage.get(element.file.guid).then((image) => { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - file: { - guid: element.file.guid, - image_url: image, - type: element.file.type - }, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg); - - }) - } else { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - file: element.file, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg) - } - } else { - let chatmsg = { - _id: element._id, - attachments: element.attachments, - channels: element.channels, - mentions: element.mentions, - msg: element.msg, - rid: element.rid, - ts: element.ts, - u: element.u, - _updatedAt: element._updatedAt, - } - - this.mgsArray.push(chatmsg) - } - - }); - await this.storage.remove('chatmsg').then(() => { - console.log('MSG REMOVE FROM STORAGE') - }); - await this.storage.set('chatmsg', this.mgsArray).then((value) => { - console.log('MSG SAVED ON STORAGE', value) - }); - - } ReactToMessage() { } 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 52558f953..5747db9e1 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -111,7 +111,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.getRoomInfo(); //this.scrollToBottom(); this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); - this.wsChatMethodsService.getGroupRoom(this.roomId).getMsgFromDB(); // console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); this.wsChatMethodsService.openRoom(this.roomId) this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked