feat: load naddr

This commit is contained in:
Vlad Stan 2023-07-18 13:34:47 +03:00
parent c15086d0cf
commit ca7a973041
3 changed files with 56 additions and 29 deletions

View file

@ -60,10 +60,9 @@
<q-item v-for="relay in relays" :key="relay"> <q-item v-for="relay in relays" :key="relay">
<q-item-section avatar> <q-item-section avatar>
<!-- <q-avatar> <q-avatar>
<img v-if="profile?.picture" :src="profile.picture" /> <q-icon name="router"></q-icon>
<img v-else src="/nostrmarket/static/images/blank-avatar.webp" /> </q-avatar>
</q-avatar> -->
</q-item-section> </q-item-section>
<q-item-section class="q-mt-sm"> <q-item-section class="q-mt-sm">
<q-item-label><strong>{{ relay}}</strong></q-item-label> <q-item-label><strong>{{ relay}}</strong></q-item-label>
@ -117,7 +116,8 @@
<q-separator /> <q-separator />
<q-card-section> <q-card-section>
<div class="float-right"> <div class="float-right">
<q-btn @click="publishNaddr" flat label="Publish Market Profile" icon="share" class="q-ml-lg" color="primary"></q-btn> <q-btn @click="publishNaddr" flat label="Share Market Profile" icon="share" class="q-ml-lg"
color="primary"></q-btn>
</div> </div>
</q-card-section> </q-card-section>
<q-card-section></q-card-section> <q-card-section></q-card-section>

View file

@ -103,7 +103,6 @@ const market = async () => {
}, },
naddr: null, naddr: null,
loading: false,
defaultBanner: '/nostrmarket/static/images/nostr-cover.png', defaultBanner: '/nostrmarket/static/images/nostr-cover.png',
defaultLogo: '/nostrmarket/static/images/nostr-avatar.png' defaultLogo: '/nostrmarket/static/images/nostr-avatar.png'
@ -129,6 +128,22 @@ const market = async () => {
}) })
} }
},
searchText(n, o) {
if (!n) return
if (n.toLowerCase().startsWith('naddr')) {
try {
const { type, data } = NostrTools.nip19.decode(n)
if (type !== 'naddr' || data.kind !== 30019) return
LNbits.utils
.confirmDialog('Do you want to import this market profile?')
.onOk(async () => {
await this.checkMarketplaceNaddr(n)
this.searchText = ''
})
} catch { }
}
} }
}, },
computed: { computed: {
@ -442,6 +457,7 @@ const market = async () => {
try { try {
const { type, data } = NostrTools.nip19.decode(naddr) const { type, data } = NostrTools.nip19.decode(naddr)
console.log('### naddr {type, data}:', type, data)
if (type !== 'naddr' || data.kind !== 30019) return // just double check if (type !== 'naddr' || data.kind !== 30019) return // just double check
this.config = { this.config = {
d: data.identifier, d: data.identifier,
@ -464,16 +480,12 @@ const market = async () => {
authors: [this.config.pubkey], authors: [this.config.pubkey],
'#d': [this.config.d] '#d': [this.config.d]
}) })
console.log('########### naddr event')
if (!event) return if (!event) return
this.config = { ... this.config, opts: JSON.parse(event.content) } this.config = { ... this.config, opts: JSON.parse(event.content) }
// add merchants this.addMerchants(this.config.opts?.merchants)
const merchantsPubkeys = this.merchants.map(m => m.publicKey)
const extraMerchants = (this.config.opts?.merchants || [])
.filter(m => merchantsPubkeys.indexOf(m) === -1)
.map(m => ({ publicKey: m, profile: null }))
this.merchants.push(...extraMerchants)
this.applyUiConfigs(this.config) this.applyUiConfigs(this.config)
} catch (error) { } catch (error) {
@ -619,6 +631,17 @@ const market = async () => {
this.$q.localStorage.set('nostrmarket.merchants', this.merchants) this.$q.localStorage.set('nostrmarket.merchants', this.merchants)
this.initNostr() // todo: improve this.initNostr() // todo: improve
}, },
addMerchants(publicKeys = []) {
console.log('### addMerchats', publicKeys)
const merchantsPubkeys = this.merchants.map(m => m.publicKey)
const newMerchants = publicKeys
.filter(p => merchantsPubkeys.indexOf(p) === -1)
.map(p => ({ publicKey: p, profile: null }))
this.merchants.unshift(...newMerchants)
this.$q.localStorage.set('nostrmarket.merchants', this.merchants)
this.initNostr() // todo: improve
},
removeMerchant(publicKey) { removeMerchant(publicKey) {
console.log('### removeMerchat', publicKey) console.log('### removeMerchat', publicKey)
this.merchants = this.merchants.filter(m => m.publicKey !== publicKey) this.merchants = this.merchants.filter(m => m.publicKey !== publicKey)
@ -927,8 +950,7 @@ const market = async () => {
if (!this.account?.privkey) { if (!this.account?.privkey) {
this.openAccountDialog() this.openAccountDialog()
this.$q.notify({ this.$q.notify({
message: 'Please login before publshing the market profile', message: 'Login Required!',
color: 'warning',
icon: 'warning' icon: 'warning'
}) })
return return
@ -936,31 +958,35 @@ const market = async () => {
const merchants = Array.from(this.merchants.map(m => m.publicKey)) const merchants = Array.from(this.merchants.map(m => m.publicKey))
const { name, about, ui } = this.config?.opts || {}
const content = { merchants, name, about, ui }
const identifier = this.config.identifier ?? crypto.randomUUID() const identifier = this.config.identifier ?? crypto.randomUUID()
const event = { const event = {
...(await NostrTools.getBlankEvent()), ...(await NostrTools.getBlankEvent()),
kind: 30019, kind: 30019,
content: JSON.stringify({ name, about, ui, merchants }), content: JSON.stringify(content),
created_at: Math.floor(Date.now() / 1000), created_at: Math.floor(Date.now() / 1000),
tags: [['d', identifier]], tags: [['d', identifier]],
pubkey: this.account.pubkey pubkey: this.account.pubkey
} }
event.id = NostrTools.getEventHash(event) event.id = NostrTools.getEventHash(event)
console.log('### event.content', event.content)
try { try {
if (this.account.useExtension) { event.sig = await NostrTools.signEvent(event, this.account.privkey)
event = await window.nostr.signEvent(event) console.log('### this.pool', this.pool)
} else if (this.account.privkey) { const pub = this.pool.publish(Array.from(this.relays), event)
event.sig = await NostrTools.signEvent(event, this.account.privkey) pub.on('ok', () => {
} console.debug(`Config event was sent`)
let pub = this.pool.publish(Array.from(this.relays), event) })
pub.on('ok', () => console.debug(`Config event was sent`)) pub.on('failed', error => {
pub.on('failed', error => console.error(error)) console.error(error)
})
} catch (err) { } catch (err) {
console.error(err) console.error(err)
this.$q.notify({ this.$q.notify({
message: `Error signing event.`, message: 'Cannot publish market profile',
color: 'negative', caption: `Error: ${err}`,
icon: 'warning' color: 'negative'
}) })
return return
} }
@ -970,7 +996,8 @@ const market = async () => {
identifier: identifier, identifier: identifier,
relays: Array.from(this.relays) relays: Array.from(this.relays)
}) })
return this.copyText(this.naddr)
console.log('### naddr', this.naddr)
} }
} }

View file

@ -114,8 +114,8 @@
<div class="col-lg-1 col-md-1 col-sm-0 auto-width"> <div class="col-lg-1 col-md-1 col-sm-0 auto-width">
<q-btn v-if="activePage === 'customer-stall'" flat color="grey" icon="content_copy" @click="copyUrl()" <q-btn v-if="activePage === 'customer-stall'" flat color="grey" icon="content_copy" @click="copyUrl()"
class="float-right"></q-btn> class="float-right"></q-btn>
<q-btn v-if="activePage === 'market-config'" flat color="primary" label="Naddr" icon="share" @click="publishNaddr" <!-- <q-btn v-if="activePage === 'market-config'" flat color="primary" label="Share Naddr" icon="share" @click="publishNaddr"
class="float-right"></q-btn> class="float-right"></q-btn> -->
</div> </div>
<div class="col-lg-2 col-md-1 col-sm-0 auto-width"></div> <div class="col-lg-2 col-md-1 col-sm-0 auto-width"></div>
</div> </div>