@estiva-app/interop 0.96.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/CHANGELOG.md +31 -0
- package/dist/activity.d.ts +7 -1
- package/dist/activity.d.ts.map +1 -1
- package/dist/activity.js +16 -3
- package/dist/activity.js.map +1 -1
- package/dist/projection.d.ts +12 -0
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +64 -2
- package/dist/projection.js.map +1 -1
- package/package.json +1 -1
- package/src/activity.ts +20 -4
- package/src/projection.ts +71 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@estiva-app/interop",
|
|
3
|
-
"version": "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/activity.ts
CHANGED
|
@@ -93,7 +93,7 @@ export interface TopicActivity {
|
|
|
93
93
|
/** Each topic's team, as asked. */
|
|
94
94
|
folders: Map<string, string | null>
|
|
95
95
|
/**
|
|
96
|
-
* Each file a topic lists directly
|
|
96
|
+
* Each file a topic lists directly, and each file one level under one (ACT-2), by address: the
|
|
97
97
|
* comments about it (`kind:1111`, on it as the fold reads it, never by a
|
|
98
98
|
* crossed tag) that the read already fetched to time its topics, oldest
|
|
99
99
|
* first. The read asks nothing more for them, so a file holds its newest
|
|
@@ -101,6 +101,12 @@ export interface TopicActivity {
|
|
|
101
101
|
* appends each new one in place (TOPI-3).
|
|
102
102
|
*/
|
|
103
103
|
comments: Map<string, SignedEvent[]>
|
|
104
|
+
/**
|
|
105
|
+
* Each file found under a listed one, by address: the file it sits directly under (its one `a`, §6.7), so a
|
|
106
|
+
* reader tells a listed project's issue from a page two levels down (ACT-2: what names you counts one level
|
|
107
|
+
* under a listed file). {@link watchTopicActivity} adds each new one in place.
|
|
108
|
+
*/
|
|
109
|
+
parents: Map<string, string>
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
/** {@link ActivityTopic}s from {@link readTopics}' answer: each topic with its resolved list. */
|
|
@@ -292,13 +298,14 @@ export async function readTopicActivity(
|
|
|
292
298
|
const files = new Map<string, ActivityFile[]>()
|
|
293
299
|
const folders = new Map<string, string | null>()
|
|
294
300
|
const comments = new Map<string, SignedEvent[]>()
|
|
301
|
+
const parents = new Map<string, string>()
|
|
295
302
|
for (const topic of topics) {
|
|
296
303
|
at.set(topic.address, topic.createdAt ? capped(topic.createdAt) : 0)
|
|
297
304
|
files.set(topic.address, [...topic.files])
|
|
298
305
|
folders.set(topic.address, topic.folder ?? null)
|
|
299
306
|
for (const file of topic.files) if (!comments.has(file.address)) comments.set(file.address, [])
|
|
300
307
|
}
|
|
301
|
-
if (topics.length === 0) return { at, files, folders, comments }
|
|
308
|
+
if (topics.length === 0) return { at, files, folders, comments, parents }
|
|
302
309
|
const bump = (topic: string, when: number) => {
|
|
303
310
|
if (capped(when) > (at.get(topic) ?? 0)) at.set(topic, capped(when))
|
|
304
311
|
}
|
|
@@ -352,6 +359,7 @@ export async function readTopicActivity(
|
|
|
352
359
|
const found = under.heard(events)
|
|
353
360
|
// Parents come before their children in a level, so a page two levels down inherits its topics.
|
|
354
361
|
for (const { parent, file, at: created } of found) {
|
|
362
|
+
parents.set(file.address, parent)
|
|
355
363
|
for (const topic of topicsOf.get(parent) ?? []) {
|
|
356
364
|
if (!topicsOf.get(file.address)?.has(topic)) files.get(topic)!.push(file)
|
|
357
365
|
onTopic(file.address, topic)
|
|
@@ -369,12 +377,15 @@ export async function readTopicActivity(
|
|
|
369
377
|
if (fresh.length === 0) continue
|
|
370
378
|
for (const topic of fresh) done.add(topic)
|
|
371
379
|
readFor.set(file, done)
|
|
372
|
-
|
|
380
|
+
// One level under a listed file, its comments are kept too: what names the reader there lights the topic (ACT-2).
|
|
381
|
+
const kept = listed.has(parents.get(file) ?? '') && !listed.has(file)
|
|
382
|
+
if (kept && !comments.has(file)) comments.set(file, [])
|
|
383
|
+
then.push(...fileProbes(target, fresh, kept))
|
|
373
384
|
}
|
|
374
385
|
open.push(...opened(then))
|
|
375
386
|
}
|
|
376
387
|
for (const list of comments.values()) list.sort(oldestFirst)
|
|
377
|
-
return { at, files, folders, comments }
|
|
388
|
+
return { at, files, folders, comments, parents }
|
|
378
389
|
|
|
379
390
|
/**
|
|
380
391
|
* Per probe, the newest event that counts for one of its topics, from the
|
|
@@ -554,9 +565,14 @@ export function watchTopicActivity(
|
|
|
554
565
|
const parent = d && first && byAddress.has(first) && CHILD_KINDS[kindOf(first) ?? -1]?.includes(event.kind) ? first : undefined
|
|
555
566
|
if (parent && d) {
|
|
556
567
|
const file = { address: `${event.kind}:${event.pubkey}:${d}`, folder: folderOf(event) }
|
|
568
|
+
// `??=`: an activity built before `parents` existed (an older read, a stub) is given one rather than thrown on.
|
|
569
|
+
const parents = (activity.parents ??= new Map())
|
|
557
570
|
const fresh = !byAddress.has(file.address)
|
|
558
571
|
const listen = fresh && (children.get(parent) ?? 0) < MAX_CHILDREN
|
|
559
572
|
if (listen) children.set(parent, (children.get(parent) ?? 0) + 1)
|
|
573
|
+
if (listen && !parents.has(file.address)) parents.set(file.address, parent)
|
|
574
|
+
// A new file one level under a listed one: its comments are kept from now on, as the read keeps them.
|
|
575
|
+
if (listen && activity.comments.has(parent) && !parents.has(parent) && !activity.comments.has(file.address)) activity.comments.set(file.address, [])
|
|
560
576
|
for (const topic of byAddress.get(parent)!) {
|
|
561
577
|
if (listen) {
|
|
562
578
|
activity.files.get(topic)?.push(file)
|
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
|
|
3946
|
-
const
|
|
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
|
}
|