From e6f7dc85e01a6611b238142413c1a5a3fd2ba538 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Mon, 20 Mar 2023 10:01:21 +0000 Subject: [PATCH 1/3] fix: only one ws connection open --- static/components/chat-dialog/chat-dialog.js | 32 ++------- .../customer-stall/customer-stall.js | 66 ++++++++++--------- 2 files changed, 39 insertions(+), 59 deletions(-) diff --git a/static/components/chat-dialog/chat-dialog.js b/static/components/chat-dialog/chat-dialog.js index abca869..72a4d17 100644 --- a/static/components/chat-dialog/chat-dialog.js +++ b/static/components/chat-dialog/chat-dialog.js @@ -129,7 +129,7 @@ async function chatDialog(path) { this.nostrMessages = Array.from(messagesMap.values()) } } catch { - console.error('Unable to decrypt message!') + console.debug('Unable to decrypt message! Not for us...') } }) this.sub = sub @@ -146,33 +146,11 @@ async function chatDialog(path) { } event.id = NostrTools.getEventHash(event) event.sig = this.signEvent(event) - // This doesn't work yet - // this.pool.publish(Array.from(this.relays), event) - // this.newMessage = '' - // We need this hack - for (const url of Array.from(this.relays)) { - try { - let relay = NostrTools.relayInit(url) - relay.on('connect', () => { - console.debug(`connected to ${relay.url}`) - }) - relay.on('error', () => { - console.debug(`failed to connect to ${relay.url}`) - }) - await relay.connect() - let pub = relay.publish(event) - pub.on('ok', () => { - console.debug(`${relay.url} has accepted our event`) - }) - pub.on('failed', reason => { - console.debug(`failed to publish to ${relay.url}: ${reason}`) - }) - this.newMessage = '' - } catch (e) { - console.error(e) - } - } + let pub = this.pool.publish(Array.from(this.relays), event) + pub.on('ok', () => console.debug(`Event was sent`)) + pub.on('failed', error => console.error(error)) + this.newMessage = '' }, async encryptMsg() { try { diff --git a/static/components/customer-stall/customer-stall.js b/static/components/customer-stall/customer-stall.js index faac15a..a4d115b 100644 --- a/static/components/customer-stall/customer-stall.js +++ b/static/components/customer-stall/customer-stall.js @@ -274,32 +274,35 @@ async function customerStall(path) { await this.sendOrder(event) }, async sendOrder(order) { - for (const url of Array.from(this.relays)) { - try { - let relay = NostrTools.relayInit(url) - relay.on('connect', () => { - console.debug(`connected to ${relay.url}`) - }) - relay.on('error', () => { - console.debug(`failed to connect to ${relay.url}`) - relay.close() - return - }) + let pub = this.pool.publish(Array.from(this.relays), order) + pub.on('ok', () => console.debug(`Order event was sent`)) + pub.on('failed', error => console.error(error)) + // for (const url of Array.from(this.relays)) { + // try { + // let relay = NostrTools.relayInit(url) + // relay.on('connect', () => { + // console.debug(`connected to ${relay.url}`) + // }) + // relay.on('error', () => { + // console.debug(`failed to connect to ${relay.url}`) + // relay.close() + // return + // }) - await relay.connect() - let pub = relay.publish(order) - pub.on('ok', () => { - console.debug(`${relay.url} has accepted our event: ${order.id}`) - relay.close() - }) - pub.on('failed', reason => { - console.debug(`failed to publish to ${relay.url}: ${reason}`) - relay.close() - }) - } catch (err) { - console.error(`Error: ${err}`) - } - } + // await relay.connect() + // let pub = relay.publish(order) + // pub.on('ok', () => { + // console.debug(`${relay.url} has accepted our event: ${order.id}`) + // relay.close() + // }) + // pub.on('failed', reason => { + // console.debug(`failed to publish to ${relay.url}: ${reason}`) + // relay.close() + // }) + // } catch (err) { + // console.error(`Error: ${err}`) + // } + // } this.loading = false this.resetCart() this.openQrCodeDialog() @@ -308,7 +311,7 @@ async function customerStall(path) { async listenMessages() { this.loading = true try { - const pool = new NostrTools.SimplePool() + // const pool = new NostrTools.SimplePool() const filters = [ { kinds: [4], @@ -320,7 +323,7 @@ async function customerStall(path) { } ] let relays = Array.from(this.relays) - let subs = pool.sub(relays, filters) + let subs = this.pool.sub(relays, filters) subs.on('event', async event => { let mine = event.pubkey == this.customerPubkey let sender = mine @@ -341,7 +344,7 @@ async function customerStall(path) { ) } - this.messageFilter(plaintext, cb => Promise.resolve(pool.close)) + this.messageFilter(plaintext, cb => subs.unsub()) } catch { console.debug('Unable to decrypt message! Probably not for us!') } @@ -355,9 +358,8 @@ async function customerStall(path) { let json = JSON.parse(text) if (json.id != this.activeOrder) return if (json.payment_options) { - let payment_request = json.payment_options.find( - o => o.type == 'ln' - ).link + let payment_request = json.payment_options.find(o => o.type == 'ln') + .link if (!payment_request) return this.loading = false this.qrCodeDialog.data.payment_request = payment_request @@ -373,7 +375,7 @@ async function customerStall(path) { icon: 'thumb_up' }) this.activeOrder = null - Promise.resolve(cb()) + return cb() } else { return } From 4a69f023b36ab80d4affb6f7ba7967086fe82ebe Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Mon, 20 Mar 2023 10:45:55 +0000 Subject: [PATCH 2/3] feat: show not enough stock message --- .../customer-stall/customer-stall.html | 5 ++- .../customer-stall/customer-stall.js | 39 ++++++------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/static/components/customer-stall/customer-stall.html b/static/components/customer-stall/customer-stall.html index 1575c55..a07aef0 100644 --- a/static/components/customer-stall/customer-stall.html +++ b/static/components/customer-stall/customer-stall.html @@ -215,7 +215,10 @@ >
- +
+

{{ qrCodeDialog.data.message }}

+
+
{ this.qrCodeDialog.dismissMsg() }, 1000) @@ -277,32 +282,7 @@ async function customerStall(path) { let pub = this.pool.publish(Array.from(this.relays), order) pub.on('ok', () => console.debug(`Order event was sent`)) pub.on('failed', error => console.error(error)) - // for (const url of Array.from(this.relays)) { - // try { - // let relay = NostrTools.relayInit(url) - // relay.on('connect', () => { - // console.debug(`connected to ${relay.url}`) - // }) - // relay.on('error', () => { - // console.debug(`failed to connect to ${relay.url}`) - // relay.close() - // return - // }) - // await relay.connect() - // let pub = relay.publish(order) - // pub.on('ok', () => { - // console.debug(`${relay.url} has accepted our event: ${order.id}`) - // relay.close() - // }) - // pub.on('failed', reason => { - // console.debug(`failed to publish to ${relay.url}: ${reason}`) - // relay.close() - // }) - // } catch (err) { - // console.error(`Error: ${err}`) - // } - // } this.loading = false this.resetCart() this.openQrCodeDialog() @@ -311,7 +291,6 @@ async function customerStall(path) { async listenMessages() { this.loading = true try { - // const pool = new NostrTools.SimplePool() const filters = [ { kinds: [4], @@ -357,6 +336,12 @@ async function customerStall(path) { if (!isJson(text)) return let json = JSON.parse(text) if (json.id != this.activeOrder) return + + if (json.payment_options.length == 0 && json.message) { + this.loading = false + this.qrCodeDialog.data.message = json.message + return cb() + } if (json.payment_options) { let payment_request = json.payment_options.find(o => o.type == 'ln') .link From 6ed5309b4fa80db6ce115f3bfe3ee29f34bd3eb6 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Mon, 20 Mar 2023 11:00:15 +0000 Subject: [PATCH 3/3] fix: check for login on checkout --- .../customer-stall/customer-stall.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/static/components/customer-stall/customer-stall.js b/static/components/customer-stall/customer-stall.js index 2f933e0..2f21262 100644 --- a/static/components/customer-stall/customer-stall.js +++ b/static/components/customer-stall/customer-stall.js @@ -182,8 +182,14 @@ async function customerStall(path) { this.checkoutDialog.data.pubkey = this.customerPubkey this.checkoutDialog.data.privkey = this.customerPrivkey }, + checkLogIn() { + this.customerPubkey = this.account?.pubkey + this.customerPrivkey = this.account?.privkey + this.customerUseExtension = this.account?.useExtension + }, openCheckout() { // Check if user is logged in + this.checkLogIn() if (this.customerPubkey) { this.checkoutDialog.data.pubkey = this.customerPubkey if (this.customerPrivkey && !this.useExtension) { @@ -337,12 +343,12 @@ async function customerStall(path) { let json = JSON.parse(text) if (json.id != this.activeOrder) return - if (json.payment_options.length == 0 && json.message) { - this.loading = false - this.qrCodeDialog.data.message = json.message - return cb() - } if (json.payment_options) { + if (json.payment_options.length == 0 && json.message) { + this.loading = false + this.qrCodeDialog.data.message = json.message + return cb() + } let payment_request = json.payment_options.find(o => o.type == 'ln') .link if (!payment_request) return @@ -367,9 +373,7 @@ async function customerStall(path) { } }, created() { - this.customerPubkey = this.account?.pubkey - this.customerPrivkey = this.account?.privkey - this.customerUseExtension = this.account?.useExtension + this.checkLogIn() let storedCart = this.$q.localStorage.getItem( `diagonAlley.carts.${this.stall.id}` )