@estiva-app/interop 0.100.0 → 0.101.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.100.0",
3
+ "version": "0.101.0",
4
4
  "description": "Render and act on another app's objects from its published NIP-89 manifest. The owner defines the projection; the consumer decides how it looks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/loadQuery.ts CHANGED
@@ -81,6 +81,42 @@ function wholeKindsOf(filter: Filter, paged: boolean): WholeKinds | null {
81
81
  return { kinds, limit: limit ?? RELAY_PAGE_CEILING }
82
82
  }
83
83
 
84
+ /**
85
+ * SPEC §6.13's list read of files by address, `{ kinds: [kind], authors:
86
+ * [pubkey], '#d': ds, limit, fields: 'newest' }` (with `text: true`, the text
87
+ * too): each `d` answers one file, its first record and its fields' current
88
+ * changes, and `limit` counts first records. So a page with room for every
89
+ * file named holds each one whole, or says it has none (deleted, or not this
90
+ * reader's).
91
+ */
92
+ interface ListRead {
93
+ kind: number
94
+ author: string
95
+ ds: string[]
96
+ text: boolean
97
+ }
98
+
99
+ function listReadOf(filter: Filter): ListRead | null {
100
+ const { kinds, authors, limit, fields, text, ...rest } = filter
101
+ const ds = rest['#d']
102
+ if (Object.keys(rest).length !== 1 || fields !== 'newest' || (text !== undefined && text !== true)) return null
103
+ if (!Array.isArray(kinds) || kinds.length !== 1 || typeof kinds[0] !== 'number') return null
104
+ if (!Array.isArray(authors) || authors.length !== 1 || typeof authors[0] !== 'string') return null
105
+ if (!Array.isArray(ds) || ds.length === 0 || !ds.every((d) => typeof d === 'string')) return null
106
+ const files = new Set(ds).size
107
+ if (typeof limit !== 'number' || limit < files || files > RELAY_PAGE_CEILING) return null
108
+ return { kind: kinds[0], author: authors[0], ds, text: text === true }
109
+ }
110
+
111
+ /** A list read's answer for one file: its first record (by `d`) and the changes whose file it is (their first `a`, §6.10). */
112
+ function fileOf(read: ListRead, d: string, answer: SignedEvent[]): SignedEvent[] {
113
+ const address = `${read.kind}:${read.author}:${d}`
114
+ return answer.filter((event) => {
115
+ if (event.kind === read.kind && event.pubkey === read.author) return event.tags.find((tag) => tag[0] === 'd')?.[1] === d
116
+ return event.tags.find((tag) => tag[0] === 'a')?.[1] === address
117
+ })
118
+ }
119
+
84
120
  /**
85
121
  * The tags a whole read answers a later read by: the ones the relay matches as
86
122
  * NIP-01 does, on every kind. Never `h`, which is a Folder's scope on the relay
@@ -163,6 +199,9 @@ export function createLoadQuery(query: QueryFn, options: LoadQueryOptions = {}):
163
199
  const known = new Map<string, Held>()
164
200
  // Per kind: every event of it, read by a filter naming only kinds.
165
201
  const wholes = new Map<number, Held>()
202
+ // Per file a list read asked for, `<text|list>:<kind>:<author>:<d>`: what the relay answered for it (APP-11).
203
+ const files = new Map<string, Promise<SignedEvent[]>>()
204
+ const fileKey = (read: ListRead, d: string) => `${read.text ? 'text' : 'list'}:${read.kind}:${read.author}:${d}`
166
205
  const flights = new Map<string, Promise<SignedEvent[]>>()
167
206
 
168
207
  const remember = <K>(memo: Map<K, Held>, key: K, entry: Held) => {
@@ -192,7 +231,30 @@ export function createLoadQuery(query: QueryFn, options: LoadQueryOptions = {}):
192
231
  // Answered from a held read; `again` is what to ask if that read's page came back full.
193
232
  const joined: { again: Filter; entry: Promise<SignedEvent[] | null> }[] = []
194
233
  const joinedByTag: { shape: ByTag; value: string; entry: Promise<SignedEvent[] | null> }[] = []
234
+ const askedFiles: ListRead[] = []
235
+ // A Set, and the files this call asks, so a file two of its filters name is answered and asked once.
236
+ const joinedFiles = new Set<Promise<SignedEvent[]>>()
237
+ const askedHere = new Set<string>()
195
238
  for (const filter of filters) {
239
+ // Several Folders list the same files: each is read once per load, the rest of a list read goes on (APP-11).
240
+ const list = listReadOf(filter)
241
+ if (list) {
242
+ const fresh: string[] = []
243
+ for (const d of new Set(list.ds)) {
244
+ const key = fileKey(list, d)
245
+ const held = files.get(key)
246
+ if (held) joinedFiles.add(held)
247
+ else if (!askedHere.has(key)) {
248
+ askedHere.add(key)
249
+ fresh.push(d)
250
+ }
251
+ }
252
+ if (fresh.length === 0) continue
253
+ // `limit` counts first records: one per file asked.
254
+ send.push({ ...filter, '#d': fresh, limit: fresh.length })
255
+ askedFiles.push({ ...list, ds: fresh })
256
+ continue
257
+ }
196
258
  const whole = wholeKindsOf(filter, paged)
197
259
  if (whole) {
198
260
  const held = whole.kinds.map((kind) => wholes.get(kind))
@@ -226,6 +288,15 @@ export function createLoadQuery(query: QueryFn, options: LoadQueryOptions = {}):
226
288
  }
227
289
 
228
290
  const answer = send.length ? ask(send) : Promise.resolve([])
291
+ for (const read of askedFiles) {
292
+ for (const d of read.ds) {
293
+ const key = fileKey(read, d)
294
+ const entry = answer.then((events) => fileOf(read, d, events))
295
+ files.set(key, entry)
296
+ // A refused read is forgotten, so the next ask goes to the relay.
297
+ entry.catch(() => files.get(key) === entry && files.delete(key))
298
+ }
299
+ }
229
300
  for (const shape of askedWhole) {
230
301
  const split = answer.then((events) => completeWhole(shape, events, paged))
231
302
  for (const kind of shape.kinds) {
@@ -240,6 +311,7 @@ export function createLoadQuery(query: QueryFn, options: LoadQueryOptions = {}):
240
311
  }
241
312
 
242
313
  const out = [...(await answer)]
314
+ for (const entry of joinedFiles) out.push(...(await entry))
243
315
  const again: Filter[] = []
244
316
  for (const { again: filter, entry } of joined) {
245
317
  const events = await entry
package/src/projection.ts CHANGED
@@ -3748,7 +3748,7 @@ export async function resolveForeignObjects(
3748
3748
  : []
3749
3749
  const [read, opened, creatorLists, fileLists] = await Promise.all([
3750
3750
  queryChunked(filters, query),
3751
- openedEvents(plans.map(({ plan }) => plan), query),
3751
+ openedEvents(plans.map(({ plan }) => plan), query, !options.withoutText),
3752
3752
  Promise.all(kinds.map((kind) => creatorsOrNone(kind, query, cache))),
3753
3753
  Promise.all(folders.map((folder) => folderFilesOf(folder, query, lookupPeople, cache))),
3754
3754
  ])
@@ -3794,13 +3794,15 @@ export async function resolveForeignObjects(
3794
3794
  // it happened to be resolved beside.
3795
3795
  const own: People = {}
3796
3796
  for (const key of peopleOf(built)) if (key in people) own[key] = people[key]
3797
- results[reference] = withFiles(
3797
+ const object = withFiles(
3798
3798
  withBorrowed(
3799
3799
  withPeople(built, own),
3800
3800
  borrowedActions(plan.pointer.kind, plan.resolved, creatorsByKind.get(plan.pointer.kind) ?? []),
3801
3801
  ),
3802
3802
  plan.pointer.kind === KIND_CHANNEL_METADATA ? filesByFolder.get(plan.pointer.identifier) : undefined,
3803
3803
  )
3804
+ // Opened without its text: what it says was not read, as a list read's (APP-11).
3805
+ results[reference] = options.withoutText && plan.opened ? textUnread([object])[0]! : object
3804
3806
  }
3805
3807
  return results
3806
3808
  }
@@ -4334,6 +4336,14 @@ export interface ArchiveReadOptions {
4334
4336
  * card would pay it every time (SHI-13's one request per tick).
4335
4337
  */
4336
4338
  archivedWith?: boolean
4339
+ /**
4340
+ * With `openRead`, open each file by the list read (SPEC §6.13) without its
4341
+ * text: its first record and each field's current change, for a list that
4342
+ * draws a file's title, summary and conversation but never what it says.
4343
+ * Each opened file's `prevs.description` reads as unread (`''`), as a list
4344
+ * read's does. The Screener's rows (APP-11: 3.5 MB of text on Miky's Desk).
4345
+ */
4346
+ withoutText?: boolean
4337
4347
  /**
4338
4348
  * For a Folder's card: also list the files in it this reader can open, as
4339
4349
  * `children` — SPEC §3.2. What its relay-signed `30890` lists, read as
@@ -4756,7 +4766,7 @@ function planObject(
4756
4766
  * reader's — has its root and every change read as before, so a deleted file
4757
4767
  * still says who deleted it and an unreadable one is still told apart from it.
4758
4768
  */
4759
- async function openedEvents(plans: readonly ObjectPlan[], query: QueryFn): Promise<SignedEvent[]> {
4769
+ async function openedEvents(plans: readonly ObjectPlan[], query: QueryFn, text = true): Promise<SignedEvent[]> {
4760
4770
  const opened = new Map<string, ObjectPlan>()
4761
4771
  for (const plan of plans) if (plan.opened) opened.set(plan.address, plan)
4762
4772
  if (opened.size === 0) return []
@@ -4771,7 +4781,7 @@ async function openedEvents(plans: readonly ObjectPlan[], query: QueryFn): Promi
4771
4781
  for (const { kind, pubkey, identifiers } of groups.values()) {
4772
4782
  for (let start = 0; start < identifiers.length; start += RELAY_PAGE_CEILING) {
4773
4783
  const chunk = identifiers.slice(start, start + RELAY_PAGE_CEILING)
4774
- filters.push({ fields: 'newest', kinds: [kind], authors: [pubkey], '#d': chunk, limit: chunk.length, text: true })
4784
+ filters.push({ fields: 'newest', kinds: [kind], authors: [pubkey], '#d': chunk, limit: chunk.length, ...(text ? { text: true } : {}) })
4775
4785
  }
4776
4786
  }
4777
4787
  const answered = await queryChunked(filters, query)