@estiva-app/interop 0.76.0 → 0.78.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/links.ts CHANGED
@@ -24,7 +24,7 @@
24
24
  * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/an-edit-says-what-the-message-links-now.md
25
25
  */
26
26
  import { MAX_PARTS } from '@estiva-app/conversation'
27
- import { partTag, valuesIn, type LinkResolver, type PartPointer, type RenderBlock } from '@estiva-app/protocol'
27
+ import { fileIdFormOf, partTag, valuesIn, type LinkResolver, type PartPointer, type RenderBlock } from '@estiva-app/protocol'
28
28
  import { matchObjectUrl, urlPatternsOf, type UrlPattern } from './objectUrl.js'
29
29
  import type { QueryFn } from './projection.js'
30
30
 
@@ -79,7 +79,11 @@ export interface LinkReader {
79
79
  * first what its links name and waiting at most `waitMs` (default 5 s): a
80
80
  * link not answered by then earns nothing, and the body keeps it.
81
81
  */
82
- tagsFor(blocks: readonly RenderBlock[], own: OwnPlace, options?: { waitMs?: number; edit?: boolean }): Promise<string[][]>
82
+ tagsFor(
83
+ blocks: readonly RenderBlock[],
84
+ own: OwnPlace,
85
+ options?: { waitMs?: number; edit?: boolean; inOrder?: readonly string[] },
86
+ ): Promise<string[][]>
83
87
  /** {@link writtenOpenerOf} against the hosts the manifests read so far declare. */
84
88
  opener(url: string | undefined, opens: string | undefined, origin: string): string | undefined
85
89
  /** Every host an app declares its paths on, as the manifests read so far said. */
@@ -217,13 +221,13 @@ export function linkReader(options: LinkReaderOptions): LinkReader {
217
221
  }
218
222
  },
219
223
  version: () => answered,
220
- async tagsFor(blocks, own, { waitMs = 5000, edit = false } = {}) {
224
+ async tagsFor(blocks, own, { waitMs = 5000, edit = false, inOrder = [] } = {}) {
221
225
  const urls = linksIn(blocks).slice(0, MAX_LINKS_ASKED)
222
226
  const waiting = urls.filter((url) => !known.has(url))
223
227
  if (waiting.length > 0) await within(Promise.allSettled(waiting.map(answerOf)), waitMs)
224
228
  const first = new Set(urls)
225
229
  const resolve: LinkResolver = (url) => (first.has(url) ? knownValue(url) : undefined)
226
- return linkTagsOf(blocks, own, resolve, edit ? undefined : (url) => (first.has(url) ? knownBlock(url) : undefined))
230
+ return linkTagsOf(blocks, own, resolve, edit ? undefined : (url) => (first.has(url) ? knownBlock(url) : undefined), inOrder)
227
231
  },
228
232
  opener: (url, opens, origin) => writtenOpenerOf(url, opens, origin, origins),
229
233
  declaredOrigins: () => origins,
@@ -256,13 +260,19 @@ export function linksIn(blocks: readonly RenderBlock[]): string[] {
256
260
  * message links now. An edit carries no `part` (pass no `blockOf`), and a
257
261
  * person is never here: `p` and `urgent` stay as the send wrote them.
258
262
  *
263
+ * A file whose type the relay keeps in order (`inOrder`) is named by its id
264
+ * too: an `o` after its `a`, and its `part` by the id (§6.4, §13.6.1). An
265
+ * edit carries the `o` as a send does (§6.8).
266
+ *
259
267
  * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/an-edit-says-what-the-message-links-now.md
268
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-named-by-its-id-in-a-capital-and-small-tag.md
260
269
  */
261
270
  export function linkTagsOf(
262
271
  blocks: readonly RenderBlock[],
263
272
  own: OwnPlace,
264
273
  resolve: LinkResolver,
265
274
  blockOf?: (url: string) => PartPointer | undefined,
275
+ inOrder: readonly string[] = [],
266
276
  ): string[][] {
267
277
  const file = own.file && sameValue(own.file)
268
278
  const parent = own.parent?.toLowerCase()
@@ -270,7 +280,11 @@ export function linkTagsOf(
270
280
  for (const value of valuesIn(blocks, resolve)) {
271
281
  if (EVENT_ID.test(value)) {
272
282
  if (value !== parent) tags.push(['q', value])
273
- } else if (value !== file) tags.push(['a', value])
283
+ } else if (value !== file) {
284
+ tags.push(['a', value])
285
+ const byId = fileIdFormOf(inOrder, value)
286
+ if (byId && !tags.some((t) => t[0] === 'o' && t[1] === byId.id)) tags.push(['o', byId.id])
287
+ }
274
288
  }
275
289
  if (blockOf) {
276
290
  const parts = new Map<string, string>()
@@ -279,7 +293,7 @@ export function linkTagsOf(
279
293
  if (part && sameValue(part.address) !== file && !parts.has(part.address)) parts.set(part.address, part.block)
280
294
  }
281
295
  // As many as a box shows under it (conversation's `boxLinksOf`): a part the box did not draw is not sent.
282
- for (const [address, block] of [...parts].slice(0, MAX_PARTS)) tags.push(partTag(address, block))
296
+ for (const [address, block] of [...parts].slice(0, MAX_PARTS)) tags.push(partTag(fileIdFormOf(inOrder, address)?.id ?? address, block))
283
297
  }
284
298
  return tags
285
299
  }
package/src/projection.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import { decodeNevent, encodeNaddr, encodeNevent, pointerToAddress, referenceToPointer, type AddressPointer, type EventPointer } from '@estiva-app/protocol'
16
16
  import { parseProfile, type Profile, type SignedEvent } from '@estiva-app/protocol'
17
- import { FILE_KINDS, MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol'
17
+ import { FILE_KINDS, MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING, fileIdFormOf } from '@estiva-app/protocol'
18
18
  import {
19
19
  attachmentsInDocument,
20
20
  BLOCK_DOCUMENT_FORMAT,
@@ -228,7 +228,7 @@ export const KIND_MESSAGE = 9
228
228
  const KIND_DELETION = 5
229
229
 
230
230
  /** The tags a comment's body earns for what it names: `ActionEventArgs.linkTags`. */
231
- const LINK_TAG_NAMES = new Set(['a', 'q', 'part'])
231
+ const LINK_TAG_NAMES = new Set(['a', 'o', 'q', 'part'])
232
232
 
233
233
  /**
234
234
  * Every kind an app's comments might be under — what it publishes **now**, plus
@@ -5933,6 +5933,13 @@ export interface ActionEventArgs {
5933
5933
  * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-its-fields-each-kept-in-order-by-the-relay.md
5934
5934
  */
5935
5935
  prev?: string
5936
+ /**
5937
+ * The types the relay keeps in order (`RelayFileReads.inOrder`): a comment
5938
+ * on a file of one names it by its id too, and a Folder command lists it by
5939
+ * its id (SPEC §6.4, "Apps move to the pair type by type"). Absent, by its
5940
+ * address alone, as every reader reads it.
5941
+ */
5942
+ inOrder?: readonly string[]
5936
5943
  pubkey: string
5937
5944
  createdAtMs: number
5938
5945
  }
@@ -5993,7 +6000,7 @@ export function buildActionEvents(
5993
6000
  return [
5994
6001
  first,
5995
6002
  ...folders.flatMap((listing) =>
5996
- planUnlistFile(args.pubkey, args.createdAtMs, { folder: listing.id, address: args.address, hasState: listing.hasState }),
6003
+ planUnlistFile(args.pubkey, args.createdAtMs, { folder: listing.id, address: args.address, hasState: listing.hasState, inOrder: args.inOrder }),
5997
6004
  ),
5998
6005
  ]
5999
6006
  }
@@ -6007,7 +6014,7 @@ export function buildActionEvents(
6007
6014
  return `"${declared.label}" lists what it creates, and a kind ${first.kind} has no address to list.`
6008
6015
  }
6009
6016
  const created = pointerToAddress({ kind: first.kind, pubkey: first.pubkey, identifier, relays: [] })
6010
- return [first, ...planPlaceFile(args.pubkey, args.createdAtMs, { folder: args.folder, address: created, hasState: args.folderHasState })]
6017
+ return [first, ...planPlaceFile(args.pubkey, args.createdAtMs, { folder: args.folder, address: created, hasState: args.folderHasState, inOrder: args.inOrder })]
6011
6018
  }
6012
6019
  // A change or a comment adds nothing to a Folder and removes nothing from it.
6013
6020
  return [first]
@@ -6136,6 +6143,8 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6136
6143
  // The file the change is about, by its id, beside its address (§6.2): the
6137
6144
  // relay keeps each field in order by it.
6138
6145
  const fileId = records.targetTag === 'a' ? identifierOf(address) : undefined
6146
+ // A comment names its file by its id too once its type is kept in order (§6.4, "A comment in both forms").
6147
+ const byId = fileIdFormOf(args.inOrder ?? [], address)
6139
6148
  const tags: string[][] = declared.emits.field
6140
6149
  ? [
6141
6150
  [records.targetTag, address],
@@ -6154,6 +6163,7 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6154
6163
  ['A', address],
6155
6164
  ['K', String(kind)],
6156
6165
  ['P', args.objectAuthor],
6166
+ ...(byId ? [['O', byId.id], ['K', byId.type]] : []),
6157
6167
  ...(replyTo
6158
6168
  ? [
6159
6169
  ['e', replyTo.id],
@@ -6169,6 +6179,7 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6169
6179
  : [
6170
6180
  ['a', address],
6171
6181
  ['k', String(kind)],
6182
+ ...(byId ? [['o', byId.id], ['k', byId.type]] : []),
6172
6183
  ['p', args.objectAuthor],
6173
6184
  ]),
6174
6185
  ['h', folder],
@@ -6202,6 +6213,8 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6202
6213
  for (const tag of args.linkTags ?? []) {
6203
6214
  if (!LINK_TAG_NAMES.has(tag[0]) || typeof tag[1] !== 'string') continue
6204
6215
  if ((tag[0] === 'a' || tag[0] === 'part') && tag[1] === address) continue
6216
+ // The comment's own file by its id is its `o`/`O` already, never a link.
6217
+ if ((tag[0] === 'o' || tag[0] === 'part') && fileId !== undefined && tag[1] === fileId) continue
6205
6218
  if (tag[0] === 'q' && tag[1] === replyTo?.id) continue
6206
6219
  if (!tags.some((t) => t[0] === tag[0] && t[1] === tag[1])) tags.push([...tag])
6207
6220
  }
package/src/references.ts CHANGED
@@ -30,6 +30,7 @@ import {
30
30
  type ProjectionCache,
31
31
  type QueryFn,
32
32
  } from './projection.js'
33
+ import { fileIdFormOf } from '@estiva-app/protocol'
33
34
 
34
35
  const KIND_PROJECT = 30850
35
36
  const KIND_ISSUE = 30851
@@ -76,6 +77,28 @@ export interface FileCandidatesOptions {
76
77
  cache?: ProjectionCache
77
78
  /** When each address was last written or read, in seconds — newer first within a tier. */
78
79
  at?: ReadonlyMap<string, number>
80
+ /** The types the relay keeps in order (`RelayFileReads.inOrder`): a project of one is picked as its link. */
81
+ inOrder?: readonly string[]
82
+ /** The host Ship serves projects from: a picked project's link is `<it>/project/<d>`. Ship's own unless said, so every app picks the same link. */
83
+ projectOrigin?: string
84
+ }
85
+
86
+ /** Where Ship serves projects (§13.1, decided 2026-11-03): one link for a project, whichever app picked it. */
87
+ export const SHIP_ORIGIN = 'https://ship.estiva.app'
88
+
89
+ /**
90
+ * How a picked file goes in the text (§13.1): a project whose type the relay
91
+ * keeps in order as Ship's path link with its id and no title, which every
92
+ * app's host answers; anything else as `nostr:naddr…`.
93
+ *
94
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-named-by-its-id-in-a-capital-and-small-tag.md
95
+ */
96
+ export function pickedFileUri(
97
+ address: string,
98
+ { inOrder = [], projectOrigin = SHIP_ORIGIN }: Pick<FileCandidatesOptions, 'inOrder' | 'projectOrigin'> = {},
99
+ ): string {
100
+ const byId = fileIdFormOf(inOrder, address)
101
+ return byId?.type === 'osnova.project' ? `${projectOrigin.replace(/\/+$/, '')}/project/${byId.id}` : fileReference(address)
79
102
  }
80
103
 
81
104
  /**
@@ -93,7 +116,7 @@ export async function fileCandidates(
93
116
  objects: readonly (ForeignObject | null | undefined)[],
94
117
  tier: number,
95
118
  query: QueryFn,
96
- { cache, at }: FileCandidatesOptions = {},
119
+ { cache, at, inOrder, projectOrigin }: FileCandidatesOptions = {},
97
120
  ): Promise<ReferenceCandidate[]> {
98
121
  const files = objects.filter(offerable)
99
122
  const parents = new Map<string, ForeignObject>()
@@ -111,7 +134,7 @@ export async function fileCandidates(
111
134
  const summary = summaryOf(o)
112
135
  return {
113
136
  id: o.address,
114
- uri: fileReference(o.address),
137
+ uri: pickedFileUri(o.address, { inOrder, projectOrigin }),
115
138
  type: typeOf(o.kind)!,
116
139
  kind: o.kind,
117
140
  title: o.slots.title!.value,
package/src/topics.ts CHANGED
@@ -44,6 +44,7 @@ import {
44
44
  conversationsOf,
45
45
  folderOf,
46
46
  isArchived,
47
+ KIND_FOLDER_STATE,
47
48
  resolveForeignRoots,
48
49
  type ForeignObject,
49
50
  type ProjectionCache,
@@ -348,6 +349,45 @@ export interface ReadTopic {
348
349
  * says.
349
350
  */
350
351
  yours: boolean
352
+ /**
353
+ * A team marks it one of its spaces (§3.3): its entry on a team's
354
+ * relay-signed list carries the role `space`. Ship's Topics never draws one.
355
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-space-is-the-teams-own-mark-on-a-topic-it-lists.md
356
+ */
357
+ space: boolean
358
+ }
359
+
360
+ /** The role on a team list's entry that marks a topic one of the team's spaces (§3.3). */
361
+ const SPACE_ROLE = 'space'
362
+
363
+ /**
364
+ * The topics a team's list marks as its spaces: an `a` entry naming the
365
+ * topic, or an `o` entry naming its id, with the role after an empty third
366
+ * element (§3.3). A topic is listed by one team, so a list that names it
367
+ * without the role — one left behind by a move that failed halfway — keeps it
368
+ * a plain topic: no team's people follow a space nobody there chose. An id
369
+ * two topics hold names neither (C31).
370
+ */
371
+ function spacesOf(states: readonly SignedEvent[], addresses: readonly string[]): Set<string> {
372
+ const known = new Set(addresses)
373
+ const holders = new Map<string, string[]>()
374
+ for (const address of addresses) {
375
+ const id = address.split(':').slice(2).join(':')
376
+ holders.set(id, [...(holders.get(id) ?? []), address])
377
+ }
378
+ const marked = new Set<string>()
379
+ const plain = new Set<string>()
380
+ for (const state of states) {
381
+ if (state.kind !== KIND_FOLDER_STATE) continue
382
+ for (const [name, value, third, role] of state.tags) {
383
+ const held = name === 'o' ? holders.get(value) : undefined
384
+ const address = name === 'a' && known.has(value) ? value : held?.length === 1 ? held[0] : undefined
385
+ if (!address) continue
386
+ if (role === SPACE_ROLE && !third) marked.add(address)
387
+ else plain.add(address)
388
+ }
389
+ }
390
+ return new Set([...marked].filter((address) => !plain.has(address)))
351
391
  }
352
392
 
353
393
  /** Every topic the viewer can read, and the membership read that judges their dots. */
@@ -413,12 +453,13 @@ function memberOverAllHistory(person: string, file: string, events: readonly Sig
413
453
 
414
454
  /**
415
455
  * Every topic the viewer can read, once (SHIPS-2): each with its list, its
416
- * archive (its own, or with its project), its Folder and whether it is the
417
- * viewer's — the one read Ship's Topics, a topic's page and Add to topic draw
418
- * from, and Leaf's Add to topic too.
456
+ * archive (its own, or with its project), its Folder, whether it is the
457
+ * viewer's and whether a team marks it a space (SHIPS-3) — the one read Ship's
458
+ * Topics, a topic's page and Add to topic draw from, and Leaf's Add to topic too.
419
459
  *
420
460
  * Three rounds whatever the number of topics: every `kind:30840` root; then
421
- * the topics as drawn, their lists and the viewer's part in them; then the
461
+ * the topics as drawn, their lists, the viewer's part in them and the team
462
+ * lists naming them; then the
422
463
  * listed ids and archived parents. No conversation is read here:
423
464
  * {@link topicConversations} is for a page that draws them.
424
465
  *
@@ -450,12 +491,18 @@ export async function readTopics(
450
491
  }
451
492
  const addresses = [...roots.keys()]
452
493
  if (addresses.length === 0) return { me, topics: [], membership: foldMemberships(me, [], nowSeconds) }
453
- const [objects, lists, part] = await Promise.all([
494
+ const [objects, lists, part, states] = await Promise.all([
454
495
  resolveForeignRoots(addresses, query, options.cache, { archivedWith: true }),
455
496
  listsOf(query, addresses, Promise.resolve(roots), options),
456
497
  // Held once each, though one comment can answer two of the filters: the folds count events.
457
498
  readAll(query, personInFilesFilters(me, addresses)),
499
+ // The team lists naming a topic, for which of them are spaces.
500
+ readAll(query, [
501
+ { kinds: [KIND_FOLDER_STATE], '#a': addresses },
502
+ { kinds: [KIND_FOLDER_STATE], '#o': [...roots.values()].map((root) => tagOf(root, 'd') ?? '') },
503
+ ]),
458
504
  ])
505
+ const spaces = spacesOf(states, addresses)
459
506
  const membership = foldMemberships(me, part.filter((e) => e.created_at >= nowSeconds - HORIZON_SECONDS), nowSeconds)
460
507
  // Each topic's own events, so each is folded over its own and not over all of them.
461
508
  const byTopic = new Map<string, SignedEvent[]>()
@@ -468,7 +515,7 @@ export async function readTopics(
468
515
  const address = object.address!
469
516
  const yours =
470
517
  streamTermsOf(address, membership, me, none).member || (!membershipJudges(address, membership, me) && memberOverAllHistory(me, address, byTopic.get(address) ?? []))
471
- return [{ address, object, folder, listed: lists.get(address) ?? [], yours }]
518
+ return [{ address, object, folder, listed: lists.get(address) ?? [], yours, space: spaces.has(address) }]
472
519
  })
473
520
  return { me, topics, membership }
474
521
  }