Re-work tryJson

This commit is contained in:
Jonathan Staab 2023-03-07 13:23:48 -06:00
parent 625c1db27c
commit 99c27455e9
6 changed files with 74 additions and 66 deletions

View File

@ -38,30 +38,28 @@ const processProfileEvents = async events => {
...person,
...updates[e.pubkey],
...switcherFn(e.kind, {
0: () => {
return tryJson(() => {
const kind0 = JSON.parse(e.content)
0: () => tryJson(() => {
const kind0 = JSON.parse(e.content)
if (e.created_at > (person.kind0_updated_at || 0)) {
if (kind0.nip05) {
verifyNip05(e.pubkey, kind0.nip05)
}
if (kind0.lud16 || kind0.lud06) {
verifyZapper(e.pubkey, kind0.lud16 || kind0.lud06)
}
return {
kind0: {
...person?.kind0,
...updates[e.pubkey]?.kind0,
...kind0,
},
kind0_updated_at: e.created_at,
}
if (e.created_at > (person.kind0_updated_at || 0)) {
if (kind0.nip05) {
verifyNip05(e.pubkey, kind0.nip05)
}
})
},
if (kind0.lud16 || kind0.lud06) {
verifyZapper(e.pubkey, kind0.lud16 || kind0.lud06)
}
return {
kind0: {
...person?.kind0,
...updates[e.pubkey]?.kind0,
...kind0,
},
kind0_updated_at: e.created_at,
}
}
}),
2: () => {
if (e.created_at > (person.relays_updated_at || 0)) {
const {relays = []} = database.getPersonWithFallback(e.pubkey)
@ -75,7 +73,7 @@ const processProfileEvents = async events => {
3: () => {
const data = {petnames: e.tags}
if (e.created_at > (person.relays_updated_at || 0)) {
if (e.content && e.created_at > (person.relays_updated_at || 0)) {
tryJson(() => {
Object.assign(data, {
relays_updated_at: e.created_at,
@ -225,24 +223,26 @@ const processRoutes = async events => {
)
},
3: () => {
tryJson(() => {
Object.entries(JSON.parse(e.content))
.forEach(([url, conditions]) => {
const {write, read} = conditions as Record<string, boolean|string>
if (e.content) {
tryJson(() => {
Object.entries(JSON.parse(e.content))
.forEach(([url, conditions]) => {
const {write, read} = conditions as Record<string, boolean|string>
if (![false, '!'].includes(write)) {
updates.push(
calculateRoute(e.pubkey, url, 'kind:3', 'write', e.created_at)
)
}
if (![false, '!'].includes(write)) {
updates.push(
calculateRoute(e.pubkey, url, 'kind:3', 'write', e.created_at)
)
}
if (![false, '!'].includes(read)) {
updates.push(
calculateRoute(e.pubkey, url, 'kind:3', 'read', e.created_at)
)
}
})
})
if (![false, '!'].includes(read)) {
updates.push(
calculateRoute(e.pubkey, url, 'kind:3', 'read', e.created_at)
)
}
})
})
}
},
// DEPRECATED
10001: () => {
@ -325,7 +325,7 @@ const verifyZapper = async (pubkey, address) => {
}
const res = await tryFetch(() => fetch(url))
const zapper = await tryJson(() => res.json())
const zapper = await tryJson(() => res?.json())
const lnurl = lnurlEncode('lnurl', url)
if (zapper?.allowsNostr && zapper?.nostrPubkey) {

View File

@ -182,7 +182,7 @@
}
// Remove our zero-length spaces
content = content.replace(/\u200B/g, '')
content = content.replace(/\u200B/g, '').trim()
return {
content,

View File

@ -13,16 +13,18 @@
export let hideFollowing = false
let q
let search
let results = []
const {petnamePubkeys} = user
database.watch('people', table => {
search = fuzzy(
table.all({'kind0.name:!nil': null}),
const search = database.watch('people', t =>
console.log(t.all({'kind0.name:!nil': null}))||
fuzzy(
t.all({'kind0.name:!nil': null}),
{keys: ["kind0.name", "kind0.about", "pubkey"]}
)
})
)
$: results = $search(q).slice(0, 50)
// Prime our database, in case we don't have any people stored yet
network.load({
@ -37,7 +39,7 @@
<Input bind:value={q} placeholder="Search for people">
<i slot="before" class="fa-solid fa-search" />
</Input>
{#each (search ? search(q) : []).slice(0, 50) as person (person.pubkey)}
{#each results as person (person.pubkey)}
{#if person.pubkey !== user.getPubkey() && !(hideFollowing && $petnamePubkeys.includes(person.pubkey))}
<PersonInfo {person} />
{/if}

View File

@ -263,25 +263,28 @@ export const stringToColor = (value, {saturation = 100, lightness = 50, opacity
return `hsl(${(hash % 360)}, ${saturation}%, ${lightness}%, ${opacity})`;
}
export const tryJson = f => {
export const tryFunc = (f, ignore) => {
try {
return f()
const r = f()
if (is(Promise, r)) {
return r.catch(e => {
if (!e.toString().includes(ignore)) {
warn(e)
}
})
} else {
return r
}
} catch (e) {
if (!e.toString().includes('JSON')) {
if (!e.toString().includes(ignore)) {
warn(e)
}
}
}
export const tryFetch = async f => {
try {
return await f()
} catch (e) {
if (!e.toString().includes('fetch')) {
warn(e)
}
}
}
export const tryJson = f => tryFunc(f, 'JSON')
export const tryFetch = f => tryFunc(f, 'fetch')
export const union = (...sets) =>
new Set(sets.flatMap(s => Array.from(s)))

View File

@ -45,6 +45,11 @@
</a>
</li>
{/if}
<li class="cursor-pointer">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/notes/follows">
<i class="fa fa-rss mr-2" /> Feed
</a>
</li>
<li class="cursor-pointer">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/search">
<i class="fa fa-search mr-2" /> Search
@ -55,11 +60,6 @@
<i class="fa fa-qrcode mr-2" /> Scan
</a>
</li>
<li class="cursor-pointer">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/notes/follows">
<i class="fa fa-rss mr-2" /> Feed
</a>
</li>
{#if $profile}
<li class="cursor-pointer relative">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/messages">

View File

@ -344,7 +344,10 @@
<div class="flex items-center justify-between">
<Popover triggerType={isMobile ? 'click' : 'mouseenter'}>
<div slot="trigger">
<Anchor type="unstyled" class="text-lg font-bold flex gap-2 items-center">
<Anchor
type="unstyled"
class="text-lg font-bold flex gap-2 items-center"
href={!isMobile && routes.person($person.pubkey)}>
<span>{displayPerson($person)}</span>
{#if $person.verified_as}
<i class="fa fa-circle-check text-accent text-sm" />