@estiva-app/interop 0.94.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.
- package/CHANGELOG.md +41 -0
- package/README.md +1 -0
- package/dist/activity.d.ts +13 -7
- package/dist/activity.d.ts.map +1 -1
- package/dist/activity.js +138 -104
- package/dist/activity.js.map +1 -1
- package/dist/folders.d.ts +22 -6
- package/dist/folders.d.ts.map +1 -1
- package/dist/folders.js +39 -12
- package/dist/folders.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/projection.d.ts +36 -0
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +95 -2
- package/dist/projection.js.map +1 -1
- package/dist/topics.d.ts +91 -0
- package/dist/topics.d.ts.map +1 -1
- package/dist/topics.js +173 -56
- package/dist/topics.js.map +1 -1
- package/dist/yourTopics.d.ts +35 -0
- package/dist/yourTopics.d.ts.map +1 -0
- package/dist/yourTopics.js +177 -0
- package/dist/yourTopics.js.map +1 -0
- package/package.json +1 -1
- package/src/activity.ts +133 -94
- package/src/folders.ts +46 -13
- package/src/index.ts +9 -0
- package/src/projection.ts +103 -2
- package/src/topics.ts +202 -51
- package/src/yourTopics.ts +239 -0
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
|
-
/**
|
|
4494
|
-
|
|
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).
|
package/src/topics.ts
CHANGED
|
@@ -337,20 +337,52 @@ async function listsOf(
|
|
|
337
337
|
rootsRead: Promise<ReadonlyMap<string, SignedEvent>>,
|
|
338
338
|
options: ListReadOptions,
|
|
339
339
|
): Promise<Map<string, ListedFile[]>> {
|
|
340
|
-
const out = new Map<string, ListedFile[]>(asked.map((topic) => [topic, []]))
|
|
341
340
|
// Paged without the list read: every move adds a change to a topic's `#O`, and an unpaged read would lose its oldest entries.
|
|
342
341
|
const [roots, events] = await Promise.all([rootsRead, options.listRead ? newestChanges(query, asked) : readAll(query, asked.map(topicListFilter))])
|
|
343
|
-
const folderOfList = (
|
|
344
|
-
|
|
342
|
+
const folderOfList = listFolderCheck(asked, roots)
|
|
343
|
+
if (options.listRead) events.push(...(await readAll(query, skippedFilters(events, folderOfList))))
|
|
344
|
+
const ids = listedIds(events, folderOfList)
|
|
345
|
+
const resolved = ids.length ? await resolveIds(query, ids, options.fileKinds) : new Map()
|
|
346
|
+
return listsFrom(asked, roots, events, resolved)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** §6.2's Folder check for the lists asked for: each list's own Folder, from its root; none for a list not asked for or unread. */
|
|
350
|
+
function listFolderCheck(asked: readonly string[], roots: ReadonlyMap<string, SignedEvent>): (list: string) => string | null {
|
|
351
|
+
const lists = new Set(asked)
|
|
352
|
+
return (list) => {
|
|
353
|
+
const root = lists.has(list) ? roots.get(list) : undefined
|
|
345
354
|
return root ? folderOf(root) : null
|
|
346
355
|
}
|
|
347
|
-
|
|
348
|
-
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The files the lists asked for hold, as the events fold: each newest entry that lists one, oldest first, each id once. */
|
|
359
|
+
function listedIds(events: readonly SignedEvent[], folderOfList: (list: string) => string | null): string[] {
|
|
360
|
+
return [...new Set(listedEntries(events, folderOfList).map((entry) => entry.file))]
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function listedEntries(events: readonly SignedEvent[], folderOfList: (list: string) => string | null): ListEntry[] {
|
|
364
|
+
return listEntries(events, folderOfList)
|
|
349
365
|
.filter((entry) => entry.listed)
|
|
350
366
|
.sort((a, b) => byOrder(a.event, b.event))
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Each list asked for, from the changes already read and its listed ids
|
|
371
|
+
* already resolved ({@link resolveIds}): its entries in the topic's order,
|
|
372
|
+
* each with its arrangement (§6.14). A list asked for that holds nothing maps
|
|
373
|
+
* to `[]`.
|
|
374
|
+
*/
|
|
375
|
+
function listsFrom(
|
|
376
|
+
asked: readonly string[],
|
|
377
|
+
roots: ReadonlyMap<string, SignedEvent>,
|
|
378
|
+
events: readonly SignedEvent[],
|
|
379
|
+
resolved: ReadonlyMap<string, { address: string; folder: string | null }>,
|
|
380
|
+
): Map<string, ListedFile[]> {
|
|
381
|
+
const out = new Map<string, ListedFile[]>(asked.map((topic) => [topic, []]))
|
|
382
|
+
const folderOfList = listFolderCheck(asked, roots)
|
|
383
|
+
const entries = listedEntries(events, folderOfList)
|
|
351
384
|
if (entries.length === 0) return out
|
|
352
385
|
const heads = arrangementHeads(events, folderOfList)
|
|
353
|
-
const resolved = await resolveIds(query, [...new Set(entries.map((entry) => entry.file))], options.fileKinds)
|
|
354
386
|
for (const entry of entries) {
|
|
355
387
|
const held = resolved.get(entry.file)
|
|
356
388
|
const arranged = arrangementOf(heads.get(entry.list), entry.file, entry.event, roots.get(entry.list)!)
|
|
@@ -380,13 +412,13 @@ const SKIPPED_READ_CAP = 100
|
|
|
380
412
|
* The list read answers each field's newest change, a malformed one too (a
|
|
381
413
|
* wrong `o` the relay takes), so the good change before it is not in the
|
|
382
414
|
* answer. For each file where that happened, read its changes on that topic
|
|
383
|
-
* again (`#O` and `#o`), so it folds as a whole `#O` read would
|
|
415
|
+
* again (`#O` and `#o`), so it folds as a whole `#O` read would: the filters
|
|
416
|
+
* for that read, none when nothing was skipped.
|
|
384
417
|
*/
|
|
385
|
-
|
|
386
|
-
query: QueryFn,
|
|
418
|
+
function skippedFilters(
|
|
387
419
|
events: readonly SignedEvent[],
|
|
388
420
|
folderOfList: (list: string) => string | null | undefined,
|
|
389
|
-
):
|
|
421
|
+
): { kinds: number[]; '#O': string[]; '#o': string[] }[] {
|
|
390
422
|
const skipped = new Map<string, Set<string>>()
|
|
391
423
|
for (const event of events) {
|
|
392
424
|
if (event.kind !== KIND_CHANGE) continue
|
|
@@ -406,8 +438,7 @@ async function skippedAgain(
|
|
|
406
438
|
skipped.set(holder[3], files)
|
|
407
439
|
}
|
|
408
440
|
}
|
|
409
|
-
|
|
410
|
-
return readAll(query, [...skipped].map(([topic, files]) => ({ kinds: [KIND_CHANGE], '#O': [topic], '#o': [...files] })))
|
|
441
|
+
return [...skipped].map(([topic, files]) => ({ kinds: [KIND_CHANGE], '#O': [topic], '#o': [...files] }))
|
|
411
442
|
}
|
|
412
443
|
|
|
413
444
|
/** A topic as {@link readTopics} reads it. */
|
|
@@ -527,20 +558,81 @@ export function answers(event: SignedEvent, filter: Record<string, unknown>): bo
|
|
|
527
558
|
*/
|
|
528
559
|
export async function readAll(query: QueryFn, filters: readonly Record<string, unknown>[]): Promise<SignedEvent[]> {
|
|
529
560
|
const out = new Map<string, SignedEvent>()
|
|
530
|
-
let open = filters
|
|
561
|
+
let open = firstPages(filters)
|
|
531
562
|
while (open.length) {
|
|
532
563
|
const events = await query(open)
|
|
533
564
|
for (const event of events) out.set(event.id, event)
|
|
534
|
-
open = open
|
|
535
|
-
const page = events.filter((event) => answers(event, filter))
|
|
536
|
-
if (page.length < filter.limit) return []
|
|
537
|
-
const until = Math.min(...page.map((event) => event.created_at))
|
|
538
|
-
return until === (filter as { until?: number }).until ? [] : [{ ...filter, until }]
|
|
539
|
-
})
|
|
565
|
+
open = nextPages(open, events)
|
|
540
566
|
}
|
|
541
567
|
return [...out.values()]
|
|
542
568
|
}
|
|
543
569
|
|
|
570
|
+
/** {@link readAll}'s first request: each filter asked with a page's `limit`. */
|
|
571
|
+
export function firstPages(filters: readonly Record<string, unknown>[]): (Record<string, unknown> & { limit: number })[] {
|
|
572
|
+
return filters.map((filter) => ({ ...filter, limit: RELAY_PAGE_CEILING }))
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* {@link readAll}'s next request after `events` came back for `open`: each
|
|
577
|
+
* filter whose page filled, from its oldest second down. A filter's page is
|
|
578
|
+
* what it alone was answered ({@link answeredBy}): an older event another
|
|
579
|
+
* filter in the request brought back is not one it read, so it never moves
|
|
580
|
+
* this one's `until` past events it has not seen.
|
|
581
|
+
*/
|
|
582
|
+
export function nextPages(
|
|
583
|
+
open: readonly (Record<string, unknown> & { limit: number })[],
|
|
584
|
+
events: readonly SignedEvent[],
|
|
585
|
+
): (Record<string, unknown> & { limit: number })[] {
|
|
586
|
+
return open.flatMap((filter) => {
|
|
587
|
+
const page = answeredBy(events, filter)
|
|
588
|
+
if (page.length < filter.limit) return []
|
|
589
|
+
const until = Math.min(...page.map((event) => event.created_at))
|
|
590
|
+
return until === (filter as { until?: number }).until ? [] : [{ ...filter, until }]
|
|
591
|
+
})
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* What `filter` alone would have been answered, from the answer to a request
|
|
596
|
+
* it shared with others: the events it matches, newest first (equal seconds
|
|
597
|
+
* by the lower id), cut at its `limit`. Another filter's answer in the same
|
|
598
|
+
* request never lands on this one's page. Not for a list read (`fields`),
|
|
599
|
+
* whose answer holds changes the filter does not match.
|
|
600
|
+
*/
|
|
601
|
+
export function answeredBy(events: readonly SignedEvent[], filter: Record<string, unknown>): SignedEvent[] {
|
|
602
|
+
const page = events.filter((event) => answers(event, filter)).sort(newestFirst)
|
|
603
|
+
return typeof filter.limit === 'number' ? page.slice(0, filter.limit) : page
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* {@link answeredBy} for many filters over one answer, the answer sorted and
|
|
608
|
+
* indexed by tag value once: each filter that names one tag value (`#a`,
|
|
609
|
+
* `#A`, `#O`… with one value) is matched against the events carrying it, any
|
|
610
|
+
* other against them all. The same pages, newest first and cut at `limit`.
|
|
611
|
+
*/
|
|
612
|
+
export function answeredByEach(events: readonly SignedEvent[]): (filter: Record<string, unknown>) => SignedEvent[] {
|
|
613
|
+
const sorted = [...events].sort(newestFirst)
|
|
614
|
+
const byTag = new Map<string, SignedEvent[]>()
|
|
615
|
+
for (const event of sorted) {
|
|
616
|
+
const keys = new Set(event.tags.filter((t) => t[0] && t[1] !== undefined).map((t) => `${t[0]}\u0000${t[1]}`))
|
|
617
|
+
for (const key of keys) {
|
|
618
|
+
const held = byTag.get(key)
|
|
619
|
+
if (held) held.push(event)
|
|
620
|
+
else byTag.set(key, [event])
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return (filter) => {
|
|
624
|
+
const one = Object.entries(filter).find(([key, values]) => key.startsWith('#') && Array.isArray(values) && values.length === 1)
|
|
625
|
+
const pool = one ? (byTag.get(`${one[0].slice(1)}\u0000${(one[1] as string[])[0]}`) ?? []) : sorted
|
|
626
|
+
const page = pool.filter((event) => answers(event, filter))
|
|
627
|
+
return typeof filter.limit === 'number' ? page.slice(0, filter.limit) : page
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** Newest first, equal seconds by the lower id: the order a relay answers a page in. */
|
|
632
|
+
function newestFirst(a: SignedEvent, b: SignedEvent): number {
|
|
633
|
+
return b.created_at - a.created_at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
|
|
634
|
+
}
|
|
635
|
+
|
|
544
636
|
/**
|
|
545
637
|
* Every topic the viewer can read, once (SHIPS-2): each with its list, its
|
|
546
638
|
* archive (its own, or with its project), its Folder, whether it is the
|
|
@@ -575,15 +667,7 @@ export async function readTopics(
|
|
|
575
667
|
options: ListReadOptions & { cache?: ProjectionCache; nowSeconds?: number; parentsListRead?: boolean } = {},
|
|
576
668
|
): Promise<TopicsRead> {
|
|
577
669
|
const nowSeconds = options.nowSeconds ?? Math.floor(Date.now() / 1000)
|
|
578
|
-
const
|
|
579
|
-
const roots = new Map<string, SignedEvent>()
|
|
580
|
-
for (const root of rootEvents) {
|
|
581
|
-
if (root.kind !== KIND_TOPIC) continue
|
|
582
|
-
const address = `${root.kind}:${root.pubkey}:${tagOf(root, 'd') ?? ''}`
|
|
583
|
-
const held = roots.get(address)
|
|
584
|
-
// Two versions of one record: NIP-01's later created_at, then the lower id (§6.3).
|
|
585
|
-
if (!held || root.created_at > held.created_at || (root.created_at === held.created_at && root.id < held.id)) roots.set(address, root)
|
|
586
|
-
}
|
|
670
|
+
const roots = topicRoots(await readAll(query, [{ kinds: [KIND_TOPIC] }]))
|
|
587
671
|
const addresses = [...roots.keys()]
|
|
588
672
|
if (addresses.length === 0) return { me, topics: [], membership: foldMemberships(me, [], nowSeconds), mentionedIn: new Set(), inConversations: new Map() }
|
|
589
673
|
const [objects, lists, part, states] = await Promise.all([
|
|
@@ -593,24 +677,11 @@ export async function readTopics(
|
|
|
593
677
|
// Held once each, though one comment can answer two of the filters: the folds count events.
|
|
594
678
|
readAll(query, personInFilesFilters(me, addresses)),
|
|
595
679
|
// The team lists naming a topic, for which of them are spaces.
|
|
596
|
-
readAll(query,
|
|
597
|
-
{ kinds: [KIND_FOLDER_STATE], '#a': addresses },
|
|
598
|
-
{ kinds: [KIND_FOLDER_STATE], '#o': [...roots.values()].map((root) => tagOf(root, 'd') ?? '') },
|
|
599
|
-
]),
|
|
680
|
+
readAll(query, teamListFilters(roots)),
|
|
600
681
|
])
|
|
601
682
|
const spaces = spacesOf(states, addresses)
|
|
602
683
|
const membership = foldMemberships(me, part.filter((e) => e.created_at >= nowSeconds - HORIZON_SECONDS), nowSeconds)
|
|
603
|
-
|
|
604
|
-
const byTopic = new Map<string, SignedEvent[]>()
|
|
605
|
-
// A change by its `a`, a level included: `candidateFilesOf` leaves levels out, since they say nothing of file membership.
|
|
606
|
-
const filesOf = (event: SignedEvent) => (event.kind === KIND_CHANGE ? event.tags.filter((t) => t[0] === 'a').map((t) => t[1]) : candidateFilesOf(event))
|
|
607
|
-
for (const event of part) {
|
|
608
|
-
for (const file of filesOf(event)) {
|
|
609
|
-
const held = byTopic.get(file)
|
|
610
|
-
if (held) held.push(event)
|
|
611
|
-
else byTopic.set(file, [event])
|
|
612
|
-
}
|
|
613
|
-
}
|
|
684
|
+
const byTopic = eventsByTopic(part)
|
|
614
685
|
const mentionedIn = new Set<string>()
|
|
615
686
|
for (const [file, events] of byTopic) if (events.some((e) => e.kind === KIND_COMMENT && membership.mentioned.has(e.id))) mentionedIn.add(file)
|
|
616
687
|
const inConversations = new Map<string, number>()
|
|
@@ -622,18 +693,98 @@ export async function readTopics(
|
|
|
622
693
|
inConversations.set(key, Math.min(inConversations.get(key) ?? Infinity, e.created_at))
|
|
623
694
|
}
|
|
624
695
|
}
|
|
625
|
-
const topics = objects.flatMap((object)
|
|
626
|
-
const
|
|
627
|
-
|
|
628
|
-
if (!root || !folder || object.unreachable) return []
|
|
629
|
-
const address = object.address!
|
|
630
|
-
// The team is the topic's own Folder; a team's default reaches no topic, so its roster is not needed (§11.8).
|
|
631
|
-
const following = followingOf({ address, team: folder }, me, { events: [root, ...(byTopic.get(address) ?? [])], listing: states })
|
|
632
|
-
return [{ address, object, folder, createdAt: root.created_at, listed: lists.get(address) ?? [], following, yours: following.in, space: spaces.has(address) }]
|
|
696
|
+
const topics = objects.flatMap((object) => {
|
|
697
|
+
const topic = readTopicOf(object, me, { roots, byTopic, states, spaces })
|
|
698
|
+
return topic ? [{ ...topic, listed: lists.get(topic.address) ?? [] }] : []
|
|
633
699
|
})
|
|
634
700
|
return { me, topics, membership, mentionedIn, inConversations }
|
|
635
701
|
}
|
|
636
702
|
|
|
703
|
+
/** Every topic's root by address, from a read of `kind:30840`: two versions of one record by NIP-01's later `created_at`, then the lower id (§6.3). */
|
|
704
|
+
export function topicRoots(events: readonly SignedEvent[]): Map<string, SignedEvent> {
|
|
705
|
+
const roots = new Map<string, SignedEvent>()
|
|
706
|
+
for (const root of events) {
|
|
707
|
+
if (root.kind !== KIND_TOPIC) continue
|
|
708
|
+
const address = `${root.kind}:${root.pubkey}:${tagOf(root, 'd') ?? ''}`
|
|
709
|
+
const held = roots.get(address)
|
|
710
|
+
if (!held || root.created_at > held.created_at || (root.created_at === held.created_at && root.id < held.id)) roots.set(address, root)
|
|
711
|
+
}
|
|
712
|
+
return roots
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/** The team lists naming the topics, by address and by id: which of them are spaces ({@link spacesOf}). */
|
|
716
|
+
export function teamListFilters(roots: ReadonlyMap<string, SignedEvent>): Record<string, unknown>[] {
|
|
717
|
+
return [
|
|
718
|
+
{ kinds: [KIND_FOLDER_STATE], '#a': [...roots.keys()] },
|
|
719
|
+
{ kinds: [KIND_FOLDER_STATE], '#o': [...roots.values()].map((root) => tagOf(root, 'd') ?? '') },
|
|
720
|
+
]
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** The changes naming the viewer on the topics: how they are in each ({@link followingOf}). */
|
|
724
|
+
export function changesNamingFilter(me: string, addresses: readonly string[]): Record<string, unknown> {
|
|
725
|
+
return personInFilesFilters(me, addresses)[0]!
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** Each topic's own events, so each is folded over its own and not over all of them. */
|
|
729
|
+
export function eventsByTopic(part: readonly SignedEvent[]): Map<string, SignedEvent[]> {
|
|
730
|
+
const byTopic = new Map<string, SignedEvent[]>()
|
|
731
|
+
// A change by its `a`, a level included: `candidateFilesOf` leaves levels out, since they say nothing of file membership.
|
|
732
|
+
const filesOf = (event: SignedEvent) => (event.kind === KIND_CHANGE ? event.tags.filter((t) => t[0] === 'a').map((t) => t[1]) : candidateFilesOf(event))
|
|
733
|
+
for (const event of part) {
|
|
734
|
+
for (const file of filesOf(event)) {
|
|
735
|
+
const held = byTopic.get(file)
|
|
736
|
+
if (held) held.push(event)
|
|
737
|
+
else byTopic.set(file, [event])
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return byTopic
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* A topic as {@link readTopics} reads it, its list aside: none for an object
|
|
745
|
+
* whose root the reader does not hold, or names no Folder, or is unreachable.
|
|
746
|
+
*/
|
|
747
|
+
export function readTopicOf(
|
|
748
|
+
object: ForeignObject,
|
|
749
|
+
me: string,
|
|
750
|
+
facts: {
|
|
751
|
+
roots: ReadonlyMap<string, SignedEvent>
|
|
752
|
+
byTopic: ReadonlyMap<string, readonly SignedEvent[]>
|
|
753
|
+
states: readonly SignedEvent[]
|
|
754
|
+
spaces: ReadonlySet<string>
|
|
755
|
+
},
|
|
756
|
+
): Omit<ReadTopic, 'listed'> | undefined {
|
|
757
|
+
const root = object.address ? facts.roots.get(object.address) : undefined
|
|
758
|
+
const folder = root ? folderOf(root) : null
|
|
759
|
+
if (!root || !folder || object.unreachable) return undefined
|
|
760
|
+
const address = object.address!
|
|
761
|
+
// The team is the topic's own Folder; a team's default reaches no topic, so its roster is not needed (§11.8).
|
|
762
|
+
const following = followingOf({ address, team: folder }, me, { events: [root, ...(facts.byTopic.get(address) ?? [])], listing: facts.states })
|
|
763
|
+
return { address, object, folder, createdAt: root.created_at, following, yours: following.in, space: facts.spaces.has(address) }
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* The second half of a topic list read for the lists asked for, from the
|
|
768
|
+
* changes already read ({@link topicListFilter} or the list read), for a
|
|
769
|
+
* reader that gathers its requests: the filters asking again about what the
|
|
770
|
+
* list read skipped (none without it), and every id to resolve whatever
|
|
771
|
+
* that answer says — the ids listed now and the ones asked about again.
|
|
772
|
+
*/
|
|
773
|
+
export function listReadPlan(
|
|
774
|
+
asked: readonly string[],
|
|
775
|
+
roots: ReadonlyMap<string, SignedEvent>,
|
|
776
|
+
events: readonly SignedEvent[],
|
|
777
|
+
listRead: boolean,
|
|
778
|
+
): { again: Record<string, unknown>[]; ids: string[] } {
|
|
779
|
+
const folderOfList = listFolderCheck(asked, roots)
|
|
780
|
+
const again = listRead ? skippedFilters(events, folderOfList) : []
|
|
781
|
+
const ids = new Set(listedIds(events, folderOfList))
|
|
782
|
+
for (const filter of again) for (const id of filter['#o']) ids.add(id)
|
|
783
|
+
return { again, ids: [...ids] }
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
export { listsFrom, newestChanges, resolveIds, spacesOf }
|
|
787
|
+
|
|
637
788
|
// An untitled topic sorts where the apps' "Untitled topic" would.
|
|
638
789
|
const titleOf = (topic: ReadTopic) => topic.object.slots.title?.value || 'Untitled topic'
|
|
639
790
|
|