conversation-options.ts (7948B)
1 import { mount, unmount, tick } from 'svelte'; 2 import { 3 renderFixture as renderThread, 4 type Scenario 5 } from './conversation-thread.ts'; 6 import Panel from './conversation-options-panel.svelte'; 7 import { 8 captureCitedProduct, 9 citedProductSnapshot, 10 closeCitedProduct, 11 conversationOptionsSnapshot, 12 requestConversationOption, 13 type CitedProduct 14 } from '../../../src/lib/messaging/cited-product.ts'; 15 import { 16 inboxReplyOwnership, 17 inboxMessageSnapshot, 18 subscribeInboxView, 19 closeInboxView 20 } from '../../../src/lib/messaging/inbox-view.ts'; 21 import { inboxSetupViewOwnership } from '../../../src/lib/messaging/inbox-setup-view.ts'; 22 import { 23 createPublicRuntimeContext, 24 mountPublicRuntime, 25 closePublicRuntime 26 } from '../../../src/lib/runtime/public-runtime.ts'; 27 import { finalizeEvent, getPublicKey } from 'applesauce-core/helpers'; 28 import { neventEncode } from 'applesauce-core/helpers/pointers'; 29 import { encodeProductReference } from '../../../src/lib/nostr/references.ts'; 30 export { beforeNavigate, goto } from './conversation-thread.ts'; 31 32 export type OptionsScenario = Scenario | 'exact' | 'stale' | 'parent_reference'; 33 export async function renderFixture(scenario: OptionsScenario = 'contexts') { 34 const publicSecret = new Uint8Array(32).fill(84); 35 const version = finalizeEvent( 36 { 37 kind: 30402, 38 created_at: scenario === 'stale' ? 1700000050 : 1700000060, 39 content: 'Independently signed carrots', 40 tags: [ 41 ['d', 'carrots'], 42 ['title', 'Verified carrots'], 43 ['summary', 'Actual signed listing'], 44 ['published_at', '1700000000'], 45 ['location', 'Victoria'], 46 ['price', '3.5', 'CAD'], 47 ['radroots:price_unit', 'lb'], 48 ['status', 'active'] 49 ] 50 }, 51 publicSecret 52 ); 53 const selectedCitation = encodeProductReference({ 54 kind: 30402, 55 pubkey: getPublicKey(publicSecret), 56 identifier: 'carrots' 57 }); 58 const seeded = ['exact', 'stale', 'parent_reference'].includes(scenario); 59 const included = neventEncode({ 60 id: scenario === 'parent_reference' ? 'f'.repeat(64) : version.id, 61 author: version.pubkey, 62 kind: 30402 63 }); 64 const body = seeded 65 ? 'About carrots\nProduct: nostr:' + 66 selectedCitation + 67 '\nVersion: nostr:' + 68 included + 69 '\nQuoted price: 4 GBP https://untrusted.example.org' 70 : undefined; 71 const kind: Scenario = 72 scenario === 'parent_reference' 73 ? 'hostile' 74 : scenario === 'exact' || scenario === 'stale' 75 ? 'inbound' 76 : scenario; 77 const thread = await renderThread(kind, undefined, false, undefined, body); 78 await thread.checkSetup(); 79 if (!(await thread.unlock())) throw Error('no genuine thread'); 80 const message = 81 thread 82 .thread() 83 .messages.find((row) => 84 row.citations.some((ref) => ref.naddr === thread.carrots) 85 ) ?? thread.thread().messages[0]; 86 if (!message) throw Error('no original message'); 87 const scope = inboxReplyOwnership( 88 thread.controller, 89 thread.conversationId, 90 message.data.rumorId 91 ); 92 const setup = scope && inboxSetupViewOwnership(scope.setup); 93 const context = createPublicRuntimeContext(); 94 let clockOffset = 0; 95 const candidate = 96 setup && 97 mountPublicRuntime(context, setup.policy, { 98 now: () => performance.now() + clockOffset, 99 schedule(callback, delay) { 100 const timer = setTimeout(callback, delay); 101 return () => clearTimeout(timer); 102 } 103 }); 104 if (!candidate) throw Error('no original runtime'); 105 const runtime = candidate; 106 const hooks: { action: string; peer: string }[] = []; 107 const target = document.createElement('div'); 108 document.body.append(target); 109 const mounted = mount(Panel, { 110 target, 111 props: { 112 controller: thread.controller, 113 conversationId: thread.conversationId, 114 runtime, 115 message, 116 hook: (action: string, peer: string) => { 117 hooks.push({ action, peer }); 118 } 119 } 120 }); 121 const tokens: CitedProduct[] = []; 122 const terminal: string[] = []; 123 const terminalOff: (() => void)[] = []; 124 let unmounted = false; 125 function capture( 126 value: unknown = thread.carrots, 127 review: unknown = 'reviewed_product_citation' 128 ) { 129 const token = captureCitedProduct( 130 thread.controller, 131 thread.conversationId, 132 message.data.rumorId, 133 value, 134 runtime, 135 review 136 ); 137 if (token) tokens.push(token); 138 return token; 139 } 140 async function closePanel() { 141 if (unmounted) return; 142 unmounted = true; 143 await unmount(mounted); 144 target.remove(); 145 } 146 return { 147 currentMessage: () => 148 !!inboxMessageSnapshot( 149 thread.controller, 150 thread.conversationId, 151 message.data.rumorId 152 ), 153 terminal: () => terminal.slice(), 154 revokeAndClick: async (action: 'logout' | 'selection' | 'close') => { 155 const button = Array.from( 156 target.querySelectorAll<HTMLButtonElement>('button') 157 ).find((row) => row.textContent === 'Reply to this message'); 158 if (!button) throw Error('no original rendered Reply control'); 159 if (action === 'logout') thread.logout(); 160 else if (action === 'selection') 161 await thread.resolve('dddddddd-dddd-4ddd-8ddd-dddddddddddd'); 162 else { 163 const revoked = () => 164 !inboxMessageSnapshot( 165 thread.controller, 166 thread.conversationId, 167 message.data.rumorId 168 ); 169 terminalOff.push( 170 subscribeInboxView(thread.controller, () => { 171 if (!revoked()) return; 172 terminal.push('reentered'); 173 closeInboxView(thread.controller); 174 }) 175 ); 176 terminalOff.push( 177 subscribeInboxView(thread.controller, () => { 178 if (revoked()) throw Error('controlled terminal observer failure'); 179 }) 180 ); 181 terminalOff.push( 182 subscribeInboxView(thread.controller, () => { 183 if (revoked()) terminal.push('survived'); 184 }) 185 ); 186 closeInboxView(thread.controller); 187 } 188 await tick(); 189 button.click(); 190 }, 191 advance: () => { 192 clockOffset += 15_001; 193 }, 194 owner: thread.owner, 195 peer: thread.peer, 196 parent: message.data.replyTo, 197 rumorId: message.data.rumorId, 198 citation: thread.carrots, 199 citedVersion: version.id, 200 delta: thread.delta, 201 native: thread.native, 202 hooks: () => hooks.slice(), 203 options: () => 204 conversationOptionsSnapshot(thread.controller, thread.conversationId), 205 capture: (value?: unknown, review?: unknown) => !!capture(value, review), 206 snapshot: () => { 207 const token = tokens.at(-1); 208 return token && citedProductSnapshot(token); 209 }, 210 copied() { 211 const token = tokens.at(-1); 212 return { 213 view: !!captureCitedProduct( 214 { ...thread.controller }, 215 thread.conversationId, 216 message.data.rumorId, 217 thread.carrots, 218 runtime, 219 'reviewed_product_citation' 220 ), 221 runtime: !!captureCitedProduct( 222 thread.controller, 223 thread.conversationId, 224 message.data.rumorId, 225 thread.carrots, 226 { ...runtime }, 227 'reviewed_product_citation' 228 ), 229 snapshot: token && citedProductSnapshot({ ...token }), 230 options: conversationOptionsSnapshot( 231 { ...thread.controller }, 232 thread.conversationId 233 ), 234 hook: requestConversationOption( 235 { ...thread.controller }, 236 thread.conversationId, 237 'block', 238 (peer) => hooks.push({ action: 'copied', peer }), 239 'reviewed_conversation_option' 240 ) 241 }; 242 }, 243 logout: thread.logout, 244 closePanel, 245 async close() { 246 for (const off of terminalOff) off(); 247 await closePanel(); 248 for (const token of tokens) closeCitedProduct(token); 249 closePublicRuntime(context); 250 await thread.close(); 251 } 252 }; 253 }