@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.
- package/CHANGELOG.md +27 -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/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- 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/index.ts +7 -0
- package/src/projection.ts +103 -2
- package/src/topics.ts +202 -51
- package/src/yourTopics.ts +239 -0
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
|
|
|
@@ -0,0 +1,239 @@
|
|
|
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, type SignedEvent } from '@estiva-app/protocol'
|
|
27
|
+
import {
|
|
28
|
+
addressPointerOf,
|
|
29
|
+
createProjectionCache,
|
|
30
|
+
isArchived,
|
|
31
|
+
markTopicsArchivedWith,
|
|
32
|
+
projectTopics,
|
|
33
|
+
seedManifests,
|
|
34
|
+
type ForeignObject,
|
|
35
|
+
type ProjectionCache,
|
|
36
|
+
type QueryFn,
|
|
37
|
+
} from './projection.js'
|
|
38
|
+
import {
|
|
39
|
+
answeredBy,
|
|
40
|
+
changesNamingFilter,
|
|
41
|
+
eventsByTopic,
|
|
42
|
+
listReadPlan,
|
|
43
|
+
listsFrom,
|
|
44
|
+
newestChanges,
|
|
45
|
+
readAll,
|
|
46
|
+
readTopicOf,
|
|
47
|
+
resolveIds,
|
|
48
|
+
spacesOf,
|
|
49
|
+
teamListFilters,
|
|
50
|
+
topicListFilter,
|
|
51
|
+
topicRoots,
|
|
52
|
+
type ListReadOptions,
|
|
53
|
+
type ReadTopic,
|
|
54
|
+
} from './topics.js'
|
|
55
|
+
import { activityTopics, byActivity, readTopicActivity } from './activity.js'
|
|
56
|
+
|
|
57
|
+
const KIND_TOPIC = 30840
|
|
58
|
+
const KIND_CHANGE = 1851
|
|
59
|
+
const KIND_APP_DATA = 30078
|
|
60
|
+
/** As {@link resolveForeignRoots} reads a file's changes when it reads them plain. */
|
|
61
|
+
const PLAIN_CHANGES_LIMIT = 500
|
|
62
|
+
|
|
63
|
+
/** {@link readYourTopics}' answer. */
|
|
64
|
+
export interface YourTopicsRead {
|
|
65
|
+
me: string
|
|
66
|
+
/**
|
|
67
|
+
* The viewer's topics: in it (`following.in`), not archived (its own, or
|
|
68
|
+
* with a parent), not muted, and not a space unless `spaces` was asked.
|
|
69
|
+
* Newest activity first ({@link byActivity} over `at`). Each as
|
|
70
|
+
* {@link readTopics} reads it, its `object` without its text when read by
|
|
71
|
+
* the list read and without `openUrl`.
|
|
72
|
+
*/
|
|
73
|
+
topics: ReadTopic[]
|
|
74
|
+
/** {@link readTopicActivity}'s `at` for exactly these topics. */
|
|
75
|
+
at: Map<string, number>
|
|
76
|
+
/** The viewer's muted keys, from their mute list; empty with none; `undefined` when it would not decrypt (then nothing is left out as muted). */
|
|
77
|
+
muted: ReadonlySet<string> | undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The viewer's topics, newest activity first, in about ten requests on a
|
|
82
|
+
* workspace like Miky's (53 topics, 12 his): see the module's rounds.
|
|
83
|
+
*
|
|
84
|
+
* `listRead`, where the relay advertises the list read (NIP-11
|
|
85
|
+
* `file_list.newest`): the topics and their parents are read as their
|
|
86
|
+
* current fields (§6.13); `readTopics(query, me, { listRead, parentsListRead:
|
|
87
|
+
* listRead })` reads the same. `decrypt` opens the mute list (NIP-44 to
|
|
88
|
+
* self); `cache` keeps the parents' manifests between reads.
|
|
89
|
+
*/
|
|
90
|
+
export async function readYourTopics(
|
|
91
|
+
query: QueryFn,
|
|
92
|
+
me: string,
|
|
93
|
+
options: ListReadOptions & {
|
|
94
|
+
decrypt: (ciphertext: string) => Promise<string | undefined>
|
|
95
|
+
spaces?: boolean
|
|
96
|
+
cache?: ProjectionCache
|
|
97
|
+
nowSeconds?: number
|
|
98
|
+
},
|
|
99
|
+
): Promise<YourTopicsRead> {
|
|
100
|
+
const listRead = options.listRead ?? false
|
|
101
|
+
const cache = options.cache ?? createProjectionCache()
|
|
102
|
+
|
|
103
|
+
// 1. Every topic's root, and the mute list.
|
|
104
|
+
const first = gathered(query)
|
|
105
|
+
const rootsRead = readAll(first.query, [{ kinds: [KIND_TOPIC] }])
|
|
106
|
+
const mutedOpened = first.query([mutedFilter(me)]).then((events) => mutedOf(events, me, options.decrypt))
|
|
107
|
+
// Its failure is the roots' too, and said by them.
|
|
108
|
+
mutedOpened.catch(() => {})
|
|
109
|
+
first.send()
|
|
110
|
+
const roots = topicRoots(await rootsRead)
|
|
111
|
+
const addresses = [...roots.keys()]
|
|
112
|
+
if (addresses.length === 0) return { me, topics: [], at: new Map(), muted: await mutedOpened }
|
|
113
|
+
|
|
114
|
+
// 2. How the viewer is in each, which are spaces, and each topic's fields.
|
|
115
|
+
const second = gathered(query)
|
|
116
|
+
const partRead = readAll(second.query, [changesNamingFilter(me, addresses)])
|
|
117
|
+
const statesRead = readAll(second.query, teamListFilters(roots))
|
|
118
|
+
// Without the list read: each topic's changes as `resolveForeignRoots` and `topicLists` read them.
|
|
119
|
+
const objectChangesRead = listRead ? Promise.resolve([]) : second.query(addresses.map(plainChangesFilter))
|
|
120
|
+
const listChangesRead = listRead ? Promise.resolve([]) : readAll(second.query, addresses.map(topicListFilter))
|
|
121
|
+
second.send()
|
|
122
|
+
const [part, states, listed, objectChanges, listChanges, muted] = await Promise.all([
|
|
123
|
+
partRead,
|
|
124
|
+
statesRead,
|
|
125
|
+
listRead ? newestChanges(query, addresses) : Promise.resolve([]),
|
|
126
|
+
objectChangesRead,
|
|
127
|
+
listChangesRead,
|
|
128
|
+
mutedOpened,
|
|
129
|
+
])
|
|
130
|
+
const spaces = spacesOf(states, addresses)
|
|
131
|
+
const byTopic = eventsByTopic(part)
|
|
132
|
+
const facts = { roots, byTopic, states, spaces }
|
|
133
|
+
const mine = (object: ForeignObject) => {
|
|
134
|
+
const topic = readTopicOf(object, me, facts)
|
|
135
|
+
return topic && topic.yours && (options.spaces || !topic.space) && !muted?.has(topic.address) ? topic : undefined
|
|
136
|
+
}
|
|
137
|
+
// A topic the list read did not answer (deleted) is read plain below, as a parent is.
|
|
138
|
+
const answered = new Set(topicRoots(listed).keys())
|
|
139
|
+
const byList = listRead ? addresses.filter((address) => answered.has(address)) : addresses
|
|
140
|
+
const objects = projectTopics([...byList.map((address) => roots.get(address)!), ...(listRead ? listed : objectChanges)], listRead)
|
|
141
|
+
if (listRead) {
|
|
142
|
+
const unanswered = addresses.filter((address) => !answered.has(address))
|
|
143
|
+
// 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.
|
|
144
|
+
const maybe = projectTopics(unanswered.map((address) => roots.get(address)!), false).flatMap((object) => (mine(object) ? [object.address!] : []))
|
|
145
|
+
if (maybe.length > 0) objects.push(...projectTopics([...maybe.map((address) => roots.get(address)!), ...(await query(maybe.map(plainChangesFilter)))], false))
|
|
146
|
+
}
|
|
147
|
+
const candidates: Omit<ReadTopic, 'listed'>[] = []
|
|
148
|
+
for (const object of objects) {
|
|
149
|
+
const topic = mine(object)
|
|
150
|
+
if (topic && !isArchived(topic.object)) candidates.push(topic)
|
|
151
|
+
}
|
|
152
|
+
if (candidates.length === 0) return { me, topics: [], at: new Map(), muted }
|
|
153
|
+
|
|
154
|
+
// 3. Their listed ids, what the list read skipped, and their parents' manifests.
|
|
155
|
+
const asked = candidates.map((topic) => topic.address)
|
|
156
|
+
const listEvents = listRead ? listed : listChanges
|
|
157
|
+
const plan = listReadPlan(asked, roots, listEvents, listRead)
|
|
158
|
+
const parents = candidates.flatMap((topic) => {
|
|
159
|
+
const parent = topic.object.parentRef ? addressPointerOf(topic.object.parentRef) : null
|
|
160
|
+
return parent ? [parent] : []
|
|
161
|
+
})
|
|
162
|
+
const third = gathered(query)
|
|
163
|
+
const resolvedRead = plan.ids.length ? resolveIds(third.query, plan.ids, options.fileKinds) : Promise.resolve(new Map())
|
|
164
|
+
const againRead = readAll(third.query, plan.again)
|
|
165
|
+
const seeded = seedManifests(parents, third.query, cache)
|
|
166
|
+
third.send()
|
|
167
|
+
const [resolved, again] = await Promise.all([resolvedRead, againRead, seeded])
|
|
168
|
+
const lists = listsFrom(asked, roots, [...listEvents, ...again], resolved)
|
|
169
|
+
const topics: ReadTopic[] = candidates.map((topic) => ({ ...topic, listed: lists.get(topic.address) ?? [] }))
|
|
170
|
+
|
|
171
|
+
// 4. Their parents, while their activity is read: one archived with a parent is left out after.
|
|
172
|
+
const [activity] = await Promise.all([
|
|
173
|
+
readTopicActivity(query, activityTopics(topics), { nowSeconds: options.nowSeconds }),
|
|
174
|
+
markTopicsArchivedWith(
|
|
175
|
+
topics.map((topic) => topic.object),
|
|
176
|
+
query,
|
|
177
|
+
cache,
|
|
178
|
+
listRead ? FILE_KINDS : [],
|
|
179
|
+
),
|
|
180
|
+
])
|
|
181
|
+
const open = topics.filter((topic) => !isArchived(topic.object))
|
|
182
|
+
const newest = byActivity(activity.at)
|
|
183
|
+
open.sort((a, b) => newest(a.address, b.address))
|
|
184
|
+
return { me, topics: open, at: new Map(open.map((topic) => [topic.address, activity.at.get(topic.address) ?? 0])), muted }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A topic's changes as {@link resolveForeignRoots} reads them plain: the newest 500 naming it by `a`. */
|
|
188
|
+
function plainChangesFilter(address: string): Record<string, unknown> {
|
|
189
|
+
return { kinds: [KIND_CHANGE], '#a': [address], limit: PLAIN_CHANGES_LIMIT }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The viewer's muted keys from a read of their mute list: none without one, `undefined` when it does not open. */
|
|
193
|
+
async function mutedOf(
|
|
194
|
+
events: readonly SignedEvent[],
|
|
195
|
+
me: string,
|
|
196
|
+
decrypt: (ciphertext: string) => Promise<string | undefined>,
|
|
197
|
+
): Promise<ReadonlySet<string> | undefined> {
|
|
198
|
+
const list = events
|
|
199
|
+
.filter((e) => e.pubkey === me && e.kind === KIND_APP_DATA && e.tags.some((t) => t[0] === 'd' && t[1] === MUTED_D_TAG))
|
|
200
|
+
.sort((a, b) => b.created_at - a.created_at)[0]
|
|
201
|
+
if (!list) return new Set()
|
|
202
|
+
const parsed = parseMutedBlob((await decrypt(list.content).catch(() => undefined)) ?? '')
|
|
203
|
+
return parsed ? new Set(parsed.keys) : undefined
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A query that holds every request made through it until {@link send}, then
|
|
208
|
+
* asks them all as one request and hands each caller what its own filters
|
|
209
|
+
* would have been answered alone ({@link answeredBy}). A request made after
|
|
210
|
+
* `send` (a later page), and a list read, which a relay takes only alone, go
|
|
211
|
+
* straight through.
|
|
212
|
+
*/
|
|
213
|
+
function gathered(query: QueryFn): { query: QueryFn; send(): void } {
|
|
214
|
+
let held: { filters: Record<string, unknown>[]; resolve: (events: SignedEvent[]) => void; reject: (error: unknown) => void }[] | null = []
|
|
215
|
+
return {
|
|
216
|
+
query: (filters) => {
|
|
217
|
+
if (!held || filters.some((filter) => 'fields' in filter)) return query(filters)
|
|
218
|
+
const asks = held
|
|
219
|
+
return new Promise((resolve, reject) => asks.push({ filters, resolve, reject }))
|
|
220
|
+
},
|
|
221
|
+
send() {
|
|
222
|
+
const asks = held ?? []
|
|
223
|
+
held = null
|
|
224
|
+
if (asks.length === 0) return
|
|
225
|
+
query(asks.flatMap((ask) => ask.filters)).then(
|
|
226
|
+
(events) => {
|
|
227
|
+
for (const ask of asks) {
|
|
228
|
+
const page = new Map<string, SignedEvent>()
|
|
229
|
+
for (const filter of ask.filters) for (const event of answeredBy(events, filter)) page.set(event.id, event)
|
|
230
|
+
ask.resolve([...page.values()])
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
(error: unknown) => {
|
|
234
|
+
for (const ask of asks) ask.reject(error)
|
|
235
|
+
},
|
|
236
|
+
)
|
|
237
|
+
},
|
|
238
|
+
}
|
|
239
|
+
}
|