@estiva-app/interop 0.75.0 → 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`,
@@ -1741,28 +1741,44 @@ function inTargetFolder(change, folders) {
1741
1741
  function foldChanges(changes, rule, root) {
1742
1742
  const fields = {};
1743
1743
  const folders = targetFolders(root);
1744
+ const id = root && tagValue(root, 'd');
1744
1745
  for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
1745
- const field = tagValue(change, rule.fieldTag);
1746
- const value = tagValue(change, rule.valueTag);
1747
- if (!field || value === undefined)
1748
- continue; // a partial change sets nothing
1749
- // A summary is new, so it takes neither of §6.2's exceptions: a change the
1750
- // reader cannot check against the root's Folder is not read (SPEC §6.12).
1751
- 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)
1752
1749
  continue;
1753
- // The format travels with the *winning* change, not with the object. SPEC
1754
- // §13.4: "an app MUST read the tag from the event it took the value from,
1755
- // never from the object's root" — a description created as marker text and
1756
- // later edited into blocks is a root with no tag and a change with one.
1757
- fields[field] = {
1758
- value,
1759
- by: change.pubkey,
1760
- at: change.created_at,
1761
- format: contentFormatOf(change),
1762
- };
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
+ }
1763
1766
  }
1764
1767
  return fields;
1765
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
+ }
1766
1782
  /**
1767
1783
  * One object's fold out of a pooled answer: its app's change kind, naming it as
1768
1784
  * the **first** target tag, as `archiveOf` reads it. Empty when the app declares
@@ -1835,6 +1851,7 @@ root, folderAdmins) {
1835
1851
  }
1836
1852
  const folders = targetFolders(root);
1837
1853
  let last;
1854
+ let archived;
1838
1855
  for (const change of changes) {
1839
1856
  // The target is the first target tag, as every other fold reads it: a
1840
1857
  // change archiving one file must not archive another it also mentions.
@@ -1842,12 +1859,16 @@ root, folderAdmins) {
1842
1859
  continue;
1843
1860
  if (!inTargetFolder(change, folders))
1844
1861
  continue;
1845
- 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)
1846
1867
  continue;
1847
1868
  if (!last || byOrder(last, change) < 0)
1848
- last = change;
1869
+ [last, archived] = [change, set.value];
1849
1870
  }
1850
- if (!last || tagValue(last, rule.valueTag) !== 'true')
1871
+ if (!last || archived !== 'true')
1851
1872
  return undefined;
1852
1873
  const note = last.content.trim();
1853
1874
  return {
@@ -4937,11 +4958,14 @@ async function readFolderRecords(query) {
4937
4958
  continue;
4938
4959
  const state = event.kind === KIND_FOLDER_STATE;
4939
4960
  const existing = byId.get(id);
4940
- 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 ?? [] };
4941
4964
  byId.set(id, {
4942
4965
  id,
4943
4966
  hasState: state || (existing?.hasState ?? false),
4944
4967
  ...(addresses.length ? { addresses } : {}),
4968
+ ...(ids.length ? { ids } : {}),
4945
4969
  });
4946
4970
  }
4947
4971
  // The title, privacy and admins are the channel's (§3.2), whichever order the events arrived.
@@ -5037,7 +5061,7 @@ export async function archiveImpact(address, query, options = {}) {
5037
5061
  for (const file of contents.files) {
5038
5062
  if (!file.address)
5039
5063
  continue;
5040
- 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));
5041
5065
  if (others.length) {
5042
5066
  staysVisible.push({ address: file.address, kind: file.kind, in: others.map(({ id, name }) => ({ id, name })) });
5043
5067
  continue;
@@ -5174,18 +5198,19 @@ async function countLiveChildren(parents, counted, query, cache) {
5174
5198
  */
5175
5199
  async function placeRecordChannels(events, byId, listedIn, query) {
5176
5200
  const containersOf = new Map();
5177
- for (const event of events) {
5178
- if (event.kind !== KIND_FOLDER_STATE)
5179
- 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) {
5180
5206
  const container = tagValue(event, 'd');
5181
5207
  if (!container)
5182
5208
  continue;
5183
- for (const tag of event.tags) {
5184
- if (tag[0] !== 'a' || !tag[1])
5185
- continue;
5209
+ const listed = listedEntriesOf(event, addressOfId);
5210
+ for (const address of [...listed.addresses, ...listed.ids.flatMap((id) => addressOfId.get(id) ?? [])]) {
5186
5211
  let pointer;
5187
5212
  try {
5188
- pointer = referenceToPointer(tag[1]);
5213
+ pointer = referenceToPointer(address);
5189
5214
  }
5190
5215
  catch {
5191
5216
  continue;
@@ -5339,7 +5364,7 @@ cache, options = {}) {
5339
5364
  ? // §5.2: one tag type lists every file, topics included, with no special
5340
5365
  // case. The order is the folder's, so it is preserved rather than sorted.
5341
5366
  {
5342
- addresses: [...new Set(state.tags.filter((t) => t[0] === 'a' && t[1]).map((t) => t[1]))],
5367
+ addresses: await listedAddressesOf(state, query),
5343
5368
  placed: new Map(),
5344
5369
  }
5345
5370
  : options.listedOnly
@@ -5578,6 +5603,54 @@ cache, options = {}) {
5578
5603
  source: state ? 'state' : 'channel',
5579
5604
  };
5580
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;
5581
5654
  /**
5582
5655
  * The contents of a folder that has no state event — containment by `h`.
5583
5656
  *