@estiva-app/interop 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,8 @@
15
15
  import { decodeNaddr, decodeNevent, encodeNaddr, encodeNevent, findNaddrs, pointerToAddress, referenceToPointer } from '@estiva-app/protocol';
16
16
  import { imetaOf, parseProfile } from '@estiva-app/protocol';
17
17
  import { MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol';
18
+ import { attachmentsInDocument, BLOCK_DOCUMENT_FORMAT, CONTENT_FORMAT_TAG, imetaTag, parseBlockDocument, } from '@estiva-app/protocol';
19
+ import { planPlaceFile, planUnlistFile } from './folders.js';
18
20
  /** The default: one kind:0 query, straight to the relay. */
19
21
  /**
20
22
  * How long a *name* may be believed.
@@ -596,6 +598,10 @@ export async function commentDecorationsOf(targets, query) {
596
598
  return { byId, reactionTargetsOmitted: ids.length - forReactions.length };
597
599
  }
598
600
  export const ACTION_EFFECTS = ['safe', 'writes', 'destructive'];
601
+ /** The declared format, when it is one this version writes. */
602
+ function proseFormatOf(declared) {
603
+ return declared === BLOCK_DOCUMENT_FORMAT ? BLOCK_DOCUMENT_FORMAT : undefined;
604
+ }
599
605
  /**
600
606
  * The aspects a generic app may declare — RFC 0.5 §10.2's three parts of a
601
607
  * file, minus `properties`, which is what a *type* adds and so what a
@@ -620,7 +626,7 @@ function resolveActions(manifest, kind, folded) {
620
626
  */
621
627
  const isChange = !!action.emits.field;
622
628
  const isComment = action.emits.scope === 'address';
623
- const isCreation = action.input?.type === 'object' && !!action.input.properties;
629
+ const isCreation = isCreationAction(action);
624
630
  // Four, since 0.25.0. Decided by the kind before the other three, so a
625
631
  // manifest that put `scope: 'address'` on its deletion — the target *is*
626
632
  // an address — is not mistaken for a comment.
@@ -634,6 +640,7 @@ function resolveActions(manifest, kind, folded) {
634
640
  ...(action.description ? { description: action.description } : {}),
635
641
  ...(action.effect ? { effect: action.effect } : {}),
636
642
  control: 'confirm',
643
+ ...(action.emits.listed === true ? { listed: true } : {}),
637
644
  });
638
645
  continue;
639
646
  }
@@ -660,6 +667,8 @@ function resolveActions(manifest, kind, folded) {
660
667
  a one-line input and drawing prose.
661
668
  */
662
669
  ...(spec.target ? { target: spec.target } : {}),
670
+ // Prose only where the value is `content` — a tag is a scalar (§7.3).
671
+ ...(spec.target === 'content' && proseFormatOf(spec.format) ? { format: proseFormatOf(spec.format) } : {}),
663
672
  ...(spec.enum && manifest.vocabularies?.[spec.enum]
664
673
  ? {
665
674
  options: manifest.vocabularies[spec.enum].map((v) => ({
@@ -674,16 +683,22 @@ function resolveActions(manifest, kind, folded) {
674
683
  // comes from `emits`, and a form that omitted it would publish an
675
684
  // orphan the owning app cannot show anywhere.
676
685
  ...(action.emits.toAddressOf ? { createsUnder: action.emits.toAddressOf } : {}),
686
+ ...(action.emits.listed === true ? { listed: true } : {}),
677
687
  });
678
688
  continue;
679
689
  }
680
690
  const vocab = action.input?.enum ? manifest.vocabularies?.[action.input.enum] : undefined;
691
+ const control = vocab ? 'select' : action.input?.type === 'pubkey' ? 'pubkey' : 'text';
692
+ // A change only: a comment's body is §13.2 marker text, and a format on
693
+ // one is nothing this version writes (`buildActionEvent` refuses it).
694
+ const format = control === 'text' && isChange ? proseFormatOf(action.input?.format) : undefined;
681
695
  out.push({
682
696
  id: action.id,
683
697
  label: action.label,
684
698
  ...(action.description ? { description: action.description } : {}),
685
699
  ...(action.effect ? { effect: action.effect } : {}),
686
- control: vocab ? 'select' : action.input?.type === 'pubkey' ? 'pubkey' : 'text',
700
+ control,
701
+ ...(format ? { format } : {}),
687
702
  options: vocab?.map((v) => ({ value: v.value, label: v.label, colour: v.colour })),
688
703
  current: action.emits.field ? folded[action.emits.field]?.value : undefined,
689
704
  field: action.emits.field,
@@ -926,6 +941,18 @@ function webTemplate(event, entity) {
926
941
  * served by the plain noun.
927
942
  */
928
943
  export const KIND_BARE_FILE = 30840;
944
+ /**
945
+ * The field that archives a file, whatever its kind — FOL-46.
946
+ *
947
+ * **One concept for every file.** A Ship project, a Ship issue, a Peek topic, a
948
+ * bare file and a Folder are all archived the same way: a change setting
949
+ * `archived` to `'true'` on the file's address, and an empty value to restore
950
+ * it. That is the shape Ship has written for projects since SPEC §6.5, so
951
+ * nothing on the relay needs migrating. It is a change, never a deletion, so it
952
+ * is reversible, attributed, and open to anyone who may write in the Folder —
953
+ * not only the author.
954
+ */
955
+ export const ARCHIVED_FIELD = 'archived';
929
956
  /**
930
957
  * Where a `ResolvedManifest.address` would name the manifest event, this names
931
958
  * the section of the specification the manifest is built from. Not an address,
@@ -933,8 +960,6 @@ export const KIND_BARE_FILE = 30840;
933
960
  * should fail loudly rather than find something.
934
961
  */
935
962
  export const BARE_FILE_MANIFEST_ADDRESS = 'spec:6.7';
936
- /** The field a bare file's `kind:1851` re-parent sets (SPEC §6.7). */
937
- const BARE_FILE_PARENT_FIELD = 'parent';
938
963
  const BARE_FILE_MANIFEST = {
939
964
  name: 'File',
940
965
  about: 'A file no app owns. Its conversation is what every file has; its type adds nothing.',
@@ -957,7 +982,11 @@ const BARE_FILE_MANIFEST = {
957
982
  // of *other* kinds are found the other way round, by their own `a`
958
983
  // (`parentRefOf` below), which is FOL-4's generic direction; this slot
959
984
  // is the one the existing `list` machinery can draw today.
960
- list: { children: { kind: KIND_BARE_FILE, via: 'a', limit: 200 } },
985
+ //
986
+ // `movedBy` is SPEC §6.7's `parent` field, declared rather than
987
+ // hard-coded since MAN-1, so a bare file moves by the rule every other
988
+ // kind does — the move action below sets the same field.
989
+ list: { children: { kind: KIND_BARE_FILE, via: 'a', limit: 200, movedBy: 'parent' } },
961
990
  },
962
991
  },
963
992
  /*
@@ -1028,9 +1057,25 @@ const BARE_FILE_MANIFEST = {
1028
1057
  'the team may; who can read the file does not change.',
1029
1058
  effect: 'writes',
1030
1059
  appliesTo: [String(KIND_BARE_FILE)],
1031
- emits: { kind: 1851, field: BARE_FILE_PARENT_FIELD },
1060
+ emits: { kind: 1851, field: 'parent' },
1032
1061
  input: { type: 'string' },
1033
1062
  },
1063
+ /*
1064
+ `archive` is the one field every file may carry (FOL-46): `true` hides the
1065
+ file and everything under it from every list, an empty value brings it
1066
+ back. A change, so anyone in the team may, and the archiving change's
1067
+ `content` is the resolution summary a person gave (`ActionEventArgs.note`).
1068
+ */
1069
+ {
1070
+ id: 'archive',
1071
+ label: 'Archive',
1072
+ description: 'Take this file and everything under it out of every list once nobody needs to see it, or bring it ' +
1073
+ 'back: `true` archives, an empty value restores. Nothing is deleted, and a link still opens it.',
1074
+ effect: 'writes',
1075
+ appliesTo: [String(KIND_BARE_FILE)],
1076
+ emits: { kind: 1851, field: ARCHIVED_FIELD },
1077
+ input: { type: 'string', enum: ARCHIVED_FIELD },
1078
+ },
1034
1079
  {
1035
1080
  id: 'delete',
1036
1081
  label: 'Delete',
@@ -1041,6 +1086,13 @@ const BARE_FILE_MANIFEST = {
1041
1086
  emits: { kind: KIND_DELETION },
1042
1087
  },
1043
1088
  ],
1089
+ vocabularies: {
1090
+ // Ship's `ARCHIVED_STATES`, value for value: one field, one vocabulary.
1091
+ [ARCHIVED_FIELD]: [
1092
+ { value: '', label: 'Active', colour: 'neutral' },
1093
+ { value: 'true', label: 'Archived', colour: 'muted' },
1094
+ ],
1095
+ },
1044
1096
  };
1045
1097
  function bareFileManifest() {
1046
1098
  return { manifest: BARE_FILE_MANIFEST, address: BARE_FILE_MANIFEST_ADDRESS, viaRecommendation: false };
@@ -1397,6 +1449,83 @@ function foldChanges(changes, rule) {
1397
1449
  }
1398
1450
  return fields;
1399
1451
  }
1452
+ /**
1453
+ * How an `archived` change is read when the file's kind declares no rule of its
1454
+ * own — a Folder, whose `kind:39000` no app owns, and a bare file. SPEC §7.1's
1455
+ * `kind:1851` shape, the one every Estiva app writes.
1456
+ */
1457
+ const ARCHIVE_RULE = {
1458
+ changeKind: 1851,
1459
+ targetTag: 'a',
1460
+ fieldTag: 'field',
1461
+ valueTag: 'value',
1462
+ order: ['ts', 'created_at', 'id'],
1463
+ rule: 'last-write-wins-per-field',
1464
+ };
1465
+ /** The rule an `archived` change on this file is folded by: its app's, or SPEC's. */
1466
+ function archiveRuleOf(manifest) {
1467
+ return manifest?.records ?? ARCHIVE_RULE;
1468
+ }
1469
+ /**
1470
+ * The note Ship wrote on every archive before FOL-46, when the note was the
1471
+ * activity feed's wording rather than anything a person typed. Twelve are on
1472
+ * production (measured 2026-09-29), and none of them is a resolution.
1473
+ */
1474
+ const PLACEHOLDER_ARCHIVE_NOTE = 'archived';
1475
+ /**
1476
+ * Whether a file — or a Folder — is archived, itself or with an ancestor.
1477
+ *
1478
+ * Reads the {@link Archive} a resolver put on it. A resolver leaves archived
1479
+ * files out of a Folder's contents, so this matters where a file is reached
1480
+ * directly: a link, a mention, a page opened by address.
1481
+ */
1482
+ export function isArchived(file) {
1483
+ return !!file?.archived;
1484
+ }
1485
+ /**
1486
+ * The file's archive, if the last `archived` change archived it.
1487
+ *
1488
+ * `changes` may hold any events; only this rule's change kind, naming this
1489
+ * address, setting this field, is read. Last write wins, by the same order as
1490
+ * every other field.
1491
+ */
1492
+ function archiveOf(changes, address, rule) {
1493
+ let last;
1494
+ for (const change of changes) {
1495
+ // The target is the first target tag, as every other fold reads it: a
1496
+ // change archiving one file must not archive another it also mentions.
1497
+ if (change.kind !== rule.changeKind || tagValue(change, rule.targetTag) !== address)
1498
+ continue;
1499
+ if (tagValue(change, rule.fieldTag) !== ARCHIVED_FIELD || tagValue(change, rule.valueTag) === undefined)
1500
+ continue;
1501
+ if (!last || byOrder(last, change) < 0)
1502
+ last = change;
1503
+ }
1504
+ if (!last || tagValue(last, rule.valueTag) !== 'true')
1505
+ return undefined;
1506
+ const note = last.content.trim();
1507
+ return {
1508
+ by: last.pubkey,
1509
+ at: last.created_at,
1510
+ ...(note && note !== PLACEHOLDER_ARCHIVE_NOTE ? { resolution: last.content } : {}),
1511
+ };
1512
+ }
1513
+ /**
1514
+ * The first archived ancestor of `address`, walking `parentOf`, with its archive.
1515
+ *
1516
+ * Stops at a cycle rather than looping: `parentRef` is written by people, and
1517
+ * two files each moved under the other is a state the relay accepts.
1518
+ */
1519
+ function archivedAncestorOf(address, parentOf, archiveAt) {
1520
+ const seen = new Set([address]);
1521
+ for (let parent = parentOf(address); parent && !seen.has(parent); parent = parentOf(parent)) {
1522
+ seen.add(parent);
1523
+ const archive = archiveAt(parent);
1524
+ if (archive)
1525
+ return { ...archive, with: archive.with ?? parent };
1526
+ }
1527
+ return undefined;
1528
+ }
1400
1529
  function truncate(text, limit) {
1401
1530
  if (!limit || text.length <= limit)
1402
1531
  return text;
@@ -1558,6 +1687,47 @@ export function actionProblems(declared) {
1558
1687
  problems.push(`${name} writes ${targeted.length} properties to content (${targeted.join(', ')}), and an event has one. A consumer refuses the whole action rather than choosing between them.`);
1559
1688
  }
1560
1689
  }
1690
+ /*
1691
+ MAN-1's three declarations, each of which fails quietly on the consumer's
1692
+ side: an unknown placement or format is refused or ignored at the button,
1693
+ and a `listed` on a change is simply never acted on.
1694
+ */
1695
+ const emits = action.emits;
1696
+ const input = action.input;
1697
+ const creates = input?.type === 'object' && !!properties;
1698
+ if (emits?.placement !== undefined && !(creates && PLACEMENTS.includes(emits.placement))) {
1699
+ problems.push(creates
1700
+ ? `${name} places its object with ${JSON.stringify(emits.placement)}, and a Folder is named by ${PLACEMENTS.join(' or ')} — a consumer refuses the action.`
1701
+ : `${name} declares a placement and creates nothing; only a new root names its Folder, so it is ignored.`);
1702
+ }
1703
+ if (emits?.listed !== undefined && emits.listed !== true) {
1704
+ problems.push(`${name} declares listed ${JSON.stringify(emits.listed)}; the only value is true.`);
1705
+ }
1706
+ else if (emits?.listed === true && !creates && emits.kind !== KIND_DELETION) {
1707
+ problems.push(`${name} declares listed on an action that neither creates nor deletes, so no Folder command follows it.`);
1708
+ }
1709
+ const formats = [
1710
+ // A scalar format means something on a change only: a comment's body is
1711
+ // marker text, and a deletion takes no value.
1712
+ ...(input?.format !== undefined
1713
+ ? [['its input', input.format, !creates && !!emits?.field && emits.kind !== KIND_DELETION]]
1714
+ : []),
1715
+ ...Object.entries(properties && typeof properties === 'object' ? properties : {})
1716
+ .filter(([, spec]) => spec?.format !== undefined)
1717
+ .map(([property, spec]) => [
1718
+ `"${property}"`,
1719
+ spec.format,
1720
+ spec.target === 'content',
1721
+ ]),
1722
+ ];
1723
+ for (const [where, format, applies] of formats) {
1724
+ if (!applies) {
1725
+ problems.push(`${name} declares a format on ${where}, which is neither a creation's content nor a change's value, so it is ignored.`);
1726
+ }
1727
+ else if (!proseFormatOf(format)) {
1728
+ problems.push(`${name} declares format ${JSON.stringify(format)} on ${where}, and the only format is ${BLOCK_DOCUMENT_FORMAT}. A consumer draws it as plain text.`);
1729
+ }
1730
+ }
1561
1731
  return problems;
1562
1732
  }
1563
1733
  export function pickWidget(declared, implemented, fallback) {
@@ -1772,7 +1942,7 @@ function buildObject(args) {
1772
1942
  comments: args.comments ?? [],
1773
1943
  listsChildren: listsChildrenOf(projection, manifest),
1774
1944
  parentRef: parentRefOf(manifest, pointer.kind, root, folded),
1775
- ...(pointer.kind === KIND_BARE_FILE ? { parentField: BARE_FILE_PARENT_FIELD } : {}),
1945
+ ...parentFieldsOf(manifest, pointer.kind),
1776
1946
  folder: tagValue(root, 'h'),
1777
1947
  // Substituted here rather than in the component: `<bech32>` is a NIP-89
1778
1948
  // detail, and the widget's job is to draw a link, not to know the spec.
@@ -1781,6 +1951,7 @@ function buildObject(args) {
1781
1951
  ? { ...action, current: held[action.field] }
1782
1952
  : action),
1783
1953
  viaRecommendation: args.viaRecommendation,
1954
+ ...(args.archived ? { archived: args.archived } : {}),
1784
1955
  };
1785
1956
  }
1786
1957
  /**
@@ -1874,7 +2045,7 @@ depth = 0,
1874
2045
  * Optional memo for the NIP-89 discovery half. See {@link ProjectionCache} —
1875
2046
  * omitting it is exactly the old behaviour.
1876
2047
  */
1877
- cache) {
2048
+ cache, options = {}) {
1878
2049
  const pointer = addressPointerOf(naddr);
1879
2050
  if (!pointer)
1880
2051
  return null;
@@ -1887,6 +2058,8 @@ cache) {
1887
2058
  const assembled = assembleObject(plan, await query(plan.filters));
1888
2059
  if (assembled.object.unreachable)
1889
2060
  return assembled.object;
2061
+ if (options.archivedWith)
2062
+ await markArchivedParents([assembled.object], query);
1890
2063
  return withPeople(assembled, await (lookupPeople ?? peopleViaRelay(query))(peopleOf(assembled)));
1891
2064
  }
1892
2065
  /**
@@ -1929,7 +2102,7 @@ export async function resolveForeignObjects(references, query,
1929
2102
  /** Defaults to asking the relay. The browser passes a cached lookup. */
1930
2103
  lookupPeople,
1931
2104
  /** See {@link ProjectionCache}. Omitting it still reads each app's manifest once per call. */
1932
- cache) {
2105
+ cache, options = {}) {
1933
2106
  const results = {};
1934
2107
  const asked = [];
1935
2108
  for (const reference of new Set(references)) {
@@ -1975,6 +2148,8 @@ cache) {
1975
2148
  }
1976
2149
  const events = (await Promise.all(chunks.map((chunk) => query(chunk)))).flat();
1977
2150
  const assembled = plans.map(({ reference, plan }) => ({ reference, built: assembleObject(plan, events) }));
2151
+ if (options.archivedWith)
2152
+ await markArchivedParents(assembled.map(({ built }) => built.object), query);
1978
2153
  const wanted = [...new Set(assembled.flatMap(({ built }) => peopleOf(built)))];
1979
2154
  const reachable = assembled.some(({ built }) => !built.object.unreachable);
1980
2155
  const people = reachable ? await (lookupPeople ?? peopleViaRelay(query))(wanted) : {};
@@ -1994,6 +2169,59 @@ cache) {
1994
2169
  }
1995
2170
  return results;
1996
2171
  }
2172
+ /**
2173
+ * Mark each object that is hidden with an archived parent — FOL-46.
2174
+ *
2175
+ * An issue whose project is archived is archived with it, and its page says so
2176
+ * rather than drawing live work. The cascade is computed here and never written
2177
+ * to the issue, so unarchiving the project restores it exactly.
2178
+ *
2179
+ * **One read for the whole set, and only when some object has a parent**, so a
2180
+ * topic page or a project page pays nothing. One level: the parent's own
2181
+ * parent is not followed, because the page is about this file and its
2182
+ * container, and a deeper walk is a round trip per level. A Folder listing
2183
+ * walks the whole chain it holds ({@link resolveFolderContents}).
2184
+ *
2185
+ * The parent is folded by SPEC's rule, which every Estiva app writes: the
2186
+ * parent may belong to another app, whose manifest is not in hand.
2187
+ */
2188
+ async function markArchivedParents(objects, query) {
2189
+ const waiting = objects.filter((o) => !o.unreachable && !o.archived && o.parentRef && addressPointerOf(o.parentRef));
2190
+ if (waiting.length === 0)
2191
+ return;
2192
+ const parents = [...new Set(waiting.map((o) => o.parentRef))];
2193
+ const changes = await readArchiveChanges(parents, query);
2194
+ for (const object of waiting) {
2195
+ const archive = archiveOf(changes, object.parentRef, ARCHIVE_RULE);
2196
+ if (archive)
2197
+ object.archived = { ...archive, with: object.parentRef };
2198
+ }
2199
+ }
2200
+ /**
2201
+ * Addresses per filter when reading archive changes. A filter's `limit` caps
2202
+ * every change naming its addresses — titles, statuses, moves — not only the
2203
+ * archives, and a relay answers newest first, so a small group keeps the
2204
+ * archive change inside the page.
2205
+ */
2206
+ const ARCHIVE_READ_GROUP = 25;
2207
+ /** The filters asking for every SPEC-shaped change naming these addresses. */
2208
+ function archiveFilters(addresses) {
2209
+ const filters = [];
2210
+ for (let start = 0; start < addresses.length; start += ARCHIVE_READ_GROUP) {
2211
+ const group = addresses.slice(start, start + ARCHIVE_READ_GROUP);
2212
+ filters.push({ kinds: [ARCHIVE_RULE.changeKind], '#a': group, limit: RELAY_PAGE_CEILING });
2213
+ }
2214
+ return filters;
2215
+ }
2216
+ /** Every SPEC-shaped change naming these addresses, in as few requests as the relay allows. */
2217
+ async function readArchiveChanges(addresses, query) {
2218
+ const filters = archiveFilters(addresses);
2219
+ const events = [];
2220
+ for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
2221
+ events.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))));
2222
+ }
2223
+ return events;
2224
+ }
1997
2225
  /** An `naddr` or a `<kind>:<pubkey>:<d>`, or null for anything else. */
1998
2226
  function addressPointerOf(reference) {
1999
2227
  try {
@@ -2038,6 +2266,9 @@ function planObject(naddr, pointer, resolved, depth) {
2038
2266
  { kinds: [pointer.kind], authors: [pointer.pubkey], '#d': [pointer.identifier], limit: 1 },
2039
2267
  // Only when the app actually declares a change kind — see `foldRuleOf`.
2040
2268
  ...(manifest.records ? [{ kinds: [manifest.records.changeKind], '#a': [address], limit: 500 }] : []),
2269
+ // Every file may be archived, including one whose app folds nothing
2270
+ // (FOL-46). SPEC's change kind, when the app has not named its own.
2271
+ ...(manifest.records ? [] : [{ kinds: [ARCHIVE_RULE.changeKind], '#a': [address], limit: 500 }]),
2041
2272
  { kinds: commentKinds, '#a': [address], limit: CONVERSATION_LIMIT },
2042
2273
  ...(childFilter ? [childFilter.filter] : []),
2043
2274
  ];
@@ -2123,6 +2354,7 @@ function assembleObject(plan, answered) {
2123
2354
  viaRecommendation,
2124
2355
  webTemplate: resolved.webTemplate,
2125
2356
  comments,
2357
+ archived: archiveOf(events, address, archiveRuleOf(manifest)),
2126
2358
  });
2127
2359
  /*
2128
2360
  The `list` slot — PRO-7.
@@ -2216,16 +2448,24 @@ function parentRefOf(manifest, kind, root, folded = {}) {
2216
2448
  direction lets Ship's project list its issues without an issue knowing what
2217
2449
  a project is; this one lets a topic sit under a project, an issue, or a kind
2218
2450
  nobody has met, without that kind's owner declaring bare files as children.
2219
- Re-parenting is a `parent` change event, seeded by the root tag, the way an
2220
- issue's `project` field works (SPEC §6.7).
2451
+ Re-parenting is a change to the declared `movedBy` field, seeded by the
2452
+ root tag — `parent` on a bare file, `project` on a Ship issue (SPEC §7.2).
2221
2453
  */
2222
- if (kind === KIND_BARE_FILE) {
2454
+ const movedBy = movedByOf(manifest, kind);
2455
+ if (movedBy) {
2223
2456
  // A folded empty value is a move to the top, not an absence: falling back
2224
2457
  // to the root tag would put the file straight back under the parent it
2225
2458
  // was moved out of (FOL-4).
2226
- const moved = folded[BARE_FILE_PARENT_FIELD];
2227
- if (moved)
2228
- return moved.value || undefined;
2459
+ // A value that is not an address of the declared parent kind names nothing
2460
+ // this object can sit under, so it is a move to nowhere rather than a ref a
2461
+ // consumer might try to fetch.
2462
+ const moved = folded[movedBy.field];
2463
+ if (moved) {
2464
+ const prefix = movedBy.parentKind === undefined ? /^\d+:[0-9a-f]{64}:/ : new RegExp(`^${movedBy.parentKind}:[0-9a-f]{64}:`);
2465
+ return prefix.test(moved.value) ? moved.value : undefined;
2466
+ }
2467
+ }
2468
+ if (kind === KIND_BARE_FILE) {
2229
2469
  return root.tags.find((t) => t[0] === 'a' && t[1] && /^\d+:[0-9a-f]{64}:/.test(t[1]))?.[1];
2230
2470
  }
2231
2471
  for (const [parentKind, projection] of Object.entries(manifest.projections ?? {})) {
@@ -2248,6 +2488,34 @@ function parentRefOf(manifest, kind, root, folded = {}) {
2248
2488
  }
2249
2489
  return undefined;
2250
2490
  }
2491
+ /**
2492
+ * The field that moves a `kind` object to another parent, and the kind of
2493
+ * parent it names — SPEC §7.2's `movedBy`, read off whichever projection lists
2494
+ * `kind` as its children (MAN-1).
2495
+ *
2496
+ * `parentKind` is absent for a bare file: §6.7 lets one sit under a file of any
2497
+ * kind, so its own list naming bare files is not a limit on where it may go.
2498
+ * Absent too for `match: 'identifier'`, where `movedBy` is ignored because a
2499
+ * move's value is an address and that list compares identifiers.
2500
+ */
2501
+ function movedByOf(manifest, kind) {
2502
+ for (const [parentKind, projection] of Object.entries(manifest.projections ?? {})) {
2503
+ const children = declaredChildSpec(projection);
2504
+ if (!children || children.kind !== kind || !children.movedBy || children.match === 'identifier')
2505
+ continue;
2506
+ return kind === KIND_BARE_FILE ? { field: children.movedBy } : { field: children.movedBy, parentKind: Number(parentKind) };
2507
+ }
2508
+ return undefined;
2509
+ }
2510
+ /** {@link movedByOf} as the two `ForeignObject` fields, both builders' shape. */
2511
+ function parentFieldsOf(manifest, kind) {
2512
+ const moved = movedByOf(manifest, kind);
2513
+ if (!moved)
2514
+ return {};
2515
+ return moved.parentKind === undefined
2516
+ ? { parentField: moved.field }
2517
+ : { parentField: moved.field, parentKind: moved.parentKind };
2518
+ }
2251
2519
  /** Both halves of the question a disclosure control asks. */
2252
2520
  function listsChildrenOf(projection, manifest) {
2253
2521
  const children = declaredChildSpec(projection);
@@ -2346,7 +2614,7 @@ function buildChildObject(args) {
2346
2614
  // an inconsistent shape is what makes a consumer defensive about a value it
2347
2615
  // should be able to trust.
2348
2616
  parentRef: parentRefOf(manifest, root.kind, root),
2349
- ...(root.kind === KIND_BARE_FILE ? { parentField: BARE_FILE_PARENT_FIELD } : {}),
2617
+ ...parentFieldsOf(manifest, root.kind),
2350
2618
  folder: tagValue(root, 'h'),
2351
2619
  /*
2352
2620
  `<bech32>` is whichever form this object actually has.
@@ -2914,6 +3182,47 @@ cache) {
2914
3182
  * it where it looks.
2915
3183
  */
2916
3184
  const isAddressableKind = (kind) => kind >= 30000 && kind < 40000;
3185
+ /**
3186
+ * The tags a new root may name its Folder with — SPEC §7.3's `placement`.
3187
+ *
3188
+ * The two a relay indexes for a Folder's containment read and {@link folderOf}
3189
+ * reads. Closed, because a root placed in any other tag belongs to no Folder
3190
+ * and nothing would say so.
3191
+ */
3192
+ const PLACEMENTS = ['h', 'buzz-channel'];
3193
+ /**
3194
+ * The tags a prose value carries — SPEC §7.3's `format`, MAN-1.
3195
+ *
3196
+ * **Tagged only when the caller says the value is in the declared format**, and
3197
+ * then checked, because a reader decides the model by the tag alone (§13.4): a
3198
+ * `content-format` on marker text renders nothing, and one on a document the
3199
+ * relay cannot parse renders JSON at a person. Without `contentFormat` the value
3200
+ * is marker text and carries nothing, which every reader must accept — so a
3201
+ * consumer that draws a text box for a prose field still publishes correctly.
3202
+ *
3203
+ * A document's attachments are named again as `imeta`, which §13.3 requires of
3204
+ * every event holding one and which ingest verifies against stored blobs.
3205
+ */
3206
+ function proseTags(args) {
3207
+ const { label, declared, contentFormat, value } = args;
3208
+ if (contentFormat === undefined)
3209
+ return [];
3210
+ if (proseFormatOf(declared) !== contentFormat) {
3211
+ return proseFormatOf(declared)
3212
+ ? `"${label}" takes ${proseFormatOf(declared)}, not ${contentFormat}.`
3213
+ : `"${label}" declares no ${contentFormat} value, so it is written as plain text.`;
3214
+ }
3215
+ // Clearing a field is not a document; an empty value says nothing to tag.
3216
+ if (value === '')
3217
+ return [];
3218
+ try {
3219
+ parseBlockDocument(value);
3220
+ }
3221
+ catch (e) {
3222
+ return `"${label}" was handed ${contentFormat} that is ${e.message}.`;
3223
+ }
3224
+ return [[CONTENT_FORMAT_TAG, contentFormat], ...attachmentsInDocument(value, contentFormat).map(imetaTag)];
3225
+ }
2917
3226
  /**
2918
3227
  * The event an object-creating action publishes.
2919
3228
  *
@@ -2974,6 +3283,19 @@ function buildCreationEvent(args) {
2974
3283
  return `"${declared.label}" declares ${bodyFields.length} fields writing to content (${bodyFields.join(', ')}), and an event has one.`;
2975
3284
  }
2976
3285
  const bodyField = bodyFields[0];
3286
+ const placement = declared.emits.placement ?? 'h';
3287
+ if (!PLACEMENTS.includes(placement)) {
3288
+ return `"${declared.label}" places its object with "${placement}", and a Folder is named by ${PLACEMENTS.join(' or ')}.`;
3289
+ }
3290
+ // Only the content field can be prose, so only its value is ever tagged.
3291
+ const prose = proseTags({
3292
+ label: declared.label,
3293
+ declared: bodyField ? properties[bodyField]?.format : undefined,
3294
+ contentFormat: args.contentFormat,
3295
+ value: bodyField ? (value[bodyField] ?? '') : '',
3296
+ });
3297
+ if (typeof prose === 'string')
3298
+ return prose;
2977
3299
  const tags = [];
2978
3300
  if (isAddressableKind(declared.emits.kind))
2979
3301
  tags.push(['d', newId]);
@@ -2989,7 +3311,7 @@ function buildCreationEvent(args) {
2989
3311
  if (declared.emits.setTag && declared.emits.toAddressOf === 'self') {
2990
3312
  tags.push([declared.emits.setTag, address]);
2991
3313
  }
2992
- tags.push(['h', folder]);
3314
+ tags.push([placement, folder], ...prose);
2993
3315
  return {
2994
3316
  pubkey: args.pubkey,
2995
3317
  created_at: Math.floor(args.createdAtMs / 1000),
@@ -2998,7 +3320,73 @@ function buildCreationEvent(args) {
2998
3320
  content: bodyField ? (value[bodyField] ?? '') : '',
2999
3321
  };
3000
3322
  }
3323
+ /**
3324
+ * Build the event that performs a manifest-declared action — one event.
3325
+ *
3326
+ * **Refuses an action declaring `emits.listed`**, which is more than one event
3327
+ * (SPEC §7.3): building the root alone would publish an object its Folder does
3328
+ * not show, and report success. {@link buildActionEvents} builds those.
3329
+ */
3001
3330
  export function buildActionEvent(args) {
3331
+ const declared = args.manifest.actions?.find((a) => a.id === args.actionId);
3332
+ if (declared?.emits.listed === true && (declared.emits.kind === KIND_DELETION || isCreationAction(declared))) {
3333
+ return `"${declared.label}" is followed by a Folder command, so it is built with buildActionEvents.`;
3334
+ }
3335
+ return buildOneActionEvent(args);
3336
+ }
3337
+ /** An action that makes a new object rather than changing one — a form. */
3338
+ function isCreationAction(declared) {
3339
+ return declared.input?.type === 'object' && !!declared.input.properties;
3340
+ }
3341
+ /**
3342
+ * Every event an action publishes, **in publish order** — SPEC §7.3, MAN-1.
3343
+ *
3344
+ * The first is what {@link buildActionEvent} builds. An action declaring
3345
+ * `emits.listed` is followed by the Folder commands that keep its Folder's
3346
+ * listing true, planned by `planPlaceFile` / `planUnlistFile`:
3347
+ *
3348
+ * - a creation: `kind:1852 add` naming the new address in the Folder it was
3349
+ * placed in, when `folderHasState` — none when the Folder has no state, where
3350
+ * a command would hide everything else filed there;
3351
+ * - a deletion: `kind:1852 remove` from each of `listedIn` that has state.
3352
+ *
3353
+ * Both facts come from the caller's read of `kind:30890` and are required
3354
+ * rather than defaulted, because a guess here is silent loss. Publish in order
3355
+ * and stop at the first refusal: each event assumes the ones before it landed.
3356
+ */
3357
+ export function buildActionEvents(args) {
3358
+ const first = buildOneActionEvent(args);
3359
+ if (typeof first === 'string')
3360
+ return first;
3361
+ const declared = args.manifest.actions.find((a) => a.id === args.actionId);
3362
+ if (declared.emits.listed !== true)
3363
+ return [first];
3364
+ if (declared.emits.kind === KIND_DELETION) {
3365
+ if (!args.listedIn)
3366
+ return `"${declared.label}" unlists what it deletes, and needs the Folders that list it.`;
3367
+ // One remove per Folder, however many reads it came back from.
3368
+ const folders = [...new Map(args.listedIn.map((listing) => [listing.id, listing])).values()];
3369
+ return [
3370
+ first,
3371
+ ...folders.flatMap((listing) => planUnlistFile(args.pubkey, args.createdAtMs, { folder: listing.id, address: args.address, hasState: listing.hasState })),
3372
+ ];
3373
+ }
3374
+ if (isCreationAction(declared)) {
3375
+ if (args.folderHasState === undefined) {
3376
+ return `"${declared.label}" lists what it creates, and needs to know whether the Folder has state.`;
3377
+ }
3378
+ // A listing names addresses, and an object with no `d` has none.
3379
+ const identifier = first.tags.find((t) => t[0] === 'd')?.[1];
3380
+ if (identifier === undefined) {
3381
+ return `"${declared.label}" lists what it creates, and a kind ${first.kind} has no address to list.`;
3382
+ }
3383
+ const created = pointerToAddress({ kind: first.kind, pubkey: first.pubkey, identifier, relays: [] });
3384
+ return [first, ...planPlaceFile(args.pubkey, args.createdAtMs, { folder: args.folder, address: created, hasState: args.folderHasState })];
3385
+ }
3386
+ // A change or a comment adds nothing to a Folder and removes nothing from it.
3387
+ return [first];
3388
+ }
3389
+ function buildOneActionEvent(args) {
3002
3390
  const { manifest, kind, address, folder, actionId, value, replyTo } = args;
3003
3391
  const records = manifest.records;
3004
3392
  const declared = manifest.actions?.find((a) => a.id === actionId);
@@ -3008,13 +3396,23 @@ export function buildActionEvent(args) {
3008
3396
  if (!appliesTo.includes(String(kind))) {
3009
3397
  return `"${declared.label}" does not apply to a kind ${kind}.`;
3010
3398
  }
3011
- const isCreation = declared.input?.type === 'object' && !!declared.input.properties;
3399
+ const isCreation = isCreationAction(declared);
3012
3400
  // A reply is a comment with a parent — what the scalar path below builds for
3013
3401
  // an action that is neither a deletion nor a change. Refused here, before any
3014
3402
  // branch that would build something else and quietly drop the parent.
3015
3403
  if (replyTo && (isCreation || declared.emits.kind === KIND_DELETION || declared.emits.field)) {
3016
3404
  return `"${declared.label}" is not a comment, so it cannot reply to one.`;
3017
3405
  }
3406
+ // Prose is a creation's content or a change's value (§7.3). A deletion has
3407
+ // no value, and a comment's body is §13.2 marker text.
3408
+ if (args.contentFormat !== undefined && !isCreation && (!declared.emits.field || declared.emits.kind === KIND_DELETION)) {
3409
+ return `"${declared.label}" takes no prose value, so it takes no content format.`;
3410
+ }
3411
+ // A note is a change's `content`. Anywhere else it would be dropped, and the
3412
+ // resolution a person typed lost without a word.
3413
+ if (args.note !== undefined && (isCreation || !declared.emits.field || declared.emits.kind === KIND_DELETION)) {
3414
+ return `"${declared.label}" is not a change, so it takes no note.`;
3415
+ }
3018
3416
  // An object-creating action is a different event entirely — a new object
3019
3417
  // rather than a change to one — so it branches before the scalar path.
3020
3418
  if (isCreation) {
@@ -3077,12 +3475,21 @@ export function buildActionEvent(args) {
3077
3475
  return `"${value}" is not one of ${vocab.map((e) => e.value).join(', ')}.`;
3078
3476
  }
3079
3477
  }
3478
+ // A prose value's `content-format` and `imeta` — a change only, per the
3479
+ // refusal above. Ship's order: the tag before `ts`, the files after it.
3480
+ const prose = declared.emits.field
3481
+ ? proseTags({ label: declared.label, declared: declared.input?.format, contentFormat: args.contentFormat, value })
3482
+ : [];
3483
+ if (typeof prose === 'string')
3484
+ return prose;
3485
+ const [formatTag, ...files] = prose;
3080
3486
  const tags = declared.emits.field
3081
3487
  ? [
3082
3488
  [records.targetTag, address],
3083
3489
  [records.fieldTag, declared.emits.field],
3084
3490
  [records.valueTag, value],
3085
3491
  ['h', folder],
3492
+ ...(formatTag ? [formatTag] : []),
3086
3493
  ]
3087
3494
  : // NIP-22 comment. Built from the ratified NIP rather than from the
3088
3495
  // manifest, which is legitimate precisely because no app owns kind:1111 —
@@ -3112,12 +3519,13 @@ export function buildActionEvent(args) {
3112
3519
  // depending on which app you asked (FRICTION.md A6).
3113
3520
  if (records.order?.includes('ts'))
3114
3521
  tags.push(['ts', String(args.createdAtMs)]);
3522
+ tags.push(...files);
3115
3523
  return {
3116
3524
  pubkey: args.pubkey,
3117
3525
  created_at: Math.floor(args.createdAtMs / 1000),
3118
3526
  kind: declared.emits.kind,
3119
3527
  tags,
3120
- content: declared.emits.field ? '' : value,
3528
+ content: declared.emits.field ? (args.note ?? '') : value,
3121
3529
  };
3122
3530
  }
3123
3531
  /**
@@ -3176,9 +3584,12 @@ const nameOf = (event) => (event ? tagValue(event, 'name') : undefined);
3176
3584
  *
3177
3585
  * Not a list of ids: a new team is a folder somebody creates, not a code
3178
3586
  * change. Moved here from Peek and Ship, which each held the same line.
3587
+ *
3588
+ * An archived Folder is not navigated by (FOL-46), and a listing that has not
3589
+ * read archives — a summary with no `archived` field — keeps every Folder.
3179
3590
  */
3180
3591
  export function topLevelFolders(folders) {
3181
- return folders.filter((folder) => folder.hasState && !folder.listedIn?.length);
3592
+ return folders.filter((folder) => folder.hasState && !folder.listedIn?.length && !folder.archived);
3182
3593
  }
3183
3594
  /**
3184
3595
  * Every folder this identity can see, for a sidebar.
@@ -3215,6 +3626,12 @@ export async function listFolders(query) {
3215
3626
  ...(addresses.length ? { addresses } : {}),
3216
3627
  });
3217
3628
  }
3629
+ for (const event of events) {
3630
+ const id = tagValue(event, 'd');
3631
+ const folder = id && event.kind === KIND_CHANNEL ? byId.get(id) : undefined;
3632
+ if (folder)
3633
+ folder.channel = pointerToAddress({ kind: KIND_CHANNEL, pubkey: event.pubkey, identifier: id, relays: [] });
3634
+ }
3218
3635
  /*
3219
3636
  A folder listed in another folder's state is a file there. Read off the
3220
3637
  states already in hand: an `a` naming a `kind:39000` is a channel, and its
@@ -3240,14 +3657,172 @@ export async function listFolders(query) {
3240
3657
  listedIn.set(identifier, containers);
3241
3658
  }
3242
3659
  }
3243
- await placeRecordChannels(events, byId, listedIn, query);
3660
+ const { recordsOf, archives } = await placeRecordChannels(events, byId, listedIn, query);
3244
3661
  for (const [id, containers] of listedIn) {
3245
3662
  const folder = byId.get(id);
3246
3663
  if (folder)
3247
3664
  folder.listedIn = containers;
3248
3665
  }
3666
+ markArchivedFolders(byId, recordsOf, archives);
3249
3667
  return [...byId.values()].sort((a, b) => (a.name ?? a.id).localeCompare(b.name ?? b.id));
3250
3668
  }
3669
+ /**
3670
+ * Which Folders are archived — FOL-46, from the changes {@link placeRecordChannels}
3671
+ * read with the record roots.
3672
+ *
3673
+ * A Folder is archived by a change naming its channel's address, the way any
3674
+ * file is. A record's channel — a Ship project's conversation — is archived
3675
+ * with its record, so an archived project's channel stops lighting its team's
3676
+ * dot the way the project stopped being listed — only when every record naming
3677
+ * the channel is archived, since two projects may share one. Its own archive
3678
+ * wins when it has one.
3679
+ *
3680
+ * A refused read throws before this, like the reads beside it: a listing that
3681
+ * silently reported every Folder live would put archived teams back in
3682
+ * everyone's sidebar.
3683
+ */
3684
+ function markArchivedFolders(byId, recordsOf, changes) {
3685
+ for (const folder of byId.values()) {
3686
+ const own = folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE) : undefined;
3687
+ if (own) {
3688
+ folder.archived = own;
3689
+ continue;
3690
+ }
3691
+ const records = recordsOf.get(folder.id) ?? [];
3692
+ const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE));
3693
+ if (records.length && archives.every(Boolean))
3694
+ folder.archived = { ...archives[0], with: records[0] };
3695
+ }
3696
+ }
3697
+ /** How many Folders deep {@link archiveImpact} follows nested Folders. A request per Folder. */
3698
+ const MAX_ARCHIVE_DEPTH = 3;
3699
+ /**
3700
+ * Count what archiving `address` would hide — the read behind the warning.
3701
+ *
3702
+ * "Hides 14 issues and 6 topics that are only here; 2 topics also in Estiva HQ
3703
+ * stay visible." The words are the app's; the counts are the same in every
3704
+ * app, which is why they are read here.
3705
+ *
3706
+ * - **A Folder** (a `kind:39000` address, `FolderSummary.channel`): what it
3707
+ * lists, split by whether another Folder lists it too, the children of what
3708
+ * would be hidden, and — three Folders deep — what a nested Folder only it
3709
+ * lists holds. Pass `folders` from the app's own `listFolders` to save that
3710
+ * read.
3711
+ * - **Any other file**: the children its kind declares (a project's issues), and
3712
+ * bare files placed under it (sub-topics), by their root's `a` tag.
3713
+ *
3714
+ * One level of children, as a listing draws them. A few requests, whatever the
3715
+ * size: the Folder, one per app's manifest (memoised by `cache`), the children,
3716
+ * and their archives.
3717
+ */
3718
+ export async function archiveImpact(address, query, options = {}) {
3719
+ const pointer = addressPointerOf(address);
3720
+ if (!pointer)
3721
+ return { hides: [], staysVisible: [] };
3722
+ const counts = new Map();
3723
+ const count = (kind, n = 1) => counts.set(kind, (counts.get(kind) ?? 0) + n);
3724
+ const staysVisible = [];
3725
+ let parents = [{ address, pointer }];
3726
+ if (pointer.kind === KIND_CHANNEL) {
3727
+ const folders = options.folders ?? (await listFolders(query));
3728
+ parents = [];
3729
+ /*
3730
+ A nested Folder only this one lists goes out of view with it, and so does
3731
+ everything only *that* one lists — walked the same way, a Folder at a
3732
+ time, to a small depth. "Only" is judged against the live Folders outside
3733
+ the tree being archived.
3734
+ */
3735
+ const inTree = new Set([pointer.identifier]);
3736
+ const walk = async (folder, depth) => {
3737
+ const contents = await resolveFolderContents(folder, query, async () => ({}), options.cache);
3738
+ for (const file of contents.files) {
3739
+ if (!file.address)
3740
+ continue;
3741
+ const others = folders.filter((other) => !inTree.has(other.id) && !other.archived && other.addresses?.includes(file.address));
3742
+ if (others.length) {
3743
+ staysVisible.push({ address: file.address, kind: file.kind, in: others.map(({ id, name }) => ({ id, name })) });
3744
+ continue;
3745
+ }
3746
+ count(file.kind);
3747
+ const filePointer = addressPointerOf(file.address);
3748
+ if (!filePointer)
3749
+ continue;
3750
+ if (filePointer.kind === KIND_CHANNEL) {
3751
+ if (depth < MAX_ARCHIVE_DEPTH && !inTree.has(filePointer.identifier)) {
3752
+ inTree.add(filePointer.identifier);
3753
+ await walk(filePointer.identifier, depth + 1);
3754
+ }
3755
+ }
3756
+ else {
3757
+ parents.push({ address: file.address, pointer: filePointer });
3758
+ }
3759
+ }
3760
+ };
3761
+ await walk(pointer.identifier, 0);
3762
+ }
3763
+ // A record channel lists a project *and* its issues; each is counted once.
3764
+ const counted = new Set(parents.map((p) => p.address));
3765
+ for (const [kind, n] of await countLiveChildren(parents, counted, query, options.cache))
3766
+ count(kind, n);
3767
+ return {
3768
+ hides: [...counts].map(([kind, n]) => ({ kind, count: n })),
3769
+ staysVisible,
3770
+ };
3771
+ }
3772
+ /** The children of each parent that are not archived already, by kind. */
3773
+ async function countLiveChildren(parents, counted, query, cache) {
3774
+ const byKind = new Map();
3775
+ if (parents.length === 0)
3776
+ return byKind;
3777
+ const byApp = new Map();
3778
+ for (const { pointer } of parents)
3779
+ byApp.set(manifestKeyOf(pointer), pointer);
3780
+ const manifests = new Map();
3781
+ await Promise.all([...byApp].map(async ([key, pointer]) => {
3782
+ const resolved = await resolveManifest(pointer, query, cache);
3783
+ if (resolved)
3784
+ manifests.set(key, resolved);
3785
+ }));
3786
+ const filters = [];
3787
+ const wanted = [];
3788
+ for (const { address, pointer } of parents) {
3789
+ const projection = manifests.get(manifestKeyOf(pointer))?.manifest.projections?.[String(pointer.kind)];
3790
+ const declared = projection && declaredChildSpec(projection);
3791
+ const specs = [
3792
+ ...(declared ? [{ kind: declared.kind, via: declared.via }] : []),
3793
+ // A sub-topic may sit under a file of any kind (SPEC §6.7).
3794
+ ...(declared?.kind === KIND_BARE_FILE && declared.via === 'a' ? [] : [{ kind: KIND_BARE_FILE, via: 'a' }]),
3795
+ ];
3796
+ for (const spec of specs) {
3797
+ wanted.push({ ...spec, parent: address });
3798
+ filters.push({ kinds: [spec.kind], [`#${spec.via}`]: [address], limit: RELAY_PAGE_CEILING });
3799
+ }
3800
+ }
3801
+ const events = [];
3802
+ for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
3803
+ events.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))));
3804
+ }
3805
+ const children = new Map();
3806
+ for (const event of events) {
3807
+ const identifier = tagValue(event, 'd');
3808
+ if (identifier === undefined)
3809
+ continue;
3810
+ if (!wanted.some((w) => w.kind === event.kind && hasTagValue(event, w.via, w.parent)))
3811
+ continue;
3812
+ const child = pointerToAddress({ kind: event.kind, pubkey: event.pubkey, identifier, relays: [] });
3813
+ if (!counted.has(child))
3814
+ children.set(child, event.kind);
3815
+ }
3816
+ if (children.size === 0)
3817
+ return byKind;
3818
+ const changes = await readArchiveChanges([...children.keys()], query);
3819
+ for (const [child, kind] of children) {
3820
+ if (archiveOf(changes, child, ARCHIVE_RULE))
3821
+ continue;
3822
+ byKind.set(kind, (byKind.get(kind) ?? 0) + 1);
3823
+ }
3824
+ return byKind;
3825
+ }
3251
3826
  /**
3252
3827
  * A record's own channel is listed wherever the record is (FOL-42).
3253
3828
  *
@@ -3318,8 +3893,18 @@ async function placeRecordChannels(events, byId, listedIn, query) {
3318
3893
  containersOf.set(key, entry);
3319
3894
  }
3320
3895
  }
3321
- if (containersOf.size === 0)
3322
- return;
3896
+ // Every Folder's archive, and every listed record's, ride with the roots
3897
+ // (FOL-46): the channel addresses are in hand from the listing and the
3898
+ // record addresses are the keys above, so neither needs the roots first.
3899
+ const channels = [...byId.values()].flatMap((folder) => (folder.channel ? [folder.channel] : []));
3900
+ const archiveAsk = archiveFilters([...new Set([...channels, ...containersOf.keys()])]);
3901
+ if (containersOf.size === 0) {
3902
+ const archives = [];
3903
+ for (let start = 0; start < archiveAsk.length; start += MAX_FILTERS_PER_QUERY) {
3904
+ archives.push(...(await query(archiveAsk.slice(start, start + MAX_FILTERS_PER_QUERY))));
3905
+ }
3906
+ return { recordsOf: new Map(), archives };
3907
+ }
3323
3908
  const groups = new Map();
3324
3909
  for (const { pointer } of containersOf.values()) {
3325
3910
  const key = `${pointer.kind}:${pointer.pubkey}`;
@@ -3334,10 +3919,14 @@ async function placeRecordChannels(events, byId, listedIn, query) {
3334
3919
  filters.push({ kinds: [kind], authors: [pubkey], '#d': chunk, limit: chunk.length });
3335
3920
  }
3336
3921
  }
3337
- const roots = [];
3922
+ filters.push(...archiveAsk);
3923
+ const answered = [];
3338
3924
  for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
3339
- roots.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))));
3925
+ answered.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))));
3340
3926
  }
3927
+ // A state lists files, never change events, so the kind tells them apart.
3928
+ const archives = answered.filter((e) => e.kind === ARCHIVE_RULE.changeKind);
3929
+ const roots = answered.filter((e) => e.kind !== ARCHIVE_RULE.changeKind);
3341
3930
  // Newest wins, as it does for any addressable event a relay has not yet replaced.
3342
3931
  const newest = new Map();
3343
3932
  for (const root of roots) {
@@ -3346,11 +3935,16 @@ async function placeRecordChannels(events, byId, listedIn, query) {
3346
3935
  if (!held || root.created_at > held.created_at)
3347
3936
  newest.set(key, root);
3348
3937
  }
3938
+ const recordsOf = new Map();
3349
3939
  for (const [key, { containers }] of containersOf) {
3350
3940
  const root = newest.get(key);
3351
3941
  const channel = root && folderOf(root);
3352
3942
  if (!channel || byId.get(channel)?.hasState)
3353
3943
  continue;
3944
+ // A file published into the Folder that lists it names that Folder, and
3945
+ // does not own it: a topic's `h` is its team's.
3946
+ if (!containers.includes(channel))
3947
+ recordsOf.set(channel, [...(recordsOf.get(channel) ?? []), key]);
3354
3948
  for (const container of containers) {
3355
3949
  if (container === channel)
3356
3950
  continue;
@@ -3360,6 +3954,7 @@ async function placeRecordChannels(events, byId, listedIn, query) {
3360
3954
  listedIn.set(channel, placed);
3361
3955
  }
3362
3956
  }
3957
+ return { recordsOf, archives };
3363
3958
  }
3364
3959
  /**
3365
3960
  * Everything in one folder, resolved through the manifests of the apps that own it.
@@ -3408,7 +4003,7 @@ export async function resolveFolderContents(folder, query,
3408
4003
  /** Defaults to asking the relay. The browser passes a cached lookup. */
3409
4004
  lookupPeople,
3410
4005
  /** See {@link ProjectionCache}. Omitting it is exactly the old behaviour. */
3411
- cache) {
4006
+ cache, options = {}) {
3412
4007
  // 1. The folder itself. Both kinds in one trip: the state is the model and
3413
4008
  // the channel is what names a folder that has none yet.
3414
4009
  const identity = await query([
@@ -3417,10 +4012,14 @@ cache) {
3417
4012
  ]);
3418
4013
  const state = identity.find((e) => e.kind === KIND_FOLDER_STATE && tagValue(e, 'd') === folder);
3419
4014
  const channel = identity.find((e) => e.kind === KIND_CHANNEL && tagValue(e, 'd') === folder);
4015
+ const channelAddress = channel
4016
+ ? pointerToAddress({ kind: KIND_CHANNEL, pubkey: channel.pubkey, identifier: folder, relays: [] })
4017
+ : undefined;
3420
4018
  const summary = {
3421
4019
  id: folder,
3422
4020
  name: nameOf(state) ?? nameOf(channel),
3423
4021
  hasState: !!state,
4022
+ ...(channelAddress ? { channel: channelAddress } : {}),
3424
4023
  ...(state
3425
4024
  ? { address: pointerToAddress({ kind: state.kind, pubkey: state.pubkey, identifier: folder, relays: [] }) }
3426
4025
  : {}),
@@ -3434,7 +4033,12 @@ cache) {
3434
4033
  }
3435
4034
  : await addressesByContainment(folder, query);
3436
4035
  if (addresses.length === 0) {
3437
- return { ...summary, files: [], source: state ? 'state' : 'channel' };
4036
+ // Nothing listed, so no change query to ride on: the Folder's own archive
4037
+ // is one small read of its own.
4038
+ const archived = channelAddress
4039
+ ? archiveOf(await readArchiveChanges([channelAddress], query), channelAddress, ARCHIVE_RULE)
4040
+ : undefined;
4041
+ return { ...summary, ...(archived ? { archived } : {}), files: [], source: state ? 'state' : 'channel' };
3438
4042
  }
3439
4043
  // 2. Group by (kind, author): one manifest answers for every file an app owns
3440
4044
  // in this folder, and the cache makes the second folder free.
@@ -3473,8 +4077,15 @@ cache) {
3473
4077
  // Change kinds are a set because two apps may fold differently, and a kind
3474
4078
  // is a u16 — the relay refuses an out-of-range one outright, so an app
3475
4079
  // declaring no `records` contributes no filter rather than an empty one.
4080
+ //
4081
+ // SPEC's change kind is always asked for, whatever the apps declare: any
4082
+ // file may be archived (FOL-46), a nested Folder included, and so may this
4083
+ // Folder, whose own address rides in the same filter.
3476
4084
  const changeKinds = [
3477
- ...new Set([...manifests.values()].flatMap((r) => (r.manifest.records ? [r.manifest.records.changeKind] : []))),
4085
+ ...new Set([
4086
+ ARCHIVE_RULE.changeKind,
4087
+ ...[...manifests.values()].flatMap((r) => (r.manifest.records ? [r.manifest.records.changeKind] : [])),
4088
+ ]),
3478
4089
  ];
3479
4090
  const events = await query([
3480
4091
  ...[...groups.values()].map(({ pointer, addresses: group }) => ({
@@ -3483,12 +4094,21 @@ cache) {
3483
4094
  '#d': group.map((a) => referenceToPointer(a).identifier),
3484
4095
  limit: group.length,
3485
4096
  })),
3486
- ...(changeKinds.length
3487
- ? [{ kinds: changeKinds, '#a': addresses, limit: 500 }]
3488
- : []),
4097
+ { kinds: changeKinds, '#a': addresses, limit: 500 },
4098
+ // The archives again, in groups small enough that a busy Folder's other
4099
+ // changes cannot push them off the page — the filter above holds 500
4100
+ // changes for every file together, and one Folder has already had 688.
4101
+ // Same request.
4102
+ ...archiveFilters(channelAddress ? [...addresses, channelAddress] : addresses),
3489
4103
  ]);
3490
- // 4. Build each file, in the order the folder listed them.
3491
- const files = [];
4104
+ const ownArchive = channelAddress ? archiveOf(events, channelAddress, ARCHIVE_RULE) : undefined;
4105
+ if (ownArchive)
4106
+ summary.archived = ownArchive;
4107
+ // 4. Build each file, in the order the folder listed them. Archived files are
4108
+ // held back until the cascade below has read their parents.
4109
+ const listed = [];
4110
+ const archiveByAddress = new Map();
4111
+ const parentByAddress = new Map();
3492
4112
  for (const { address, pointer } of pointers) {
3493
4113
  const resolved = manifests.get(manifestKeyOf(pointer));
3494
4114
  // No app claims this kind, so there is no projection to draw it with.
@@ -3503,11 +4123,16 @@ cache) {
3503
4123
  continue;
3504
4124
  const records = foldRuleOf(resolved.manifest);
3505
4125
  const folded = foldChanges(events.filter((e) => e.kind === records.changeKind && hasTagValue(e, records.targetTag, address)), records);
4126
+ const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest));
4127
+ archiveByAddress.set(address, archive);
4128
+ parentByAddress.set(address, parentRefOf(resolved.manifest, pointer.kind, root, folded));
3506
4129
  // An app hiding a record from its own lists is saying it is not part of the
3507
- // folder any more. `hiddenWhen` is the app's own declaration of that.
3508
- if (records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals) {
4130
+ // folder any more. `hiddenWhen` is the app's own declaration of that — the
4131
+ // archive above is the same statement for a kind whose app declared none.
4132
+ // Not reported as an archive: the app said "hidden", which may mean more.
4133
+ const hidden = !!records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals;
4134
+ if (hidden && !archive)
3509
4135
  continue;
3510
- }
3511
4136
  /*
3512
4137
  A placed file stays listed only while the placement is current.
3513
4138
 
@@ -3533,15 +4158,55 @@ cache) {
3533
4158
  if (!current)
3534
4159
  continue;
3535
4160
  }
3536
- files.push(buildObject({
3537
- root,
3538
- pointer,
3539
- manifest: resolved.manifest,
3540
- projection,
3541
- folded,
3542
- viaRecommendation: resolved.viaRecommendation,
3543
- webTemplate: resolved.webTemplate,
3544
- }));
4161
+ listed.push({
4162
+ archive,
4163
+ file: buildObject({
4164
+ root,
4165
+ pointer,
4166
+ manifest: resolved.manifest,
4167
+ projection,
4168
+ folded,
4169
+ viaRecommendation: resolved.viaRecommendation,
4170
+ webTemplate: resolved.webTemplate,
4171
+ }),
4172
+ });
4173
+ }
4174
+ /*
4175
+ 5. The cascade (FOL-46): a file under an archived file is hidden with it —
4176
+ an issue with its project, a sub-topic with its topic. Computed here and
4177
+ never written to the children, so unarchiving the parent restores
4178
+ exactly what was there.
4179
+
4180
+ The chain is walked through everything this Folder lists. A parent that
4181
+ lives in another Folder is read once for all such parents, one level: a
4182
+ project whose record channel lists its issues is the case that matters,
4183
+ and there the project is listed too.
4184
+
4185
+ Only by parent. A file this Folder lists is never hidden because some
4186
+ *other* Folder that lists it is archived — that is what lets a file
4187
+ placed in two Folders stay visible in the one that is not archived.
4188
+ */
4189
+ const outside = [
4190
+ ...new Set(listed.flatMap(({ file }) => {
4191
+ const parent = file.address && parentByAddress.get(file.address);
4192
+ return parent && !archiveByAddress.has(parent) && addressPointerOf(parent) ? [parent] : [];
4193
+ })),
4194
+ ];
4195
+ if (outside.length) {
4196
+ const changes = await readArchiveChanges(outside, query);
4197
+ for (const parent of outside)
4198
+ archiveByAddress.set(parent, archiveOf(changes, parent, ARCHIVE_RULE));
4199
+ }
4200
+ const files = [];
4201
+ for (const { file, archive } of listed) {
4202
+ const effective = archive ??
4203
+ (file.address
4204
+ ? archivedAncestorOf(file.address, (a) => parentByAddress.get(a), (a) => archiveByAddress.get(a))
4205
+ : undefined);
4206
+ if (!effective)
4207
+ files.push(file);
4208
+ else if (options.includeArchived)
4209
+ files.push({ ...file, archived: effective });
3545
4210
  }
3546
4211
  const people = await (lookupPeople ?? peopleViaRelay(query))([
3547
4212
  ...new Set(files.flatMap(pubkeysIn)),