Clean up nip 04 channel syncing

This commit is contained in:
Jonathan Staab 2023-10-17 09:37:32 -07:00
parent 8700e2efe1
commit ca3a7b073e
3 changed files with 40 additions and 32 deletions

View File

@ -40,7 +40,7 @@
{/if}
</NavItem>
{#if $canUseGiftWrap}
<NavItem href="/channels">
<NavItem disabled={!$canSign} href="/channels">
<i class="fa fa-envelope mr-2" /> Messages
{#if $hasNewNip24Messages || $hasNewNip04Messages}
<div

View File

@ -32,43 +32,51 @@ projections.addHandler(EventKind.AppData, async e => {
const nip04 = getNip04(session)
if (d === appDataKeys.NIP04_LAST_CHECKED) {
channels.mapStore.updateAsync(async $channels => {
await tryJson(async () => {
const payload = JSON.parse(await nip04.decryptAsUser(e.content, e.pubkey))
const payload = tryJson(async () => JSON.parse(await nip04.decryptAsUser(e.content, e.pubkey)))
for (const [id, ts] of Object.entries(payload) as [string, number][]) {
// Ignore weird old stuff
if (id === "undefined" || id.includes("/")) {
continue
}
if (payload) {
const updates = {}
const channel =
$channels.get(id) ||
({
id,
type: "nip04",
relays: getEventHints(e),
messages: [],
} as Channel)
$channels.set(id, {
...channel,
last_checked: Math.max(ts, channel.last_checked || 0),
})
// No need to lock up the UI decrypting a bunch of stuff
await sleep(16)
for (const [id, ts] of Object.entries(payload) as [string, number][]) {
// Ignore weird old stuff
if (id === "undefined" || id.includes("/")) {
continue
}
})
return $channels
})
const channel =
channels.key(id).get() ||
({
id,
type: "nip04",
relays: getEventHints(e),
messages: [],
} as Channel)
updates[id] = {
...channel,
last_checked: Math.max(ts, channel.last_checked || 0),
}
// No need to lock up the UI decrypting a bunch of stuff
await sleep(16)
}
channels.mapStore.update($channels => {
for (const [k, v] of Object.entries(updates)) {
$channels.set(k, v as Channel)
}
return $channels
})
}
}
if (d === appDataKeys.NIP24_LAST_CHECKED) {
await tryJson(async () => {
const payload = JSON.parse(await nip04.decryptAsUser(e.content, e.pubkey))
const payload = await tryJson(async () =>
JSON.parse(await nip04.decryptAsUser(e.content, e.pubkey))
)
if (payload) {
channels.mapStore.update($channels => {
for (const [id, ts] of Object.entries(payload) as [string, number][]) {
const channel = $channels.get(id)
@ -81,7 +89,7 @@ projections.addHandler(EventKind.AppData, async e => {
return $channels
})
})
}
}
})

View File

@ -22,7 +22,7 @@ export const nip59 = derived(
([$session, $nip44, $signer]) => new Nip59($session, $nip44, $signer)
)
export const canSign = signer.derived($signer => $signer.canSign())
export const canSign = signer.derived($signer => $signer.canSign() && window.crypto.subtle)
export const canUseGiftWrap = session.derived($session => $session?.method === "privkey")