@estiva-app/interop 0.40.0 → 0.42.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.
@@ -12,11 +12,19 @@
12
12
  * anything: it becomes an integration written against one app, which is the
13
13
  * thing the whole exercise argues against.
14
14
  */
15
- import { decodeNaddr, decodeNevent, encodeNaddr, encodeNevent, findNaddrs, pointerToAddress, referenceToPointer } from '@estiva-app/protocol';
16
- import { imetaOf, parseProfile } from '@estiva-app/protocol';
15
+ import { decodeNevent, encodeNaddr, encodeNevent, pointerToAddress, referenceToPointer } from '@estiva-app/protocol';
16
+ import { parseProfile } from '@estiva-app/protocol';
17
17
  import { MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol';
18
18
  import { attachmentsInDocument, BLOCK_DOCUMENT_FORMAT, CONTENT_FORMAT_TAG, imetaTag, parseBlockDocument, } from '@estiva-app/protocol';
19
19
  import { planPlaceFile, planUnlistFile } from './folders.js';
20
+ /*
21
+ SPEC §6.4's comment strength and §6.3's ordering are the conversation
22
+ package's (CON-5): one copy of each rule, read here for a file's `comments`,
23
+ `conversationsOf` and every change fold. Its `isCommentOn` is stricter than
24
+ the copy that lived here — a `kind:9` is never a comment (CON-20 migrated
25
+ them), a reply is never a root, and `ts` is trusted only inside its own second.
26
+ */
27
+ import { byOrder, isCommentOn, orderingMs } from '@estiva-app/conversation';
20
28
  /** The default: one kind:0 query, straight to the relay. */
21
29
  /**
22
30
  * How long a *name* may be believed.
@@ -247,7 +255,8 @@ function rootOf(event) {
247
255
  * on another file, a `kind:9` whose body names this one — and those are
248
256
  * *Mentioned in*, which §6.4 says MUST NOT be presented as the file's own
249
257
  * discussion. A badge counting them said "3" beside an issue with one comment.
250
- * See `isCommentOn` for the per-kind rule.
258
+ * See `@estiva-app/conversation`'s `isCommentOn` for the per-kind rule, which
259
+ * this passes the owner's declared comment kinds (§7.3).
251
260
  */
252
261
  export async function conversationsOf(files, query,
253
262
  /** Warm from the read that produced these files; see {@link ProjectionCache}. */
@@ -296,6 +305,7 @@ cache) {
296
305
  if (asked.length === 0)
297
306
  return {};
298
307
  const refOf = new Map(asked.map((a) => [a.address, a.ref]));
308
+ const kindsOf = new Map(asked.map((a) => [a.address, a.filter.kinds]));
299
309
  const conversations = {};
300
310
  for (const a of asked)
301
311
  conversations[a.ref] = [];
@@ -323,7 +333,7 @@ cache) {
323
333
  neither when it merely mentions them. `isCommentOn` is §6.4's rule.
324
334
  */
325
335
  const ref = event.tags
326
- .filter((t) => t[0] === 'a' && t[1] && refOf.has(t[1]) && isCommentOn(event, t[1]))
336
+ .filter((t) => t[0] === 'a' && t[1] && refOf.has(t[1]) && isCommentOn(event, t[1], kindsOf.get(t[1])))
327
337
  .map((t) => refOf.get(t[1]))
328
338
  .find((found) => found !== undefined);
329
339
  if (ref !== undefined) {
@@ -417,186 +427,6 @@ limit = CONVERSATION_LIMIT) {
417
427
  }
418
428
  return { roots, replies };
419
429
  }
420
- /**
421
- * How many targets a reaction read asks about — SPEC §6.6, decided by CON-1.
422
- *
423
- * A `kind:7` carries no `h` and no `a`, so reactions are found only by asking
424
- * for the ids on screen, and the number asked for is a horizon every reader
425
- * shares: two apps with different N disagree about a count, legitimately and
426
- * unfixably. The value is the specification's, not this package's.
427
- */
428
- export const REACTION_HORIZON = 100;
429
- /** The three kinds a comment's decorations come in. Numbers, and here for the same reason `KIND_BARE_FILE` is. */
430
- const KIND_REACTION = 7;
431
- const KIND_ASSERTION = 9101;
432
- const KIND_MESSAGE_EDIT = 40003;
433
- /**
434
- * The message a `kind:40003` edits: **the first `e` whose value is 64 hex,
435
- * marker ignored**, lowercased (SPEC §6.8, PEE-38). That is exactly the event
436
- * whose ownership the relay checked (`validate_edit_ownership`), and a reader
437
- * MUST NOT apply an edit to any other `e` — nothing refuses an edit carrying a
438
- * second one, so `['e', <own>, '', 'mention'], ['e', <victim>]` is accepted on
439
- * the writer's own message and must not be drawn on the victim's.
440
- *
441
- * Undefined when there is none, which the relay refuses; a reader holding one
442
- * anyway applies it nowhere.
443
- */
444
- export function editTargetOf(event) {
445
- const tag = event.tags.find((t) => t[0] === 'e' && typeof t[1] === 'string' && /^[0-9a-fA-F]{64}$/.test(t[1]));
446
- return tag?.[1].toLowerCase();
447
- }
448
- /** Ids per `#e` filter. A comfortable fraction of the relay's page, so one filter's answer is never cut. */
449
- const DECORATION_TARGETS_PER_FILTER = 100;
450
- /**
451
- * What has been done to a set of comments — edits, reactions, resolutions — in
452
- * one request, read by `#e`.
453
- *
454
- * **A second round trip, and that is why this is a function rather than part of
455
- * {@link resolveForeignObject}.** Everything that read returns is addressed by
456
- * `#a`, so it arrives with the root. An edit (`kind:40003`), a reaction
457
- * (`kind:7`) and a resolution (`kind:9101`) name the *comment* by `e` and not
458
- * the file, so they cannot be asked for until the comment ids are known. A
459
- * widget that never draws comments should not pay for that, so a consumer
460
- * that draws a conversation asks here with the ids it has — the roots off the
461
- * object, the replies off its thread read — and pays once for both.
462
- *
463
- * Three rules, each from the ticket that established it:
464
- *
465
- * - **Edits fold newest-wins** (CON-8). The original stays on the relay and so
466
- * does every edit; the fold is the reader's, and this is it. Attachments
467
- * fold separately (CON-5): the newest edit carrying `imeta` sets them.
468
- * - **Reactions have a horizon of {@link REACTION_HORIZON} targets** (SPEC
469
- * §6.6, CON-1) — the *newest* targets by `at`, one budget across whatever
470
- * id spaces the caller mixes, and the number left out is reported. Edits
471
- * and resolutions have no horizon: they are one event per change, not one
472
- * per reader.
473
- * - **A resolution is a `kind:9101` carrying `t=resolution`** (PEEK-128); its
474
- * `action` tag is the state, its content the rationale, and a second `e`
475
- * marked `support` names the reply that carried it.
476
- *
477
- * Nothing here checks who wrote an edit against who wrote the comment. The
478
- * relay adjudicates writes; a `40003` it stored is one it accepted — **for
479
- * the `e` it checked**, which is why an edit lands only on
480
- * {@link editTargetOf} and never on another `e` it carries.
481
- */
482
- export async function commentDecorationsOf(targets, query) {
483
- const byId = {};
484
- const unique = new Map();
485
- for (const t of targets)
486
- if (!unique.has(t.id))
487
- unique.set(t.id, t.at);
488
- for (const id of unique.keys())
489
- byId[id] = { reactions: [], resolutions: [] };
490
- if (unique.size === 0)
491
- return { byId, reactionTargetsOmitted: 0 };
492
- const ids = [...unique.keys()];
493
- // Newest first, ties on the higher id so the cut is stable between reads.
494
- const forReactions = [...unique]
495
- .sort((a, b) => b[1] - a[1] || (a[0] > b[0] ? -1 : 1))
496
- .slice(0, REACTION_HORIZON)
497
- .map(([id]) => id);
498
- const filters = [];
499
- const chunked = (list) => {
500
- const out = [];
501
- for (let i = 0; i < list.length; i += DECORATION_TARGETS_PER_FILTER) {
502
- out.push(list.slice(i, i + DECORATION_TARGETS_PER_FILTER));
503
- }
504
- return out;
505
- };
506
- for (const chunk of chunked(ids)) {
507
- filters.push({ kinds: [KIND_MESSAGE_EDIT, KIND_ASSERTION], '#e': chunk, limit: RELAY_PAGE_CEILING });
508
- }
509
- for (const chunk of chunked(forReactions)) {
510
- filters.push({ kinds: [KIND_REACTION], '#e': chunk, limit: RELAY_PAGE_CEILING });
511
- }
512
- const seen = new Set();
513
- const edits = new Map();
514
- const bodies = new Map();
515
- for (const t of targets)
516
- if (t.body !== undefined && !bodies.has(t.id))
517
- bodies.set(t.id, t.body);
518
- for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
519
- const events = await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY));
520
- for (const event of events) {
521
- if (seen.has(event.id))
522
- continue;
523
- seen.add(event.id);
524
- /*
525
- An edit's target is the one the relay checked ownership of, and no
526
- other (SPEC §6.8, PEE-38): see {@link editTargetOf}. For the rest, the
527
- `e` that names one of *our* targets — a resolution carries a second `e`
528
- for its supporting reply, so its first `e` is not necessarily the target.
529
- `hasOwn`, not `in`: an `e` of `constructor` is anybody's to write, and
530
- `in` finds it on the prototype.
531
- */
532
- const ours = (id) => id !== undefined && Object.hasOwn(byId, id);
533
- const target = event.kind === KIND_MESSAGE_EDIT
534
- ? editTargetOf(event)
535
- : event.tags.find((t) => t[0] === 'e' && ours(t[1]) && !t[3])?.[1];
536
- if (!ours(target))
537
- continue;
538
- const into = byId[target];
539
- if (event.kind === KIND_MESSAGE_EDIT) {
540
- edits.set(target, [...(edits.get(target) ?? []), event]);
541
- }
542
- else if (event.kind === KIND_REACTION) {
543
- if (!forReactions.includes(target))
544
- continue;
545
- into.reactions.push({ id: event.id, emoji: event.content, by: event.pubkey, at: event.created_at });
546
- }
547
- else if (event.kind === KIND_ASSERTION) {
548
- if (tagValue(event, 't') !== 'resolution')
549
- continue;
550
- const action = tagValue(event, 'action');
551
- if (action !== 'resolved' && action !== 'reopened')
552
- continue;
553
- const support = event.tags.find((t) => t[0] === 'e' && t[3] === 'support')?.[1];
554
- into.resolutions.push({
555
- id: event.id,
556
- action,
557
- by: event.pubkey,
558
- at: event.created_at,
559
- ...(event.content ? { message: event.content } : {}),
560
- ...(support ? { supportingEventId: support } : {}),
561
- });
562
- }
563
- }
564
- }
565
- for (const [target, list] of edits) {
566
- const ordered = [...list].sort(byOrder);
567
- const into = byId[target];
568
- const newest = ordered[ordered.length - 1];
569
- let body = bodies.get(target);
570
- if (body === undefined) {
571
- into.edit = { body: newest.content, at: newest.created_at, by: newest.pubkey };
572
- }
573
- else {
574
- // Replayed from the target's own body, so "byte-identical to the body it
575
- // replaces" is judged against the body as it then stood.
576
- let changed;
577
- for (const edit of ordered) {
578
- if (edit.content !== body)
579
- changed = edit;
580
- body = edit.content;
581
- }
582
- if (changed)
583
- into.edit = { body: newest.content, at: changed.created_at, by: changed.pubkey };
584
- }
585
- for (let i = ordered.length - 1; i >= 0; i--) {
586
- const set = imetaOf(ordered[i]);
587
- if (set.length > 0) {
588
- into.attachments = set;
589
- break;
590
- }
591
- }
592
- }
593
- const oldestFirst = (a, b) => a.at - b.at || (a.id < b.id ? -1 : 1);
594
- for (const decoration of Object.values(byId)) {
595
- decoration.reactions.sort(oldestFirst);
596
- decoration.resolutions.sort(oldestFirst);
597
- }
598
- return { byId, reactionTargetsOmitted: ids.length - forReactions.length };
599
- }
600
430
  export const ACTION_EFFECTS = ['safe', 'writes', 'destructive'];
601
431
  /** The declared format, when it is one this version writes. */
602
432
  function proseFormatOf(declared) {
@@ -794,52 +624,6 @@ export function folderOf(root) {
794
624
  * answered the way the relay answered it.
795
625
  */
796
626
  const hasTagValue = (e, name, value) => e.tags.some((t) => t[0] === name && t[1] === value);
797
- /** The addresses a body names by `nostr:naddr…`; a pointer that will not decode is prose. */
798
- function namedInBody(body) {
799
- const addresses = new Set();
800
- for (const naddr of findNaddrs(body)) {
801
- try {
802
- addresses.add(pointerToAddress(decodeNaddr(naddr)));
803
- }
804
- catch {
805
- // A malformed pointer is prose.
806
- }
807
- }
808
- return addresses;
809
- }
810
- /**
811
- * Is this event a *comment* on the address, rather than a message elsewhere
812
- * that merely names it?
813
- *
814
- * Both arrive by the object's `#a` read — the `a` tag is the index of every
815
- * reference, whichever strength — and SPEC §6.4 says an app MUST NOT present
816
- * the two as one list: a comment is the object's own discussion, a mention is
817
- * *Mentioned in*. Until CON-15 this package handed the raw `#a` union to
818
- * {@link resolveForeignObject}'s `comments` and {@link conversationsOf}, and
819
- * a widget said "3" beside an issue with one comment and two mentions. The
820
- * rule is §6.4's per-tag one, applied per kind (CON-13, CON-14):
821
- *
822
- * - A `kind:1111`'s uppercase `A` is the comment — NIP-22's thread root. One
823
- * whose `A` is another file is that file's conversation; its `a` for this
824
- * one is the index of a reference. One with no `A` at all is read by its
825
- * `a`, the way §6.4 says to read it rather than drop the thread.
826
- * - Any other comment kind has no `A`, so its `a` is read against its body.
827
- * A `kind:9` whose body names the address is the index of that reference —
828
- * what Peek writes for every `[`-menu reference in a topic message — and a
829
- * mention. One whose body does not is the anchor of a comment written
830
- * before REW-10 moved comments to `1111`, which is not replaceable and so
831
- * reads as a comment for good.
832
- *
833
- * `commentDecorationsOf` is deliberately not behind this: a reaction or a
834
- * status on a comment is about the *comment*, whatever that comment is about.
835
- */
836
- function isCommentOn(event, address) {
837
- if (event.kind === KIND_COMMENT) {
838
- const roots = event.tags.filter((t) => t[0] === 'A' && t[1]);
839
- return roots.length > 0 ? roots.some((t) => t[1] === address) : hasTagValue(event, 'a', address);
840
- }
841
- return hasTagValue(event, 'a', address) && !namedInBody(event.content).has(address);
842
- }
843
627
  /**
844
628
  * A manifest event's `content`, or null when it is not parseable JSON.
845
629
  *
@@ -953,6 +737,20 @@ function webTemplate(event, entity) {
953
737
  * served by the plain noun.
954
738
  */
955
739
  export const KIND_BARE_FILE = 30840;
740
+ /**
741
+ * The tag a move carries naming its new parent — SPEC §7.2 (FOL-45).
742
+ *
743
+ * A move is a change to the field a list declares as `movedBy`, and its new
744
+ * parent is the change's `value`, which no relay indexes. So a move also
745
+ * carries `["A", <new parent address>]`, after `ts`, and a list reads
746
+ * `{ kinds: [changeKind], "#A": [parent] }` to find the children moved in.
747
+ *
748
+ * Uppercase because a lowercase `a` is the change's target to every reader —
749
+ * and Folder listings match *every* `a` on a change, so a second one would
750
+ * fold the move into the parent itself. Only on a change, never read without a
751
+ * `kinds` filter, and only ever a hint: the child's own fold decides.
752
+ */
753
+ export const MOVED_TO_TAG = 'A';
956
754
  /**
957
755
  * The field that archives a file, whatever its kind — FOL-46.
958
756
  *
@@ -1452,33 +1250,6 @@ export async function resolveActingManifest(pointer, declaredBy, query, cache) {
1452
1250
  const creators = await creatorsOn(pointer.kind, query, cache);
1453
1251
  return borrowedFrom(pointer.kind, owner, creators).find((creator) => creator.address === declaredBy) ?? null;
1454
1252
  }
1455
- /**
1456
- * Ordering key for an append-only event, per the manifest's `records.order`.
1457
- *
1458
- * The manifest declares `["ts", "created_at", "id"]` and, crucially, the
1459
- * condition under which `ts` may be believed: only when it agrees with
1460
- * `created_at` to the second. `created_at` has one-second resolution and the
1461
- * relay validates it, so a `ts` pinned inside that second inherits that
1462
- * validation and can only refine ordering *within* it.
1463
- *
1464
- * Peek enforces that bound itself rather than trusting the writer. Under the
1465
- * honour-system model nothing validates these events (RFC_UPDATES.md §3), so a
1466
- * buggy or pushy client claiming a far-future `ts` would otherwise win every
1467
- * fold forever — in Peek's rendering as much as in the owning app's.
1468
- */
1469
- function orderingMs(event) {
1470
- const raw = Number.parseInt(tagValue(event, 'ts') ?? '', 10);
1471
- if (Number.isFinite(raw) && Math.abs(Math.floor(raw / 1000) - event.created_at) <= 1)
1472
- return raw;
1473
- return event.created_at * 1000;
1474
- }
1475
- /** Total order, oldest first. Ties break on the lower event id (NIP-01's rule). */
1476
- function byOrder(a, b) {
1477
- const [at, bt] = [orderingMs(a), orderingMs(b)];
1478
- if (at !== bt)
1479
- return at - bt;
1480
- return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
1481
- }
1482
1253
  /** Replay change events into current field values. Last write wins per field. */
1483
1254
  /**
1484
1255
  * The fold rule a manifest declares, or one that folds nothing — PRO-6.
@@ -2462,6 +2233,7 @@ function planObject(naddr, pointer, resolved, depth) {
2462
2233
  ...(manifest.records ? [] : [{ kinds: [ARCHIVE_RULE.changeKind], '#a': [address], limit: 500 }]),
2463
2234
  { kinds: commentKinds, '#a': [address], limit: CONVERSATION_LIMIT },
2464
2235
  ...(childFilter ? [childFilter.filter] : []),
2236
+ ...(childFilter?.movedIn ? [childFilter.movedIn] : []),
2465
2237
  ];
2466
2238
  return { naddr, pointer, address, resolved, projection, records, commentKinds, childFilter, filters, openUrl };
2467
2239
  }
@@ -2529,10 +2301,10 @@ function assembleObject(plan, answered, childChanges = []) {
2529
2301
 
2530
2302
  Then narrowed to §6.4's *comment* strength (CON-15): the `a` tag is also the
2531
2303
  index of a mention, and a mention is not this object's discussion. See
2532
- `isCommentOn` for the per-kind rule.
2304
+ `isCommentOn` (`@estiva-app/conversation`) for the per-kind rule.
2533
2305
  */
2534
2306
  const comments = events
2535
- .filter((e) => commentKinds.includes(e.kind) && isCommentOn(e, address))
2307
+ .filter((e) => commentKinds.includes(e.kind) && isCommentOn(e, address, commentKinds))
2536
2308
  .sort(byOrder)
2537
2309
  .map((e) => ({ id: e.id, author: e.pubkey, body: e.content, createdAt: e.created_at }));
2538
2310
  const object = buildObject({
@@ -2728,6 +2500,7 @@ function childFilterFor(args) {
2728
2500
  const parent = children.match === 'identifier'
2729
2501
  ? pointer.identifier
2730
2502
  : pointerToAddress({ ...pointer, relays: [] });
2503
+ const movedBy = children.movedBy && children.match !== 'identifier' ? children.movedBy : undefined;
2731
2504
  return {
2732
2505
  filter: { kinds: [children.kind], [`#${children.via}`]: [parent], limit: children.limit ?? 100 },
2733
2506
  kind: children.kind,
@@ -2735,9 +2508,33 @@ function childFilterFor(args) {
2735
2508
  parent,
2736
2509
  // This list's own, not whichever projection declares one for the kind — and
2737
2510
  // none on an identifier list, where a move's address never names the parent.
2738
- ...(children.movedBy && children.match !== 'identifier' ? { movedBy: children.movedBy } : {}),
2511
+ ...(movedBy ? { movedBy } : {}),
2512
+ // The children moved *in*: the moves naming this parent by `MOVED_TO_TAG`
2513
+ // (SPEC §7.2, FOL-45). A `#via` read cannot see them — their parent is in a
2514
+ // `value` — so without this a child moved here was drawn under nothing.
2515
+ ...(movedBy && manifest.records
2516
+ ? { movedIn: { kinds: [manifest.records.changeKind], [`#${MOVED_TO_TAG}`]: [parent], limit: children.limit ?? 100 } }
2517
+ : {}),
2739
2518
  };
2740
2519
  }
2520
+ /**
2521
+ * The addresses of children a pooled answer says were moved in to this parent:
2522
+ * the target of every change naming it by `MOVED_TO_TAG`, of the child kind.
2523
+ *
2524
+ * **A hint, never the answer.** A later move away names the *new* parent, not
2525
+ * this one, so a change found here may be stale; the child's own fold decides
2526
+ * (`childrenFrom`), exactly as it decides for a child created here.
2527
+ */
2528
+ function movedInAddressesOf(events, childFilter, records) {
2529
+ if (!childFilter.movedIn || !records)
2530
+ return [];
2531
+ const prefix = `${childFilter.kind}:`;
2532
+ const targets = events
2533
+ .filter((e) => e.kind === records.changeKind && hasTagValue(e, MOVED_TO_TAG, childFilter.parent))
2534
+ .map((e) => tagValue(e, records.targetTag))
2535
+ .filter((address) => !!address && address.startsWith(prefix));
2536
+ return [...new Set(targets)];
2537
+ }
2741
2538
  /**
2742
2539
  * The child objects, picked back out of the merged result set.
2743
2540
  *
@@ -2760,7 +2557,21 @@ function childrenFrom(args) {
2760
2557
  return [];
2761
2558
  const records = foldRuleOf(manifest);
2762
2559
  const { movedBy } = childFilter;
2763
- return childRootsOf(events, childFilter).flatMap((event) => {
2560
+ const created = childRootsOf(events, childFilter);
2561
+ const createdAddresses = new Set(created.map(childAddressOf));
2562
+ // Moved in: the roots `childChangeFilters` read for the moves naming this parent,
2563
+ // newest version of each, and only those not already found as created here.
2564
+ const movedIn = new Set(movedInAddressesOf(events, childFilter, manifest.records).filter((a) => !createdAddresses.has(a)));
2565
+ const movedInRoots = new Map();
2566
+ for (const event of childChanges) {
2567
+ const address = event.kind === childFilter.kind ? childAddressOf(event) : undefined;
2568
+ if (!address || !movedIn.has(address))
2569
+ continue;
2570
+ const current = movedInRoots.get(address);
2571
+ if (!current || event.created_at > current.created_at)
2572
+ movedInRoots.set(address, event);
2573
+ }
2574
+ return [...created, ...[...movedInRoots.values()].sort(byOrder)].flatMap((event) => {
2764
2575
  const address = childAddressOf(event);
2765
2576
  /*
2766
2577
  The child's own fold — MAN-7. Until this, a card's children were built
@@ -2778,13 +2589,16 @@ function childrenFrom(args) {
2778
2589
  found the child.
2779
2590
  - **hidden** by the app's own `hiddenWhen`, or **archived** (FOL-46).
2780
2591
 
2781
- A child moved *in* is not found here and cannot be: its new parent is in
2782
- a `value`, which no relay indexes. SPEC §7.2 names the Folder listing as
2783
- the only read that sees one.
2592
+ - **moved in** and since moved on, or never really here — FOL-45. A
2593
+ child found by its move's `MOVED_TO_TAG` has no `via` naming this
2594
+ parent, so only a folded value that names it keeps it: the tag is how
2595
+ it was found, not proof that it is still here.
2784
2596
  */
2785
2597
  const folded = address ? foldOf(childChanges, address, manifest) : {};
2786
2598
  if (movedBy && folded[movedBy] && folded[movedBy].value !== childFilter.parent)
2787
2599
  return [];
2600
+ if (address && movedIn.has(address) && folded[movedBy]?.value !== childFilter.parent)
2601
+ return [];
2788
2602
  if (records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals)
2789
2603
  return [];
2790
2604
  if (address && manifest.records && archiveOf(childChanges, address, records))
@@ -2841,8 +2655,21 @@ function childChangeFilters(plan, answered) {
2841
2655
  if (!answered.some((e) => e.kind === pointer.kind && e.pubkey === pointer.pubkey && tagValue(e, 'd') === pointer.identifier)) {
2842
2656
  return [];
2843
2657
  }
2844
- const addresses = [...new Set(childRootsOf(answered, childFilter).flatMap((e) => childAddressOf(e) ?? []))];
2845
- return addresses.length ? archiveFilters(addresses, [records], CHILD_CHANGE_GROUP) : [];
2658
+ const created = childRootsOf(answered, childFilter).flatMap((e) => childAddressOf(e) ?? []);
2659
+ // Moved in (FOL-45): found by their moves, so their roots are not in the answer
2660
+ // yet. They are read here with the changes, in the same POST, and folded the same.
2661
+ const movedIn = movedInAddressesOf(answered, childFilter, records).filter((address) => !created.includes(address));
2662
+ const addresses = [...new Set([...created, ...movedIn])];
2663
+ const roots = new Map();
2664
+ for (const address of movedIn) {
2665
+ // A `d` may itself hold ':' — everything after the pubkey is the identifier.
2666
+ const [, pubkey, ...rest] = address.split(':');
2667
+ roots.set(pubkey, [...(roots.get(pubkey) ?? []), rest.join(':')]);
2668
+ }
2669
+ return [
2670
+ ...(addresses.length ? archiveFilters(addresses, [records], CHILD_CHANGE_GROUP) : []),
2671
+ ...[...roots].map(([pubkey, identifiers]) => ({ kinds: [childFilter.kind], authors: [pubkey], '#d': identifiers, limit: identifiers.length })),
2672
+ ];
2846
2673
  }
2847
2674
  /** Addresses per filter when reading a card's children's changes — see {@link childChangeFilters}. */
2848
2675
  const CHILD_CHANGE_GROUP = 10;
@@ -3797,6 +3624,16 @@ function buildOneActionEvent(args) {
3797
3624
  if (records.order?.includes('ts'))
3798
3625
  tags.push(['ts', String(args.createdAtMs)]);
3799
3626
  tags.push(...files);
3627
+ // A move also names its new parent where a relay indexes it (SPEC §7.2, FOL-45):
3628
+ // `value` is not indexed, so without this no read by the new parent finds a
3629
+ // child moved in. Only for the field the kind declares as `movedBy`, and only
3630
+ // for an address of the declaring kind (any kind for a bare file) — a move to
3631
+ // the top names no parent, and a value of another kind names none either.
3632
+ const moved = declared.emits.field ? movedByOf(manifest, kind) : undefined;
3633
+ const parentAddress = moved?.parentKind === undefined ? /^\d+:[0-9a-f]{64}:/ : new RegExp(`^${moved.parentKind}:[0-9a-f]{64}:`);
3634
+ if (moved?.field === declared.emits.field && typeof value === 'string' && parentAddress.test(value)) {
3635
+ tags.push([MOVED_TO_TAG, value]);
3636
+ }
3800
3637
  return {
3801
3638
  pubkey: args.pubkey,
3802
3639
  created_at: Math.floor(args.createdAtMs / 1000),