@estiva-app/interop 0.75.0 → 0.77.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, fileIdFormOf } 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`,
@@ -151,7 +151,7 @@ export const KIND_MESSAGE = 9;
151
151
  */
152
152
  const KIND_DELETION = 5;
153
153
  /** The tags a comment's body earns for what it names: `ActionEventArgs.linkTags`. */
154
- const LINK_TAG_NAMES = new Set(['a', 'q', 'part']);
154
+ const LINK_TAG_NAMES = new Set(['a', 'o', 'q', 'part']);
155
155
  /**
156
156
  * Every kind an app's comments might be under — what it publishes **now**, plus
157
157
  * any it has published before.
@@ -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 {
@@ -4512,7 +4533,7 @@ export function buildActionEvents(args) {
4512
4533
  const folders = [...new Map(args.listedIn.map((listing) => [listing.id, listing])).values()];
4513
4534
  return [
4514
4535
  first,
4515
- ...folders.flatMap((listing) => planUnlistFile(args.pubkey, args.createdAtMs, { folder: listing.id, address: args.address, hasState: listing.hasState })),
4536
+ ...folders.flatMap((listing) => planUnlistFile(args.pubkey, args.createdAtMs, { folder: listing.id, address: args.address, hasState: listing.hasState, inOrder: args.inOrder })),
4516
4537
  ];
4517
4538
  }
4518
4539
  if (isCreationAction(declared)) {
@@ -4525,7 +4546,7 @@ export function buildActionEvents(args) {
4525
4546
  return `"${declared.label}" lists what it creates, and a kind ${first.kind} has no address to list.`;
4526
4547
  }
4527
4548
  const created = pointerToAddress({ kind: first.kind, pubkey: first.pubkey, identifier, relays: [] });
4528
- return [first, ...planPlaceFile(args.pubkey, args.createdAtMs, { folder: args.folder, address: created, hasState: args.folderHasState })];
4549
+ return [first, ...planPlaceFile(args.pubkey, args.createdAtMs, { folder: args.folder, address: created, hasState: args.folderHasState, inOrder: args.inOrder })];
4529
4550
  }
4530
4551
  // A change or a comment adds nothing to a Folder and removes nothing from it.
4531
4552
  return [first];
@@ -4646,6 +4667,8 @@ function buildOneActionEvent(args) {
4646
4667
  // The file the change is about, by its id, beside its address (§6.2): the
4647
4668
  // relay keeps each field in order by it.
4648
4669
  const fileId = records.targetTag === 'a' ? identifierOf(address) : undefined;
4670
+ // A comment names its file by its id too once its type is kept in order (§6.4, "A comment in both forms").
4671
+ const byId = fileIdFormOf(args.inOrder ?? [], address);
4649
4672
  const tags = declared.emits.field
4650
4673
  ? [
4651
4674
  [records.targetTag, address],
@@ -4664,6 +4687,7 @@ function buildOneActionEvent(args) {
4664
4687
  ['A', address],
4665
4688
  ['K', String(kind)],
4666
4689
  ['P', args.objectAuthor],
4690
+ ...(byId ? [['O', byId.id], ['K', byId.type]] : []),
4667
4691
  ...(replyTo
4668
4692
  ? [
4669
4693
  ['e', replyTo.id],
@@ -4679,6 +4703,7 @@ function buildOneActionEvent(args) {
4679
4703
  : [
4680
4704
  ['a', address],
4681
4705
  ['k', String(kind)],
4706
+ ...(byId ? [['o', byId.id], ['k', byId.type]] : []),
4682
4707
  ['p', args.objectAuthor],
4683
4708
  ]),
4684
4709
  ['h', folder],
@@ -4718,6 +4743,9 @@ function buildOneActionEvent(args) {
4718
4743
  continue;
4719
4744
  if ((tag[0] === 'a' || tag[0] === 'part') && tag[1] === address)
4720
4745
  continue;
4746
+ // The comment's own file by its id is its `o`/`O` already, never a link.
4747
+ if ((tag[0] === 'o' || tag[0] === 'part') && fileId !== undefined && tag[1] === fileId)
4748
+ continue;
4721
4749
  if (tag[0] === 'q' && tag[1] === replyTo?.id)
4722
4750
  continue;
4723
4751
  if (!tags.some((t) => t[0] === tag[0] && t[1] === tag[1]))
@@ -4937,11 +4965,14 @@ async function readFolderRecords(query) {
4937
4965
  continue;
4938
4966
  const state = event.kind === KIND_FOLDER_STATE;
4939
4967
  const existing = byId.get(id);
4940
- const addresses = state ? event.tags.filter((tag) => tag[0] === 'a' && tag[1]).map((tag) => tag[1]) : (existing?.addresses ?? []);
4968
+ const { addresses, ids } = state
4969
+ ? listedEntriesOf(event, new Map())
4970
+ : { addresses: existing?.addresses ?? [], ids: existing?.ids ?? [] };
4941
4971
  byId.set(id, {
4942
4972
  id,
4943
4973
  hasState: state || (existing?.hasState ?? false),
4944
4974
  ...(addresses.length ? { addresses } : {}),
4975
+ ...(ids.length ? { ids } : {}),
4945
4976
  });
4946
4977
  }
4947
4978
  // The title, privacy and admins are the channel's (§3.2), whichever order the events arrived.
@@ -5037,7 +5068,7 @@ export async function archiveImpact(address, query, options = {}) {
5037
5068
  for (const file of contents.files) {
5038
5069
  if (!file.address)
5039
5070
  continue;
5040
- const others = folders.filter((other) => !inTree.has(other.id) && !other.archived && other.addresses?.includes(file.address));
5071
+ const others = folders.filter((other) => !inTree.has(other.id) && !other.archived && listsFile(other, file.address));
5041
5072
  if (others.length) {
5042
5073
  staysVisible.push({ address: file.address, kind: file.kind, in: others.map(({ id, name }) => ({ id, name })) });
5043
5074
  continue;
@@ -5174,18 +5205,19 @@ async function countLiveChildren(parents, counted, query, cache) {
5174
5205
  */
5175
5206
  async function placeRecordChannels(events, byId, listedIn, query) {
5176
5207
  const containersOf = new Map();
5177
- for (const event of events) {
5178
- if (event.kind !== KIND_FOLDER_STATE)
5179
- continue;
5208
+ const states = events.filter((event) => event.kind === KIND_FOLDER_STATE);
5209
+ // A file listed by its id alone (§3.3) costs one read for every such entry, before the roots.
5210
+ const ids = [...new Set(states.flatMap((state) => listedEntriesOf(state, new Map()).ids))];
5211
+ const addressOfId = ids.length ? await addressesOfIds(ids, query) : new Map();
5212
+ for (const event of states) {
5180
5213
  const container = tagValue(event, 'd');
5181
5214
  if (!container)
5182
5215
  continue;
5183
- for (const tag of event.tags) {
5184
- if (tag[0] !== 'a' || !tag[1])
5185
- continue;
5216
+ const listed = listedEntriesOf(event, addressOfId);
5217
+ for (const address of [...listed.addresses, ...listed.ids.flatMap((id) => addressOfId.get(id) ?? [])]) {
5186
5218
  let pointer;
5187
5219
  try {
5188
- pointer = referenceToPointer(tag[1]);
5220
+ pointer = referenceToPointer(address);
5189
5221
  }
5190
5222
  catch {
5191
5223
  continue;
@@ -5339,7 +5371,7 @@ cache, options = {}) {
5339
5371
  ? // §5.2: one tag type lists every file, topics included, with no special
5340
5372
  // case. The order is the folder's, so it is preserved rather than sorted.
5341
5373
  {
5342
- addresses: [...new Set(state.tags.filter((t) => t[0] === 'a' && t[1]).map((t) => t[1]))],
5374
+ addresses: await listedAddressesOf(state, query),
5343
5375
  placed: new Map(),
5344
5376
  }
5345
5377
  : options.listedOnly
@@ -5578,6 +5610,54 @@ cache, options = {}) {
5578
5610
  source: state ? 'state' : 'channel',
5579
5611
  };
5580
5612
  }
5613
+ /**
5614
+ * A Folder state's files as addresses, in its order, each once (SPEC §3.3).
5615
+ *
5616
+ * An `o` entry names a file by its id alone, so it costs a read: one request
5617
+ * for every such entry, asking each id's addressable roots, and an id is
5618
+ * the file whose one kind and author answer it, as a link by id is resolved
5619
+ * (§7.7). An id answered by none, or by two files, is dropped like an `a`
5620
+ * nothing parses. A state with no `o` entry reads nothing.
5621
+ */
5622
+ async function listedAddressesOf(state, query) {
5623
+ const all = listedEntriesOf(state, new Map());
5624
+ if (all.ids.length === 0)
5625
+ return all.order;
5626
+ const addressOf = await addressesOfIds(all.ids, query);
5627
+ // Folded against the addresses found: an `a` with an `o`'s id and another kind or author is drawn beside it.
5628
+ const { order, ids } = listedEntriesOf(state, addressOf);
5629
+ const byId = new Set(ids);
5630
+ const listed = order.flatMap((entry) => (byId.has(entry) ? (addressOf.get(entry) ?? []) : [entry]));
5631
+ return [...new Set(listed)];
5632
+ }
5633
+ /**
5634
+ * The address of each file named by its id alone: the one file kind and
5635
+ * author whose root holds that `d`, as a link by id is resolved (§7.7). Only
5636
+ * the file kinds are asked: the relay keeps a `d` unique across those alone,
5637
+ * so under any other kind anyone could claim a listed file's id. One request
5638
+ * for all of them; an id answered by none, or by two files, is absent.
5639
+ */
5640
+ async function addressesOfIds(ids, query) {
5641
+ const found = await queryChunked(Array.from({ length: Math.ceil(ids.length / ID_GROUP) }, (_, i) => {
5642
+ const group = ids.slice(i * ID_GROUP, (i + 1) * ID_GROUP);
5643
+ return { kinds: [...FILE_KINDS], '#d': group, limit: group.length * 2 };
5644
+ }), query);
5645
+ const asked = new Set(ids);
5646
+ const owners = new Map();
5647
+ for (const event of found) {
5648
+ const id = tagValue(event, 'd');
5649
+ if (id === undefined || !asked.has(id) || !FILE_KINDS.includes(event.kind))
5650
+ continue;
5651
+ owners.set(id, (owners.get(id) ?? new Set()).add(`${event.kind}:${event.pubkey.toLowerCase()}`));
5652
+ }
5653
+ const out = new Map();
5654
+ for (const [id, one] of owners)
5655
+ if (one.size === 1)
5656
+ out.set(id, `${[...one][0]}:${id}`);
5657
+ return out;
5658
+ }
5659
+ /** Ids per filter when a Folder state's `o` entries are resolved. */
5660
+ const ID_GROUP = 50;
5581
5661
  /**
5582
5662
  * The contents of a folder that has no state event — containment by `h`.
5583
5663
  *