This commit is contained in:
Peter Maquiran
2022-02-07 20:18:48 +01:00
parent 202b430966
commit 0d9adb0bb8
9 changed files with 97 additions and 57 deletions
+30 -27
View File
@@ -69,7 +69,7 @@ export class RoomService {
this.NativeNotificationService.askForPermission()
}
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService), _updatedAt }) {
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) {
this.customFields = customFields
this.id = id
this.name = name
@@ -138,6 +138,7 @@ export class RoomService {
addMessageDB(ChatMessage) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
@@ -205,7 +206,7 @@ export class RoomService {
/**
* @description sen text message
*/
send({file = null, attachments = null, temporary= {}}) {
async send({file = null, attachments = null, temporaryData = {}}) {
let offlineChatMessage = {
@@ -213,8 +214,10 @@ export class RoomService {
msg: this.message,
attachments,
file,
temporary
temporaryData
}
console.log('offlineChatMessage', offlineChatMessage)
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
@@ -235,9 +238,8 @@ export class RoomService {
} else {
console.log('complex send')
const result = this.NfService.beforeSendAttachment(message, this)
delete message.temporaryData;
const result = await this.NfService.beforeSendAttachment(message, this)
if(result) {
message.hasSendAttachment = true
@@ -338,57 +340,58 @@ export class RoomService {
}
restoreMessageFromDB() {
this.storage.get('chatmsg' + this.id).then((messages = []) => {
let localMessages: MessageService[] = []
async restoreMessageFromDB() {
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
if(messages==null) messages = []
messages.forEach((ChatMessage, index) => {
await messages.forEach( async (ChatMessage, index) => {
const wewMessage = this.prepareMessage(ChatMessage)
if(wewMessage.offline == true) {
// this.WsChatService.send({roomId:this.id, msg:wewMessage.msg, attachments:wewMessage.attachments, file: wewMessage.file}).then((data: any) => {
// let _ChatMessage = data.result
// this.redefinedMessage(wewMessage, _ChatMessage)
// messages[index] = _ChatMessage
// this.storage.set('chatmsg' + this.id, messages)
// })
}
localMessages.push(wewMessage)
});
this.messages = alasql('SELECT * FROM ? ORDER BY _updatedAt',[localMessages]);
this.messages = alasql('SELECT * FROM ? ORDER BY _updatedAt',[ this.messages]);
setTimeout(()=> {
this.scrollDown()
}, 50)
})
}
// runs onces only
loadHistory(limit = 100) {
async loadHistory(limit = 100) {
if (this.hasLoadHistory) { return false }
this.restoreMessageFromDB()
await this.restoreMessageFromDB()
this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => {
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
this.messages = []
let localMessages = []
await chatHistory.result.messages.reverse().forEach( async (message) => {
this.prepareMessage(message)
chatHistory.result.messages.reverse().forEach(message => {
const wewMessage = this.prepareMessage(message)
localMessages.push(wewMessage)
// const result = alasql(`SELECT * FROM ? WHERE _id = "${message._id}" `,[ this.messages]);
// if(result.length == 0) {
// this.prepareMessage(message)
// this.storage.set('chatmsg' + this.id, chatHistory.result.messages.concat([message]))
// }
});
this.messages = localMessages
console.log(chatHistory.result.messages);
this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse())
})
setTimeout(() => {
@@ -402,7 +405,7 @@ export class RoomService {
prepareMessage(message): MessageService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
wewMessage.setData(message)
this.messages.push(wewMessage)