@estiva-app/interop 0.95.0 → 0.96.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.
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Your topics, lean: only the topics the viewer is in, newest activity first —
3
+ * what a sidebar draws (ALO-17), where {@link readTopics} reads every topic the
4
+ * viewer can open, the viewer's whole part in each and their text.
5
+ *
6
+ * The same answer as {@link readTopics}, the filter every app applies (in it,
7
+ * not archived, its own or with a parent, not muted, not a space) and
8
+ * {@link readTopicActivity} over what is left, for fewer requests and bytes:
9
+ *
10
+ * 1. every topic's root, and the viewer's mute list, in one request;
11
+ * 2. the changes naming the viewer on any topic and the team lists naming
12
+ * them (one request), beside every topic's current fields by the list read
13
+ * (another: a relay refuses a list read mixed with other filters) — each
14
+ * topic's title, archive and parent come from that answer, and only the
15
+ * topics the viewer is in are read further;
16
+ * 3. their listed ids, what the list read skipped and the manifests of their
17
+ * parents, in one request;
18
+ * 4. their parents, a level a request by the list read, to see whether one is
19
+ * archived, while their activity is read.
20
+ *
21
+ * Without the list read (the relay does not advertise it), round 2 reads each
22
+ * topic's changes plain, as {@link readTopics} does. A topic the list read
23
+ * does not answer (deleted) is read plain, as a parent is.
24
+ */
25
+ import { mutedFilter, parseMutedBlob, MUTED_D_TAG } from '@estiva-app/conversation';
26
+ import { FILE_KINDS } from '@estiva-app/protocol';
27
+ import { addressPointerOf, createProjectionCache, isArchived, markTopicsArchivedWith, projectTopics, seedManifests, } from './projection.js';
28
+ import { answeredBy, changesNamingFilter, eventsByTopic, listReadPlan, listsFrom, newestChanges, readAll, readTopicOf, resolveIds, spacesOf, teamListFilters, topicListFilter, topicRoots, } from './topics.js';
29
+ import { activityTopics, byActivity, readTopicActivity } from './activity.js';
30
+ const KIND_TOPIC = 30840;
31
+ const KIND_CHANGE = 1851;
32
+ const KIND_APP_DATA = 30078;
33
+ /** As {@link resolveForeignRoots} reads a file's changes when it reads them plain. */
34
+ const PLAIN_CHANGES_LIMIT = 500;
35
+ /**
36
+ * The viewer's topics, newest activity first, in about ten requests on a
37
+ * workspace like Miky's (53 topics, 12 his): see the module's rounds.
38
+ *
39
+ * `listRead`, where the relay advertises the list read (NIP-11
40
+ * `file_list.newest`): the topics and their parents are read as their
41
+ * current fields (§6.13); `readTopics(query, me, { listRead, parentsListRead:
42
+ * listRead })` reads the same. `decrypt` opens the mute list (NIP-44 to
43
+ * self); `cache` keeps the parents' manifests between reads.
44
+ */
45
+ export async function readYourTopics(query, me, options) {
46
+ const listRead = options.listRead ?? false;
47
+ const cache = options.cache ?? createProjectionCache();
48
+ // 1. Every topic's root, and the mute list.
49
+ const first = gathered(query);
50
+ const rootsRead = readAll(first.query, [{ kinds: [KIND_TOPIC] }]);
51
+ const mutedOpened = first.query([mutedFilter(me)]).then((events) => mutedOf(events, me, options.decrypt));
52
+ // Its failure is the roots' too, and said by them.
53
+ mutedOpened.catch(() => { });
54
+ first.send();
55
+ const roots = topicRoots(await rootsRead);
56
+ const addresses = [...roots.keys()];
57
+ if (addresses.length === 0)
58
+ return { me, topics: [], at: new Map(), muted: await mutedOpened };
59
+ // 2. How the viewer is in each, which are spaces, and each topic's fields.
60
+ const second = gathered(query);
61
+ const partRead = readAll(second.query, [changesNamingFilter(me, addresses)]);
62
+ const statesRead = readAll(second.query, teamListFilters(roots));
63
+ // Without the list read: each topic's changes as `resolveForeignRoots` and `topicLists` read them.
64
+ const objectChangesRead = listRead ? Promise.resolve([]) : second.query(addresses.map(plainChangesFilter));
65
+ const listChangesRead = listRead ? Promise.resolve([]) : readAll(second.query, addresses.map(topicListFilter));
66
+ second.send();
67
+ const [part, states, listed, objectChanges, listChanges, muted] = await Promise.all([
68
+ partRead,
69
+ statesRead,
70
+ listRead ? newestChanges(query, addresses) : Promise.resolve([]),
71
+ objectChangesRead,
72
+ listChangesRead,
73
+ mutedOpened,
74
+ ]);
75
+ const spaces = spacesOf(states, addresses);
76
+ const byTopic = eventsByTopic(part);
77
+ const facts = { roots, byTopic, states, spaces };
78
+ const mine = (object) => {
79
+ const topic = readTopicOf(object, me, facts);
80
+ return topic && topic.yours && (options.spaces || !topic.space) && !muted?.has(topic.address) ? topic : undefined;
81
+ };
82
+ // A topic the list read did not answer (deleted) is read plain below, as a parent is.
83
+ const answered = new Set(topicRoots(listed).keys());
84
+ const byList = listRead ? addresses.filter((address) => answered.has(address)) : addresses;
85
+ const objects = projectTopics([...byList.map((address) => roots.get(address)), ...(listRead ? listed : objectChanges)], listRead);
86
+ if (listRead) {
87
+ const unanswered = addresses.filter((address) => !answered.has(address));
88
+ // Asked about only when one would be the viewer's, by its root alone: whether they are in it does not depend on its fields.
89
+ const maybe = projectTopics(unanswered.map((address) => roots.get(address)), false).flatMap((object) => (mine(object) ? [object.address] : []));
90
+ if (maybe.length > 0)
91
+ objects.push(...projectTopics([...maybe.map((address) => roots.get(address)), ...(await query(maybe.map(plainChangesFilter)))], false));
92
+ }
93
+ const candidates = [];
94
+ for (const object of objects) {
95
+ const topic = mine(object);
96
+ if (topic && !isArchived(topic.object))
97
+ candidates.push(topic);
98
+ }
99
+ if (candidates.length === 0)
100
+ return { me, topics: [], at: new Map(), muted };
101
+ // 3. Their listed ids, what the list read skipped, and their parents' manifests.
102
+ const asked = candidates.map((topic) => topic.address);
103
+ const listEvents = listRead ? listed : listChanges;
104
+ const plan = listReadPlan(asked, roots, listEvents, listRead);
105
+ const parents = candidates.flatMap((topic) => {
106
+ const parent = topic.object.parentRef ? addressPointerOf(topic.object.parentRef) : null;
107
+ return parent ? [parent] : [];
108
+ });
109
+ const third = gathered(query);
110
+ const resolvedRead = plan.ids.length ? resolveIds(third.query, plan.ids, options.fileKinds) : Promise.resolve(new Map());
111
+ const againRead = readAll(third.query, plan.again);
112
+ const seeded = seedManifests(parents, third.query, cache);
113
+ third.send();
114
+ const [resolved, again] = await Promise.all([resolvedRead, againRead, seeded]);
115
+ const lists = listsFrom(asked, roots, [...listEvents, ...again], resolved);
116
+ const topics = candidates.map((topic) => ({ ...topic, listed: lists.get(topic.address) ?? [] }));
117
+ // 4. Their parents, while their activity is read: one archived with a parent is left out after.
118
+ const [activity] = await Promise.all([
119
+ readTopicActivity(query, activityTopics(topics), { nowSeconds: options.nowSeconds }),
120
+ markTopicsArchivedWith(topics.map((topic) => topic.object), query, cache, listRead ? FILE_KINDS : []),
121
+ ]);
122
+ const open = topics.filter((topic) => !isArchived(topic.object));
123
+ const newest = byActivity(activity.at);
124
+ open.sort((a, b) => newest(a.address, b.address));
125
+ return { me, topics: open, at: new Map(open.map((topic) => [topic.address, activity.at.get(topic.address) ?? 0])), muted };
126
+ }
127
+ /** A topic's changes as {@link resolveForeignRoots} reads them plain: the newest 500 naming it by `a`. */
128
+ function plainChangesFilter(address) {
129
+ return { kinds: [KIND_CHANGE], '#a': [address], limit: PLAIN_CHANGES_LIMIT };
130
+ }
131
+ /** The viewer's muted keys from a read of their mute list: none without one, `undefined` when it does not open. */
132
+ async function mutedOf(events, me, decrypt) {
133
+ const list = events
134
+ .filter((e) => e.pubkey === me && e.kind === KIND_APP_DATA && e.tags.some((t) => t[0] === 'd' && t[1] === MUTED_D_TAG))
135
+ .sort((a, b) => b.created_at - a.created_at)[0];
136
+ if (!list)
137
+ return new Set();
138
+ const parsed = parseMutedBlob((await decrypt(list.content).catch(() => undefined)) ?? '');
139
+ return parsed ? new Set(parsed.keys) : undefined;
140
+ }
141
+ /**
142
+ * A query that holds every request made through it until {@link send}, then
143
+ * asks them all as one request and hands each caller what its own filters
144
+ * would have been answered alone ({@link answeredBy}). A request made after
145
+ * `send` (a later page), and a list read, which a relay takes only alone, go
146
+ * straight through.
147
+ */
148
+ function gathered(query) {
149
+ let held = [];
150
+ return {
151
+ query: (filters) => {
152
+ if (!held || filters.some((filter) => 'fields' in filter))
153
+ return query(filters);
154
+ const asks = held;
155
+ return new Promise((resolve, reject) => asks.push({ filters, resolve, reject }));
156
+ },
157
+ send() {
158
+ const asks = held ?? [];
159
+ held = null;
160
+ if (asks.length === 0)
161
+ return;
162
+ query(asks.flatMap((ask) => ask.filters)).then((events) => {
163
+ for (const ask of asks) {
164
+ const page = new Map();
165
+ for (const filter of ask.filters)
166
+ for (const event of answeredBy(events, filter))
167
+ page.set(event.id, event);
168
+ ask.resolve([...page.values()]);
169
+ }
170
+ }, (error) => {
171
+ for (const ask of asks)
172
+ ask.reject(error);
173
+ });
174
+ },
175
+ };
176
+ }
177
+ //# sourceMappingURL=yourTopics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yourTopics.js","sourceRoot":"","sources":["../src/yourTopics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AACnF,OAAO,EAAE,UAAU,EAAoB,MAAM,sBAAsB,CAAA;AACnE,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,UAAU,EACV,sBAAsB,EACtB,aAAa,EACb,aAAa,GAId,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,SAAS,EACT,aAAa,EACb,OAAO,EACP,WAAW,EACX,UAAU,EACV,QAAQ,EACR,eAAe,EACf,eAAe,EACf,UAAU,GAGX,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAE7E,MAAM,UAAU,GAAG,KAAK,CAAA;AACxB,MAAM,WAAW,GAAG,IAAI,CAAA;AACxB,MAAM,aAAa,GAAG,KAAK,CAAA;AAC3B,sFAAsF;AACtF,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAmB/B;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,KAAc,EACd,EAAU,EACV,OAKC;IAED,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAA;IAC1C,MA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AM;YAC7B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAC5C,CAAC,MAAM,EAAE,EAAE;gBACT,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;oBACvB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAuB,CAAA;oBAC3C,KAAK,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO;wBAAE,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC;4BAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAA;oBAC3G,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;gBACjC,CAAC;YACH,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;gBACjB,KAAK,MAAM,GAAG,IAAI,IAAI;oBAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC,CACF,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.95.0",
3
+ "version": "0.96.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
@@ -33,12 +33,14 @@
33
33
  * The rule takes any set of topics, so an Explore page orders every topic the
34
34
  * reader can open with the same read ({@link readTopicActivity}), and a live
35
35
  * app keeps it current with {@link watchTopicActivity}.
36
- * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/each-file-on-a-topic-keeps-its-own-place-and-look.md
36
+ *
37
+ * No decision record states this order of topics: the rule lives here, and
38
+ * writing it up as SPEC §6.14 text is on the project's Later list.
37
39
  */
38
- import { fileIdOf, MAX_FILTERS_PER_QUERY, type LiveRelay, type SignedEvent } from '@estiva-app/protocol'
40
+ import { fileIdOf, type LiveRelay, type SignedEvent } from '@estiva-app/protocol'
39
41
  import { isAbout, KIND_COMMENT } from '@estiva-app/conversation'
40
42
  import { folderOf, KIND_MESSAGE, type QueryFn } from './projection.js'
41
- import { answers, readAll, type ReadTopic } from './topics.js'
43
+ import { answeredByEach, answers, firstPages, nextPages, type ReadTopic } from './topics.js'
42
44
  import { LISTED_WATCH_REBUILD_MS, MAX_FILTERS_PER_REQ, openWhenKnown } from './listedConversations.js'
43
45
 
44
46
  const KIND_CHANGE = 1851
@@ -175,62 +177,76 @@ function isOn(event: SignedEvent, address: string, folder: string | null): boole
175
177
  return tagValues(event, 'A')[0] === address
176
178
  }
177
179
 
178
- async function queryChunked(query: QueryFn, filters: readonly Record<string, unknown>[]): Promise<SignedEvent[]> {
179
- const runs: Promise<SignedEvent[]>[] = []
180
- for (let i = 0; i < filters.length; i += MAX_FILTERS_PER_QUERY) runs.push(query(filters.slice(i, i + MAX_FILTERS_PER_QUERY)))
181
- return (await Promise.all(runs)).flat()
182
- }
183
-
184
180
  /**
185
- * Every file under the given ones, any depth: a project's issues, a
186
- * document's pages, each by its root's `a` (§6.7). One request per level.
187
- * Each found file with its root's team and its root's time.
181
+ * The files under the given ones, a level at a time: a project's issues, a
182
+ * document's pages, each by its root's `a` (§6.7), to {@link MAX_DEPTH}
183
+ * levels. The caller sends {@link FilesUnder.asking} in its request (every
184
+ * one, however many: paged past the relay's cap rather than cut at it) and
185
+ * hands the answer to {@link FilesUnder.heard}, which says the files a level
186
+ * held once its last page is in, each with its root's team and time.
188
187
  */
189
- async function filesUnder(
190
- query: QueryFn,
191
- parents: readonly string[],
192
- ): Promise<{ parent: string; file: ActivityFile; at: number }[]> {
193
- const out: { parent: string; file: ActivityFile; at: number }[] = []
188
+ interface FilesUnder {
189
+ asking: (Record<string, unknown> & { limit: number })[]
190
+ heard(events: readonly SignedEvent[]): { parent: string; file: ActivityFile; at: number }[]
191
+ }
192
+
193
+ function filesUnder(parents: readonly string[]): FilesUnder {
194
194
  // Each file under one is answered once per parent, even when it is listed itself (on another topic, say);
195
195
  // each parent is walked once.
196
196
  const found = new Set<string>()
197
197
  let level = parents.filter((address) => CHILD_KINDS[kindOf(address) ?? -1])
198
198
  const walked = new Set(level)
199
- for (let depth = 0; depth < MAX_DEPTH && level.length > 0; depth++) {
199
+ let depth = 0
200
+ let roots = new Map<string, SignedEvent>()
201
+ const ask = (): (Record<string, unknown> & { limit: number })[] => {
202
+ if (depth >= MAX_DEPTH || level.length === 0) return []
200
203
  const kinds = [...new Set(level.flatMap((address) => CHILD_KINDS[kindOf(address)!]!))]
201
204
  const filters: Record<string, unknown>[] = []
202
205
  for (let i = 0; i < level.length; i += VALUES_PER_FILTER) filters.push({ kinds, '#a': level.slice(i, i + VALUES_PER_FILTER) })
203
- // Every one, however many: paged past the relay's cap rather than cut at it.
204
- const roots = await readAll(query, filters)
205
- const asked = new Set(level)
206
- const next: string[] = []
207
- // A replaceable root the relay still holds twice is one file: its newest names its team.
208
- const newest = new Map<string, { root: SignedEvent; parent: string }>()
209
- for (const root of roots) {
210
- const d = tagValues(root, 'd')[0]
211
- // A root's parent is its one `a` (§6.7): a later one is no parent.
212
- const parent = tagValues(root, 'a')[0]
213
- if (!d || !parent || !asked.has(parent) || !(CHILD_KINDS[kindOf(parent)!] ?? []).includes(root.kind)) continue
214
- const address = `${root.kind}:${root.pubkey}:${d}`
215
- const held = newest.get(address)
216
- if (!held || root.created_at > held.root.created_at) newest.set(address, { root, parent })
217
- }
218
- const perParent = new Map<string, number>()
219
- for (const [address, { root, parent }] of [...newest].sort((a, b) => b[1].root.created_at - a[1].root.created_at)) {
220
- const count = perParent.get(parent) ?? 0
221
- if (count >= MAX_CHILDREN) continue
222
- perParent.set(parent, count + 1)
223
- if (found.has(`${parent} ${address}`)) continue
224
- found.add(`${parent} ${address}`)
225
- out.push({ parent, file: { address, folder: folderOf(root) }, at: root.created_at })
226
- if (CHILD_KINDS[root.kind] && !walked.has(address)) {
227
- walked.add(address)
228
- next.push(address)
206
+ return firstPages(filters)
207
+ }
208
+ const walk: FilesUnder = {
209
+ asking: ask(),
210
+ heard(events) {
211
+ if (walk.asking.length === 0) return []
212
+ for (const event of events) if (walk.asking.some((filter) => answers(event, filter))) roots.set(event.id, event)
213
+ walk.asking = nextPages(walk.asking, events)
214
+ if (walk.asking.length > 0) return []
215
+ const out: { parent: string; file: ActivityFile; at: number }[] = []
216
+ const asked = new Set(level)
217
+ const next: string[] = []
218
+ // A replaceable root the relay still holds twice is one file: its newest names its team.
219
+ const newest = new Map<string, { root: SignedEvent; parent: string }>()
220
+ for (const root of roots.values()) {
221
+ const d = tagValues(root, 'd')[0]
222
+ // A root's parent is its one `a` (§6.7): a later one is no parent.
223
+ const parent = tagValues(root, 'a')[0]
224
+ if (!d || !parent || !asked.has(parent) || !(CHILD_KINDS[kindOf(parent)!] ?? []).includes(root.kind)) continue
225
+ const address = `${root.kind}:${root.pubkey}:${d}`
226
+ const held = newest.get(address)
227
+ if (!held || root.created_at > held.root.created_at) newest.set(address, { root, parent })
229
228
  }
230
- }
231
- level = next
229
+ const perParent = new Map<string, number>()
230
+ for (const [address, { root, parent }] of [...newest].sort((a, b) => b[1].root.created_at - a[1].root.created_at)) {
231
+ const count = perParent.get(parent) ?? 0
232
+ if (count >= MAX_CHILDREN) continue
233
+ perParent.set(parent, count + 1)
234
+ if (found.has(`${parent} ${address}`)) continue
235
+ found.add(`${parent} ${address}`)
236
+ out.push({ parent, file: { address, folder: folderOf(root) }, at: root.created_at })
237
+ if (CHILD_KINDS[root.kind] && !walked.has(address)) {
238
+ walked.add(address)
239
+ next.push(address)
240
+ }
241
+ }
242
+ level = next
243
+ depth++
244
+ roots = new Map()
245
+ walk.asking = ask()
246
+ return out
247
+ },
232
248
  }
233
- return out
249
+ return walk
234
250
  }
235
251
 
236
252
  /** One thing to find the newest counting event of: a filter, the file it is on, and the topics its answer is news for. */
@@ -246,12 +262,16 @@ interface Probe {
246
262
  * Each topic's newest activity (the rule above), for any set of topics the
247
263
  * reader holds — theirs, or every one they can open.
248
264
  *
249
- * Reads, whatever the number of topics: one request per level of files under
250
- * the listed ones (a project's issues, then each further level of pages),
251
- * then the newest few events of each topic's conversation and own changes and
252
- * of each file's changes and comments, every target one filter in the same
253
- * request (another per {@link MAX_FILTERS_PER_QUERY}). A target whose newest
254
- * events are all tidying is asked again from older ones, up to a few rounds.
265
+ * Reads, whatever the number of topics, one request a tick: the newest few
266
+ * events of each topic's conversation and own changes and of each listed
267
+ * file's changes and comments, every target one filter, beside the first
268
+ * level of files under the listed ones (a project's issues, a document's
269
+ * pages); then each tick the next level, the first page of each file the
270
+ * level before found, and every target still open, from older events. A
271
+ * target whose newest events are all tidying is asked again from older ones,
272
+ * up to a few pages. No filter names a team (`#h`), the only values a relay
273
+ * caps per request, so however many filters a tick holds it is one request:
274
+ * a topic list with one level of files under it is about four.
255
275
  *
256
276
  * Measured 9 Nov 2026 on production over the 47 topics the agent can read
257
277
  * (Miky is in 24 of them): about 1.9 MB in 7 requests, 1 s. Most of it is the
@@ -315,65 +335,84 @@ export async function readTopicActivity(
315
335
  }
316
336
  for (const [file, on] of listed) first.push(...fileProbes({ address: file, folder: folderOfFile.get(file) ?? null }, [...on], true))
317
337
 
318
- // The topics and their listed files are read while the files under them are found, and those as soon as they are.
319
- const deep = async () => {
320
- const under = await filesUnder(query, [...listed.keys()])
321
- // Parents come before their children in `under`, so a page two levels down inherits its topics.
322
- for (const { parent, file, at: created } of under) {
338
+ // One request a tick: the next level of files under the listed ones, the
339
+ // first page of each file found by the level before, and every probe still
340
+ // open, a page further back. No filter here names a team (`#h`), the only
341
+ // values a relay caps per request, so none is split across requests.
342
+ const under = filesUnder([...listed.keys()])
343
+ // Each file found under a listed one, as last found, and the topics it has been read for.
344
+ const asFound = new Map<string, ActivityFile>()
345
+ const readFor = new Map<string, Set<string>>()
346
+ type Open = { probe: Probe; filter: Record<string, unknown>; round: number }
347
+ const opened = (probes: readonly Probe[]): Open[] => probes.map((probe) => ({ probe, filter: { ...probe.filter, limit: FIRST_PAGE }, round: 0 }))
348
+ let open = opened(first)
349
+ while (under.asking.length > 0 || open.length > 0) {
350
+ const asked = [...under.asking, ...open.map((o) => o.filter)]
351
+ const events = await query(asked)
352
+ const found = under.heard(events)
353
+ // Parents come before their children in a level, so a page two levels down inherits its topics.
354
+ for (const { parent, file, at: created } of found) {
323
355
  for (const topic of topicsOf.get(parent) ?? []) {
324
356
  if (!topicsOf.get(file.address)?.has(topic)) files.get(topic)!.push(file)
325
357
  onTopic(file.address, topic)
326
358
  // A new issue filed in a listed project is news.
327
359
  bump(topic, created)
328
360
  }
361
+ asFound.set(file.address, file)
329
362
  }
363
+ open = stillOpen(open, events)
330
364
  const then: Probe[] = []
331
- const asFound = new Map(under.map((found) => [found.file.address, found.file]))
332
365
  for (const [file, target] of asFound) {
333
- // Only for the topics it was not already read for as a listed file.
334
- const fresh = [...(topicsOf.get(file) ?? [])].filter((topic) => !listed.get(file)?.has(topic))
335
- if (fresh.length > 0) then.push(...fileProbes(target, fresh))
366
+ // Only for the topics it was not already read for, as a listed file or found before.
367
+ const done = readFor.get(file) ?? new Set(listed.get(file))
368
+ const fresh = [...(topicsOf.get(file) ?? [])].filter((topic) => !done.has(topic))
369
+ if (fresh.length === 0) continue
370
+ for (const topic of fresh) done.add(topic)
371
+ readFor.set(file, done)
372
+ then.push(...fileProbes(target, fresh))
336
373
  }
337
- await newestOf(then)
374
+ open.push(...opened(then))
338
375
  }
339
- await Promise.all([deep(), newestOf(first)])
340
376
  for (const list of comments.values()) list.sort(oldestFirst)
341
377
  return { at, files, folders, comments }
342
378
 
343
- /** Per probe, the newest event that counts for one of its topics. Read in rounds; each round asks only what is still open. */
344
- async function newestOf(probes: readonly Probe[]): Promise<void> {
345
- let open = probes.map((probe) => ({ probe, filter: { ...probe.filter, limit: FIRST_PAGE } as Record<string, unknown> }))
346
- for (let round = 0; round < MAX_ROUNDS && open.length > 0; round++) {
347
- const events = await queryChunked(query, open.map((o) => o.filter))
348
- const still: typeof open = []
349
- for (const o of open) {
350
- const page = events.filter((event) => answers(event, o.filter)).sort((a, b) => b.created_at - a.created_at)
351
- if (o.probe.keepsComments) {
352
- const kept = comments.get(o.probe.target.address)
353
- for (const event of page) {
354
- if (!kept || event.kind !== KIND_COMMENT || !isOn(event, o.probe.target.address, o.probe.target.folder)) continue
355
- if (!kept.some((held) => held.id === event.id)) kept.push(event)
356
- }
357
- }
358
- const left = new Set(o.probe.topics)
379
+ /**
380
+ * Per probe, the newest event that counts for one of its topics, from the
381
+ * page this tick answered it: the probes still open, each a page further
382
+ * back, up to {@link MAX_ROUNDS} pages each.
383
+ */
384
+ function stillOpen(open: readonly Open[], events: readonly SignedEvent[]): Open[] {
385
+ const still: Open[] = []
386
+ // The answer indexed once a tick, not filtered and sorted once per probe: a listed project's issues make thousands.
387
+ const pageOf = answeredByEach(events)
388
+ for (const o of open) {
389
+ // Only what this filter alone would have been answered: the request holds every other probe's answer too.
390
+ const page = pageOf(o.filter)
391
+ if (o.probe.keepsComments) {
392
+ const kept = comments.get(o.probe.target.address)
359
393
  for (const event of page) {
360
- if (!isOn(event, o.probe.target.address, o.probe.target.folder)) continue
361
- for (const topic of [...left]) {
362
- if (!countsAsActivity(event, topic)) continue
363
- bump(topic, event.created_at)
364
- left.delete(topic)
365
- }
366
- if (left.size === 0) break
394
+ if (!kept || event.kind !== KIND_COMMENT || !isOn(event, o.probe.target.address, o.probe.target.folder)) continue
395
+ if (!kept.some((held) => held.id === event.id)) kept.push(event)
396
+ }
397
+ }
398
+ const left = new Set(o.probe.topics)
399
+ for (const event of page) {
400
+ if (!isOn(event, o.probe.target.address, o.probe.target.folder)) continue
401
+ for (const topic of [...left]) {
402
+ if (!countsAsActivity(event, topic)) continue
403
+ bump(topic, event.created_at)
404
+ left.delete(topic)
367
405
  }
368
- const limit = o.filter.limit as number
369
- if (left.size === 0 || page.length < limit) continue
370
- const until = page[page.length - 1]!.created_at
371
- // A second holding a whole page on its own: ask a bigger page of it rather than step past it.
372
- const same = until === (o.filter.until as number | undefined)
373
- still.push({ probe: { ...o.probe, topics: [...left] }, filter: { ...o.probe.filter, until, limit: same ? limit * 4 : limit * 2 } })
406
+ if (left.size === 0) break
374
407
  }
375
- open = still
408
+ const limit = o.filter.limit as number
409
+ if (left.size === 0 || page.length < limit || o.round + 1 >= MAX_ROUNDS) continue
410
+ const until = page[page.length - 1]!.created_at
411
+ // A second holding a whole page on its own: ask a bigger page of it rather than step past it.
412
+ const same = until === (o.filter.until as number | undefined)
413
+ still.push({ probe: { ...o.probe, topics: [...left] }, filter: { ...o.probe.filter, until, limit: same ? limit * 4 : limit * 2 }, round: o.round + 1 })
376
414
  }
415
+ return still
377
416
  }
378
417
  }
379
418
 
package/src/index.ts CHANGED
@@ -224,6 +224,13 @@ export {
224
224
  type ActivityWatch,
225
225
  } from './activity.js'
226
226
 
227
+ /**
228
+ * Your topics alone, lean: the topics the viewer is in, not archived, muted or
229
+ * a space, newest activity first — what a sidebar draws, in about ten
230
+ * requests where {@link readTopics} and {@link readTopicActivity} take twenty.
231
+ */
232
+ export { readYourTopics, type YourTopicsRead } from './yourTopics.js'
233
+
227
234
  /**
228
235
  * A topic's All: the conversations of every file on its list, read in one go
229
236
  * and kept live, each file's events ready for the shared fold (SPEC §11.8).
package/src/projection.ts CHANGED
@@ -3960,6 +3960,104 @@ export async function resolveForeignRoots(
3960
3960
  return objects
3961
3961
  }
3962
3962
 
3963
+ /**
3964
+ * Topics as their roots and changes draw them, through the bare file's
3965
+ * built-in manifest (§6.7) — what {@link resolveForeignRoots} draws a topic
3966
+ * as, title, summary, parent and own archive, without asking which app opens
3967
+ * one (no `openUrl`, no app name): for a reader that draws neither. `listRead`
3968
+ * when the changes came from the list read, which carries no text.
3969
+ * @internal readYourTopics'.
3970
+ */
3971
+ export function projectTopics(events: readonly SignedEvent[], listRead: boolean): ForeignObject[] {
3972
+ const projected = projectEvents(events, (pointer) => (pointer.kind === KIND_BARE_FILE ? bareFileManifest() : undefined))
3973
+ return listRead ? textUnread(projected) : projected
3974
+ }
3975
+
3976
+ /**
3977
+ * {@link resolveForeignRoots}' `archivedWith` for topics drawn by
3978
+ * {@link projectTopics}: each one archived with its closest archived parent,
3979
+ * marked in place. Each parent's manifest is read through `cache`, which
3980
+ * {@link seedManifests} may have filled. `listKinds` as
3981
+ * {@link markArchivedParents}'.
3982
+ * @internal readYourTopics'.
3983
+ */
3984
+ export async function markTopicsArchivedWith(
3985
+ objects: readonly ForeignObject[],
3986
+ query: QueryFn,
3987
+ cache: ProjectionCache | undefined,
3988
+ listKinds: readonly number[],
3989
+ ): Promise<void> {
3990
+ await markArchivedParents(
3991
+ objects.map((object) => ({ object, manifest: BARE_FILE_MANIFEST })),
3992
+ query,
3993
+ cache,
3994
+ listKinds,
3995
+ )
3996
+ }
3997
+
3998
+ /**
3999
+ * {@link resolveManifest}'s answer for many files' apps at once, remembered
4000
+ * in `cache`: one request, the `kind:31990` claims per kind and every
4001
+ * author's `kind:31989` recommendation in one filter, where a resolve each
4002
+ * asks two requests per `kind:pubkey`. Each answer is the one
4003
+ * {@link resolveManifestUncached} gives from the same events; a
4004
+ * recommendation naming a manifest that no claim of its kind carries is left
4005
+ * to `resolveManifest`, which reads it by its address. A key `cache` already
4006
+ * holds is not asked, and the built-in kinds never are. The query is sent
4007
+ * before the first `await`, so a caller gathering requests holds it at once.
4008
+ * @internal readYourTopics'.
4009
+ */
4010
+ export async function seedManifests(pointers: Iterable<AddressPointer>, query: QueryFn, cache: ProjectionCache): Promise<void> {
4011
+ const now = Date.now()
4012
+ const wanted = new Map<string, AddressPointer>()
4013
+ for (const pointer of pointers) {
4014
+ if ([KIND_BARE_FILE, KIND_COMMENT, KIND_MESSAGE, KIND_CHANNEL_METADATA].includes(pointer.kind)) continue
4015
+ const key = `${pointer.kind}:${pointer.pubkey}`
4016
+ if (!wanted.has(key) && !cache.lookup(key, now)) wanted.set(key, pointer)
4017
+ }
4018
+ if (wanted.size === 0) return
4019
+ const kinds = [...new Set([...wanted.values()].map((pointer) => String(pointer.kind)))]
4020
+ const authors = [...new Set([...wanted.values()].map((pointer) => pointer.pubkey))]
4021
+ const claims = kinds.map((kind) => ({ kinds: [KIND_HANDLER_INFORMATION], '#k': [kind], limit: 20 }))
4022
+ // One filter each, as `resolveManifestUncached` asks: an author's older versions never crowd out another author's recommendation.
4023
+ const recommendations = authors.flatMap((author) => kinds.map((kind) => ({ kinds: [KIND_HANDLER_RECOMMENDATION], authors: [author], '#d': [kind], limit: 1 })))
4024
+ const events = await query([...claims, ...recommendations])
4025
+ const addressOf = (event: SignedEvent) => `${event.kind}:${event.pubkey}:${tagValue(event, 'd') ?? ''}`
4026
+ const newestFirst = (a: SignedEvent, b: SignedEvent) => b.created_at - a.created_at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
4027
+ for (const [key, pointer] of wanted) {
4028
+ const kind = String(pointer.kind)
4029
+ // As the claim's own request answers: its newest 20.
4030
+ const claimed = events
4031
+ .filter((e) => e.kind === KIND_HANDLER_INFORMATION && e.tags.some((t) => t[0] === 'k' && t[1] === kind))
4032
+ .sort(newestFirst)
4033
+ .slice(0, 20)
4034
+ const recommended = events
4035
+ .filter((e) => e.kind === KIND_HANDLER_RECOMMENDATION && e.pubkey === pointer.pubkey && tagValue(e, 'd') === kind)
4036
+ .sort(newestFirst)[0]
4037
+ const manifestAddr = recommended?.tags.find((t) => t[0] === 'a')?.[1]
4038
+ let answer: ResolvedManifest | null | undefined
4039
+ if (manifestAddr) {
4040
+ const [akind, apubkey, ...rest] = manifestAddr.split(':')
4041
+ const copy = claimed.find((event) => addressOf(event) === `${Number(akind)}:${apubkey}:${rest.join(':')}`)
4042
+ // Not among the claims: `resolveManifest` reads it by its address.
4043
+ if (!copy) continue
4044
+ const manifest = parseManifest(copy)
4045
+ if (manifest) answer = { manifest, address: manifestAddr, viaRecommendation: true, webTemplate: webTemplate(copy, 'naddr') }
4046
+ }
4047
+ if (answer === undefined) {
4048
+ answer = null
4049
+ for (const candidate of claimed) {
4050
+ const manifest = parseManifest(candidate)
4051
+ if (manifest?.projections?.[kind]) {
4052
+ answer = { manifest, address: addressOf(candidate), viaRecommendation: false, webTemplate: webTemplate(candidate, 'naddr') }
4053
+ break
4054
+ }
4055
+ }
4056
+ }
4057
+ cache.remember(key, answer, now)
4058
+ }
4059
+ }
4060
+
3963
4061
  /**
3964
4062
  * SPEC §6.13's list read of these files: each one's first record and each
3965
4063
  * field's current change, without the text and without any older change. One
@@ -4490,8 +4588,11 @@ async function queryChunked(filters: Record<string, unknown>[], query: QueryFn):
4490
4588
  return (await Promise.all(chunks.map((chunk) => query(chunk)))).flat()
4491
4589
  }
4492
4590
 
4493
- /** An `naddr` or a `<kind>:<pubkey>:<d>`, or null for anything else. */
4494
- function addressPointerOf(reference: string): AddressPointer | null {
4591
+ /**
4592
+ * An `naddr` or a `<kind>:<pubkey>:<d>`, or null for anything else.
4593
+ * @internal readYourTopics' too.
4594
+ */
4595
+ export function addressPointerOf(reference: string): AddressPointer | null {
4495
4596
  try {
4496
4597
  // Either form: a body carries `naddr1…`, an `a` tag carries the plain
4497
4598
  // address, and both name the same object (FEE-2).