app

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

send-status.ts (2851B)


      1 import {
      2   privateRecordSnapshot,
      3   type PrivateRecordHandle,
      4   type PrivateTargetReceipt
      5 } from '../persistence/private-records.ts';
      6 // Pure detached status from strict local evidence, never EventStore seen-relay
      7 // metadata, a remote read receipt, effect permission or a person-delivery claim.
      8 export function privateSendStatus(
      9   record: PrivateRecordHandle,
     10   owner: unknown,
     11   id: unknown
     12 ) {
     13   const row = privateRecordSnapshot(record, owner, id);
     14   if (!row || row.family !== 'private_send_operation') return undefined;
     15   const facts = row.receipts ?? [];
     16   function role(role: 'peer' | 'self_archive', targets: readonly string[]) {
     17     const matching = facts.filter((f) => f.role === role);
     18     function origins(status: PrivateTargetReceipt['status']) {
     19       return targets.filter((origin) =>
     20         matching.some((f) => f.origin === origin && f.status === status)
     21       );
     22     }
     23     const acceptedTargets = origins('accepted'),
     24       readbackTargets = origins('readback');
     25     return {
     26       acceptedTargets,
     27       readbackTargets,
     28       pendingTargets: targets.filter(
     29         (origin) => !acceptedTargets.includes(origin)
     30       ),
     31       refusedTargets: origins('refused'),
     32       timeoutTargets: origins('timed_out'),
     33       unknownTargets: origins('unknown'),
     34       stoppedTargets: origins('stopped'),
     35       uncertainTargets: targets.filter(
     36         (origin) =>
     37           !acceptedTargets.includes(origin) &&
     38           matching.some(
     39             (f) =>
     40               f.origin === origin &&
     41               ['unknown', 'timed_out', 'stopped'].includes(f.status)
     42           )
     43       )
     44     };
     45   }
     46   const recipientFacts = role(
     47       'peer',
     48       row.deliveryPlan?.routes.peer.targets ?? []
     49     ),
     50     archiveFacts = role(
     51       'self_archive',
     52       row.deliveryPlan?.routes.archive.targets ?? []
     53     );
     54   const recipient = {
     55       ...recipientFacts,
     56       state: recipientFacts.acceptedTargets.length
     57         ? ('accepted_by_inbox_relay' as const)
     58         : ('unknown' as const)
     59     },
     60     archive = {
     61       ...archiveFacts,
     62       state:
     63         archiveFacts.acceptedTargets.length &&
     64         !archiveFacts.pendingTargets.length
     65           ? ('accepted_by_inbox_relay' as const)
     66           : ('pending' as const)
     67     };
     68   const anyAccepted =
     69       recipient.acceptedTargets.length + archive.acceptedTargets.length > 0,
     70     partial =
     71       anyAccepted &&
     72       (recipient.state === 'unknown' ||
     73         archive.state === 'pending' ||
     74         recipient.pendingTargets.length > 0);
     75   const labels: string[] = [
     76     'Saved encrypted locally',
     77     recipient.state === 'accepted_by_inbox_relay'
     78       ? 'Accepted by recipient inbox relay'
     79       : 'Recipient delivery unknown',
     80     ...(partial ? ['Partial delivery'] : []),
     81     ...(archive.state === 'pending' ? ['Sender archive pending'] : [])
     82   ];
     83   return { recipient, archive, partial, labels };
     84 }