app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

new-listing-readiness.ts (8093B)


      1 import {
      2   identityMessagingOwnership,
      3   type IdentitySession
      4 } from '../runtime/identity-session.ts';
      5 import { privateSessionOwnership } from '../runtime/private-session.ts';
      6 import {
      7   inboxSetupViewOwnership,
      8   inboxSetupViewSnapshot,
      9   type InboxSetupView
     10 } from '../messaging/inbox-setup-view.ts';
     11 import {
     12   inboxResolutionSnapshot,
     13   inboxResolverCurrentAfterSample
     14 } from '../messaging/resolve-inbox.ts';
     15 import { existingInboxSetupSnapshot } from '../messaging/inbox-setup-verification.ts';
     16 import type { InboxAccessObservation } from '../messaging/readiness.ts';
     17 import type { RelayPolicy } from '../config/relays.ts';
     18 import {
     19   authorizeOwnedEditSource,
     20   type OwnedEditSource
     21 } from './edit-source.ts';
     22 
     23 declare const readinessBrand: unique symbol;
     24 export type NewListingReadiness = Readonly<{ [readinessBrand]: true }>;
     25 type Ownership = Readonly<{
     26   owner: string;
     27   identity: IdentitySession;
     28   policy: RelayPolicy;
     29   current(this: void): boolean;
     30   signingCurrent(this: void): boolean;
     31 }>;
     32 const reviews = new WeakMap<
     33   NewListingReadiness,
     34   {
     35     ownership: Ownership;
     36     close(): void;
     37   }
     38 >();
     39 
     40 // Readiness is a prerequisite for a later new-listing command, not permission
     41 // to sign, write a preference, reserve a listing or publish an event. Its owners
     42 // must still validate the exact draft/author/template and final explicit action.
     43 function sample(identity: IdentitySession, setup: InboxSetupView | undefined) {
     44   try {
     45     if (typeof window === 'undefined') return;
     46     const actor = identityMessagingOwnership(identity);
     47     const own = inboxSetupViewOwnership(setup);
     48     const state = inboxSetupViewSnapshot(setup);
     49     if (
     50       !actor?.current() ||
     51       !own?.current() ||
     52       own.identity !== identity ||
     53       !own.session ||
     54       !own.resolver ||
     55       state.busy ||
     56       !state.setupComplete
     57     )
     58       return;
     59     const privateOwner = privateSessionOwnership(own.session);
     60     if (
     61       !privateOwner?.current() ||
     62       privateOwner.owner !== actor.owner ||
     63       privateOwner.session !== actor.session
     64     )
     65       return;
     66     const initial = inboxResolutionSnapshot(own.resolver);
     67     if (!initial.knownBase || initial.author !== actor.owner) return;
     68     let access: InboxAccessObservation | undefined;
     69     const ready = existingInboxSetupSnapshot(
     70       identity,
     71       own.session,
     72       own.policy,
     73       own.resolver,
     74       own.observeAccess
     75         ? (context) => {
     76             access = own.observeAccess!(context);
     77             return access;
     78           }
     79         : undefined
     80     );
     81     const final = inboxResolutionSnapshot(own.resolver);
     82     const accessCurrent = access?.current() ?? false;
     83     const actual = inboxSetupViewOwnership(setup);
     84     const finalState = inboxSetupViewSnapshot(setup);
     85     if (
     86       !ready.setupComplete ||
     87       !ready.newListingReady ||
     88       !accessCurrent ||
     89       final.knownBase?.id !== initial.knownBase.id ||
     90       final.status !== 'ready' ||
     91       !actor.current() ||
     92       !privateOwner.current() ||
     93       !own.current() ||
     94       !actual?.current() ||
     95       actual.identity !== identity ||
     96       actual.policy !== own.policy ||
     97       actual.session !== own.session ||
     98       actual.resolver !== own.resolver ||
     99       finalState.busy ||
    100       !finalState.setupComplete ||
    101       !inboxResolverCurrentAfterSample(own.resolver)
    102     )
    103       return;
    104     return {
    105       actor,
    106       own,
    107       privateOwner,
    108       preferenceId: final.knownBase.id,
    109       accessCurrent: () => access?.current() ?? false
    110     };
    111   } catch {
    112     return;
    113   }
    114 }
    115 
    116 // Detached display information never mints or replaces the original review.
    117 // Draft saving has no dependency on this module and remains available here.
    118 export function newListingReadinessSnapshot(
    119   identity: IdentitySession,
    120   setup: InboxSetupView | undefined
    121 ) {
    122   const ready = !!sample(identity, setup);
    123   return {
    124     draft: true as const,
    125     readyToReview: ready,
    126     preferenceWrite: 'not_requested' as const,
    127     copy: ready
    128       ? 'Messages: ready to receive replies'
    129       : 'Enable messages before publishing so buyers can contact you.'
    130   };
    131 }
    132 export function captureNewListingReadiness(
    133   identity: IdentitySession,
    134   setup: InboxSetupView | undefined,
    135   review: unknown
    136 ): NewListingReadiness | undefined {
    137   if (review !== 'reviewed_new_listing_inbox') return;
    138   const captured = sample(identity, setup);
    139   if (!captured) return;
    140   const capturedResolver = captured.own.resolver;
    141   if (!capturedResolver) return;
    142   let active = true;
    143   // Continuity of an already admitted original signing action. Reacquiring
    144   // messaging capability during that action would reject its own busy prompt.
    145   // This checks retained original generations/access, never admits a new action.
    146   const signingCurrent = () => {
    147     const actual = inboxSetupViewOwnership(setup);
    148     const state = inboxSetupViewSnapshot(setup);
    149     const resolved = inboxResolutionSnapshot(capturedResolver);
    150     return (
    151       active &&
    152       captured.actor.current() &&
    153       captured.own.current() &&
    154       captured.privateOwner.current() &&
    155       captured.accessCurrent() &&
    156       !!actual?.current() &&
    157       actual.identity === captured.own.identity &&
    158       actual.policy === captured.own.policy &&
    159       actual.session === captured.own.session &&
    160       actual.resolver === captured.own.resolver &&
    161       !state.busy &&
    162       state.setupComplete &&
    163       resolved.status === 'ready' &&
    164       resolved.author === captured.actor.owner &&
    165       resolved.knownBase?.id === captured.preferenceId &&
    166       inboxResolverCurrentAfterSample(capturedResolver) &&
    167       active &&
    168       captured.actor.current() &&
    169       captured.privateOwner.current()
    170     );
    171   };
    172   const current = () => {
    173     if (
    174       !active ||
    175       !captured.actor.current() ||
    176       !captured.own.current() ||
    177       !captured.privateOwner.current()
    178     )
    179       return false;
    180     const actual = sample(identity, setup);
    181     return (
    182       !!actual &&
    183       active &&
    184       actual.actor.owner === captured.actor.owner &&
    185       actual.actor.session === captured.actor.session &&
    186       actual.own.identity === captured.own.identity &&
    187       actual.own.policy === captured.own.policy &&
    188       actual.own.session === captured.own.session &&
    189       actual.own.resolver === captured.own.resolver &&
    190       actual.preferenceId === captured.preferenceId &&
    191       captured.actor.current() &&
    192       captured.own.current() &&
    193       captured.privateOwner.current()
    194     );
    195   };
    196   const token = Object.freeze({}) as NewListingReadiness;
    197   reviews.set(token, {
    198     ownership: {
    199       owner: captured.actor.owner,
    200       identity,
    201       policy: captured.own.policy,
    202       current,
    203       signingCurrent
    204     },
    205     close: () => {
    206       active = false;
    207     }
    208   });
    209   return token;
    210 }
    211 export function newListingReadinessOwnership(
    212   token: NewListingReadiness | undefined
    213 ): Ownership | undefined {
    214   const original = token && reviews.get(token);
    215   return original?.ownership.current() ? { ...original.ownership } : undefined;
    216 }
    217 export function closeNewListingReadiness(token: NewListingReadiness): void {
    218   reviews.get(token)?.close();
    219 }
    220 // Existing announcements need original source/current-head custody, rather than
    221 // minting or changing an inbox preference. Final signing still uses its own lease.
    222 export function captureOwnedListingReadiness(
    223   source: OwnedEditSource,
    224   eventId: string,
    225   review: unknown
    226 ): NewListingReadiness | undefined {
    227   if (
    228     review !== 'reviewed_owned_listing' &&
    229     review !== 'reviewed_owned_withdrawal'
    230   )
    231     return;
    232   const original = authorizeOwnedEditSource(source);
    233   if (!original) return;
    234   let active = true;
    235   const current = () =>
    236     active &&
    237     (review === 'reviewed_owned_withdrawal'
    238       ? original.currentWithdrawal(eventId)
    239       : original.currentRevision(eventId));
    240   const token = Object.freeze({}) as NewListingReadiness;
    241   reviews.set(token, {
    242     ownership: {
    243       owner: original.owner,
    244       identity: original.identity,
    245       policy: original.policy,
    246       current,
    247       signingCurrent: current
    248     },
    249     close: () => {
    250       active = false;
    251     }
    252   });
    253   return token;
    254 }