app

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

enquiry-context.ts (4230B)


      1 import {
      2   enquiryReferences,
      3   type EnquiryReferences
      4 } from '../nostr/enquiry-references.ts';
      5 import {
      6   verifiedEnvelopeSnapshot,
      7   type VerifiedEnvelope
      8 } from '../nostr/verified-envelope.ts';
      9 import { projectFoodParts } from '../contracts/food-availability-v1/read.ts';
     10 import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts';
     11 import { buildUnsignedMessageTemplate } from '../nostr/message-template.ts';
     12 import type { MessageWireParts } from '../contracts/message-v1/index.ts';
     13 declare const enquiryBrand: unique symbol;
     14 export type EnquiryContext = Readonly<{ readonly [enquiryBrand]: true }>;
     15 export type EnquiryContextSnapshot = EnquiryReferences &
     16   Readonly<{
     17     title: string;
     18     advertisedPrice: string;
     19     status: 'active' | 'sold';
     20     claims: 'signed_advertisement_assertions';
     21   }>;
     22 const owners = new WeakMap<EnquiryContext, EnquiryContextSnapshot>();
     23 // This immutable observation is not current listing availability, account,
     24 // room, inbox, encryption or Send authority. Those gates have separate owners.
     25 export function captureEnquiryContext(
     26   proof: VerifiedEnvelope
     27 ): EnquiryContext | undefined {
     28   const event = verifiedEnvelopeSnapshot(proof),
     29     refs = enquiryReferences(proof);
     30   if (!event || !refs) return undefined;
     31   const food = projectFoodParts(event);
     32   if (
     33     food.outcome !== 'focused' ||
     34     food.projection.identifier !== refs.identifier
     35   )
     36     return undefined;
     37   const token = Object.freeze({}) as EnquiryContext;
     38   owners.set(token, {
     39     ...refs,
     40     title: food.projection.title,
     41     advertisedPrice: `${food.projection.price.amount} ${food.projection.price.currency}/${food.projection.price.unit}`,
     42     status: food.projection.status,
     43     claims: 'signed_advertisement_assertions'
     44   });
     45   return token;
     46 }
     47 export function enquiryContextSnapshot(
     48   context: EnquiryContext
     49 ): EnquiryContextSnapshot | undefined {
     50   const owner = owners.get(context);
     51   return owner ? { ...owner } : undefined;
     52 }
     53 export function buildEnquiryMessage(
     54   context: EnquiryContext,
     55   text: unknown
     56 ): MessageWireParts | undefined {
     57   const owner = owners.get(context);
     58   if (
     59     !owner ||
     60     owner.status !== 'active' ||
     61     typeof text !== 'string' ||
     62     !boundedUtf8(text, 4096)
     63   )
     64     return undefined;
     65   const seed = `Enquiry about ${JSON.stringify(owner.title)}\nProduct: nostr:${owner.naddr}\nViewed listing: nostr:${owner.nevent}\nAdvertised price (seller assertion): ${owner.advertisedPrice}\nThis is a message, not a confirmed order.`;
     66   const content = seed + (text.length ? '\n\n' + text : '');
     67   return buildUnsignedMessageTemplate(
     68     JSON.stringify({
     69       recipients: [{ public_key: owner.peer, relay_url: null }],
     70       content,
     71       reply_to: null,
     72       subject: null
     73     })
     74   );
     75 }
     76 export type EnquiryCitationAssessment =
     77   | Readonly<{
     78       outcome: 'unresolved';
     79       reason:
     80         | 'missing_history'
     81         | 'different_publisher'
     82         | 'different_version'
     83         | 'unsupported_listing';
     84     }>
     85   | Readonly<{
     86       outcome: 'matched_signed_advertisement';
     87       peer: string;
     88       eventId: string;
     89     }>;
     90 // The caller supplies independently verified public history. No arbitrary body
     91 // parser, URL fetch, private parent search or business-state mutation occurs.
     92 export function assessEnquiryCitation(
     93   context: EnquiryContext,
     94   proof: VerifiedEnvelope | undefined
     95 ): EnquiryCitationAssessment | undefined {
     96   const owner = owners.get(context);
     97   if (!owner) return undefined;
     98   const event = proof ? verifiedEnvelopeSnapshot(proof) : undefined;
     99   if (!event) return { outcome: 'unresolved', reason: 'missing_history' };
    100   if (event.kind !== 30402)
    101     return { outcome: 'unresolved', reason: 'unsupported_listing' };
    102   if (event.pubkey !== owner.peer)
    103     return { outcome: 'unresolved', reason: 'different_publisher' };
    104   if (event.id !== owner.eventId)
    105     return { outcome: 'unresolved', reason: 'different_version' };
    106   const food = projectFoodParts(event);
    107   if (
    108     food.outcome !== 'focused' ||
    109     food.projection.identifier !== owner.identifier
    110   )
    111     return { outcome: 'unresolved', reason: 'unsupported_listing' };
    112   return {
    113     outcome: 'matched_signed_advertisement',
    114     peer: owner.peer,
    115     eventId: owner.eventId
    116   };
    117 }