app

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

deletions.ts (4182B)


      1 import {
      2   verifiedEnvelopeSnapshot,
      3   type VerifiedEnvelope
      4 } from '../nostr/verified-envelope.ts';
      5 import {
      6   canonicalDeletionCoordinate,
      7   deletionRequestSnapshot,
      8   type DeletionRequest
      9 } from '../nostr/deletion-adapter.ts';
     10 import { publicHeadEnvelope, type PublicHead } from './heads.ts';
     11 
     12 export type DeletionDecision = Readonly<{
     13   outcome: 'visible' | 'suppressed';
     14   reason:
     15     | 'deletion_request_immune'
     16     | 'deletion_no_authorized_reference'
     17     | 'deletion_request_author_mismatch'
     18     | 'deletion_address_cutoff_precedes_target'
     19     | 'deletion_event_id_reference'
     20     | 'deletion_address_reference'
     21     | 'deletion_event_id_and_address_reference';
     22   eventReference: Readonly<{ requestId: string }> | null;
     23   addressReference: Readonly<{
     24     coordinate: string;
     25     inclusiveCutoff: number;
     26     requestId: string;
     27   }> | null;
     28 }>;
     29 // Pure reduction over caller-retained admitted proofs. Storage admission and
     30 // coherent retention/eviction remain working-set duties; no implicit cache here.
     31 export function evaluateDeletion(
     32   target: VerifiedEnvelope,
     33   requests: readonly DeletionRequest[]
     34 ): DeletionDecision {
     35   const event = verifiedEnvelopeSnapshot(target);
     36   if (!event) throw new Error('deletion_target_proof_invalid');
     37   if (event.kind === 5)
     38     return {
     39       outcome: 'visible',
     40       reason: 'deletion_request_immune',
     41       eventReference: null,
     42       addressReference: null
     43     };
     44   const replaceable =
     45     event.kind === 0 ||
     46     event.kind === 3 ||
     47     (event.kind >= 10000 && event.kind <= 19999);
     48   const identifier = replaceable
     49     ? ''
     50     : event.kind >= 30000 && event.kind <= 39999
     51       ? event.tags.find((t) => t[0] === 'd')?.[1]
     52       : undefined;
     53   // Unlike generic head ordering, missing/malformed FIRST d is no coordinate.
     54   const coordinate =
     55     identifier === undefined
     56       ? undefined
     57       : canonicalDeletionCoordinate(
     58           `${event.kind}:${event.pubkey}:${identifier}`
     59         );
     60   let eventReference: DeletionDecision['eventReference'] = null;
     61   let addressReference: DeletionDecision['addressReference'] = null;
     62   let unauthorized = false;
     63   for (const token of requests) {
     64     const request = deletionRequestSnapshot(token);
     65     if (!request) continue;
     66     const exact = request.eventTargets.some((v) => v.eventId === event.id);
     67     const address =
     68       coordinate !== undefined &&
     69       request.addressTargets.some((v) => v.coordinate === coordinate);
     70     if (!exact && !address) continue;
     71     if (request.pubkey !== event.pubkey) {
     72       unauthorized = true;
     73       continue;
     74     }
     75     if (
     76       exact &&
     77       (eventReference === null || request.id < eventReference.requestId)
     78     )
     79       eventReference = { requestId: request.id };
     80     if (
     81       address &&
     82       (addressReference === null ||
     83         request.created_at > addressReference.inclusiveCutoff ||
     84         (request.created_at === addressReference.inclusiveCutoff &&
     85           request.id < addressReference.requestId))
     86     )
     87       addressReference = {
     88         coordinate,
     89         inclusiveCutoff: request.created_at,
     90         requestId: request.id
     91       };
     92   }
     93   const addressApplies =
     94     addressReference !== null &&
     95     event.created_at <= addressReference.inclusiveCutoff;
     96   const reason: DeletionDecision['reason'] =
     97     eventReference !== null
     98       ? addressApplies
     99         ? 'deletion_event_id_and_address_reference'
    100         : 'deletion_event_id_reference'
    101       : addressApplies
    102         ? 'deletion_address_reference'
    103         : addressReference !== null
    104           ? 'deletion_address_cutoff_precedes_target'
    105           : unauthorized
    106             ? 'deletion_request_author_mismatch'
    107             : 'deletion_no_authorized_reference';
    108   return {
    109     outcome:
    110       eventReference !== null || addressApplies ? 'suppressed' : 'visible',
    111     reason,
    112     eventReference,
    113     addressReference
    114   };
    115 }
    116 // Evaluate the actual selected winner, never an older focused search match.
    117 // Suppression retains both the winner and request evidence; it claims no erase.
    118 export function evaluatePublicHeadDeletion(
    119   head: PublicHead,
    120   requests: readonly DeletionRequest[]
    121 ): DeletionDecision {
    122   return evaluateDeletion(publicHeadEnvelope(head), requests);
    123 }