@estiva-app/interop 0.85.0 → 0.87.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/src/topics.ts CHANGED
@@ -24,20 +24,20 @@
24
24
  * archive chain and a Folder move all ignore it. It carries no `p`, so it adds
25
25
  * nobody to the file (§11.8).
26
26
  */
27
- import { type SignedEvent, type UnsignedEvent } from '@estiva-app/protocol'
27
+ import { encodeNpub, type SignedEvent, type UnsignedEvent } from '@estiva-app/protocol'
28
28
  import { RELAY_PAGE_CEILING } from '@estiva-app/protocol'
29
29
  import {
30
30
  byOrder,
31
31
  candidateFilesOf,
32
32
  fileContext,
33
33
  foldMemberships,
34
+ followingOf,
34
35
  HORIZON_SECONDS,
35
- membersOf,
36
- membershipJudges,
36
+ isUnreadOnPlace,
37
+ mentions,
37
38
  REPLY_FLOOR_CONTEXT,
38
- streamHasUnread,
39
- streamTermsOf,
40
39
  THREAD_RULE_FROM,
40
+ type Following,
41
41
  type MembershipRead,
42
42
  } from '@estiva-app/conversation'
43
43
  import {
@@ -46,6 +46,7 @@ import {
46
46
  isArchived,
47
47
  KIND_FOLDER_STATE,
48
48
  resolveForeignRoots,
49
+ rootOf,
49
50
  type ForeignObject,
50
51
  type ProjectionCache,
51
52
  type QueryFn,
@@ -270,6 +271,8 @@ export interface ListedFile {
270
271
  file: string
271
272
  /** Its address when exactly one file the reader can read holds the id, else `null`: not drawn, not counted. */
272
273
  address: string | null
274
+ /** The resolved file's Folder, from its root (§6.2), when it names one; `null` when unresolved or it names none. */
275
+ folder: string | null
273
276
  entry: ListEntry
274
277
  }
275
278
 
@@ -329,7 +332,10 @@ async function listsOf(
329
332
  .sort((a, b) => byOrder(a.event, b.event))
330
333
  if (entries.length === 0) return out
331
334
  const resolved = await resolveIds(query, [...new Set(entries.map((entry) => entry.file))], options.fileKinds)
332
- for (const entry of entries) out.get(entry.list)!.push({ file: entry.file, address: resolved.get(entry.file) ?? null, entry })
335
+ for (const entry of entries) {
336
+ const held = resolved.get(entry.file)
337
+ out.get(entry.list)!.push({ file: entry.file, address: held?.address ?? null, folder: held?.folder ?? null, entry })
338
+ }
333
339
  return out
334
340
  }
335
341
 
@@ -343,11 +349,15 @@ export interface ReadTopic {
343
349
  /** Its list, as {@link topicLists} reads it. */
344
350
  listed: ListedFile[]
345
351
  /**
346
- * The viewer is a member (§11.8): by the horizon's membership read, or,
347
- * where that read has no word on them, over all history — a topic joined
348
- * before the horizon with nothing since is still theirs, as Peek's Topics
349
- * says.
352
+ * How the viewer is in it (§11.8 *Places and following*), by the shared
353
+ * fold every app uses: a level they chose, an Add or a join, or having made
354
+ * it, over all history. Writing in it is not one. A space's team default is
355
+ * not read here (no roster, no private teams list): Ship's Topics draws no
356
+ * space.
357
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/writing-in-a-place-never-follows-it.md
350
358
  */
359
+ following: Following
360
+ /** The viewer is in it: `following.in`. */
351
361
  yours: boolean
352
362
  /**
353
363
  * A team marks it one of its spaces (§3.3): its entry on a team's
@@ -394,7 +404,22 @@ function spacesOf(states: readonly SignedEvent[], addresses: readonly string[]):
394
404
  export interface TopicsRead {
395
405
  me: string
396
406
  topics: ReadTopic[]
407
+ /** Folded from the viewer's part inside the horizon: `mentioned` says which messages name them. */
397
408
  membership: MembershipRead
409
+ /** The topics where a message inside the horizon names the viewer: a mention lights at any level, and when they are not in (§11.8). */
410
+ mentionedIn: ReadonlySet<string>
411
+ /**
412
+ * The conversations the viewer is in, by {@link conversationKey} (topic and
413
+ * root id): the second they first wrote in one or were named in it, over all
414
+ * history — so a reply of theirs from before the reply floor still makes a
415
+ * later reply light at Important.
416
+ */
417
+ inConversations: ReadonlyMap<string, number>
418
+ }
419
+
420
+ /** A conversation's key in {@link TopicsRead.inConversations}: the topic it is anchored to and its root's id. */
421
+ export function conversationKey(topic: string, root: string): string {
422
+ return `${topic} ${root}`
398
423
  }
399
424
 
400
425
  /** The filters for a person's part in some files over all history: the changes naming them, what they wrote, what names them. */
@@ -443,14 +468,6 @@ async function readAll(query: QueryFn, filters: readonly Record<string, unknown>
443
468
  return [...out.values()]
444
469
  }
445
470
 
446
- /** Whether the person is a member of the file over all history, folded as its own page folds it. */
447
- function memberOverAllHistory(person: string, file: string, events: readonly SignedEvent[]): boolean {
448
- const [kind, pubkey, ...d] = file.split(':')
449
- // The root, dated before everything: its author is a member unless a leave of theirs says otherwise.
450
- const root = { id: '0'.repeat(64), pubkey: pubkey ?? '', kind: Number(kind), created_at: 0, tags: [['d', d.join(':')]], content: '', sig: '' }
451
- return membersOf(file, [root, ...events]).has(person)
452
- }
453
-
454
471
  /**
455
472
  * Every topic the viewer can read, once (SHIPS-2): each with its list, its
456
473
  * archive (its own, or with its project), its Folder, whether it is the
@@ -470,8 +487,8 @@ function memberOverAllHistory(person: string, file: string, events: readonly Sig
470
487
  * production, QA-2's horizon read over every file was 500 KB; over the topics
471
488
  * it is a few.
472
489
  *
473
- * "Yours" is today's membership fold (§11.8); following replaces it in this
474
- * same function (Teams and following, milestone 5).
490
+ * How the viewer is in each topic is {@link followingOf} over its root and
491
+ * the changes naming them (TEA-6), the fold Peek's switch reads too.
475
492
  * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-topic-keeps-its-own-list-of-files.md
476
493
  */
477
494
  export async function readTopics(
@@ -490,7 +507,7 @@ export async function readTopics(
490
507
  if (!held || root.created_at > held.created_at || (root.created_at === held.created_at && root.id < held.id)) roots.set(address, root)
491
508
  }
492
509
  const addresses = [...roots.keys()]
493
- if (addresses.length === 0) return { me, topics: [], membership: foldMemberships(me, [], nowSeconds) }
510
+ if (addresses.length === 0) return { me, topics: [], membership: foldMemberships(me, [], nowSeconds), mentionedIn: new Set(), inConversations: new Map() }
494
511
  const [objects, lists, part, states] = await Promise.all([
495
512
  resolveForeignRoots(addresses, query, options.cache, { archivedWith: true }),
496
513
  listsOf(query, addresses, Promise.resolve(roots), options),
@@ -506,18 +523,36 @@ export async function readTopics(
506
523
  const membership = foldMemberships(me, part.filter((e) => e.created_at >= nowSeconds - HORIZON_SECONDS), nowSeconds)
507
524
  // Each topic's own events, so each is folded over its own and not over all of them.
508
525
  const byTopic = new Map<string, SignedEvent[]>()
509
- for (const event of part) for (const file of candidateFilesOf(event)) byTopic.set(file, [...(byTopic.get(file) ?? []), event])
510
- const none = new Set<string>()
526
+ // A change by its `a`, a level included: `candidateFilesOf` leaves levels out, since they say nothing of file membership.
527
+ const filesOf = (event: SignedEvent) => (event.kind === KIND_CHANGE ? event.tags.filter((t) => t[0] === 'a').map((t) => t[1]) : candidateFilesOf(event))
528
+ for (const event of part) {
529
+ for (const file of filesOf(event)) {
530
+ const held = byTopic.get(file)
531
+ if (held) held.push(event)
532
+ else byTopic.set(file, [event])
533
+ }
534
+ }
535
+ const mentionedIn = new Set<string>()
536
+ for (const [file, events] of byTopic) if (events.some((e) => e.kind === KIND_COMMENT && membership.mentioned.has(e.id))) mentionedIn.add(file)
537
+ const inConversations = new Map<string, number>()
538
+ for (const e of part) {
539
+ if (e.kind !== KIND_COMMENT || (e.pubkey !== me && !mentions(e, me))) continue
540
+ // Keyed by the topic the comment is anchored to as well: an `E` naming another topic's thread puts nobody in that thread.
541
+ for (const file of candidateFilesOf(e)) {
542
+ const key = conversationKey(file, rootOf(e))
543
+ inConversations.set(key, Math.min(inConversations.get(key) ?? Infinity, e.created_at))
544
+ }
545
+ }
511
546
  const topics = objects.flatMap((object): ReadTopic[] => {
512
547
  const root = object.address ? roots.get(object.address) : undefined
513
548
  const folder = root ? folderOf(root) : null
514
549
  if (!root || !folder || object.unreachable) return []
515
550
  const address = object.address!
516
- const yours =
517
- streamTermsOf(address, membership, me, none).member || (!membershipJudges(address, membership, me) && memberOverAllHistory(me, address, byTopic.get(address) ?? []))
518
- return [{ address, object, folder, listed: lists.get(address) ?? [], yours, space: spaces.has(address) }]
551
+ // The team is the topic's own Folder; a team's default reaches no topic, so its roster is not needed (§11.8).
552
+ const following = followingOf({ address, team: folder }, me, { events: [root, ...(byTopic.get(address) ?? [])], listing: states })
553
+ return [{ address, object, folder, listed: lists.get(address) ?? [], following, yours: following.in, space: spaces.has(address) }]
519
554
  })
520
- return { me, topics, membership }
555
+ return { me, topics, membership, mentionedIn, inConversations }
521
556
  }
522
557
 
523
558
  // An untitled topic sorts where the apps' "Untitled topic" would.
@@ -549,17 +584,23 @@ export interface TopicConversation {
549
584
  /**
550
585
  * Each topic's conversations and dot, as Peek's Topics shows them (SHIPS-2):
551
586
  * the count is the thread roots addressed to the topic (`conversationsOf`),
552
- * the dot is {@link streamHasUnread} on the topic's own marker, gated by
553
- * membership (§11.8), on the viewer's open topics only. `fresh` is the same
554
- * judge over each thread alone (a root and its replies), so Ship's Topics can
555
- * say how many of them are new where Peek shows the dot (SHIPS-3).
587
+ * the dot is §11.8's *What is unread on a place* ({@link isUnreadOnPlace}) at
588
+ * the viewer's level on it ({@link ReadTopic.following}): at Important a new
589
+ * conversation, or a reply in one they wrote in or were named in; at All every
590
+ * message since they chose it; a mention at any level, and when they are not
591
+ * in. An archived topic lights nothing. `fresh` is the same judge over each
592
+ * thread alone (a root and its replies), so Ship's Topics can say how many of
593
+ * them are new where Peek shows the dot (SHIPS-3).
556
594
  *
557
595
  * The read state and the muted list are encrypted, so the app hands in what
558
596
  * it holds: `merged` (the merged markers, once the first merge has landed)
559
- * and `muted`. Replies are read only for the topics a dot is judged on, and
560
- * only from the latest second before which none can be unread: the thread
561
- * rule's cut-over, the reply floor, and when the viewer joined (§11.3: a
562
- * reply is read by its thread's marker or the floor, never the topic's).
597
+ * and `muted`. Replies are read only for the topics a dot is judged on — the
598
+ * open ones the viewer is in or is named in — and only from the latest second
599
+ * before which none can be unread: the thread rule's cut-over and the reply
600
+ * floor (§11.3: a reply is read by its thread's marker or the floor, never the
601
+ * topic's). Not from when the viewer came in: a reply of theirs from before
602
+ * then still puts them in its conversation.
603
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/writing-in-a-place-never-follows-it.md
563
604
  */
564
605
  export async function topicConversations(
565
606
  read: TopicsRead,
@@ -568,15 +609,20 @@ export async function topicConversations(
568
609
  options: { cache?: ProjectionCache } = {},
569
610
  ): Promise<Map<string, TopicConversation>> {
570
611
  const nowSeconds = state.nowSeconds ?? Math.floor(Date.now() / 1000)
571
- const terms = (address: string) => streamTermsOf(address, read.membership, read.me, state.muted)
572
- const judged = new Set(read.topics.filter((topic) => topic.yours && !isArchived(topic.object)).map((topic) => topic.address))
612
+ const judged = new Set(
613
+ read.topics
614
+ .filter((topic) => (topic.following.in || read.mentionedIn.has(topic.address)) && !isArchived(topic.object))
615
+ .map((topic) => topic.address),
616
+ )
573
617
  const repliesSince = (address: string) => {
574
618
  if (!judged.has(address)) return null
575
- const floor = state.merged[REPLY_FLOOR_CONTEXT]
576
- return Math.max(THREAD_RULE_FROM, floor ?? 0, terms(address).since ?? 0)
619
+ // Not from when they came in: a reply of theirs from before then still says they are in that conversation.
620
+ return Math.max(THREAD_RULE_FROM, state.merged[REPLY_FLOOR_CONTEXT] ?? 0)
577
621
  }
578
622
  const files = read.topics.map((topic) => ({ ...topic.object, ref: topic.address }))
579
623
  const held = await conversationsOf(files, query, options.cache, { replies: true, repliesSince })
624
+ // The messages carry no body: one the membership read found naming the viewer is judged as the body it would have had.
625
+ const named = `nostr:${encodeNpub(read.me)}`
580
626
  const out = new Map<string, TopicConversation>()
581
627
  for (const topic of read.topics) {
582
628
  const messages = held[topic.address]
@@ -592,11 +638,15 @@ export async function topicConversations(
592
638
  if (thread) thread.push(m)
593
639
  else threads.set(m.root, [m])
594
640
  }
595
- const topicTerms = terms(topic.address)
596
- const frontier = { merged: state.merged, me: read.me, nowSeconds, horizonSeconds: HORIZON_SECONDS }
641
+ const judge = { stream: context, me: read.me, following: topic.following, muted: state.muted.has(topic.address), floor: nowSeconds - HORIZON_SECONDS }
597
642
  // Every thread here is one of the topic's: `conversationsOf` keeps a reply only under a root of this file.
598
643
  for (const thread of threads.values()) {
599
- if (streamHasUnread(thread, context, topicTerms, frontier, (id) => read.membership.mentioned.has(id))) fresh++
644
+ const asEvents = thread
645
+ .sort((a, b) => a.at - b.at)
646
+ .map((m) => ({ id: m.id, pubkey: m.by, kind: KIND_COMMENT, created_at: m.at, tags: [], content: read.membership.mentioned.has(m.id) ? named : '', root: m.root }))
647
+ // When the viewer came into this conversation (wrote in it or was named in it), before the reply floor too.
648
+ const inSince = read.inConversations.get(conversationKey(topic.address, thread[0]!.root))
649
+ if (asEvents.some((m) => isUnreadOnPlace(m, m.root === m.id ? undefined : m.root, state.merged, judge, inSince))) fresh++
600
650
  }
601
651
  }
602
652
  out.set(topic.address, { conversations: messages.filter((m) => m.root === m.id).length, unread: fresh > 0, fresh })
@@ -605,7 +655,11 @@ export async function topicConversations(
605
655
  }
606
656
 
607
657
  /** Each id's address when exactly one file of `fileKinds` the reader can read holds it; an id held by none or by two is left out. */
608
- async function resolveIds(query: QueryFn, ids: readonly string[], fileKinds?: readonly number[]): Promise<Map<string, string>> {
658
+ async function resolveIds(
659
+ query: QueryFn,
660
+ ids: readonly string[],
661
+ fileKinds?: readonly number[],
662
+ ): Promise<Map<string, { address: string; folder: string | null }>> {
609
663
  // Never a Folder's `39000`, which shares its uuid (§11.1): an id only a Folder holds resolves to nothing.
610
664
  const kinds = (fileKinds ?? LISTED_FILE_KINDS).filter((kind) => kind !== KIND_FOLDER)
611
665
  // Room for a second holder of every id, so two holders are seen as two; in
@@ -617,15 +671,22 @@ async function resolveIds(query: QueryFn, ids: readonly string[], fileKinds?: re
617
671
  // first), and one holder would then pass for the only one: every id of that
618
672
  // batch stays unresolved rather than drawn as somebody else's file.
619
673
  const unsure = new Set(batches.flatMap((batch, i) => (answers[i].length >= batch.length * 2 ? batch : [])))
620
- const holders = new Map<string, Set<string>>()
674
+ // Each holder's newest root: a replaceable file the relay still holds two versions of names its Folder by the newer.
675
+ const holders = new Map<string, Map<string, SignedEvent>>()
621
676
  for (const event of answers.flat()) {
622
677
  const d = tagOf(event, 'd')
623
678
  if (!d || !kinds.includes(event.kind)) continue
624
- const set = holders.get(d) ?? new Set<string>()
625
- set.add(`${event.kind}:${event.pubkey}`)
626
- holders.set(d, set)
679
+ const roots = holders.get(d) ?? new Map<string, SignedEvent>()
680
+ const holder = `${event.kind}:${event.pubkey}`
681
+ const kept = roots.get(holder)
682
+ if (!kept || event.created_at > kept.created_at) roots.set(holder, event)
683
+ holders.set(d, roots)
684
+ }
685
+ const out = new Map<string, { address: string; folder: string | null }>()
686
+ for (const [d, roots] of holders) {
687
+ if (roots.size !== 1 || unsure.has(d)) continue
688
+ const [[holder, root]] = roots
689
+ out.set(d, { address: `${holder}:${d}`, folder: folderOf(root) })
627
690
  }
628
- const out = new Map<string, string>()
629
- for (const [d, set] of holders) if (set.size === 1 && !unsure.has(d)) out.set(d, `${[...set][0]}:${d}`)
630
691
  return out
631
692
  }