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

View File

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

View File

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

View File

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

View File

@ -45,6 +45,11 @@
</a> </a>
</li> </li>
{/if} {/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"> <li class="cursor-pointer">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/search"> <a class="block px-4 py-2 hover:bg-accent transition-all" href="/search">
<i class="fa fa-search mr-2" /> Search <i class="fa fa-search mr-2" /> Search
@ -55,11 +60,6 @@
<i class="fa fa-qrcode mr-2" /> Scan <i class="fa fa-qrcode mr-2" /> Scan
</a> </a>
</li> </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} {#if $profile}
<li class="cursor-pointer relative"> <li class="cursor-pointer relative">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/messages"> <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"> <div class="flex items-center justify-between">
<Popover triggerType={isMobile ? 'click' : 'mouseenter'}> <Popover triggerType={isMobile ? 'click' : 'mouseenter'}>
<div slot="trigger"> <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> <span>{displayPerson($person)}</span>
{#if $person.verified_as} {#if $person.verified_as}
<i class="fa fa-circle-check text-accent text-sm" /> <i class="fa fa-circle-check text-accent text-sm" />