edit-source.ts (7870B)
1 // HC_TEST_ONLY_RELAY/QUALIFICATION_PORT: actual original repositories, signed 2 // events, anonymous SDK resolver and native IndexedDB; no live/operator Q. 3 import { mount, unmount } from 'svelte'; 4 import ConflictNotice from '../../../src/lib/components/ConflictNotice.svelte'; 5 import ListingEditor from '../../../src/lib/components/ListingEditor.svelte'; 6 import { decodeProductReference } from '../../../src/lib/nostr/references.ts'; 7 import { 8 openOwnedEditSource, 9 ownedEditSourceSnapshot, 10 authorizeOwnedEditSource, 11 closeOwnedEditSource, 12 type OwnedEditSource 13 } from '../../../src/lib/publishing/edit-source.ts'; 14 import { createFixture as originalFixture } from './listing-operation-view.ts'; 15 import { makeFixture } from './approved-signing.ts'; 16 import { fixturePolicy } from './inbox-setup.ts'; 17 import { 18 createIdentityViewContext, 19 mountIdentityView, 20 connectIdentityView, 21 closeIdentityView, 22 disconnectIdentityView, 23 IDENTITY_VIEW_CONTEXT 24 } from '../../../src/lib/runtime/view-context.ts'; 25 import { 26 createPublicRuntimeContext, 27 mountPublicRuntime, 28 closePublicRuntime, 29 PUBLIC_RUNTIME_CONTEXT, 30 createPublicView, 31 beginPublicViewRun, 32 subscribePublicView, 33 disposePublicView, 34 type PublicView 35 } from '../../../src/lib/runtime/public-runtime.ts'; 36 import { 37 openBrowserDatabase, 38 closeBrowserDatabase, 39 browserDatabaseTransaction 40 } from '../../../src/lib/persistence/database.ts'; 41 import { createPublicPublicationRepository } from '../../../src/lib/persistence/public-publications.ts'; 42 import { encodeProductReference } from '../../../src/lib/nostr/references.ts'; 43 import { projectFoodParts } from '../../../src/lib/contracts/food-availability-v1/read.ts'; 44 import type { PublicDraftForm } from '../../../src/lib/contracts/local-records.ts'; 45 export async function createFixture( 46 flags = { postingEnabled: true, messagingEnabled: true } 47 ) { 48 const original = await originalFixture(); 49 if ((await original.signed()) !== 'prepared') 50 throw Error('actual original signature missing'); 51 const row = await original.row(); 52 if (!row.artifact) throw Error('actual artifact missing'); 53 const event = JSON.parse(row.artifact.wire) as ReturnType< 54 ReturnType<typeof makeFixture>['sign'] 55 >; 56 const projected = projectFoodParts(event); 57 if (projected.outcome !== 'focused') 58 throw Error('actual supported source missing'); 59 const naddr = encodeProductReference({ 60 kind: 30402, 61 pubkey: row.owner, 62 identifier: projected.projection.identifier 63 }); 64 if (!naddr) throw Error('actual coordinate missing'); 65 const context = createIdentityViewContext(); 66 mountIdentityView(context); 67 await connectIdentityView(context); 68 const opened = await openBrowserDatabase(); 69 if (opened.state !== 'ready') throw Error(opened.reason); 70 const database = opened.owner, 71 repository = createPublicPublicationRepository(database, row.owner); 72 if (!repository) throw Error('actual repository missing'); 73 const publicContext = createPublicRuntimeContext(); 74 const runtime = mountPublicRuntime(publicContext, fixturePolicy(flags)); 75 if (!runtime) throw Error('actual runtime missing'); 76 let source: OwnedEditSource | undefined, 77 panel: ReturnType<typeof mount> | undefined, 78 target: HTMLElement | undefined; 79 const cancellation = new AbortController(); 80 const lookups: PublicView[] = []; 81 return { 82 event, 83 naddr, 84 owner: row.owner, 85 counts: () => original.counts(), 86 async open(network = true, address = naddr) { 87 source = await openOwnedEditSource( 88 repository, 89 context, 90 address, 91 network ? runtime : undefined, 92 cancellation.signal 93 ); 94 return source && ownedEditSourceSnapshot(source); 95 }, 96 snapshot: (copy = false) => 97 source && ownedEditSourceSnapshot(copy ? { ...source } : source), 98 authorized: (copy = false) => 99 !!(source && authorizeOwnedEditSource(copy ? { ...source } : source)), 100 copiedClose() { 101 if (source) closeOwnedEditSource({ ...source }); 102 }, 103 async external() { 104 await original.externalSource(); 105 }, 106 async competing( 107 title = 'Actual current carrots', 108 status: 'active' | 'sold' = 'active' 109 ) { 110 const provider = window.nostr as 111 | { 112 signEvent: ( 113 input: Parameters<ReturnType<typeof makeFixture>['sign']>[0] 114 ) => Promise<ReturnType<ReturnType<typeof makeFixture>['sign']>>; 115 } 116 | undefined; 117 if (!provider) throw Error('original controlled provider missing'); 118 return await provider.signEvent({ 119 kind: event.kind, 120 content: event.content, 121 created_at: event.created_at + 1, 122 tags: event.tags.map((tag) => 123 tag[0] === 'title' 124 ? ['title', title] 125 : tag[0] === 'status' 126 ? ['status', status] 127 : tag 128 ) 129 }); 130 }, 131 foreign() { 132 return encodeProductReference({ 133 kind: 30402, 134 pubkey: makeFixture().owner, 135 identifier: projected.projection.identifier 136 })!; 137 }, 138 async render(proposed: PublicDraftForm) { 139 if (!source) throw Error('actual source missing'); 140 if (panel) await unmount(panel); 141 target?.remove(); 142 target = document.createElement('div'); 143 target.id = 'hcp118-conflict'; 144 document.body.append(target); 145 panel = mount(ConflictNotice, { target, props: { source, proposed } }); 146 }, 147 async renderEditor() { 148 if (panel) await unmount(panel); 149 target?.remove(); 150 target = document.createElement('div'); 151 target.id = 'hcp118-conflict'; 152 document.body.append(target); 153 panel = mount(ListingEditor, { 154 target, 155 props: { route: { mode: 'edit', naddr } }, 156 context: new Map<string, unknown>([ 157 [IDENTITY_VIEW_CONTEXT, context], 158 [PUBLIC_RUNTIME_CONTEXT, publicContext] 159 ]) 160 }); 161 }, 162 lookupAgain() { 163 const reference = decodeProductReference(naddr); 164 if (!reference) throw Error('actual reference missing'); 165 const view = createPublicView(runtime); 166 lookups.push(view); 167 const run = beginPublicViewRun(view); 168 subscribePublicView( 169 view, 170 run, 171 'head', 172 [ 173 { 174 kinds: [30402], 175 authors: [reference.pubkey], 176 '#d': [reference.identifier], 177 limit: 20 178 } 179 ], 180 () => {} 181 ); 182 }, 183 async drafts() { 184 const transaction = browserDatabaseTransaction( 185 database, 186 ['public_drafts'], 187 'readonly' 188 ); 189 const request = transaction.objectStore('public_drafts').getAll(); 190 return await new Promise< 191 { owner: string; id: string; form: PublicDraftForm }[] 192 >((resolve, reject) => { 193 transaction.addEventListener('complete', () => 194 resolve( 195 (request.result as { owner: string; wire: string }[]) 196 .filter((row) => row.owner === original.owner) 197 .map( 198 (row) => 199 JSON.parse(row.wire) as { 200 owner: string; 201 id: string; 202 form: PublicDraftForm; 203 } 204 ) 205 ) 206 ); 207 transaction.addEventListener('abort', () => 208 reject(Error('actual native draft read failed')) 209 ); 210 }); 211 }, 212 cancel() { 213 cancellation.abort(); 214 }, 215 disconnect() { 216 disconnectIdentityView(context); 217 }, 218 async close() { 219 cancellation.abort(); 220 if (source) closeOwnedEditSource(source); 221 if (panel) await unmount(panel); 222 target?.remove(); 223 for (const view of lookups) disposePublicView(view); 224 closePublicRuntime(publicContext); 225 closeIdentityView(context); 226 closeBrowserDatabase(database); 227 original.close(); 228 } 229 }; 230 }