app

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

enquiry-readiness.ts (3322B)


      1 import {
      2   productViewSnapshot,
      3   productViewListingOwnership,
      4   type ProductView,
      5   type ProductStatusAcknowledgement
      6 } from '../catalog/product-view.ts';
      7 import {
      8   identitySessionSnapshot,
      9   type IdentitySession
     10 } from '../runtime/identity-session.ts';
     11 import {
     12   inboxResolutionSnapshot,
     13   inboxResolverCurrentAfterSample,
     14   type InboxResolver
     15 } from './resolve-inbox.ts';
     16 import { inboxRelayTargets, type RelayPolicy } from '../config/relays.ts';
     17 import { inboxPreferenceSnapshot } from '../nostr/inbox-preferences.ts';
     18 
     19 export type SellerInboxStatus =
     20   'unchecked' | 'compatible' | 'missing' | 'unsupported' | 'inconclusive';
     21 // Informational observations of original owners, never Send or route authority.
     22 export function sellerInboxStatus(
     23   resolver: InboxResolver | undefined,
     24   author: string,
     25   policy?: RelayPolicy
     26 ): SellerInboxStatus {
     27   if (!resolver) return 'unchecked';
     28   try {
     29     const observed = inboxResolutionSnapshot(resolver);
     30     if (
     31       observed.author !== author ||
     32       !inboxResolverCurrentAfterSample(resolver)
     33     )
     34       return 'inconclusive';
     35     if (observed.status === 'ready' && policy) {
     36       const preference =
     37         observed.head && inboxPreferenceSnapshot(observed.head);
     38       if (
     39         !preference ||
     40         inboxRelayTargets(policy, preference.relays, 'write').length === 0
     41       )
     42         return 'unsupported';
     43     }
     44     return observed.status === 'ready' ? 'compatible' : observed.status;
     45   } catch {
     46     return 'inconclusive';
     47   }
     48 }
     49 export function sellerInboxCopy(status: SellerInboxStatus): string {
     50   return status === 'missing'
     51     ? 'In-app messaging is not available for this seller in this prototype.'
     52     : status === 'unsupported'
     53       ? 'The seller inbox configuration is unsupported in this prototype.'
     54       : status === 'inconclusive'
     55         ? 'The seller inbox could not be confirmed from the sources checked.'
     56         : '';
     57 }
     58 export function enquiryReadinessSnapshot(
     59   view: ProductView,
     60   identity?: IdentitySession,
     61   acknowledgement?: ProductStatusAcknowledgement,
     62   resolver?: InboxResolver
     63 ): Readonly<{
     64   action:
     65     | 'unavailable'
     66     | 'sold'
     67     | 'withdrawn'
     68     | 'own'
     69     | 'acknowledgement_required'
     70     | 'available';
     71   naddr?: string;
     72   sellerInbox: SellerInboxStatus;
     73 }> {
     74   const blocked = {
     75     action: 'unavailable' as const,
     76     sellerInbox: 'unchecked' as const
     77   };
     78   try {
     79     const observed = productViewSnapshot(view);
     80     const actor = identity && identitySessionSnapshot(identity);
     81     const inbox = sellerInboxStatus(resolver, observed.reference.pubkey);
     82     const base = { naddr: observed.reference.naddr, sellerInbox: inbox };
     83     if (
     84       observed.eventId &&
     85       actor &&
     86       'publicKey' in actor &&
     87       actor.publicKey === observed.reference.pubkey
     88     )
     89       return { ...base, action: 'own' };
     90     if (observed.outcome === 'sold' || observed.outcome === 'withdrawn')
     91       return { ...base, action: observed.outcome };
     92     if (observed.outcome !== 'active')
     93       return { ...base, action: 'unavailable' };
     94     const listing = productViewListingOwnership(view, acknowledgement);
     95     return {
     96       ...base,
     97       action: listing?.current()
     98         ? 'available'
     99         : observed.lastKnown
    100           ? 'acknowledgement_required'
    101           : 'unavailable'
    102     };
    103   } catch {
    104     return blocked;
    105   }
    106 }