@estiva-app/interop 0.74.1 → 0.76.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.
@@ -14,9 +14,9 @@
14
14
  */
15
15
  import { decodeNevent, encodeNaddr, encodeNevent, pointerToAddress, referenceToPointer } from '@estiva-app/protocol';
16
16
  import { parseProfile } from '@estiva-app/protocol';
17
- import { MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol';
17
+ import { FILE_KINDS, MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol';
18
18
  import { attachmentsInDocument, BLOCK_DOCUMENT_FORMAT, CONTENT_FORMAT_TAG, imetaTag, parseBlockDocument, } from '@estiva-app/protocol';
19
- import { identifierOf, planPlaceFile, planUnlistFile } from './folders.js';
19
+ import { identifierOf, listedEntriesOf, listsFile, planPlaceFile, planUnlistFile } from './folders.js';
20
20
  /*
21
21
  SPEC §6.4's comment strength and §6.3's ordering are the conversation
22
22
  package's (CON-5): one copy of each rule, read here for a file's `comments`,
@@ -331,9 +331,14 @@ cache, options = {}) {
331
331
  const conversations = {};
332
332
  for (const a of asked)
333
333
  conversations[a.ref] = [];
334
- const filters = asked.flatMap((a) => options.replies
335
- ? [a.filter, { kinds: [KIND_COMMENT], '#A': [a.address], limit: CONVERSATION_LIMIT }]
336
- : [a.filter]);
334
+ const filters = asked.flatMap((a) => {
335
+ if (!options.replies)
336
+ return [a.filter];
337
+ const since = options.repliesSince?.(a.address);
338
+ if (since === null)
339
+ return [a.filter];
340
+ return [a.filter, { kinds: [KIND_COMMENT], '#A': [a.address], limit: CONVERSATION_LIMIT, ...(since === undefined ? {} : { since }) }];
341
+ });
337
342
  /** The file in this list an event is a comment on (§6.4), by every `a` it carries. */
338
343
  const commentedFile = (event) => event.tags
339
344
  .filter((t) => t[0] === 'a' && t[1] && refOf.has(t[1]) && isCommentOn(event, t[1], kindsOf.get(t[1])))
@@ -1736,28 +1741,44 @@ function inTargetFolder(change, folders) {
1736
1741
  function foldChanges(changes, rule, root) {
1737
1742
  const fields = {};
1738
1743
  const folders = targetFolders(root);
1744
+ const id = root && tagValue(root, 'd');
1739
1745
  for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
1740
- const field = tagValue(change, rule.fieldTag);
1741
- const value = tagValue(change, rule.valueTag);
1742
- if (!field || value === undefined)
1743
- continue; // a partial change sets nothing
1744
- // A summary is new, so it takes neither of §6.2's exceptions: a change the
1745
- // reader cannot check against the root's Folder is not read (SPEC §6.12).
1746
- if (field === SUMMARY_FIELD && !folders)
1746
+ // A change whose `O` is not its `a`'s file is about neither (SPEC §6.2).
1747
+ const O = tagValue(change, 'O');
1748
+ if (O !== undefined && id !== undefined && O !== id)
1747
1749
  continue;
1748
- // The format travels with the *winning* change, not with the object. SPEC
1749
- // §13.4: "an app MUST read the tag from the event it took the value from,
1750
- // never from the object's root" — a description created as marker text and
1751
- // later edited into blocks is a root with no tag and a change with one.
1752
- fields[field] = {
1753
- value,
1754
- by: change.pubkey,
1755
- at: change.created_at,
1756
- format: contentFormatOf(change),
1757
- };
1750
+ for (const { field, value } of settingsOf(change, rule)) {
1751
+ // A summary is new, so it takes neither of §6.2's exceptions: a change the
1752
+ // reader cannot check against the root's Folder is not read (SPEC §6.12).
1753
+ if (field === SUMMARY_FIELD && !folders)
1754
+ continue;
1755
+ // The format travels with the *winning* change, not with the object. SPEC
1756
+ // §13.4: "an app MUST read the tag from the event it took the value from,
1757
+ // never from the object's root" — a description created as marker text and
1758
+ // later edited into blocks is a root with no tag and a change with one.
1759
+ fields[field] = {
1760
+ value,
1761
+ by: change.pubkey,
1762
+ at: change.created_at,
1763
+ format: contentFormatOf(change),
1764
+ };
1765
+ }
1758
1766
  }
1759
1767
  return fields;
1760
1768
  }
1769
+ /**
1770
+ * The fields one change sets under `rule`: one per `set` when it carries any
1771
+ * (SPEC §6.2, applied at the change's time as that many one-field changes),
1772
+ * else its field and value tags. A partial change sets nothing.
1773
+ */
1774
+ function settingsOf(change, rule) {
1775
+ const sets = change.tags.filter((t) => t[0] === 'set' && t[1] && typeof t[2] === 'string');
1776
+ if (sets.length)
1777
+ return sets.map((t) => ({ field: t[1], value: t[2] }));
1778
+ const field = tagValue(change, rule.fieldTag);
1779
+ const value = tagValue(change, rule.valueTag);
1780
+ return field && value !== undefined ? [{ field, value }] : [];
1781
+ }
1761
1782
  /**
1762
1783
  * One object's fold out of a pooled answer: its app's change kind, naming it as
1763
1784
  * the **first** target tag, as `archiveOf` reads it. Empty when the app declares
@@ -1830,6 +1851,7 @@ root, folderAdmins) {
1830
1851
  }
1831
1852
  const folders = targetFolders(root);
1832
1853
  let last;
1854
+ let archived;
1833
1855
  for (const change of changes) {
1834
1856
  // The target is the first target tag, as every other fold reads it: a
1835
1857
  // change archiving one file must not archive another it also mentions.
@@ -1837,12 +1859,16 @@ root, folderAdmins) {
1837
1859
  continue;
1838
1860
  if (!inTargetFolder(change, folders))
1839
1861
  continue;
1840
- if (tagValue(change, rule.fieldTag) !== ARCHIVED_FIELD || tagValue(change, rule.valueTag) === undefined)
1862
+ const O = tagValue(change, 'O');
1863
+ if (O !== undefined && O !== identifier)
1864
+ continue;
1865
+ const set = settingsOf(change, rule).find((s) => s.field === ARCHIVED_FIELD);
1866
+ if (!set)
1841
1867
  continue;
1842
1868
  if (!last || byOrder(last, change) < 0)
1843
- last = change;
1869
+ [last, archived] = [change, set.value];
1844
1870
  }
1845
- if (!last || tagValue(last, rule.valueTag) !== 'true')
1871
+ if (!last || archived !== 'true')
1846
1872
  return undefined;
1847
1873
  const note = last.content.trim();
1848
1874
  return {
@@ -2763,7 +2789,9 @@ manifestFor) {
2763
2789
  * An address whose root the reader is not handed is simply absent, so a file
2764
2790
  * in a Folder they cannot read never comes back. A failed read rejects.
2765
2791
  */
2766
- export async function resolveForeignRoots(references, query, cache) {
2792
+ export async function resolveForeignRoots(references, query, cache,
2793
+ /** `archivedWith` marks a file archived with an archived parent, as {@link resolveForeignObject}'s does (SHIPS-2). */
2794
+ options = {}) {
2767
2795
  const pointers = new Map();
2768
2796
  for (const reference of references) {
2769
2797
  const pointer = addressPointerOf(reference);
@@ -2793,7 +2821,15 @@ export async function resolveForeignRoots(references, query, cache) {
2793
2821
  if (filters.length === 0)
2794
2822
  return [];
2795
2823
  const events = await queryChunked(filters, query);
2796
- return projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)));
2824
+ const objects = projectEvents(events, (pointer) => manifests.get(manifestKeyOf(pointer)));
2825
+ if (options.archivedWith) {
2826
+ const manifestOf = (object) => {
2827
+ const pointer = object.address ? addressPointerOf(object.address) : undefined;
2828
+ return pointer ? manifests.get(manifestKeyOf(pointer))?.manifest : undefined;
2829
+ };
2830
+ await markArchivedParents(objects.map((object) => ({ object, manifest: manifestOf(object) })), query, cache);
2831
+ }
2832
+ return objects;
2797
2833
  }
2798
2834
  /**
2799
2835
  * The parent each file sits under, and nothing else — FOL-50.
@@ -4922,11 +4958,14 @@ async function readFolderRecords(query) {
4922
4958
  continue;
4923
4959
  const state = event.kind === KIND_FOLDER_STATE;
4924
4960
  const existing = byId.get(id);
4925
- const addresses = state ? event.tags.filter((tag) => tag[0] === 'a' && tag[1]).map((tag) => tag[1]) : (existing?.addresses ?? []);
4961
+ const { addresses, ids } = state
4962
+ ? listedEntriesOf(event, new Map())
4963
+ : { addresses: existing?.addresses ?? [], ids: existing?.ids ?? [] };
4926
4964
  byId.set(id, {
4927
4965
  id,
4928
4966
  hasState: state || (existing?.hasState ?? false),
4929
4967
  ...(addresses.length ? { addresses } : {}),
4968
+ ...(ids.length ? { ids } : {}),
4930
4969
  });
4931
4970
  }
4932
4971
  // The title, privacy and admins are the channel's (§3.2), whichever order the events arrived.
@@ -5022,7 +5061,7 @@ export async function archiveImpact(address, query, options = {}) {
5022
5061
  for (const file of contents.files) {
5023
5062
  if (!file.address)
5024
5063
  continue;
5025
- const others = folders.filter((other) => !inTree.has(other.id) && !other.archived && other.addresses?.includes(file.address));
5064
+ const others = folders.filter((other) => !inTree.has(other.id) && !other.archived && listsFile(other, file.address));
5026
5065
  if (others.length) {
5027
5066
  staysVisible.push({ address: file.address, kind: file.kind, in: others.map(({ id, name }) => ({ id, name })) });
5028
5067
  continue;
@@ -5159,18 +5198,19 @@ async function countLiveChildren(parents, counted, query, cache) {
5159
5198
  */
5160
5199
  async function placeRecordChannels(events, byId, listedIn, query) {
5161
5200
  const containersOf = new Map();
5162
- for (const event of events) {
5163
- if (event.kind !== KIND_FOLDER_STATE)
5164
- continue;
5201
+ const states = events.filter((event) => event.kind === KIND_FOLDER_STATE);
5202
+ // A file listed by its id alone (§3.3) costs one read for every such entry, before the roots.
5203
+ const ids = [...new Set(states.flatMap((state) => listedEntriesOf(state, new Map()).ids))];
5204
+ const addressOfId = ids.length ? await addressesOfIds(ids, query) : new Map();
5205
+ for (const event of states) {
5165
5206
  const container = tagValue(event, 'd');
5166
5207
  if (!container)
5167
5208
  continue;
5168
- for (const tag of event.tags) {
5169
- if (tag[0] !== 'a' || !tag[1])
5170
- continue;
5209
+ const listed = listedEntriesOf(event, addressOfId);
5210
+ for (const address of [...listed.addresses, ...listed.ids.flatMap((id) => addressOfId.get(id) ?? [])]) {
5171
5211
  let pointer;
5172
5212
  try {
5173
- pointer = referenceToPointer(tag[1]);
5213
+ pointer = referenceToPointer(address);
5174
5214
  }
5175
5215
  catch {
5176
5216
  continue;
@@ -5324,7 +5364,7 @@ cache, options = {}) {
5324
5364
  ? // §5.2: one tag type lists every file, topics included, with no special
5325
5365
  // case. The order is the folder's, so it is preserved rather than sorted.
5326
5366
  {
5327
- addresses: [...new Set(state.tags.filter((t) => t[0] === 'a' && t[1]).map((t) => t[1]))],
5367
+ addresses: await listedAddressesOf(state, query),
5328
5368
  placed: new Map(),
5329
5369
  }
5330
5370
  : options.listedOnly
@@ -5563,6 +5603,54 @@ cache, options = {}) {
5563
5603
  source: state ? 'state' : 'channel',
5564
5604
  };
5565
5605
  }
5606
+ /**
5607
+ * A Folder state's files as addresses, in its order, each once (SPEC §3.3).
5608
+ *
5609
+ * An `o` entry names a file by its id alone, so it costs a read: one request
5610
+ * for every such entry, asking each id's addressable roots, and an id is
5611
+ * the file whose one kind and author answer it, as a link by id is resolved
5612
+ * (§7.7). An id answered by none, or by two files, is dropped like an `a`
5613
+ * nothing parses. A state with no `o` entry reads nothing.
5614
+ */
5615
+ async function listedAddressesOf(state, query) {
5616
+ const all = listedEntriesOf(state, new Map());
5617
+ if (all.ids.length === 0)
5618
+ return all.order;
5619
+ const addressOf = await addressesOfIds(all.ids, query);
5620
+ // Folded against the addresses found: an `a` with an `o`'s id and another kind or author is drawn beside it.
5621
+ const { order, ids } = listedEntriesOf(state, addressOf);
5622
+ const byId = new Set(ids);
5623
+ const listed = order.flatMap((entry) => (byId.has(entry) ? (addressOf.get(entry) ?? []) : [entry]));
5624
+ return [...new Set(listed)];
5625
+ }
5626
+ /**
5627
+ * The address of each file named by its id alone: the one file kind and
5628
+ * author whose root holds that `d`, as a link by id is resolved (§7.7). Only
5629
+ * the file kinds are asked: the relay keeps a `d` unique across those alone,
5630
+ * so under any other kind anyone could claim a listed file's id. One request
5631
+ * for all of them; an id answered by none, or by two files, is absent.
5632
+ */
5633
+ async function addressesOfIds(ids, query) {
5634
+ const found = await queryChunked(Array.from({ length: Math.ceil(ids.length / ID_GROUP) }, (_, i) => {
5635
+ const group = ids.slice(i * ID_GROUP, (i + 1) * ID_GROUP);
5636
+ return { kinds: [...FILE_KINDS], '#d': group, limit: group.length * 2 };
5637
+ }), query);
5638
+ const asked = new Set(ids);
5639
+ const owners = new Map();
5640
+ for (const event of found) {
5641
+ const id = tagValue(event, 'd');
5642
+ if (id === undefined || !asked.has(id) || !FILE_KINDS.includes(event.kind))
5643
+ continue;
5644
+ owners.set(id, (owners.get(id) ?? new Set()).add(`${event.kind}:${event.pubkey.toLowerCase()}`));
5645
+ }
5646
+ const out = new Map();
5647
+ for (const [id, one] of owners)
5648
+ if (one.size === 1)
5649
+ out.set(id, `${[...one][0]}:${id}`);
5650
+ return out;
5651
+ }
5652
+ /** Ids per filter when a Folder state's `o` entries are resolved. */
5653
+ const ID_GROUP = 50;
5566
5654
  /**
5567
5655
  * The contents of a folder that has no state event — containment by `h`.
5568
5656
  *