From ce7eb44197a9691a4eb25e2452899615f5874545 Mon Sep 17 00:00:00 2001 From: Phillip Lang Martinez Date: Sat, 29 Oct 2022 18:25:55 +0300 Subject: [PATCH] refactor newMessage and initial --- .../plugins/core/components/ChatPage.js | 268 ++---------------- 1 file changed, 31 insertions(+), 237 deletions(-) diff --git a/qortal-ui-plugins/plugins/core/components/ChatPage.js b/qortal-ui-plugins/plugins/core/components/ChatPage.js index bc4e85b8..f0168fc7 100644 --- a/qortal-ui-plugins/plugins/core/components/ChatPage.js +++ b/qortal-ui-plugins/plugins/core/components/ChatPage.js @@ -456,14 +456,14 @@ class ChatPage extends LitElement { }) - const updateMessages2 = await replaceMessagesEdited({ + const replacedMessages = await replaceMessagesEdited({ decodedMessages: decodeMsgs, parentEpml, isReceipient: this.isReceipient, decodeMessageFunc: this.decodeMessage, _publicKey: this._publicKey }) - this.messagesRendered = [...updateMessages2, ...this.messagesRendered].sort(function (a, b) { + this.messagesRendered = [...replacedMessages, ...this.messagesRendered].sort(function (a, b) { return a.timestamp - b.timestamp }) @@ -479,100 +479,18 @@ class ChatPage extends LitElement { const decodeMsgs = getInitialMessages.map((eachMessage) => { - return this.decodeMessage(eachMessage) - - - - - }) - const findNewMessages = decodeMsgs.map(async(msg)=> { - - let msgItem = msg - try { - let msgQuery = `&involving=${msg.recipient}&involving=${msg.sender}` - if(!this.isReceipient){ - msgQuery = `&txGroupId=${msg.txGroupId}` - } - const response = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?chatreference=${msg.reference}&reverse=true${msgQuery}`, - }); - - if(response && Array.isArray(response) && response.length !== 0){ - let responseItem = {...response[0]} - delete responseItem.timestamp - msgItem = { - ...msg, - ...responseItem, - editedTimestamp : response[0].timestamp - } - } - - } catch (error) { - console.log(error) - } - - return msgItem - }) - const updateMessages = await Promise.all(findNewMessages) - const findNewMessages2 = updateMessages.map(async(msg)=> { - - let parsedMessageObj = msg - try { - parsedMessageObj = JSON.parse(msg.decodedMessage) - } catch (error) { - return msg - } - - - let msgItem = msg - try { - let msgQuery = `&involving=${msg.recipient}&involving=${msg.sender}` - if(!this.isReceipient){ - msgQuery = `&txGroupId=${msg.txGroupId}` - } - if(parsedMessageObj.repliedTo){ - const response = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?chatreference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response && Array.isArray(response) && response.length !== 0){ - - msgItem = { - ...msg, - repliedToData : this.decodeMessage(response[0]) - } - } else { - - const response2 = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?reference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response2 && Array.isArray(response2) && response2.length !== 0){ - - msgItem = { - ...msg, - repliedToData : this.decodeMessage(response2[0]) - } - } - } - - } - - - } catch (error) { - console.log(error) - } - - return msgItem + const replacedMessages = await replaceMessagesEdited({ + decodedMessages: decodeMsgs, + parentEpml, + isReceipient: this.isReceipient, + decodeMessageFunc: this.decodeMessage, + _publicKey: this._publicKey }) - const updateMessages2 = await Promise.all(findNewMessages2) - this.messagesRendered = [...updateMessages2, ...this.messagesRendered].sort(function (a, b) { + + this.messagesRendered = [...replacedMessages, ...this.messagesRendered].sort(function (a, b) { return a.timestamp - b.timestamp }) @@ -586,40 +504,7 @@ class ChatPage extends LitElement { async processMessages(messages, isInitial) { const isReceipient = this.chatId.includes('direct') - const findNewMessages = messages.map(async(msg)=> { - - let msgItem = msg - try { - let msgQuery = `&involving=${msg.recipient}&involving=${msg.sender}` - if(!isReceipient){ - msgQuery = `&txGroupId=${msg.txGroupId}` - } - const response = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?chatreference=${msg.reference}&reverse=true${msgQuery}`, - }); - - if(response && Array.isArray(response) && response.length !== 0){ - let responseItem = {...response[0]} - delete responseItem.timestamp - msgItem = { - ...msg, - ...responseItem, - editedTimestamp : response[0].timestamp - } - } - - } catch (error) { - console.log(error) - } - - return msgItem - }) - const updateMessages = await Promise.all(findNewMessages) - - if (isInitial) { - - const decodedMessages = updateMessages.map((eachMessage) => { + const decodedMessages = messages.map((eachMessage) => { if (eachMessage.isText === true) { this.messageSignature = eachMessage.signature @@ -631,66 +516,20 @@ class ChatPage extends LitElement { return _eachMessage } }) + + if (isInitial) { + - const findNewMessages2 = decodedMessages.map(async(msg)=> { - - let parsedMessageObj = msg - try { - parsedMessageObj = JSON.parse(msg.decodedMessage) - } catch (error) { - return msg - } - - - let msgItem = msg - try { - let msgQuery = `&involving=${msg.recipient}&involving=${msg.sender}` - if(!isReceipient){ - msgQuery = `&txGroupId=${msg.txGroupId}` - } - if(parsedMessageObj.repliedTo){ - const response = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?chatreference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response && Array.isArray(response) && response.length !== 0){ - - msgItem = { - ...msg, - repliedToData : this.decodeMessage(response[0]) - } - } else { - - const response2 = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?reference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response2 && Array.isArray(response2) && response2.length !== 0){ - - msgItem = { - ...msg, - repliedToData : this.decodeMessage(response2[0]) - } - } - } - - } - - - } catch (error) { - console.log(error) - } - - return msgItem + const replacedMessages = await replaceMessagesEdited({ + decodedMessages: decodedMessages, + parentEpml, + isReceipient: isReceipient, + decodeMessageFunc: this.decodeMessage, + _publicKey: this._publicKey }) - const updateMessages2 = await Promise.all(findNewMessages2) - - - this._messages = updateMessages2.sort(function (a, b) { + this._messages = replacedMessages.sort(function (a, b) { return a.timestamp - b.timestamp }) @@ -707,64 +546,19 @@ class ChatPage extends LitElement { this.isLoadingMessages = false setTimeout(() => this.downElementObserver(), 500) } else { + const replacedMessages = await replaceMessagesEdited({ + decodedMessages: decodedMessages, + parentEpml, + isReceipient: isReceipient, + decodeMessageFunc: this.decodeMessage, + _publicKey: this._publicKey + }) - const findNewMessages2 = messages.map(async(msg)=> { - const _eachMessage = this.decodeMessage(msg) - let parsedMessageObj = _eachMessage - try { - parsedMessageObj = JSON.parse(_eachMessage.decodedMessage) - } catch (error) { - return msg - } - - - let msgItem = _eachMessage - - let msgQuery = `&involving=${_eachMessage.recipient}&involving=${_eachMessage.sender}` - if(!isReceipient){ - msgQuery = `&txGroupId=${_eachMessage.txGroupId}` - } - try { - if(parsedMessageObj.repliedTo){ - const response = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?chatreference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response && Array.isArray(response) && response.length !== 0){ - - msgItem = { - ..._eachMessage, - repliedToData : this.decodeMessage(response[0]) - } - } else { - - const response2 = await parentEpml.request('apiCall', { - type: 'api', - url: `/chat/messages?reference=${parsedMessageObj.repliedTo}&reverse=true${msgQuery}`, - }); - - if(response2 && Array.isArray(response2) && response2.length !== 0){ - - msgItem = { - ..._eachMessage, - repliedToData : this.decodeMessage(response2[0]) - } - } - } - - } - - - } catch (error) { - console.log(error) - } - - - this.renderNewMessage(msgItem) + const renderEachMessage = replacedMessages.map(async(msg)=> { + await this.renderNewMessage(msg) }) - await Promise.all(findNewMessages2) + await Promise.all(renderEachMessage) // this.newMessages = this.newMessages.concat(_newMessages)