@estiva-app/interop 0.97.0 → 0.98.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.97.0",
3
+ "version": "0.98.0",
4
4
  "description": "Render and act on another app's objects from its published NIP-89 manifest. The owner defines the projection; the consumer decides how it looks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/projection.ts CHANGED
@@ -3357,6 +3357,12 @@ export interface ForeignObject {
3357
3357
  * `created_at`. Absent on an unreachable stub, which has no root to read.
3358
3358
  */
3359
3359
  changedAt?: number
3360
+ /**
3361
+ * Who made the change {@link changedAt} names, as a pubkey: the author of the
3362
+ * newest folded field's winning change, or the root's when no change is newer
3363
+ * than it. For "edited by … , 2h ago" on a row. Absent where `changedAt` is.
3364
+ */
3365
+ changedBy?: string
3360
3366
  /**
3361
3367
  * Each changed field's current change id: the `prev` a change to that field
3362
3368
  * names (SPEC §6.2). A field never changed is absent, and its first change
@@ -3391,6 +3397,13 @@ function changedAtOf(root: SignedEvent, folded: Record<string, { at?: number }>)
3391
3397
  return Math.max(root.created_at, ...Object.values(folded).map((f) => f.at ?? 0))
3392
3398
  }
3393
3399
 
3400
+ /** Who made the newest of a root and its folded fields' changes: the root's author on a tie, as nothing changed it after. */
3401
+ function changedByOf(root: SignedEvent, folded: Record<string, { at?: number; by?: string }>): string {
3402
+ let newest = { at: root.created_at, by: root.pubkey }
3403
+ for (const f of Object.values(folded)) if (f.by && (f.at ?? 0) > newest.at) newest = { at: f.at!, by: f.by }
3404
+ return newest.by
3405
+ }
3406
+
3394
3407
 
3395
3408
  /**
3396
3409
  * One resolved object, from its root event and the changes that have landed on
@@ -3442,6 +3455,7 @@ function buildObject(args: {
3442
3455
  viaRecommendation: args.viaRecommendation,
3443
3456
  ...(args.archived ? { archived: args.archived } : {}),
3444
3457
  changedAt: changedAtOf(root, folded),
3458
+ changedBy: changedByOf(root, folded),
3445
3459
  ...(Object.keys(prevs).length ? { prevs } : {}),
3446
3460
  ...(deleted?.value === 'true' && deleted.by ? { deleted: { by: deleted.by, at: deleted.at ?? root.created_at } } : {}),
3447
3461
  }
@@ -3912,8 +3926,13 @@ export async function resolveForeignRoots(
3912
3926
  * deleted file is absent. Other kinds are read as before.
3913
3927
  * `parentsListRead`, with `archivedWith`, reads only the parents walked for an archive by the list read (with
3914
3928
  * `listRead`'s fallback for one it leaves out); the files asked for are still read in full, their text included.
3929
+ * `children`, with `listRead`, also gives each listed file whose app declares a list of files the files under it,
3930
+ * one level down, folded as {@link resolveForeignObjects}' are: by the same list read, so a child's text is never
3931
+ * read, and a child moved in (§7.2) found by its move. Archived children are dropped, and a child carries no
3932
+ * actions. A list of other kinds (a topic's messages) is not read, and its `children` stay absent. Without
3933
+ * `listRead` it asks nothing more.
3915
3934
  */
3916
- options: Pick<ArchiveReadOptions, 'archivedWith'> & { listRead?: boolean; parentsListRead?: boolean } = {},
3935
+ options: Pick<ArchiveReadOptions, 'archivedWith'> & { listRead?: boolean; parentsListRead?: boolean; children?: boolean } = {},
3917
3936
  ): Promise<ForeignObject[]> {
3918
3937
  const pointers = new Map<string, AddressPointer>()
3919
3938
  for (const reference of references) {
@@ -3940,10 +3959,43 @@ export async function resolveForeignRoots(
3940
3959
  filters.push(root, { kinds: [...changeKinds], '#a': [address], limit: 500 })
3941
3960
  }
3942
3961
  const listed = listReadFilters(listedPointers)
3962
+ // The children's first records ride in the list read (TOPI-8). The moves naming a parent are asked on their own:
3963
+ // every move that ever named it comes back, stale ones too, so they only say which children to read and are never
3964
+ // folded — a child's place is its list read's current change, in the relay's order (§6.13).
3965
+ const lists = options.children ? childListsOf(listedPointers, manifests) : []
3966
+ const moves: Record<string, unknown>[] = []
3967
+ for (const { childFilter } of lists) {
3968
+ listed.push({ ...childFilter.filter, fields: 'newest' })
3969
+ if (childFilter.movedIn) moves.push(childFilter.movedIn)
3970
+ }
3943
3971
  if (filters.length === 0 && listed.length === 0) return []
3944
3972
  // A relay refuses a request mixing list reads with plain ones, so each goes on its own.
3945
- const events = (await Promise.all([queryChunked(filters, query), queryChunked(listed, query)])).flat()
3946
- const projected = projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)))
3973
+ const [plain, answered, named] = await Promise.all([queryChunked(filters, query), queryChunked(listed, query), queryChunked(moves, query)])
3974
+ const events = [...plain, ...answered]
3975
+ // A child moved in is named only by its move, so its own first record is read once those are back.
3976
+ const held = new Set(answered.flatMap((e) => childAddressOf(e) ?? []))
3977
+ const movedIn = lists.flatMap(({ childFilter, resolved }) =>
3978
+ movedInAddressesOf(named, childFilter, resolved.manifest.records).filter((address) => !held.has(address)),
3979
+ )
3980
+ const moved = movedIn.flatMap((address) => addressPointerOf(address) ?? [])
3981
+ const childEvents = [...answered, ...(moved.length ? await queryChunked(listReadFilters(moved), query) : [])]
3982
+ const all = projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)))
3983
+ // The children's own first records came back too; only the files asked for are answers.
3984
+ const projected = lists.length ? all.filter((object) => !!object.address && pointers.has(object.address)) : all
3985
+ for (const { childFilter, resolved, address } of lists) {
3986
+ const parent = projected.find((object) => object.address === address)
3987
+ if (!parent) continue
3988
+ const children = childrenFrom({
3989
+ // The moves only find who moved in; each child folds from its list read alone.
3990
+ events: [...answered, ...named],
3991
+ childFilter,
3992
+ manifest: resolved.manifest,
3993
+ webTemplate: resolved.webTemplate,
3994
+ viaRecommendation: resolved.viaRecommendation,
3995
+ childChanges: childEvents,
3996
+ })
3997
+ if (children) parent.children = textUnread(children)
3998
+ }
3947
3999
  const objects = options.listRead ? textUnread(projected) : projected
3948
4000
  if (options.archivedWith) {
3949
4001
  const manifestOf = (object: ForeignObject) => {
@@ -3960,6 +4012,21 @@ export async function resolveForeignRoots(
3960
4012
  return objects
3961
4013
  }
3962
4014
 
4015
+ /** The child list each listed file's app declares, for {@link resolveForeignRoots}' `children`: files of the list read only. */
4016
+ function childListsOf(
4017
+ pointers: readonly AddressPointer[],
4018
+ manifests: Map<string, ResolvedManifest>,
4019
+ ): { address: string; resolved: ResolvedManifest; childFilter: NonNullable<ReturnType<typeof childFilterFor>> }[] {
4020
+ return pointers.flatMap((pointer) => {
4021
+ const resolved = manifests.get(manifestKeyOf(pointer))
4022
+ const projection = resolved?.manifest.projections?.[String(pointer.kind)]
4023
+ if (!resolved || !projection) return []
4024
+ const childFilter = childFilterFor({ projection, manifest: resolved.manifest, pointer, depth: 0 })
4025
+ if (!childFilter || !FILE_KINDS.includes(childFilter.kind)) return []
4026
+ return [{ address: `${pointer.kind}:${pointer.pubkey}:${pointer.identifier}`, resolved, childFilter }]
4027
+ })
4028
+ }
4029
+
3963
4030
  /**
3964
4031
  * Topics as their roots and changes draw them, through the bare file's
3965
4032
  * built-in manifest (§6.7) — what {@link resolveForeignRoots} draws a topic
@@ -5394,6 +5461,7 @@ function buildChildObject(args: {
5394
5461
  actions: addressable && !nevent && ownsKind ? actionsOfFile(manifest, projection, root, folded) : [],
5395
5462
  viaRecommendation,
5396
5463
  changedAt: changedAtOf(root, folded),
5464
+ changedBy: changedByOf(root, folded),
5397
5465
  ...(archived ? { archived } : {}),
5398
5466
  }
5399
5467
  }