@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.
- package/CHANGELOG.md +36 -0
- package/dist/folders.d.ts +30 -0
- package/dist/folders.d.ts.map +1 -1
- package/dist/folders.js +58 -2
- package/dist/folders.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mentions.d.ts +7 -1
- package/dist/mentions.d.ts.map +1 -1
- package/dist/mentions.js +21 -8
- package/dist/mentions.js.map +1 -1
- package/dist/projection.d.ts +6 -0
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +105 -32
- package/dist/projection.js.map +1 -1
- package/package.json +5 -5
- package/src/folders.ts +59 -1
- package/src/index.ts +1 -0
- package/src/mentions.ts +24 -8
- package/src/projection.ts +110 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@estiva-app/interop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.76.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",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"prepublishOnly": "npm run build"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@estiva-app/conversation": ">=0.
|
|
51
|
-
"@estiva-app/protocol": ">=0.
|
|
50
|
+
"@estiva-app/conversation": ">=0.26.0",
|
|
51
|
+
"@estiva-app/protocol": ">=0.50.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@estiva-app/conversation": ">=0.
|
|
55
|
-
"@estiva-app/protocol": ">=0.
|
|
54
|
+
"@estiva-app/conversation": ">=0.26.0",
|
|
55
|
+
"@estiva-app/protocol": ">=0.50.0",
|
|
56
56
|
"typescript": "~5.9.3"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/folders.ts
CHANGED
|
@@ -50,6 +50,7 @@ import {
|
|
|
50
50
|
buildDeleteChannel,
|
|
51
51
|
buildEditChannelMetadata,
|
|
52
52
|
buildFolderCommand,
|
|
53
|
+
isFileId,
|
|
53
54
|
KIND,
|
|
54
55
|
type ChannelVisibility,
|
|
55
56
|
type UnsignedEvent,
|
|
@@ -77,6 +78,8 @@ export interface FolderRef {
|
|
|
77
78
|
* file even when the file's row did not resolve.
|
|
78
79
|
*/
|
|
79
80
|
addresses?: readonly string[]
|
|
81
|
+
/** `FolderSummary.ids`: the files its state lists by id. Read with {@link listsFile}. */
|
|
82
|
+
ids?: readonly string[]
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
/**
|
|
@@ -230,6 +233,61 @@ export function identifierOf(address: string): string | undefined {
|
|
|
230
233
|
return /^\d+:[0-9a-f]{64}:([^]*)$/.exec(address)?.[1]
|
|
231
234
|
}
|
|
232
235
|
|
|
236
|
+
/**
|
|
237
|
+
* A Folder state's entries (SPEC §3.3), each file once, at its first entry: an
|
|
238
|
+
* `a` by its address, an `o` by its id. A file's address and its id are one
|
|
239
|
+
* entry only when the address is the full address of the file holding the id
|
|
240
|
+
* (its kind, its first record's author, the id), so an `o` and an `a` with
|
|
241
|
+
* that `d` fold into the first of them only then.
|
|
242
|
+
*
|
|
243
|
+
* `holders` is each id's address as a reader resolved it (`addressesOfIds` in
|
|
244
|
+
* projection): an `a` folds with an `o` only when it is that address, so one
|
|
245
|
+
* with another kind or author is drawn beside the file. A reader that has
|
|
246
|
+
* not resolved them passes an empty map, which folds nothing: both entries
|
|
247
|
+
* stay, as §3.3 says of a reader that cannot tell.
|
|
248
|
+
*/
|
|
249
|
+
export function listedEntriesOf(
|
|
250
|
+
state: { tags: string[][] },
|
|
251
|
+
holders: ReadonlyMap<string, string>,
|
|
252
|
+
): { addresses: string[]; ids: string[]; order: string[] } {
|
|
253
|
+
const addresses: string[] = []
|
|
254
|
+
const ids: string[] = []
|
|
255
|
+
const order: string[] = []
|
|
256
|
+
const seenAddress = new Set<string>()
|
|
257
|
+
const seenId = new Set<string>()
|
|
258
|
+
const idsOfAddresses = new Set<string>()
|
|
259
|
+
const holds = (address: string, id: string) => holders.get(id)?.toLowerCase() === address.toLowerCase()
|
|
260
|
+
for (const [name, value] of state.tags) {
|
|
261
|
+
if (!value) continue
|
|
262
|
+
if (name === 'a') {
|
|
263
|
+
const id = identifierOf(value)
|
|
264
|
+
const folded = id !== undefined && seenId.has(id) && holds(value, id)
|
|
265
|
+
if (seenAddress.has(value) || folded) continue
|
|
266
|
+
seenAddress.add(value)
|
|
267
|
+
if (id !== undefined && holds(value, id)) idsOfAddresses.add(id)
|
|
268
|
+
addresses.push(value)
|
|
269
|
+
order.push(value)
|
|
270
|
+
} else if (name === 'o') {
|
|
271
|
+
// An `o` is a file's id; any other value names no file (§3.3).
|
|
272
|
+
if (!isFileId(value) || seenId.has(value) || idsOfAddresses.has(value)) continue
|
|
273
|
+
seenId.add(value)
|
|
274
|
+
ids.push(value)
|
|
275
|
+
order.push(value)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return { addresses, ids, order }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Whether a Folder lists the file at `address`, by its address or by its id
|
|
283
|
+
* (`FolderSummary.addresses` and `.ids`, SPEC §3.3).
|
|
284
|
+
*/
|
|
285
|
+
export function listsFile(folder: { addresses?: readonly string[]; ids?: readonly string[] }, address: string): boolean {
|
|
286
|
+
if (folder.addresses?.includes(address)) return true
|
|
287
|
+
const id = identifierOf(address)
|
|
288
|
+
return id !== undefined && !!folder.ids?.includes(id)
|
|
289
|
+
}
|
|
290
|
+
|
|
233
291
|
/**
|
|
234
292
|
* A file as a move needs it. A `ForeignObject` from `resolveFolderContents` is
|
|
235
293
|
* one: `ref` is its identity, `parentRef` the file it is drawn beneath, and
|
|
@@ -422,7 +480,7 @@ export function planMoveFromFolders(
|
|
|
422
480
|
for (const from of sources) {
|
|
423
481
|
const listing = args.listings.get(from.id) ?? null
|
|
424
482
|
const unresolved =
|
|
425
|
-
!!listing && !!file.address &&
|
|
483
|
+
!!listing && !!file.address && listsFile(from, file.address) && !listing.some((candidate) => candidate.address === file.address)
|
|
426
484
|
const plan = planMoveFile(pubkey, createdAtMs, {
|
|
427
485
|
file,
|
|
428
486
|
from,
|
package/src/index.ts
CHANGED
package/src/mentions.ts
CHANGED
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/an-edit-says-what-the-message-links-now.md
|
|
16
16
|
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/activity-lists-only-mentions-from-elsewhere.md
|
|
17
17
|
*/
|
|
18
|
-
import {
|
|
19
|
-
import { toRenderTree, valuesIn, type LinkResolver, type SignedEvent } from '@estiva-app/protocol'
|
|
18
|
+
import { editsByTarget, editTargetOf, isAbout, isCommentOn, isCrossedCommentOn, KIND_COMMENT, KIND_EDIT, KIND_MESSAGE } from '@estiva-app/conversation'
|
|
19
|
+
import { fileIdOf, toRenderTree, valuesIn, type LinkResolver, type SignedEvent } from '@estiva-app/protocol'
|
|
20
20
|
import { linksIn, within, MAX_LINKS_ASKED, type LinkReader } from './links.js'
|
|
21
21
|
import type { QueryFn } from './projection.js'
|
|
22
22
|
|
|
@@ -50,11 +50,25 @@ export const MAX_MENTIONED_IN_LINKS_ASKED = 64
|
|
|
50
50
|
*/
|
|
51
51
|
const linksNaming = (tree: ReturnType<typeof toRenderTree>) => linksIn(tree).slice(0, MAX_LINKS_ASKED)
|
|
52
52
|
|
|
53
|
-
/** The
|
|
53
|
+
/** The filter for the candidates pointing at the file by its address: every message, comment and edit with its `a`. */
|
|
54
54
|
export function mentionedInFilter(address: string, limit = MENTIONED_IN_LIMIT): Record<string, unknown> {
|
|
55
55
|
return { kinds: [KIND_COMMENT, KIND_MESSAGE, KIND_EDIT], '#a': [address], limit }
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Every filter for the candidates: {@link mentionedInFilter}, and the one
|
|
60
|
+
* asking by the file's id, since a mention names a file by `o` alone once its
|
|
61
|
+
* type's apps name it by id (§6.4). One request either way.
|
|
62
|
+
*/
|
|
63
|
+
export function mentionedInFilters(address: string, limit = MENTIONED_IN_LIMIT): Record<string, unknown>[] {
|
|
64
|
+
const id = fileIdOf(address)
|
|
65
|
+
return [mentionedInFilter(address, limit), ...(id ? [{ kinds: [KIND_COMMENT, KIND_MESSAGE, KIND_EDIT], '#o': [id], limit }] : [])]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Whether a tag points at the file: an `a` with its address, or an `o` with its id. */
|
|
69
|
+
const pointsAt = (t: readonly string[], address: string, id: string | undefined) =>
|
|
70
|
+
(t[0] === 'a' && t[1] === address) || (id !== undefined && t[0] === 'o' && t[1] === id)
|
|
71
|
+
|
|
58
72
|
const isMessage = (event: SignedEvent) => event.kind === KIND_COMMENT || event.kind === KIND_MESSAGE
|
|
59
73
|
|
|
60
74
|
/**
|
|
@@ -79,8 +93,9 @@ export function mentionedInOf(
|
|
|
79
93
|
for (const event of events) if (isMessage(event)) messages.set(event.id.toLowerCase(), event)
|
|
80
94
|
const edits = editsByTarget(events)
|
|
81
95
|
const pointed = new Set<string>()
|
|
96
|
+
const fileId = fileIdOf(address)
|
|
82
97
|
for (const event of events) {
|
|
83
|
-
if (!event.tags.some((t) => t
|
|
98
|
+
if (!event.tags.some((t) => pointsAt(t, address, fileId))) continue
|
|
84
99
|
const id = event.kind === KIND_EDIT ? editTargetOf(event) : isMessage(event) ? event.id.toLowerCase() : undefined
|
|
85
100
|
if (id) pointed.add(id)
|
|
86
101
|
}
|
|
@@ -88,13 +103,14 @@ export function mentionedInOf(
|
|
|
88
103
|
const out: MentionedIn[] = []
|
|
89
104
|
for (const id of pointed) {
|
|
90
105
|
const event = messages.get(id)
|
|
91
|
-
|
|
92
|
-
if (
|
|
106
|
+
// A comment whose root and `O` disagree is ignored under every file (§6.4), its body's links included.
|
|
107
|
+
if (!event || isCrossedCommentOn(event, address)) continue
|
|
108
|
+
if ((event.kind === KIND_COMMENT && isAbout(event, address)) || isCommentOn(event, address, commentKinds)) continue
|
|
93
109
|
// An edit by anyone but the author is not the message's (the relay refuses one; a reader does not trust that).
|
|
94
110
|
const edit = edits.get(id)?.filter((e) => e.pubkey === event.pubkey).at(-1)
|
|
95
111
|
const body = edit?.content ?? event.content
|
|
96
112
|
const tree = toRenderTree(body, 'marker')
|
|
97
|
-
const pointsNow = (edit ?? event).tags.some((t) => t
|
|
113
|
+
const pointsNow = (edit ?? event).tags.some((t) => pointsAt(t, address, fileId))
|
|
98
114
|
const first = linksNaming(tree)
|
|
99
115
|
const named = new Set(first)
|
|
100
116
|
const resolve: LinkResolver | undefined = resolveLink && ((url) => (named.has(url) ? resolveLink(url) : undefined))
|
|
@@ -116,7 +132,7 @@ export async function readMentionedIn(
|
|
|
116
132
|
address: string,
|
|
117
133
|
options: { links?: LinkReader; limit?: number; waitMs?: number; commentKinds?: readonly number[] } = {},
|
|
118
134
|
): Promise<MentionedIn[]> {
|
|
119
|
-
const candidates = await query(
|
|
135
|
+
const candidates = await query(mentionedInFilters(address, options.limit))
|
|
120
136
|
const held = new Set(candidates.filter(isMessage).map((e) => e.id.toLowerCase()))
|
|
121
137
|
const missing = [...new Set(candidates.filter((e) => e.kind === KIND_EDIT).map(editTargetOf))].filter((id): id is string => !!id && !held.has(id))
|
|
122
138
|
const targets = missing.length ? await query([{ ids: missing, kinds: [KIND_COMMENT, KIND_MESSAGE] }]) : []
|
package/src/projection.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { decodeNevent, encodeNaddr, encodeNevent, pointerToAddress, referenceToPointer, type AddressPointer, type EventPointer } from '@estiva-app/protocol'
|
|
16
16
|
import { parseProfile, type Profile, type SignedEvent } 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 {
|
|
19
19
|
attachmentsInDocument,
|
|
20
20
|
BLOCK_DOCUMENT_FORMAT,
|
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
imetaTag,
|
|
23
23
|
parseBlockDocument,
|
|
24
24
|
} from '@estiva-app/protocol'
|
|
25
|
-
import { identifierOf, planPlaceFile, planUnlistFile, type FolderRef } from './folders.js'
|
|
25
|
+
import { identifierOf, listedEntriesOf, listsFile, planPlaceFile, planUnlistFile, type FolderRef } from './folders.js'
|
|
26
26
|
/*
|
|
27
27
|
SPEC §6.4's comment strength and §6.3's ordering are the conversation
|
|
28
28
|
package's (CON-5): one copy of each rule, read here for a file's `comments`,
|
|
@@ -2392,27 +2392,43 @@ function inTargetFolder(change: SignedEvent, folders: ReadonlySet<string> | unde
|
|
|
2392
2392
|
function foldChanges(changes: SignedEvent[], rule: RecordsRule, root: SignedEvent | undefined) {
|
|
2393
2393
|
const fields: Record<string, { value: string; by: string; at: number; format: ContentFormat }> = {}
|
|
2394
2394
|
const folders = targetFolders(root)
|
|
2395
|
+
const id = root && tagValue(root, 'd')
|
|
2395
2396
|
for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
|
|
2396
|
-
|
|
2397
|
-
const
|
|
2398
|
-
if (
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2397
|
+
// A change whose `O` is not its `a`'s file is about neither (SPEC §6.2).
|
|
2398
|
+
const O = tagValue(change, 'O')
|
|
2399
|
+
if (O !== undefined && id !== undefined && O !== id) continue
|
|
2400
|
+
for (const { field, value } of settingsOf(change, rule)) {
|
|
2401
|
+
// A summary is new, so it takes neither of §6.2's exceptions: a change the
|
|
2402
|
+
// reader cannot check against the root's Folder is not read (SPEC §6.12).
|
|
2403
|
+
if (field === SUMMARY_FIELD && !folders) continue
|
|
2404
|
+
// The format travels with the *winning* change, not with the object. SPEC
|
|
2405
|
+
// §13.4: "an app MUST read the tag from the event it took the value from,
|
|
2406
|
+
// never from the object's root" — a description created as marker text and
|
|
2407
|
+
// later edited into blocks is a root with no tag and a change with one.
|
|
2408
|
+
fields[field] = {
|
|
2409
|
+
value,
|
|
2410
|
+
by: change.pubkey,
|
|
2411
|
+
at: change.created_at,
|
|
2412
|
+
format: contentFormatOf(change),
|
|
2413
|
+
}
|
|
2411
2414
|
}
|
|
2412
2415
|
}
|
|
2413
2416
|
return fields
|
|
2414
2417
|
}
|
|
2415
2418
|
|
|
2419
|
+
/**
|
|
2420
|
+
* The fields one change sets under `rule`: one per `set` when it carries any
|
|
2421
|
+
* (SPEC §6.2, applied at the change's time as that many one-field changes),
|
|
2422
|
+
* else its field and value tags. A partial change sets nothing.
|
|
2423
|
+
*/
|
|
2424
|
+
function settingsOf(change: SignedEvent, rule: RecordsRule): { field: string; value: string }[] {
|
|
2425
|
+
const sets = change.tags.filter((t) => t[0] === 'set' && t[1] && typeof t[2] === 'string')
|
|
2426
|
+
if (sets.length) return sets.map((t) => ({ field: t[1], value: t[2] }))
|
|
2427
|
+
const field = tagValue(change, rule.fieldTag)
|
|
2428
|
+
const value = tagValue(change, rule.valueTag)
|
|
2429
|
+
return field && value !== undefined ? [{ field, value }] : []
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2416
2432
|
/**
|
|
2417
2433
|
* One object's fold out of a pooled answer: its app's change kind, naming it as
|
|
2418
2434
|
* the **first** target tag, as `archiveOf` reads it. Empty when the app declares
|
|
@@ -2522,15 +2538,19 @@ function archiveOf(
|
|
|
2522
2538
|
}
|
|
2523
2539
|
const folders = targetFolders(root)
|
|
2524
2540
|
let last: SignedEvent | undefined
|
|
2541
|
+
let archived: string | undefined
|
|
2525
2542
|
for (const change of changes) {
|
|
2526
2543
|
// The target is the first target tag, as every other fold reads it: a
|
|
2527
2544
|
// change archiving one file must not archive another it also mentions.
|
|
2528
2545
|
if (change.kind !== rule.changeKind || tagValue(change, rule.targetTag) !== address) continue
|
|
2529
2546
|
if (!inTargetFolder(change, folders)) continue
|
|
2530
|
-
|
|
2531
|
-
if (
|
|
2547
|
+
const O = tagValue(change, 'O')
|
|
2548
|
+
if (O !== undefined && O !== identifier) continue
|
|
2549
|
+
const set = settingsOf(change, rule).find((s) => s.field === ARCHIVED_FIELD)
|
|
2550
|
+
if (!set) continue
|
|
2551
|
+
if (!last || byOrder(last, change) < 0) [last, archived] = [change, set.value]
|
|
2532
2552
|
}
|
|
2533
|
-
if (!last ||
|
|
2553
|
+
if (!last || archived !== 'true') return undefined
|
|
2534
2554
|
const note = last.content.trim()
|
|
2535
2555
|
return {
|
|
2536
2556
|
by: last.pubkey,
|
|
@@ -6358,6 +6378,12 @@ export interface FolderSummary {
|
|
|
6358
6378
|
* another app's files resolves them with {@link resolveFolderContents}.
|
|
6359
6379
|
*/
|
|
6360
6380
|
addresses?: string[]
|
|
6381
|
+
/**
|
|
6382
|
+
* The files this folder's state lists by their id (an `o` entry, SPEC §3.3),
|
|
6383
|
+
* other than one it already lists by address — present only when there are
|
|
6384
|
+
* any. Ask whether a folder lists a file with `listsFile`, which reads both.
|
|
6385
|
+
*/
|
|
6386
|
+
ids?: string[]
|
|
6361
6387
|
/**
|
|
6362
6388
|
* The Folder's own address as a file — its `kind:39000`, `39000:<relay>:<id>`.
|
|
6363
6389
|
* What a state lists when it holds this Folder, and what an `archived`
|
|
@@ -6545,11 +6571,14 @@ async function readFolderRecords(query: QueryFn): Promise<{ events: SignedEvent[
|
|
|
6545
6571
|
if (event.kind !== KIND_FOLDER_STATE && event.kind !== KIND_CHANNEL) continue
|
|
6546
6572
|
const state = event.kind === KIND_FOLDER_STATE
|
|
6547
6573
|
const existing = byId.get(id)
|
|
6548
|
-
const addresses = state
|
|
6574
|
+
const { addresses, ids } = state
|
|
6575
|
+
? listedEntriesOf(event, new Map())
|
|
6576
|
+
: { addresses: existing?.addresses ?? [], ids: existing?.ids ?? [] }
|
|
6549
6577
|
byId.set(id, {
|
|
6550
6578
|
id,
|
|
6551
6579
|
hasState: state || (existing?.hasState ?? false),
|
|
6552
6580
|
...(addresses.length ? { addresses } : {}),
|
|
6581
|
+
...(ids.length ? { ids } : {}),
|
|
6553
6582
|
})
|
|
6554
6583
|
}
|
|
6555
6584
|
// The title, privacy and admins are the channel's (§3.2), whichever order the events arrived.
|
|
@@ -6668,7 +6697,7 @@ export async function archiveImpact(
|
|
|
6668
6697
|
for (const file of contents.files) {
|
|
6669
6698
|
if (!file.address) continue
|
|
6670
6699
|
const others = folders.filter(
|
|
6671
|
-
(other) => !inTree.has(other.id) && !other.archived && other
|
|
6700
|
+
(other) => !inTree.has(other.id) && !other.archived && listsFile(other, file.address!),
|
|
6672
6701
|
)
|
|
6673
6702
|
if (others.length) {
|
|
6674
6703
|
staysVisible.push({ address: file.address, kind: file.kind, in: others.map(({ id, name }) => ({ id, name })) })
|
|
@@ -6809,15 +6838,18 @@ async function placeRecordChannels(
|
|
|
6809
6838
|
query: QueryFn,
|
|
6810
6839
|
): Promise<{ recordsOf: Map<string, string[]>; archives: SignedEvent[]; rootOf: Map<string, SignedEvent> }> {
|
|
6811
6840
|
const containersOf = new Map<string, { pointer: AddressPointer; containers: string[] }>()
|
|
6812
|
-
|
|
6813
|
-
|
|
6841
|
+
const states = events.filter((event) => event.kind === KIND_FOLDER_STATE)
|
|
6842
|
+
// A file listed by its id alone (§3.3) costs one read for every such entry, before the roots.
|
|
6843
|
+
const ids = [...new Set(states.flatMap((state) => listedEntriesOf(state, new Map()).ids))]
|
|
6844
|
+
const addressOfId = ids.length ? await addressesOfIds(ids, query) : new Map<string, string>()
|
|
6845
|
+
for (const event of states) {
|
|
6814
6846
|
const container = tagValue(event, 'd')
|
|
6815
6847
|
if (!container) continue
|
|
6816
|
-
|
|
6817
|
-
|
|
6848
|
+
const listed = listedEntriesOf(event, addressOfId)
|
|
6849
|
+
for (const address of [...listed.addresses, ...listed.ids.flatMap((id) => addressOfId.get(id) ?? [])]) {
|
|
6818
6850
|
let pointer: AddressPointer
|
|
6819
6851
|
try {
|
|
6820
|
-
pointer = referenceToPointer(
|
|
6852
|
+
pointer = referenceToPointer(address)
|
|
6821
6853
|
} catch {
|
|
6822
6854
|
continue
|
|
6823
6855
|
}
|
|
@@ -7001,7 +7033,7 @@ export async function resolveFolderContents(
|
|
|
7001
7033
|
? // §5.2: one tag type lists every file, topics included, with no special
|
|
7002
7034
|
// case. The order is the folder's, so it is preserved rather than sorted.
|
|
7003
7035
|
{
|
|
7004
|
-
addresses:
|
|
7036
|
+
addresses: await listedAddressesOf(state, query),
|
|
7005
7037
|
placed: new Map<string, SignedEvent[]>(),
|
|
7006
7038
|
}
|
|
7007
7039
|
: options.listedOnly
|
|
@@ -7251,6 +7283,56 @@ export async function resolveFolderContents(
|
|
|
7251
7283
|
}
|
|
7252
7284
|
}
|
|
7253
7285
|
|
|
7286
|
+
/**
|
|
7287
|
+
* A Folder state's files as addresses, in its order, each once (SPEC §3.3).
|
|
7288
|
+
*
|
|
7289
|
+
* An `o` entry names a file by its id alone, so it costs a read: one request
|
|
7290
|
+
* for every such entry, asking each id's addressable roots, and an id is
|
|
7291
|
+
* the file whose one kind and author answer it, as a link by id is resolved
|
|
7292
|
+
* (§7.7). An id answered by none, or by two files, is dropped like an `a`
|
|
7293
|
+
* nothing parses. A state with no `o` entry reads nothing.
|
|
7294
|
+
*/
|
|
7295
|
+
async function listedAddressesOf(state: SignedEvent, query: QueryFn): Promise<string[]> {
|
|
7296
|
+
const all = listedEntriesOf(state, new Map())
|
|
7297
|
+
if (all.ids.length === 0) return all.order
|
|
7298
|
+
const addressOf = await addressesOfIds(all.ids, query)
|
|
7299
|
+
// Folded against the addresses found: an `a` with an `o`'s id and another kind or author is drawn beside it.
|
|
7300
|
+
const { order, ids } = listedEntriesOf(state, addressOf)
|
|
7301
|
+
const byId = new Set(ids)
|
|
7302
|
+
const listed = order.flatMap((entry) => (byId.has(entry) ? (addressOf.get(entry) ?? []) : [entry]))
|
|
7303
|
+
return [...new Set(listed)]
|
|
7304
|
+
}
|
|
7305
|
+
|
|
7306
|
+
/**
|
|
7307
|
+
* The address of each file named by its id alone: the one file kind and
|
|
7308
|
+
* author whose root holds that `d`, as a link by id is resolved (§7.7). Only
|
|
7309
|
+
* the file kinds are asked: the relay keeps a `d` unique across those alone,
|
|
7310
|
+
* so under any other kind anyone could claim a listed file's id. One request
|
|
7311
|
+
* for all of them; an id answered by none, or by two files, is absent.
|
|
7312
|
+
*/
|
|
7313
|
+
async function addressesOfIds(ids: readonly string[], query: QueryFn): Promise<Map<string, string>> {
|
|
7314
|
+
const found = await queryChunked(
|
|
7315
|
+
Array.from({ length: Math.ceil(ids.length / ID_GROUP) }, (_, i) => {
|
|
7316
|
+
const group = ids.slice(i * ID_GROUP, (i + 1) * ID_GROUP)
|
|
7317
|
+
return { kinds: [...FILE_KINDS], '#d': group, limit: group.length * 2 }
|
|
7318
|
+
}),
|
|
7319
|
+
query,
|
|
7320
|
+
)
|
|
7321
|
+
const asked = new Set(ids)
|
|
7322
|
+
const owners = new Map<string, Set<string>>()
|
|
7323
|
+
for (const event of found) {
|
|
7324
|
+
const id = tagValue(event, 'd')
|
|
7325
|
+
if (id === undefined || !asked.has(id) || !FILE_KINDS.includes(event.kind)) continue
|
|
7326
|
+
owners.set(id, (owners.get(id) ?? new Set()).add(`${event.kind}:${event.pubkey.toLowerCase()}`))
|
|
7327
|
+
}
|
|
7328
|
+
const out = new Map<string, string>()
|
|
7329
|
+
for (const [id, one] of owners) if (one.size === 1) out.set(id, `${[...one][0]}:${id}`)
|
|
7330
|
+
return out
|
|
7331
|
+
}
|
|
7332
|
+
|
|
7333
|
+
/** Ids per filter when a Folder state's `o` entries are resolved. */
|
|
7334
|
+
const ID_GROUP = 50
|
|
7335
|
+
|
|
7254
7336
|
/**
|
|
7255
7337
|
* The contents of a folder that has no state event — containment by `h`.
|
|
7256
7338
|
*
|