chore: code clean-up

This commit is contained in:
Vlad Stan 2023-07-18 14:43:08 +03:00
parent 4a07f1423a
commit 28683087aa
2 changed files with 24 additions and 94 deletions

View file

@ -8,7 +8,6 @@ async function marketConfig(path) {
data: function () { data: function () {
return { return {
tab: 'merchants', tab: 'merchants',
pubkeys: new Set(),
profiles: new Map(), profiles: new Map(),
merchantPubkey: null, merchantPubkey: null,
relayUrl: null, relayUrl: null,

View file

@ -79,7 +79,6 @@ const market = async () => {
groupByStall: false, groupByStall: false,
drawer: true, drawer: true,
pubkeys: new Set(),
relays: new Set(), relays: new Set(),
events: [], events: [],
stalls: [], stalls: [],
@ -102,7 +101,6 @@ const market = async () => {
opts: null opts: null
}, },
naddr: null,
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'
@ -110,7 +108,6 @@ const market = async () => {
}, },
watch: { watch: {
config(n, o) { config(n, o) {
console.log('#### watch config', n)
if (!n?.opts?.ui?.banner) { if (!n?.opts?.ui?.banner) {
this.bannerImage = this.defaultBanner this.bannerImage = this.defaultBanner
} else if (n?.opts?.ui?.banner !== o?.opts?.ui?.banner) { } else if (n?.opts?.ui?.banner !== o?.opts?.ui?.banner) {
@ -222,7 +219,6 @@ const market = async () => {
selected: this.filterCategories.indexOf(category) !== -1 selected: this.filterCategories.indexOf(category) !== -1
})) }))
.sort((a, b) => b.count - a.count) .sort((a, b) => b.count - a.count)
console.log('### x', x)
return x return x
} }
}, },
@ -279,10 +275,9 @@ const market = async () => {
this.account = this.$q.localStorage.getItem('nostrmarket.account') || null this.account = this.$q.localStorage.getItem('nostrmarket.account') || null
const uiConfig = this.$q.localStorage.getItem('nostrmarket.marketplaceConfig') || { ui: { darkMode: false } } const uiConfig = this.$q.localStorage.getItem('nostrmarket.marketplaceConfig') || { ui: { darkMode: false } }
console.log('### uiConfig storage: ', uiConfig)
// trigger the `watch` logic // trigger the `watch` logic
this.config = { ...this.config, opts: { ...this.config.opts, ...uiConfig } } this.config = { ...this.config, opts: { ...this.config.opts, ...uiConfig } }
console.log('#### restoreFromStorage this.config', this.config)
this.applyUiConfigs(this.config) this.applyUiConfigs(this.config)
@ -295,11 +290,9 @@ const market = async () => {
const relays = this.$q.localStorage.getItem('nostrmarket.relays') const relays = this.$q.localStorage.getItem('nostrmarket.relays')
this.relays = new Set(relays?.length ? relays : defaultRelays) this.relays = new Set(relays?.length ? relays : defaultRelays)
console.log('#### relays', relays, this.relays)
}, },
applyUiConfigs(config = {}) { applyUiConfigs(config = {}) {
const { name, about, ui } = config?.opts || {} const { name, about, ui } = config?.opts || {}
console.log('### applyUiConfigs', name, about, ui)
this.$q.localStorage.set('nostrmarket.marketplaceConfig', { name, about, ui }) this.$q.localStorage.set('nostrmarket.marketplaceConfig', { name, about, ui })
if (config.opts?.ui?.theme) { if (config.opts?.ui?.theme) {
document.body.setAttribute('data-theme', this.config.opts.ui.theme) document.body.setAttribute('data-theme', this.config.opts.ui.theme)
@ -327,7 +320,6 @@ const market = async () => {
}) })
}, },
async createAccount(useExtension = false) { async createAccount(useExtension = false) {
console.log('### createAccount',)
let nip07 let nip07
if (useExtension) { if (useExtension) {
await this.getFromExtension() await this.getFromExtension()
@ -368,14 +360,12 @@ const market = async () => {
async updateUiConfig(updateData) { async updateUiConfig(updateData) {
console.log('### updateUiConfig', updateData)
const { name, about, ui } = updateData const { name, about, ui } = updateData
this.config = { ...this.config, opts: { ...this.config.opts, name, about, ui } } this.config = { ...this.config, opts: { ...this.config.opts, name, about, ui } }
this.applyUiConfigs(this.config) this.applyUiConfigs(this.config)
}, },
async updateData(events) { async updateData(events) {
console.log('### updateData', events)
if (events.length < 1) { if (events.length < 1) {
this.$q.notify({ this.$q.notify({
message: 'No matches were found!' message: 'No matches were found!'
@ -422,19 +412,14 @@ const market = async () => {
return obj return obj
}) })
.filter(f => f) .filter(f => f)
console.log('### products', this.products)
}, },
async initNostr() { async initNostr() {
this.$q.loading.show() this.$q.loading.show()
const pool = new NostrTools.SimplePool() const pool = new NostrTools.SimplePool()
let relays = Array.from(this.relays) let relays = Array.from(this.relays)
const authors = Array.from(this.pubkeys).concat(this.merchants.map(m => m.publicKey)) const authors = this.merchants.map(m => m.publicKey)
console.log('### stupid', authors)
// Get metadata and market data from the pubkeys
await pool await pool
.list(relays, [ .list(relays, [
{ {
@ -443,7 +428,6 @@ const market = async () => {
} }
]) ])
.then(async events => { .then(async events => {
console.log('### stupid response', events)
if (!events || events.length == 0) return if (!events || events.length == 0) return
await this.updateData(events) await this.updateData(events)
}) })
@ -453,13 +437,29 @@ const market = async () => {
this.poolSubscribe() this.poolSubscribe()
return return
}, },
async poolSubscribe() {
const authors = this.merchants.map(m => m.publicKey)
this.poolSub = this.pool.sub(Array.from(this.relays), [
{
kinds: [0, 30017, 30018],
authors,
since: Date.now() / 1000
}
])
this.poolSub.on(
'event',
event => {
this.updateData([event])
},
{ id: 'masterSub' } //pass ID to cancel previous sub
)
},
async checkMarketNaddr(naddr) { async checkMarketNaddr(naddr) {
if (!naddr) return if (!naddr) return
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,
@ -482,7 +482,7 @@ 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) }
@ -495,24 +495,7 @@ const market = async () => {
} }
}, },
async poolSubscribe() {
const authors = Array.from(this.pubkeys).concat(this.merchants.map(m => m.publicKey))
console.log('### poolSubscribe.authors', authors)
this.poolSub = this.pool.sub(Array.from(this.relays), [
{
kinds: [0, 30017, 30018],
authors,
since: Date.now() / 1000
}
])
this.poolSub.on(
'event',
event => {
this.updateData([event])
},
{ id: 'masterSub' } //pass ID to cancel previous sub
)
},
navigateTo(page, opts = { stall: null, product: null, pubkey: null }) { navigateTo(page, opts = { stall: null, product: null, pubkey: null }) {
console.log("### navigateTo", page, opts) console.log("### navigateTo", page, opts)
@ -564,45 +547,6 @@ const market = async () => {
getAmountFormated(amount, unit = 'USD') { getAmountFormated(amount, unit = 'USD') {
return LNbits.utils.formatCurrency(amount, unit) return LNbits.utils.formatCurrency(amount, unit)
}, },
async addPubkey(pubkey) {
if (!pubkey) {
pubkey = String(this.inputPubkey).trim()
}
let regExp = /^#([0-9a-f]{3}){1,2}$/i
if (pubkey.startsWith('n')) {
try {
let { type, data } = NostrTools.nip19.decode(pubkey)
if (type === 'npub') pubkey = data
else if (type === 'nprofile') {
pubkey = data.pubkey
givenRelays = data.relays
}
} catch (err) {
console.error(err)
}
} else if (regExp.test(pubkey)) {
pubkey = pubkey
}
this.pubkeys.add(pubkey)
this.inputPubkey = null
this.$q.localStorage.set(
`diagonAlley.merchants`,
Array.from(this.pubkeys)
)
this.initNostr()
},
removePubkey(pubkey) {
// Needs a hack for Vue reactivity
let pubkeys = this.pubkeys
pubkeys.delete(pubkey)
this.profiles.delete(pubkey)
this.pubkeys = new Set(Array.from(pubkeys))
this.$q.localStorage.set(
`diagonAlley.merchants`,
Array.from(this.pubkeys)
)
this.initNostr()
},
setActivePage(page = 'market') { setActivePage(page = 'market') {
this.activePage = page this.activePage = page
@ -624,8 +568,6 @@ const market = async () => {
addMerchant(publicKey) { addMerchant(publicKey) {
console.log('### addMerchat', publicKey)
this.merchants.unshift({ this.merchants.unshift({
publicKey, publicKey,
profile: null profile: null
@ -634,7 +576,6 @@ const market = async () => {
this.initNostr() // todo: improve this.initNostr() // todo: improve
}, },
addMerchants(publicKeys = []) { addMerchants(publicKeys = []) {
console.log('### addMerchats', publicKeys)
const merchantsPubkeys = this.merchants.map(m => m.publicKey) const merchantsPubkeys = this.merchants.map(m => m.publicKey)
const newMerchants = publicKeys const newMerchants = publicKeys
@ -645,7 +586,6 @@ const market = async () => {
this.initNostr() // todo: improve this.initNostr() // todo: improve
}, },
removeMerchant(publicKey) { removeMerchant(publicKey) {
console.log('### removeMerchat', publicKey)
this.merchants = this.merchants.filter(m => m.publicKey !== publicKey) this.merchants = this.merchants.filter(m => m.publicKey !== publicKey)
this.$q.localStorage.set('nostrmarket.merchants', this.merchants) this.$q.localStorage.set('nostrmarket.merchants', this.merchants)
this.products = this.products.filter(p => p.pubkey !== publicKey) this.products = this.products.filter(p => p.pubkey !== publicKey)
@ -654,7 +594,6 @@ const market = async () => {
}, },
addProductToCart(item) { addProductToCart(item) {
console.log('### addProductToCart', item)
let stallCart = this.shoppingCarts.find(s => s.id === item.stall_id) let stallCart = this.shoppingCarts.find(s => s.id === item.stall_id)
if (!stallCart) { if (!stallCart) {
stallCart = { stallCart = {
@ -755,7 +694,6 @@ const market = async () => {
}, },
async listenForIncommingDms(from) { async listenForIncommingDms(from) {
console.log('### from', from)
if (!this.account?.privkey) { if (!this.account?.privkey) {
return return
} }
@ -769,13 +707,12 @@ const market = async () => {
authors: [this.account.pubkey], authors: [this.account.pubkey],
}] }]
console.log('### filters', filters)
const subs = this.pool.sub(Array.from(this.relays), filters) const subs = this.pool.sub(Array.from(this.relays), filters)
subs.on('event', async event => { subs.on('event', async event => {
const receiverPubkey = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1] const receiverPubkey = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
const isSentByMe = event.pubkey === this.account.pubkey const isSentByMe = event.pubkey === this.account.pubkey
if (receiverPubkey !== this.account.pubkey && !isSentByMe) { if (receiverPubkey !== this.account.pubkey && !isSentByMe) {
console.log('Unexpected DM. Dropped!') console.warn('Unexpected DM. Dropped!')
return return
} }
this.persistDMEvent(event) this.persistDMEvent(event)
@ -799,7 +736,6 @@ const market = async () => {
if (!isJson(plainText)) return if (!isJson(plainText)) return
const jsonData = JSON.parse(plainText) const jsonData = JSON.parse(plainText)
console.log('###### type: ', jsonData.type)
if ([0, 1, 2].indexOf(jsonData.type) !== -1) { if ([0, 1, 2].indexOf(jsonData.type) !== -1) {
this.persistOrderUpdate(peerPubkey, event.created_at, jsonData) this.persistOrderUpdate(peerPubkey, event.created_at, jsonData)
} }
@ -860,7 +796,6 @@ const market = async () => {
dms.events.push(event) dms.events.push(event)
dms.events.sort((a, b) => a - b) dms.events.sort((a, b) => a - b)
dms.lastCreatedAt = dms.events[dms.events.length - 1].created_at dms.lastCreatedAt = dms.events[dms.events.length - 1].created_at
console.log('### dms', dms)
this.$q.localStorage.set(`nostrmarket.dm.${event.pubkey}`, dms) this.$q.localStorage.set(`nostrmarket.dm.${event.pubkey}`, dms)
}, },
@ -871,7 +806,6 @@ const market = async () => {
}, },
persistOrderUpdate(pubkey, eventCreatedAt, orderUpdate) { persistOrderUpdate(pubkey, eventCreatedAt, orderUpdate) {
console.log('### persistOrderUpdate', pubkey, eventCreatedAt, orderUpdate)
let orders = this.$q.localStorage.getItem(`nostrmarket.orders.${pubkey}`) || [] let orders = this.$q.localStorage.getItem(`nostrmarket.orders.${pubkey}`) || []
const orderIndex = orders.findIndex(o => o.id === orderUpdate.id) const orderIndex = orders.findIndex(o => o.id === orderUpdate.id)
@ -972,10 +906,8 @@ const market = async () => {
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 {
event.sig = await NostrTools.signEvent(event, this.account.privkey) 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) const pub = this.pool.publish(Array.from(this.relays), event)
pub.on('ok', () => { pub.on('ok', () => {
console.debug(`Config event was sent`) console.debug(`Config event was sent`)
@ -992,14 +924,13 @@ const market = async () => {
}) })
return return
} }
this.naddr = NostrTools.nip19.naddrEncode({ const naddr = NostrTools.nip19.naddrEncode({
pubkey: event.pubkey, pubkey: event.pubkey,
kind: 30019, kind: 30019,
identifier: identifier, identifier: identifier,
relays: Array.from(this.relays) relays: Array.from(this.relays)
}) })
this.copyText(this.naddr) this.copyText(naddr)
console.log('### naddr', this.naddr)
} }
} }