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-section avatar>
<!-- <q-avatar>
<img v-if="profile?.picture" :src="profile.picture" />
<img v-else src="/nostrmarket/static/images/blank-avatar.webp" />
</q-avatar> -->
<q-avatar>
<q-icon name="router"></q-icon>
</q-avatar>
</q-item-section>
<q-item-section class="q-mt-sm">
<q-item-label><strong>{{ relay}}</strong></q-item-label>
@ -117,7 +116,8 @@
<q-separator />
<q-card-section>
<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>
</q-card-section>
<q-card-section></q-card-section>

View file

@ -103,7 +103,6 @@ const market = async () => {
},
naddr: null,
loading: false,
defaultBanner: '/nostrmarket/static/images/nostr-cover.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: {
@ -442,6 +457,7 @@ const market = async () => {
try {
const { type, data } = NostrTools.nip19.decode(naddr)
console.log('### naddr {type, data}:', type, data)
if (type !== 'naddr' || data.kind !== 30019) return // just double check
this.config = {
d: data.identifier,
@ -464,16 +480,12 @@ const market = async () => {
authors: [this.config.pubkey],
'#d': [this.config.d]
})
console.log('########### naddr event')
if (!event) return
this.config = { ... this.config, opts: JSON.parse(event.content) }
// add 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.addMerchants(this.config.opts?.merchants)
this.applyUiConfigs(this.config)
} catch (error) {
@ -619,6 +631,17 @@ const market = async () => {
this.$q.localStorage.set('nostrmarket.merchants', this.merchants)
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) {
console.log('### removeMerchat', publicKey)
this.merchants = this.merchants.filter(m => m.publicKey !== publicKey)
@ -927,8 +950,7 @@ const market = async () => {
if (!this.account?.privkey) {
this.openAccountDialog()
this.$q.notify({
message: 'Please login before publshing the market profile',
color: 'warning',
message: 'Login Required!',
icon: 'warning'
})
return
@ -936,31 +958,35 @@ const market = async () => {
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 event = {
...(await NostrTools.getBlankEvent()),
kind: 30019,
content: JSON.stringify({ name, about, ui, merchants }),
content: JSON.stringify(content),
created_at: Math.floor(Date.now() / 1000),
tags: [['d', identifier]],
pubkey: this.account.pubkey
}
event.id = NostrTools.getEventHash(event)
console.log('### event.content', event.content)
try {
if (this.account.useExtension) {
event = await window.nostr.signEvent(event)
} else if (this.account.privkey) {
event.sig = await NostrTools.signEvent(event, this.account.privkey)
}
let pub = this.pool.publish(Array.from(this.relays), event)
pub.on('ok', () => console.debug(`Config event was sent`))
pub.on('failed', error => console.error(error))
event.sig = await NostrTools.signEvent(event, this.account.privkey)
console.log('### this.pool', this.pool)
const pub = this.pool.publish(Array.from(this.relays), event)
pub.on('ok', () => {
console.debug(`Config event was sent`)
})
pub.on('failed', error => {
console.error(error)
})
} catch (err) {
console.error(err)
this.$q.notify({
message: `Error signing event.`,
color: 'negative',
icon: 'warning'
message: 'Cannot publish market profile',
caption: `Error: ${err}`,
color: 'negative'
})
return
}
@ -970,7 +996,8 @@ const market = async () => {
identifier: identifier,
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">
<q-btn v-if="activePage === 'customer-stall'" flat color="grey" icon="content_copy" @click="copyUrl()"
class="float-right"></q-btn>
<q-btn v-if="activePage === 'market-config'" flat color="primary" label="Naddr" icon="share" @click="publishNaddr"
class="float-right"></q-btn>
<!-- <q-btn v-if="activePage === 'market-config'" flat color="primary" label="Share Naddr" icon="share" @click="publishNaddr"
class="float-right"></q-btn> -->
</div>
<div class="col-lg-2 col-md-1 col-sm-0 auto-width"></div>
</div>