@estiva-app/interop 0.83.0 → 0.85.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.
@@ -112,6 +112,8 @@ function pubkeysIn(object) {
112
112
  if (action.control === 'pubkey' && action.current)
113
113
  keys.push(action.current);
114
114
  }
115
+ if (object.deleted)
116
+ keys.push(object.deleted.by);
115
117
  return keys;
116
118
  }
117
119
  /** kind:0, the profile every other app publishes too. */
@@ -1753,6 +1755,10 @@ function foldChanges(changes, rule, root) {
1753
1755
  // reader cannot check against the root's Folder is not read (SPEC §6.12).
1754
1756
  if (field === SUMMARY_FIELD && !folders)
1755
1757
  continue;
1758
+ // A delete is by "anyone who may write in the file's team" (SPEC §6.5), and
1759
+ // a root that names no team has none: no change can delete it.
1760
+ if (field === DELETED_FIELD && !folders)
1761
+ continue;
1756
1762
  // The format travels with the *winning* change, not with the object. SPEC
1757
1763
  // §13.4: "an app MUST read the tag from the event it took the value from,
1758
1764
  // never from the object's root" — a description created as marker text and
@@ -1762,6 +1768,7 @@ function foldChanges(changes, rule, root) {
1762
1768
  by: change.pubkey,
1763
1769
  at: change.created_at,
1764
1770
  format: contentFormatOf(change),
1771
+ id: change.id,
1765
1772
  };
1766
1773
  }
1767
1774
  }
@@ -2342,6 +2349,17 @@ function resolveSlots(projection, root, folded, manifest) {
2342
2349
  }
2343
2350
  return { slots, meta };
2344
2351
  }
2352
+ /**
2353
+ * The change a change to `field` of `object` follows (SPEC §6.2): its current
2354
+ * change, else the first record. Undefined for the text of a file read by a
2355
+ * list, which carries no text: open it to write it.
2356
+ */
2357
+ export function prevOf(object, field) {
2358
+ const id = object.prevs?.[field];
2359
+ if (id === '')
2360
+ return undefined;
2361
+ return id ?? object.eventId;
2362
+ }
2345
2363
  /** The newest of a root's `created_at` and its folded fields' changes. */
2346
2364
  function changedAtOf(root, folded) {
2347
2365
  return Math.max(root.created_at, ...Object.values(folded).map((f) => f.at ?? 0));
@@ -2357,6 +2375,9 @@ function buildObject(args) {
2357
2375
  const naddr = encodeNaddr(pointer);
2358
2376
  const { slots, meta } = resolveSlots(projection, root, folded, manifest);
2359
2377
  const objectAddress = pointerToAddress(pointer);
2378
+ // A list read's text placeholder keeps its empty id: the text's `prev` is unknown there, not the first record.
2379
+ const prevs = Object.fromEntries(Object.entries(folded).flatMap(([field, f]) => (f.id !== undefined ? [[field, f.id]] : [])));
2380
+ const deleted = folded[DELETED_FIELD];
2360
2381
  return {
2361
2382
  // An addressable object's `ref` is its address: stable across the author
2362
2383
  // replacing the event, which the event id is not.
@@ -2383,8 +2404,12 @@ function buildObject(args) {
2383
2404
  viaRecommendation: args.viaRecommendation,
2384
2405
  ...(args.archived ? { archived: args.archived } : {}),
2385
2406
  changedAt: changedAtOf(root, folded),
2407
+ ...(Object.keys(prevs).length ? { prevs } : {}),
2408
+ ...(deleted?.value === 'true' && deleted.by ? { deleted: { by: deleted.by, at: deleted.at ?? root.created_at } } : {}),
2386
2409
  };
2387
2410
  }
2411
+ /** The field a file is deleted by (SPEC §6.5): `true` deletes it, the empty value restores it. */
2412
+ const DELETED_FIELD = 'deleted';
2388
2413
  /**
2389
2414
  * An addressable file's actions, each with what its field holds now, however
2390
2415
  * it got there.
@@ -2509,19 +2534,21 @@ cache, options = {}) {
2509
2534
  const resolved = await resolveManifest(pointer, query, cache);
2510
2535
  if (!resolved)
2511
2536
  return null;
2512
- const plan = planObject(naddr, pointer, resolved, depth);
2537
+ const plan = planObject(naddr, pointer, resolved, depth, options.openRead);
2513
2538
  if (!plan)
2514
2539
  return null;
2515
2540
  // Beside the object's own read, not after it: the sweep is cached, and cold
2516
2541
  // it would otherwise add a round trip to every resolve that pays for it.
2517
2542
  // A Folder card's files too, for the same reason.
2518
- const [answered, creators, files] = await Promise.all([
2543
+ const [read, opened, creators, files] = await Promise.all([
2519
2544
  query(plan.filters),
2545
+ openedEvents([plan], query),
2520
2546
  creatorsOrNone(pointer.kind, query, cache),
2521
2547
  options.folderFiles && pointer.kind === KIND_CHANNEL_METADATA
2522
2548
  ? folderFilesOf(pointer.identifier, query, lookupPeople, cache)
2523
2549
  : undefined,
2524
2550
  ]);
2551
+ const answered = [...read, ...opened];
2525
2552
  const followUp = childChangeFilters(plan, answered);
2526
2553
  const childChanges = followUp.length ? await queryChunked(followUp, query) : [];
2527
2554
  const childOwners = await childOwnersOf(plan, answered, childChanges, query, cache);
@@ -2608,7 +2635,7 @@ cache, options = {}) {
2608
2635
  }));
2609
2636
  const plans = asked.flatMap(({ reference, pointer }) => {
2610
2637
  const resolved = manifests.get(manifestKeyOf(pointer));
2611
- const plan = resolved && planObject(reference, pointer, resolved, 0);
2638
+ const plan = resolved && planObject(reference, pointer, resolved, 0, options.openRead);
2612
2639
  return plan ? [{ reference, plan }] : [];
2613
2640
  });
2614
2641
  if (plans.length === 0)
@@ -2632,11 +2659,13 @@ cache, options = {}) {
2632
2659
  ...new Set(plans.filter(({ plan }) => plan.pointer.kind === KIND_CHANNEL_METADATA).map(({ plan }) => plan.pointer.identifier)),
2633
2660
  ]
2634
2661
  : [];
2635
- const [events, creatorLists, fileLists] = await Promise.all([
2662
+ const [read, opened, creatorLists, fileLists] = await Promise.all([
2636
2663
  queryChunked(filters, query),
2664
+ openedEvents(plans.map(({ plan }) => plan), query),
2637
2665
  Promise.all(kinds.map((kind) => creatorsOrNone(kind, query, cache))),
2638
2666
  Promise.all(folders.map((folder) => folderFilesOf(folder, query, lookupPeople, cache))),
2639
2667
  ]);
2668
+ const events = [...read, ...opened];
2640
2669
  const creatorsByKind = new Map(kinds.map((kind, i) => [kind, creatorLists[i]]));
2641
2670
  const filesByFolder = new Map(folders.map((folder, i) => [folder, fileLists[i]]));
2642
2671
  // Every card's children's changes in one more read for the whole set, not
@@ -2837,7 +2866,8 @@ options = {}) {
2837
2866
  return [];
2838
2867
  // A relay refuses a request mixing list reads with plain ones, so each goes on its own.
2839
2868
  const events = (await Promise.all([queryChunked(filters, query), queryChunked(listed, query)])).flat();
2840
- const objects = projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)));
2869
+ const projected = projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)));
2870
+ const objects = options.listRead ? textUnread(projected) : projected;
2841
2871
  if (options.archivedWith) {
2842
2872
  const manifestOf = (object) => {
2843
2873
  const pointer = object.address ? addressPointerOf(object.address) : undefined;
@@ -2876,7 +2906,15 @@ export async function projectListedFiles(events, query, cache) {
2876
2906
  if (pointers.length === 0)
2877
2907
  return [];
2878
2908
  const manifests = await manifestsOf(pointers, query, cache);
2879
- return projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)));
2909
+ return textUnread(projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer))));
2910
+ }
2911
+ /**
2912
+ * Files read by a list carry no text (§6.13), and a change that set it with other
2913
+ * fields may be stale: the text's `prev` is unknown, `''`, so {@link prevOf}
2914
+ * answers nothing for it rather than an old change or the first record.
2915
+ */
2916
+ function textUnread(objects) {
2917
+ return objects.map((object) => FILE_KINDS.includes(object.kind) ? { ...object, prevs: { ...object.prevs, description: '' } } : object);
2880
2918
  }
2881
2919
  /**
2882
2920
  * The parent each file sits under, and nothing else — FOL-50.
@@ -3177,7 +3215,7 @@ function addressPointerOf(reference) {
3177
3215
  }
3178
3216
  }
3179
3217
  /** Null when the owning app has no projection for the kind. */
3180
- function planObject(naddr, pointer, resolved, depth) {
3218
+ function planObject(naddr, pointer, resolved, depth, openRead) {
3181
3219
  const { manifest } = resolved;
3182
3220
  const address = pointerToAddress(pointer);
3183
3221
  // Substituted here rather than in the component: `<bech32>` is a NIP-89
@@ -3205,18 +3243,55 @@ function planObject(naddr, pointer, resolved, depth) {
3205
3243
  one, and with the manifest memoised it is the whole cost of a tick.
3206
3244
  */
3207
3245
  const childFilter = childFilterFor({ projection, manifest, pointer, depth });
3208
- const filters = [
3246
+ const history = [
3209
3247
  { kinds: [pointer.kind], authors: [pointer.pubkey], '#d': [pointer.identifier], limit: 1 },
3210
3248
  // Only when the app actually declares a change kind — see `foldRuleOf`.
3211
3249
  ...(manifest.records ? [{ kinds: [manifest.records.changeKind], '#a': [address], limit: 500 }] : []),
3212
3250
  // Every file may be archived, including one whose app folds nothing
3213
3251
  // (FOL-46). SPEC's change kind, when the app has not named its own.
3214
3252
  ...(manifest.records ? [] : [{ kinds: [ARCHIVE_RULE.changeKind], '#a': [address], limit: 500 }]),
3253
+ ];
3254
+ const opened = !!openRead?.includes(pointer.kind) && FILE_KINDS.includes(pointer.kind);
3255
+ const filters = [
3256
+ ...(opened ? [] : history),
3215
3257
  { kinds: commentKinds, '#a': [address], limit: CONVERSATION_LIMIT },
3216
3258
  ...(childFilter ? [childFilter.filter] : []),
3217
3259
  ...(childFilter?.movedIn ? [childFilter.movedIn] : []),
3218
3260
  ];
3219
- return { naddr, pointer, address, resolved, projection, records, commentKinds, childFilter, filters, openUrl };
3261
+ return { naddr, pointer, address, resolved, projection, records, commentKinds, childFilter, filters, opened, history, openUrl };
3262
+ }
3263
+ /**
3264
+ * What the open read answers for the planned files it opens (SPEC §6.13): one
3265
+ * read per author and kind, apart from the plain reads, since a relay refuses a
3266
+ * request mixing the two. A file it does not answer — deleted, or not this
3267
+ * reader's — has its root and every change read as before, so a deleted file
3268
+ * still says who deleted it and an unreadable one is still told apart from it.
3269
+ */
3270
+ async function openedEvents(plans, query) {
3271
+ const opened = new Map();
3272
+ for (const plan of plans)
3273
+ if (plan.opened)
3274
+ opened.set(plan.address, plan);
3275
+ if (opened.size === 0)
3276
+ return [];
3277
+ const groups = new Map();
3278
+ for (const { pointer } of opened.values()) {
3279
+ const key = `${pointer.kind}:${pointer.pubkey}`;
3280
+ const group = groups.get(key) ?? { kind: pointer.kind, pubkey: pointer.pubkey, identifiers: [] };
3281
+ group.identifiers.push(pointer.identifier);
3282
+ groups.set(key, group);
3283
+ }
3284
+ const filters = [];
3285
+ for (const { kind, pubkey, identifiers } of groups.values()) {
3286
+ for (let start = 0; start < identifiers.length; start += RELAY_PAGE_CEILING) {
3287
+ const chunk = identifiers.slice(start, start + RELAY_PAGE_CEILING);
3288
+ filters.push({ fields: 'newest', kinds: [kind], authors: [pubkey], '#d': chunk, limit: chunk.length, text: true });
3289
+ }
3290
+ }
3291
+ const answered = await queryChunked(filters, query);
3292
+ const unanswered = [...opened.values()].filter(({ pointer }) => !answered.some((e) => e.kind === pointer.kind && e.pubkey === pointer.pubkey && tagValue(e, 'd') === pointer.identifier));
3293
+ const history = unanswered.length ? await queryChunked(unanswered.flatMap((plan) => plan.history), query) : [];
3294
+ return [...answered, ...history];
3220
3295
  }
3221
3296
  /**
3222
3297
  * Build a planned object out of whatever the relay answered.
@@ -5552,7 +5627,7 @@ cache, options = {}) {
5552
5627
  // other fields may be stale: the text is unread, never the seed the file
5553
5628
  // was created with drawn as if it were current.
5554
5629
  if (byFields(pointer.kind)) {
5555
- folded.description = { value: '', by: root.pubkey, at: root.created_at, format: 'marker' };
5630
+ folded.description = { value: '', by: root.pubkey, at: root.created_at, format: 'marker', id: '' };
5556
5631
  }
5557
5632
  const nestedAdmins = pointer.kind === KIND_CHANNEL
5558
5633
  ? new Set(folderAdminsOf(events.find((e) => e.kind === KIND_CHANNEL_ADMINS && tagValue(e, 'd') === pointer.identifier), root))
@@ -5685,6 +5760,51 @@ cache, options = {}) {
5685
5760
  source: state ? 'state' : 'channel',
5686
5761
  };
5687
5762
  }
5763
+ /**
5764
+ * The deleted files a Folder lists, each with its `deleted` (SPEC §6.5): what
5765
+ * an Archive's Deleted filter draws, with who deleted each and when. A deleted
5766
+ * file stays listed in its Folder and every list leaves it out
5767
+ * ({@link resolveFolderContents}), so this is a read of its own, made where it
5768
+ * is drawn and nowhere else.
5769
+ *
5770
+ * With `listRead`, the Folder's files of the relay's file kinds are listed by
5771
+ * their fields first, in one request, and only the ones that answer nothing —
5772
+ * deleted, or not this reader's — are read whole: a deleted file is in no read
5773
+ * by its fields (§6.13), but stays readable by its id. Without it, each is read
5774
+ * whole, as every read was before.
5775
+ */
5776
+ export async function resolveDeletedFiles(folder, query, lookupPeople, cache, options = {}) {
5777
+ const [state] = await query([{ kinds: [KIND_FOLDER_STATE], '#d': [folder], limit: 1 }]);
5778
+ const everyAddress = state && tagValue(state, 'd') === folder
5779
+ ? await listedAddressesOf(state, query)
5780
+ : (await addressesByContainment(folder, query)).addresses;
5781
+ const files = everyAddress.flatMap((address) => {
5782
+ const pointer = addressPointerOf(address);
5783
+ return pointer && FILE_KINDS.includes(pointer.kind) ? [{ address, pointer }] : [];
5784
+ });
5785
+ let asked = files.map(({ address }) => address);
5786
+ if (options.listRead && files.length) {
5787
+ const groups = new Map();
5788
+ for (const { pointer } of files) {
5789
+ const key = `${pointer.kind}:${pointer.pubkey}`;
5790
+ const group = groups.get(key);
5791
+ if (group)
5792
+ group.ds.push(pointer.identifier);
5793
+ else
5794
+ groups.set(key, { pointer, ds: [pointer.identifier] });
5795
+ }
5796
+ const listed = await queryChunked([...groups.values()].map(({ pointer, ds }) => ({ kinds: [pointer.kind], authors: [pointer.pubkey], '#d': ds, limit: ds.length, fields: 'newest' })), query);
5797
+ const answered = new Set(listed.filter((e) => FILE_KINDS.includes(e.kind)).map((e) => `${e.kind}:${e.pubkey}:${tagValue(e, 'd') ?? ''}`));
5798
+ asked = asked.filter((address) => !answered.has(address));
5799
+ }
5800
+ if (!asked.length)
5801
+ return [];
5802
+ const objects = await resolveForeignObjects(asked, query, lookupPeople, cache);
5803
+ return asked.flatMap((address) => {
5804
+ const object = objects[address];
5805
+ return object && !object.unreachable && object.deleted ? [object] : [];
5806
+ });
5807
+ }
5688
5808
  /**
5689
5809
  * A Folder state's files as addresses, in its order, each once (SPEC §3.3).
5690
5810
  *