From 6c8f405cb831fcea8d775e1e688f2c55100d133a Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 30 Mar 2023 16:58:34 +0100 Subject: [PATCH 1/3] remove json from chat messages --- .../components/chat-dialog/chat-dialog.html | 9 +++- static/components/chat-dialog/chat-dialog.js | 44 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/static/components/chat-dialog/chat-dialog.html b/static/components/chat-dialog/chat-dialog.html index e163c25..a88395a 100644 --- a/static/components/chat-dialog/chat-dialog.html +++ b/static/components/chat-dialog/chat-dialog.html @@ -41,10 +41,15 @@ :name="message.sender" :text="[message.msg]" :sent="message.sender == 'Me'" - :bg-color="message.sender == 'Me' ? 'white' : 'light-green-2'" + :bg-color="message.json ? 'yellow-11' : message.sender == 'Me' ? 'white' : 'light-green-2'" :stamp="message.timestamp" size="6" - /> + > diff --git a/static/components/chat-dialog/chat-dialog.js b/static/components/chat-dialog/chat-dialog.js index 6cc42d9..83ed5ba 100644 --- a/static/components/chat-dialog/chat-dialog.js +++ b/static/components/chat-dialog/chat-dialog.js @@ -59,19 +59,41 @@ async function chatDialog(path) { }, computed: { sortedMessages() { - return this.nostrMessages.sort((a, b) => b.created_at - a.created_at) + return this.nostrMessages + .map(m => { + if (!isJson(m.msg)) return m + let msg = JSON.parse(m.msg) + if (msg?.message) { + m.json = m.msg + m.msg = msg.message + return m + } + if (msg?.items) { + m.json = m.msg + m.msg = 'Order placed!' + return m + } + if (msg?.payment_options) { + m.json = m.msg + m.msg = '⚡︎ Invoice sent!' + return m + } + return m + }) + .sort((a, b) => b.created_at - a.created_at) }, ordersList() { - let orders = this.nostrMessages - .sort((a, b) => b.created_at - a.created_at) - .filter(o => isJson(o.msg)) - .reduce((acc, cur) => { - const obj = JSON.parse(cur.msg) - const key = obj.id - const curGroup = acc[key] ?? {created_at: cur.timestamp} - return {...acc, [key]: {...curGroup, ...obj}} - }, {}) - return Object.values(orders) + return Object.values( + this.nostrMessages + .sort((a, b) => b.created_at - a.created_at) + .filter(o => isJson(o.msg)) + .reduce((acc, cur) => { + const obj = JSON.parse(cur.msg) + const key = obj.id + const curGroup = acc[key] ?? {created_at: cur.timestamp} + return {...acc, [key]: {...curGroup, ...obj}} + }, {}) + ) } }, methods: { From bb0e6c069f5ad75f133081872c37036df693e915 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 31 Mar 2023 14:40:25 +0100 Subject: [PATCH 2/3] fix: order not showing behaviour after loading --- static/components/chat-dialog/chat-dialog.js | 105 ++++++++++--------- 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/static/components/chat-dialog/chat-dialog.js b/static/components/chat-dialog/chat-dialog.js index 83ed5ba..b9c6d66 100644 --- a/static/components/chat-dialog/chat-dialog.js +++ b/static/components/chat-dialog/chat-dialog.js @@ -11,6 +11,7 @@ async function chatDialog(path) { dialog: false, isChat: true, loading: false, + messagesMap: new Map(), nostrMessages: [], newMessage: '', ordersTable: { @@ -83,17 +84,17 @@ async function chatDialog(path) { .sort((a, b) => b.created_at - a.created_at) }, ordersList() { - return Object.values( - this.nostrMessages - .sort((a, b) => b.created_at - a.created_at) - .filter(o => isJson(o.msg)) - .reduce((acc, cur) => { - const obj = JSON.parse(cur.msg) - const key = obj.id - const curGroup = acc[key] ?? {created_at: cur.timestamp} - return {...acc, [key]: {...curGroup, ...obj}} - }, {}) - ) + let orders = this.nostrMessages + .sort((a, b) => b.created_at - a.created_at) + .filter(o => isJson(o.json)) + .reduce((acc, cur) => { + const obj = JSON.parse(cur.json) + const key = obj.id + const curGroup = acc[key] ?? {created_at: cur.timestamp} + return {...acc, [key]: {...curGroup, ...obj}} + }, {}) + + return Object.values(orders) } }, methods: { @@ -107,8 +108,8 @@ async function chatDialog(path) { }, async startPool() { this.loading = true - let messagesMap = new Map() - let sub = this.pool.sub(Array.from(this.relays), [ + let relays = Array.from(this.relays) + let filters = [ { kinds: [4], authors: [this.account.pubkey] @@ -117,45 +118,55 @@ async function chatDialog(path) { kinds: [4], '#p': [this.account.pubkey] } - ]) + ] + let events = await this.pool.list(relays, filters) - sub.on('eose', () => { - this.loading = false - this.nostrMessages = Array.from(messagesMap.values()) - }) + for (const event of events) { + await this.processMessage(event) + } + + this.nostrMessages = Array.from(this.messagesMap.values()) + this.loading = false + + let sub = this.pool.sub( + relays, + filters.map(f => ({...f, since: Date.now() / 1000})) + ) sub.on('event', async event => { - let mine = event.pubkey == this.account.pubkey - let sender = mine ? this.merchant : event.pubkey - - try { - let plaintext - if (this.account.privkey) { - plaintext = await NostrTools.nip04.decrypt( - this.account.privkey, - sender, - event.content - ) - } else if (this.account.useExtension && this.hasNip07) { - plaintext = await window.nostr.nip04.decrypt( - sender, - event.content - ) - } - if (plaintext) { - messagesMap.set(event.id, { - created_at: event.created_at, - msg: plaintext, - timestamp: timeFromNow(event.created_at * 1000), - sender: `${mine ? 'Me' : 'Merchant'}` - }) - this.nostrMessages = Array.from(messagesMap.values()) - } - } catch { - console.debug('Unable to decrypt message! Not for us...') - } + await this.processMessage(event) + this.nostrMessages = Array.from(this.messagesMap.values()) }) this.sub = sub }, + async processMessage(event) { + let mine = event.pubkey == this.account.pubkey + let sender = mine ? this.merchant : event.pubkey + + try { + let plaintext + if (this.account.privkey) { + plaintext = await NostrTools.nip04.decrypt( + this.account.privkey, + sender, + event.content + ) + } else if (this.account.useExtension && this.hasNip07) { + plaintext = await window.nostr.nip04.decrypt(sender, event.content) + } + if (plaintext) { + this.messagesMap.set(event.id, { + created_at: event.created_at, + msg: plaintext, + timestamp: timeFromNow(event.created_at * 1000), + sender: `${mine ? 'Me' : 'Merchant'}` + }) + } + return null + } catch { + console.debug('Unable to decrypt message! Not for us...') + return null + } + }, async sendMessage() { if (this.newMessage && this.newMessage.length < 1) return let event = { From 1cccfee95d07dedbd0bfb460839acf1d38a98e91 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 31 Mar 2023 16:00:33 +0100 Subject: [PATCH 3/3] some bling bling to the chat --- .../components/chat-dialog/chat-dialog.html | 3 +- static/components/chat-dialog/chat-dialog.js | 51 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/static/components/chat-dialog/chat-dialog.html b/static/components/chat-dialog/chat-dialog.html index a88395a..5f15ec0 100644 --- a/static/components/chat-dialog/chat-dialog.html +++ b/static/components/chat-dialog/chat-dialog.html @@ -41,11 +41,12 @@ :name="message.sender" :text="[message.msg]" :sent="message.sender == 'Me'" - :bg-color="message.json ? 'yellow-11' : message.sender == 'Me' ? 'white' : 'light-green-2'" + :bg-color="message.json ? 'purple-2' : message.sender == 'Me' ? 'white' : 'light-green-2'" :stamp="message.timestamp" size="6" > { - if (!isJson(m.msg)) return m - let msg = JSON.parse(m.msg) - if (msg?.message) { - m.json = m.msg - m.msg = msg.message - return m - } - if (msg?.items) { - m.json = m.msg - m.msg = 'Order placed!' - return m - } - if (msg?.payment_options) { - m.json = m.msg - m.msg = '⚡︎ Invoice sent!' - return m - } - return m - }) - .sort((a, b) => b.created_at - a.created_at) + return this.nostrMessages.sort((a, b) => b.created_at - a.created_at) }, ordersList() { let orders = this.nostrMessages + .filter(o => o.json) .sort((a, b) => b.created_at - a.created_at) - .filter(o => isJson(o.json)) .reduce((acc, cur) => { - const obj = JSON.parse(cur.json) + const obj = cur.json const key = obj.id const curGroup = acc[key] ?? {created_at: cur.timestamp} return {...acc, [key]: {...curGroup, ...obj}} @@ -154,11 +133,13 @@ async function chatDialog(path) { plaintext = await window.nostr.nip04.decrypt(sender, event.content) } if (plaintext) { + let {text, json} = this.filterJsonMsg(plaintext) this.messagesMap.set(event.id, { created_at: event.created_at, - msg: plaintext, + msg: text, timestamp: timeFromNow(event.created_at * 1000), - sender: `${mine ? 'Me' : 'Merchant'}` + sender: `${mine ? 'Me' : 'Merchant'}`, + json }) } return null @@ -167,6 +148,24 @@ async function chatDialog(path) { return null } }, + filterJsonMsg(text) { + let json = null + + if (!isJson(text)) return {text, json} + + json = JSON.parse(text) + if (json.message) { + text = json.message + } else if (json.items) { + text = `Order placed!
OrderID: ${json.id}` + } else if (json.payment_options) { + text = `Invoice for order: ${json.id}
LN ⚡︎` + } + + return {text, json} + }, async sendMessage() { if (this.newMessage && this.newMessage.length < 1) return let event = {