@estiva-app/interop 0.85.0 → 0.86.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
@@ -270,6 +270,8 @@ export interface ListedFile {
270
270
  file: string
271
271
  /** Its address when exactly one file the reader can read holds the id, else `null`: not drawn, not counted. */
272
272
  address: string | null
273
+ /** The resolved file's Folder, from its root (§6.2), when it names one; `null` when unresolved or it names none. */
274
+ folder: string | null
273
275
  entry: ListEntry
274
276
  }
275
277
 
@@ -329,7 +331,10 @@ async function listsOf(
329
331
  .sort((a, b) => byOrder(a.event, b.event))
330
332
  if (entries.length === 0) return out
331
333
  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 })
334
+ for (const entry of entries) {
335
+ const held = resolved.get(entry.file)
336
+ out.get(entry.list)!.push({ file: entry.file, address: held?.address ?? null, folder: held?.folder ?? null, entry })
337
+ }
333
338
  return out
334
339
  }
335
340
 
@@ -605,7 +610,11 @@ export async function topicConversations(
605
610
  }
606
611
 
607
612
  /** 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>> {
613
+ async function resolveIds(
614
+ query: QueryFn,
615
+ ids: readonly string[],
616
+ fileKinds?: readonly number[],
617
+ ): Promise<Map<string, { address: string; folder: string | null }>> {
609
618
  // Never a Folder's `39000`, which shares its uuid (§11.1): an id only a Folder holds resolves to nothing.
610
619
  const kinds = (fileKinds ?? LISTED_FILE_KINDS).filter((kind) => kind !== KIND_FOLDER)
611
620
  // Room for a second holder of every id, so two holders are seen as two; in
@@ -617,15 +626,22 @@ async function resolveIds(query: QueryFn, ids: readonly string[], fileKinds?: re
617
626
  // first), and one holder would then pass for the only one: every id of that
618
627
  // batch stays unresolved rather than drawn as somebody else's file.
619
628
  const unsure = new Set(batches.flatMap((batch, i) => (answers[i].length >= batch.length * 2 ? batch : [])))
620
- const holders = new Map<string, Set<string>>()
629
+ // Each holder's newest root: a replaceable file the relay still holds two versions of names its Folder by the newer.
630
+ const holders = new Map<string, Map<string, SignedEvent>>()
621
631
  for (const event of answers.flat()) {
622
632
  const d = tagOf(event, 'd')
623
633
  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)
634
+ const roots = holders.get(d) ?? new Map<string, SignedEvent>()
635
+ const holder = `${event.kind}:${event.pubkey}`
636
+ const kept = roots.get(holder)
637
+ if (!kept || event.created_at > kept.created_at) roots.set(holder, event)
638
+ holders.set(d, roots)
639
+ }
640
+ const out = new Map<string, { address: string; folder: string | null }>()
641
+ for (const [d, roots] of holders) {
642
+ if (roots.size !== 1 || unsure.has(d)) continue
643
+ const [[holder, root]] = roots
644
+ out.set(d, { address: `${holder}:${d}`, folder: folderOf(root) })
627
645
  }
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
646
  return out
631
647
  }