@estiva-app/interop 0.71.0 → 0.73.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/projection.ts CHANGED
@@ -22,7 +22,7 @@ import {
22
22
  imetaTag,
23
23
  parseBlockDocument,
24
24
  } from '@estiva-app/protocol'
25
- import { planPlaceFile, planUnlistFile, type FolderRef } from './folders.js'
25
+ import { identifierOf, planPlaceFile, planUnlistFile, type FolderRef } from './folders.js'
26
26
  /*
27
27
  SPEC §6.4's comment strength and §6.3's ordering are the conversation
28
28
  package's (CON-5): one copy of each rule, read here for a file's `comments`,
@@ -5805,6 +5805,9 @@ function buildCreationEvent(args: {
5805
5805
  */
5806
5806
  const PERSON_KEY = /^[0-9a-f]{64}$/
5807
5807
 
5808
+ /** An event id as the relay reads a `prev`: lower-case hex. */
5809
+ const EVENT_ID = /^[0-9a-f]{64}$/
5810
+
5808
5811
  /** Why `value` is neither `''` nor a person's key, or undefined when it is one. */
5809
5812
  function personKeyProblem(label: string, value: string): string | undefined {
5810
5813
  if (value === '' || PERSON_KEY.test(value)) return undefined
@@ -5879,6 +5882,17 @@ export interface ActionEventArgs {
5879
5882
  * only: a comment's text is its `value`, and a creation has its own body.
5880
5883
  */
5881
5884
  note?: string
5885
+ /**
5886
+ * The change this one follows for its field: the id of that field's current
5887
+ * change as the writer read it, or the file's first record's id for its
5888
+ * first change (SPEC §6.2). A change only. Every writer should send it; a
5889
+ * relay that keeps the type in order refuses a change without it, and one
5890
+ * naming a change that is no longer current with `conflict:`, which
5891
+ * {@link writeChange} answers.
5892
+ *
5893
+ * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-its-fields-each-kept-in-order-by-the-relay.md
5894
+ */
5895
+ prev?: string
5882
5896
  pubkey: string
5883
5897
  createdAtMs: number
5884
5898
  }
@@ -5988,6 +6002,13 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
5988
6002
  if (args.note !== undefined && (isCreation || !declared.emits.field || declared.emits.kind === KIND_DELETION)) {
5989
6003
  return `"${declared.label}" is not a change, so it takes no note.`
5990
6004
  }
6005
+ // Only a change follows another for its field (§6.2).
6006
+ if (args.prev !== undefined && (isCreation || !declared.emits.field || declared.emits.kind === KIND_DELETION)) {
6007
+ return `"${declared.label}" is not a change, so it follows no change.`
6008
+ }
6009
+ if (args.prev !== undefined && !EVENT_ID.test(args.prev)) {
6010
+ return `"${declared.label}" follows ${JSON.stringify(args.prev)}, which is not an event id.`
6011
+ }
5991
6012
 
5992
6013
  // An object-creating action is a different event entirely — a new object
5993
6014
  // rather than a change to one — so it branches before the scalar path.
@@ -6072,9 +6093,13 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6072
6093
  if (typeof prose === 'string') return prose
6073
6094
  const [formatTag, ...files] = prose
6074
6095
 
6096
+ // The file the change is about, by its id, beside its address (§6.2): the
6097
+ // relay keeps each field in order by it.
6098
+ const fileId = records.targetTag === 'a' ? identifierOf(address) : undefined
6075
6099
  const tags: string[][] = declared.emits.field
6076
6100
  ? [
6077
6101
  [records.targetTag, address],
6102
+ ...(fileId !== undefined ? [['O', fileId]] : []),
6078
6103
  [records.fieldTag, declared.emits.field],
6079
6104
  [records.valueTag, value],
6080
6105
  ['h', folder],
@@ -6114,6 +6139,7 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6114
6139
  // second would be separated by event id — arbitrarily, and differently
6115
6140
  // depending on which app you asked (FRICTION.md A6).
6116
6141
  if (records.order?.includes('ts')) tags.push(['ts', String(args.createdAtMs)])
6142
+ if (args.prev !== undefined) tags.push(['prev', args.prev])
6117
6143
  tags.push(...files)
6118
6144
  // The person, where the relay indexes them: `value` is not indexed, so
6119
6145
  // without this `#p` never finds the change and §11.8 makes nobody a member.
@@ -6150,6 +6176,12 @@ function buildOneActionEvent(args: ActionEventArgs): UnsignedActionEvent | strin
6150
6176
  if (moved?.field === declared.emits.field && typeof value === 'string' && parentAddress.test(value)) {
6151
6177
  tags.push([MOVED_TO_TAG, value])
6152
6178
  }
6179
+ // What the chosen option means, last (§6.2's `stage`): a status says
6180
+ // whether it is open, started, done or dropped, whichever app reads it.
6181
+ const stage = declared.emits.field && declared.input?.enum
6182
+ ? manifest.vocabularies?.[declared.input.enum]?.find((entry) => entry.value === value)?.stage
6183
+ : undefined
6184
+ if (declared.emits.field && stage && STAGES.has(stage as Stage)) tags.push(['stage', declared.emits.field, stage])
6153
6185
 
6154
6186
  return {
6155
6187
  pubkey: args.pubkey,
package/src/topics.ts CHANGED
@@ -264,32 +264,63 @@ export async function topicList(
264
264
  topic: string,
265
265
  options: { fileKinds?: readonly number[] } = {},
266
266
  ): Promise<ListedFile[]> {
267
- const [roots, events] = await Promise.all([rootsOf(query, [topic]), query([topicListFilter(topic)])])
268
- const root = roots.get(topic)
269
- const folder = root ? folderOf(root) : null
270
- if (!folder) return []
271
- const entries = listEntries(events, (list) => (list === topic ? folder : null))
267
+ return (await topicLists(query, [topic], options)).get(topic) ?? []
268
+ }
269
+
270
+ /**
271
+ * Many topics' lists at once, by address (TOP-4, Peek's Topics screen): the
272
+ * rules of {@link topicList} for each, in three requests whatever the number
273
+ * of topics (one more per 100 listed files): the roots, every topic's changes
274
+ * (one `#O` filter per topic) and the batched `#d` resolve, where one
275
+ * `topicList` per topic would be three requests each and trips a relay's rate
276
+ * limit at a few dozen topics. Every address asked for is a key; one that
277
+ * lists nothing, or that the reader cannot read, maps to `[]`.
278
+ */
279
+ export async function topicLists(
280
+ query: QueryFn,
281
+ topics: readonly string[],
282
+ options: { fileKinds?: readonly number[] } = {},
283
+ ): Promise<Map<string, ListedFile[]>> {
284
+ const asked = [...new Set(topics)]
285
+ const out = new Map<string, ListedFile[]>(asked.map((topic) => [topic, []]))
286
+ if (asked.length === 0) return out
287
+ // One filter per topic in one request, so a relay's cap on an answer cuts no more than it would for that topic alone.
288
+ const [roots, events] = await Promise.all([rootsOf(query, asked), query(asked.map(topicListFilter))])
289
+ const folderOfList = (list: string) => {
290
+ const root = out.has(list) ? roots.get(list) : undefined
291
+ return root ? folderOf(root) : null
292
+ }
293
+ const entries = listEntries(events, folderOfList)
272
294
  .filter((entry) => entry.listed)
273
295
  .sort((a, b) => byOrder(a.event, b.event))
274
- if (entries.length === 0) return []
275
- const ids = [...new Set(entries.map((entry) => entry.file))]
296
+ if (entries.length === 0) return out
297
+ const resolved = await resolveIds(query, [...new Set(entries.map((entry) => entry.file))], options.fileKinds)
298
+ for (const entry of entries) out.get(entry.list)!.push({ file: entry.file, address: resolved.get(entry.file) ?? null, entry })
299
+ return out
300
+ }
301
+
302
+ /** 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. */
303
+ async function resolveIds(query: QueryFn, ids: readonly string[], fileKinds?: readonly number[]): Promise<Map<string, string>> {
276
304
  // Never a Folder's `39000`, which shares its uuid (§11.1): an id only a Folder holds resolves to nothing.
277
- const kinds = (options.fileKinds ?? LISTED_FILE_KINDS).filter((kind) => kind !== KIND_FOLDER)
305
+ const kinds = (fileKinds ?? LISTED_FILE_KINDS).filter((kind) => kind !== KIND_FOLDER)
278
306
  // Room for a second holder of every id, so two holders are seen as two; in
279
307
  // batches, so a relay's cap on `limit` cannot cut a long list short.
280
308
  const batches: string[][] = []
281
309
  for (let i = 0; i < ids.length; i += RESOLVE_BATCH) batches.push(ids.slice(i, i + RESOLVE_BATCH))
282
- const found = (await Promise.all(batches.map((batch) => query([{ kinds, '#d': batch, limit: batch.length * 2 }])))).flat()
310
+ const answers = await Promise.all(batches.map((batch) => query([{ kinds, '#d': batch, limit: batch.length * 2 }])))
311
+ // An answer that filled its limit may have cut a second holder (newest
312
+ // first), and one holder would then pass for the only one: every id of that
313
+ // batch stays unresolved rather than drawn as somebody else's file.
314
+ const unsure = new Set(batches.flatMap((batch, i) => (answers[i].length >= batch.length * 2 ? batch : [])))
283
315
  const holders = new Map<string, Set<string>>()
284
- for (const event of found) {
316
+ for (const event of answers.flat()) {
285
317
  const d = tagOf(event, 'd')
286
318
  if (!d || !kinds.includes(event.kind)) continue
287
319
  const set = holders.get(d) ?? new Set<string>()
288
320
  set.add(`${event.kind}:${event.pubkey}`)
289
321
  holders.set(d, set)
290
322
  }
291
- return entries.map((entry) => {
292
- const set = holders.get(entry.file)
293
- return { file: entry.file, address: set?.size === 1 ? `${[...set][0]}:${entry.file}` : null, entry }
294
- })
323
+ const out = new Map<string, string>()
324
+ for (const [d, set] of holders) if (set.size === 1 && !unsure.has(d)) out.set(d, `${[...set][0]}:${d}`)
325
+ return out
295
326
  }