This commit is contained in:
Peter Maquiran
2022-03-03 22:57:33 +01:00
parent 3a81e6df4d
commit f5880fec2a
15 changed files with 877 additions and 801 deletions
+114 -129
View File
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
@@ -15,156 +16,140 @@ export class ChatStorageService {
* @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 = []) => {
async deleteMessageFromDb(messageId, roomId) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
messages.forEach((message, index) => {
await messages.forEach( async (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)
});
})
}
}
async updateMessageDB(ChatMessage, roomId, identificator) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
let index;
const find = messages.find((message, _index)=> {
if(message?.localReference == ChatMessage?.localReference ||
message?._id == ChatMessage?._id) {
index = _index
return true
}
return false
})
if(find) {
messages[index] = Object.assign(messages[index], ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
} else {
console.log('failed to update', identificator, ':',ChatMessage)
}
})
}
}
getMsgFromDB() {
async updateChat(history, roomId, identificator = '_id') {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
/* 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)
});
}) */
}
history.forEach( async(ChatMessage)=>{
let index;
const find = messages.find((message, _index)=> {
if(message[identificator]) {
if(message[identificator] == ChatMessage[identificator]) {
index = _index
return true
}
}
return false
})
if(find) {
messages[index] = Object.assign(messages[index], ChatMessage)
// if(messages[index].msg.includes('***********')) {
// console.log('storage update')
// console.log(JSON.stringify(messages[index]))
// console.log(JSON.stringify(ChatMessage))
// }
await this.storage.set('chatmsg' + roomId, messages)
} else {
console.log('failed to update', identificator)
}
})
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)
// });
})
}
}
async addMessageDB(ChatMessage, roomId) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
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)
// });
// });
if(!ChatMessage._id && environment.chatOffline) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
console.log('add to DB', ChatMessage)
} else {
const find = messages.find((message)=> {
return message._id == ChatMessage._id
})
if(!find) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
console.log('add to DB', ChatMessage)
}
}
})
}
}
}