app

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

conversation-thread.ts (2943B)


      1 import { groupInboxMessages } from './inbox-list.ts';
      2 import type { ConversationData } from './admit-conversation.ts';
      3 import { canonicalPublicKey } from '../contracts/public-key.ts';
      4 import {
      5   decodeProductReference,
      6   type ProductReference
      7 } from '../nostr/references.ts';
      8 import {
      9   privateRecordIdentity,
     10   privateRecordSnapshot,
     11   type PrivateRecordHandle
     12 } from '../persistence/private-records.ts';
     13 import { privateSendStatus } from './send-status.ts';
     14 export type ThreadMessage = Readonly<{
     15   data: ConversationData;
     16   citations: readonly ProductReference[];
     17   parent: 'present' | 'missing' | null;
     18   delivery: readonly string[] | null;
     19 }>;
     20 export type ThreadSnapshot = Readonly<{
     21   status: 'ready' | 'unavailable';
     22   messages: readonly ThreadMessage[];
     23 }>;
     24 export function unavailableThread(): ThreadSnapshot {
     25   return { status: 'unavailable', messages: [] };
     26 }
     27 // Detached presentation only; authentication and display custody stay with the
     28 // current original InboxView room proofs, never these copied data or receipts.
     29 export function projectConversationThread(
     30   owner: unknown,
     31   peer: unknown,
     32   messages: readonly ConversationData[],
     33   records: readonly PrivateRecordHandle[]
     34 ): ThreadSnapshot {
     35   if (
     36     !canonicalPublicKey(peer) ||
     37     peer === owner ||
     38     groupInboxMessages(owner, messages, []).status !== 'ready' ||
     39     messages.some((row) => row.peer !== peer)
     40   )
     41     return unavailableThread();
     42   const rows = new Map<string, ConversationData>();
     43   for (const row of messages) rows.set(row.rumorId, { ...row });
     44   const receipts = new Map<string, readonly string[]>();
     45   for (const token of records) {
     46     const identity = privateRecordIdentity(token),
     47       record = identity && privateRecordSnapshot(token, owner, identity.id);
     48     if (record?.family !== 'private_send_operation' || record.peer !== peer)
     49       continue;
     50     const status = privateSendStatus(token, owner, record.id);
     51     if (status) receipts.set(record.rumorHash, status.labels.slice());
     52   }
     53   return {
     54     status: 'ready',
     55     messages: Array.from(rows.values())
     56       .sort(
     57         (a, b) =>
     58           a.createdAt - b.createdAt || a.rumorId.localeCompare(b.rumorId)
     59       )
     60       .map((data) => {
     61         const citations = new Map<string, ProductReference>();
     62         for (const match of data.content.matchAll(
     63           /\bnostr:(naddr[0-9a-zA-Z]+)/g
     64         )) {
     65           const reference = decodeProductReference(match[1]);
     66           if (reference) citations.set(reference.naddr, reference);
     67         }
     68         return {
     69           data,
     70           citations: Array.from(citations.values()),
     71           parent:
     72             data.replyTo === null
     73               ? null
     74               : rows.has(data.replyTo)
     75                 ? 'present'
     76                 : 'missing',
     77           delivery:
     78             data.role === 'inbound'
     79               ? null
     80               : (receipts.get(data.rumorId) ?? ['Recipient delivery unknown'])
     81         };
     82       })
     83   };
     84 }