Browse Source

fix(redirect): dont get issue or patch event twice

but instead pass event directly to ensureIssueFull() or
ensureProposalFull()
master
DanConwayDev 2 years ago
parent
commit
42fb8a21a2
No known key found for this signature in database
GPG Key ID: 68E15486D73F75E1
  1. 104
      src/lib/stores/Issue.ts
  2. 113
      src/lib/stores/Proposal.ts
  3. 4
      src/routes/e/[nostr_ref]/+page.svelte

104
src/lib/stores/Issue.ts

@ -1,4 +1,4 @@
import { NDKRelaySet, type NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk' import { NDKRelaySet, NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk'
import { writable, type Writable } from 'svelte/store' import { writable, type Writable } from 'svelte/store'
import { base_relays, ndk } from './ndk' import { base_relays, ndk } from './ndk'
import { type IssueFull, full_defaults } from '$lib/components/issues/type' import { type IssueFull, full_defaults } from '$lib/components/issues/type'
@ -28,7 +28,14 @@ let sub_replies: NDKSubscription
const sub_replies_to_replies: NDKSubscription[] = [] const sub_replies_to_replies: NDKSubscription[] = []
export const ensureIssueFull = (repo_a: string, issue_id: string) => { export const ensureIssueFull = (
repo_a: string,
issue_id_or_event: string | NDKEvent
) => {
const issue_id =
typeof issue_id_or_event === 'string'
? issue_id_or_event
: issue_id_or_event.id
if (selected_issue_id == issue_id) return if (selected_issue_id == issue_id) return
if (issue_id == '') { if (issue_id == '') {
selected_issue_full.set({ ...full_defaults }) selected_issue_full.set({ ...full_defaults })
@ -64,54 +71,59 @@ export const ensureIssueFull = (repo_a: string, issue_id: string) => {
? repo.relays ? repo.relays
: [...base_relays].concat(repo ? repo.relays : []) : [...base_relays].concat(repo ? repo.relays : [])
sub = ndk.subscribe( const setEvent = (event: NDKEvent) => {
{
ids: [issue_id],
limit: 100,
},
{
closeOnEose: false,
},
NDKRelaySet.fromRelayUrls(relays_to_use, ndk)
)
sub.on('event', (event: NDKEvent) => {
try { try {
if (event.id == issue_id) { selected_issue_full.update((full) => {
selected_issue_full.update((full) => { return {
return { ...full,
...full, issue_event: event,
issue_event: event, summary: {
summary: { ...full.summary,
...full.summary, title: extractIssueTitle(event.content),
title: extractIssueTitle(event.content), descritpion: extractIssueDescription(event.content),
descritpion: extractIssueDescription(event.content), created_at: event.created_at,
created_at: event.created_at, comments: 0,
comments: 0, author: event.pubkey,
author: event.pubkey, loading: false,
loading: false, },
}, }
} })
})
}
} catch {} } catch {}
}) }
if (typeof issue_id_or_event !== 'string') {
setEvent(issue_id_or_event)
} else {
sub = ndk.subscribe(
{
ids: [issue_id],
limit: 100,
},
{
closeOnEose: false,
},
NDKRelaySet.fromRelayUrls(relays_to_use, ndk)
)
sub.on('event', (event: NDKEvent) => {
if (event.id == issue_id) setEvent(event)
})
sub.on('eose', () => { sub.on('eose', () => {
selected_issue_full.update((full) => { selected_issue_full.update((full) => {
const updated = { const updated = {
...full, ...full,
summary: { summary: {
...full.summary, ...full.summary,
loading: false, loading: false,
}, },
} }
if (full.loading === false) { if (full.loading === false) {
r({ ...updated }) r({ ...updated })
} }
return updated return updated
})
}) })
}) }
sub_replies = ndk.subscribe( sub_replies = ndk.subscribe(
{ {

113
src/lib/stores/Proposal.ts

@ -28,7 +28,14 @@ let sub_replies: NDKSubscription
const sub_replies_to_replies: NDKSubscription[] = [] const sub_replies_to_replies: NDKSubscription[] = []
export const ensureProposalFull = (repo_a: string, proposal_id: string) => { export const ensureProposalFull = (
repo_a: string,
proposal_id_or_event: string | NDKEvent
) => {
const proposal_id =
typeof proposal_id_or_event === 'string'
? proposal_id_or_event
: proposal_id_or_event.id
if (selected_proposal_id == proposal_id) return if (selected_proposal_id == proposal_id) return
if (proposal_id == '') { if (proposal_id == '') {
selected_proposal_full.set({ ...full_defaults }) selected_proposal_full.set({ ...full_defaults })
@ -64,59 +71,65 @@ export const ensureProposalFull = (repo_a: string, proposal_id: string) => {
? repo.relays ? repo.relays
: [...base_relays].concat(repo ? repo.relays : []) : [...base_relays].concat(repo ? repo.relays : [])
sub = ndk.subscribe( const setEvent = (event: NDKEvent) => {
{
ids: [proposal_id],
limit: 100,
},
{
closeOnEose: false,
},
NDKRelaySet.fromRelayUrls(relays_to_use, ndk)
)
sub.on('event', (event: NDKEvent) => {
try { try {
if (event.id == proposal_id) { selected_proposal_full.update((full) => {
selected_proposal_full.update((full) => { return {
return { ...full,
...full, proposal_event: event,
proposal_event: event, summary: {
summary: { ...full.summary,
...full.summary, title: (
title: ( event.tagValue('name') ||
event.tagValue('name') || event.tagValue('description') ||
event.tagValue('description') || extractPatchMessage(event.content) ||
extractPatchMessage(event.content) || ''
'' ).split('\n')[0],
).split('\n')[0], descritpion: event.tagValue('description') || '',
descritpion: event.tagValue('description') || '', created_at: event.created_at,
created_at: event.created_at, comments: 0,
comments: 0, author: event.pubkey,
author: event.pubkey, loading: false,
loading: false, },
}, }
} })
})
}
} catch {} } catch {}
}) }
sub.on('eose', () => { if (typeof proposal_id_or_event !== 'string') {
selected_proposal_full.update((full) => { setEvent(proposal_id_or_event)
const updated = { } else {
...full, sub = ndk.subscribe(
summary: { {
...full.summary, ids: [proposal_id],
loading: false, limit: 100,
}, },
} {
if (full.loading === false) { closeOnEose: false,
r({ ...updated }) },
} NDKRelaySet.fromRelayUrls(relays_to_use, ndk)
return updated )
sub.on('event', (event: NDKEvent) => {
if (event.id == proposal_id) setEvent(event)
}) })
})
sub.on('eose', () => {
selected_proposal_full.update((full) => {
const updated = {
...full,
summary: {
...full.summary,
loading: false,
},
}
if (full.loading === false) {
r({ ...updated })
}
return updated
})
})
}
sub_replies = ndk.subscribe( sub_replies = ndk.subscribe(
{ {

4
src/routes/e/[nostr_ref]/+page.svelte

@ -45,10 +45,10 @@
) )
} else { } else {
if (event.kind === issue_kind) { if (event.kind === issue_kind) {
ensureIssueFull(a, id) ensureIssueFull(a, event)
goto(`/r/${aToNaddr(a)}/issues/${nip19.noteEncode(id)}`) goto(`/r/${aToNaddr(a)}/issues/${nip19.noteEncode(id)}`)
} else if (event.kind === patch_kind) { } else if (event.kind === patch_kind) {
ensureProposalFull(a, id) ensureProposalFull(a, event)
goto(`/r/${aToNaddr(a)}/proposals/${nip19.noteEncode(id)}`) goto(`/r/${aToNaddr(a)}/proposals/${nip19.noteEncode(id)}`)
} else { } else {
showError() showError()

Loading…
Cancel
Save