@drfed/graphql 0.2.0-dev.714 → 0.2.0-dev.717

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.
@@ -0,0 +1,177 @@
1
+ import { Account } from "@drfed/models/schema";
2
+ import { Database } from "@drfed/models";
3
+ import { PgColumn } from "drizzle-orm/pg-core";
4
+ import { Uuid } from "@drfed/models/uuid";
5
+ import { SQL, SQLWrapper } from "drizzle-orm";
6
+ //#region src/readable.d.ts
7
+ /**
8
+ * The IRIs that address an activity or object to the public: the full one and
9
+ * the compacted forms a JSON-LD document may carry.
10
+ */
11
+ declare const PUBLIC_IRIS: readonly string[];
12
+ /**
13
+ * Who reads, and the database to decide what they may read in.
14
+ */
15
+ interface Viewer {
16
+ readonly db: Database;
17
+ readonly account?: Account;
18
+ }
19
+ /**
20
+ * The local instances an account is an accepted member of; pending members,
21
+ * who have been invited but have not accepted yet, do not count.
22
+ * @returns A query of their IDs.
23
+ */
24
+ declare const memberInstances: (db: Database, accountId: Uuid, ...conditions: SQL[]) => Omit<import("drizzle-orm/pg-core").PgAsyncSelectBase<any, {
25
+ instanceId: any;
26
+ }, "partial", any, false, "from" | "where", any[], {
27
+ instanceId: any;
28
+ }>, "from" | "where">;
29
+ /**
30
+ * Restricts rows to those of the local instances whose private records the
31
+ * viewer may read: every local instance for an administrator, and those the
32
+ * viewer is an accepted member of otherwise, as the `admin` and
33
+ * `localInstanceMember` scopes decide for one instance. Filtering rows by it
34
+ * before paging keeps a connection from telling of rows the viewer may not
35
+ * read.
36
+ * @param viewer The viewer and the database.
37
+ * @param instanceId The column of the instance a row belongs to.
38
+ * @returns The condition on the column.
39
+ */
40
+ declare function viewableInstance(viewer: Viewer, instanceId: PgColumn): SQL;
41
+ /**
42
+ * Restricts resources to those the viewer may read. Received content is an
43
+ * inbox's, which ActivityPub filters by the requester's permission (5.2) and
44
+ * opens without authentication only when addressed to the public (5.6):
45
+ *
46
+ * - An administrator reads everything.
47
+ * - An activity of a remote actor is readable when it is addressed to the
48
+ * public, or when one of the local instances the viewer is a member of
49
+ * received it, i.e. an inbound delivery to it is linked to the activity
50
+ * and carried it as stored. A delivery of the same IRI with other
51
+ * contents stays linked but opens nothing.
52
+ * - An object of a remote actor is readable when it is addressed to the
53
+ * public, when the activity that carried it embedded in it is, or when
54
+ * one of the local instances the viewer is a member of received that
55
+ * activity and the object as stored with it. An object fetched apart
56
+ * from the activity is opened neither by the activity being public nor
57
+ * by a delivery but one that fetched it as stored.
58
+ * - Everything else, including the activities and objects of local actors
59
+ * whatever their addressing, is readable.
60
+ *
61
+ * Filtering rows by it before paging keeps a connection from telling of rows
62
+ * the viewer may not read.
63
+ * @param viewer The viewer and the database.
64
+ * @param id The resource ID in an outer query.
65
+ * @returns The condition on the ID.
66
+ */
67
+ declare function readableResource(viewer: Viewer, id: SQLWrapper): SQL;
68
+ /**
69
+ * Excludes deleted actors and objects, including resources authored or
70
+ * owned by deleted actors. Revisions do not use it: a deletion appends a
71
+ * whiteout, and the history before it stays readable, as
72
+ * {@link readableRevision} tells.
73
+ * @param id The resource ID in an outer query.
74
+ * @returns The condition on the ID.
75
+ */
76
+ declare function visibleResource(id: SQLWrapper): SQL;
77
+ /**
78
+ * Restricts revisions to those the viewer may read. An administrator reads
79
+ * every revision. Anyone else reads a revision by evidence of that
80
+ * particular revision, never by the current state of its resource:
81
+ *
82
+ * - DrFed saved it for a local actor, so it is readable by anyone, with
83
+ * its blind recipients and whether published or not, as
84
+ * `Object.document` is.
85
+ * - Its own addressing is public.
86
+ * - A receipt links it to an inbound delivery to a local instance the
87
+ * viewer may read, as {@link viewableInstance} tells.
88
+ * - It is the current revision of a resource with a legacy typed row that
89
+ * {@link readableResource} lets the viewer read, since that row
90
+ * describes only its current version.
91
+ *
92
+ * What a revision embeds, and the revisions its embedded documents came
93
+ * from, never decide whether it is readable: a document recording its
94
+ * sources reads as the same document pasted without them. Only following
95
+ * a source to its revision checks that revision.
96
+ *
97
+ * A later revision being restricted, or deleted, hides no earlier one that
98
+ * was public, and a later delivery or public revision of the same IRI opens
99
+ * no earlier private one. A revision of a deleted resource, or of one
100
+ * whose author is deleted, stays readable to whoever may read it.
101
+ * @param viewer The viewer and the database.
102
+ * @param id The revision ID in an outer query.
103
+ * @returns The condition on the ID.
104
+ */
105
+ declare function readableRevision(viewer: Viewer, id: SQLWrapper): SQL;
106
+ /**
107
+ * Restricts resources to those the viewer may know of as `Resource` nodes:
108
+ * those {@link readableResource} lets the viewer read, and those with a
109
+ * revision the viewer may read, as {@link readableRevision} tells. A
110
+ * resource's current state, which its legacy typed row describes, may be
111
+ * restricted after an earlier revision was public or received; that hides
112
+ * neither the revision nor the resource it is read through. The typed
113
+ * details of the resource still follow {@link readableResource}.
114
+ * @param viewer The viewer and the database.
115
+ * @param id The resource ID in an outer query.
116
+ * @returns The condition on the ID.
117
+ */
118
+ declare function readableResourceNode(viewer: Viewer, id: SQLWrapper): SQL;
119
+ /**
120
+ * Finds the revisions among the given IDs that the viewer may not read, as
121
+ * {@link readableRevision} decides. IDs of no stored revision are never
122
+ * among them. A local write takes it to check the sources a writer
123
+ * declares, so that a writer embeds as a snapshot or derivation only what
124
+ * they may read.
125
+ * @returns The IDs the viewer may not read.
126
+ */
127
+ declare function unreadableRevisions(viewer: Viewer, ids: readonly Uuid[]): Promise<ReadonlySet<Uuid>>;
128
+ /**
129
+ * Finds the resources among the given IDs that the viewer may not read, as
130
+ * {@link readableResource} decides. IDs of anything but a resource are never
131
+ * among them.
132
+ * @returns The IDs the viewer may not read.
133
+ */
134
+ declare function unreadableResources(viewer: Viewer, ids: readonly Uuid[]): Promise<ReadonlySet<Uuid>>;
135
+ /**
136
+ * Determines whether the viewer may read the resource.
137
+ * @returns Whether {@link readableResource} holds for it.
138
+ */
139
+ declare const isReadableResource: (viewer: Viewer, id: Uuid) => Promise<boolean>;
140
+ /**
141
+ * Finds the resources among the given IDs that the viewer may not know of as
142
+ * `Resource` nodes, as {@link readableResourceNode} decides. IDs of anything
143
+ * but a resource are never among them.
144
+ * @returns The IDs the viewer may not know of.
145
+ */
146
+ declare function unreadableResourceNodes(viewer: Viewer, ids: readonly Uuid[]): Promise<ReadonlySet<Uuid>>;
147
+ /**
148
+ * Determines whether the viewer may know of the resource as a `Resource`
149
+ * node.
150
+ * @returns Whether {@link readableResourceNode} holds for it.
151
+ */
152
+ declare const isReadableResourceNode: (viewer: Viewer, id: Uuid) => Promise<boolean>;
153
+ /**
154
+ * Tells the resource a Relay node belongs to without being its typed detail,
155
+ * as a revision belongs to its resource. Such a node is hidden by its own
156
+ * readability, as {@link readableRevision} tells, and nothing of its
157
+ * resource's, such as a deletion, hides it.
158
+ * @returns The resource ID, or null for any other node.
159
+ */
160
+ declare const owningResourceOf: (node: unknown) => Uuid | null;
161
+ /**
162
+ * Replaces the Relay nodes the viewer may not read by null, as if they did
163
+ * not exist. Activities, objects, and resources share their IDs, so the
164
+ * caller tells which nodes are `Resource` nodes, which are checked by
165
+ * {@link readableResourceNode}; the ID of any other node tells whether
166
+ * {@link readableResource} lets the viewer read it. A node that belongs to a
167
+ * resource, such as a revision, is checked by {@link readableRevision}
168
+ * instead, as {@link owningResourceOf} tells.
169
+ * @param viewer The viewer and the database.
170
+ * @param nodes The resolved nodes.
171
+ * @param resourceNodes Whether the node at each index is a `Resource` node.
172
+ * @returns The nodes, in the same order.
173
+ */
174
+ declare function hideUnreadableNodes(viewer: Viewer, nodes: readonly unknown[], resourceNodes?: readonly boolean[]): Promise<unknown[]>;
175
+ //#endregion
176
+ export { PUBLIC_IRIS, Viewer, hideUnreadableNodes, isReadableResource, isReadableResourceNode, memberInstances, owningResourceOf, readableResource, readableResourceNode, readableRevision, unreadableResourceNodes, unreadableResources, unreadableRevisions, viewableInstance, visibleResource };
177
+ //# sourceMappingURL=readable.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readable.d.mts","names":[],"sources":["../src/readable.ts"],"mappings":";;;;;;;;;;cAmDa;;;;UASI;WACN,IAAI;WACJ,UAAU;;;;;;;cAQR,kBAAe,IACtB,UAAQ,WACD,SAAI,YACA,UAAK,mCAAA;;;;;;;;;;;;;;;;iBA0BN,iBAAiB,QAAQ,QAAQ,YAAY,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqHxD,iBAAiB,QAAQ,QAAQ,IAAI,aAAa;;;;;;;;;iBAalD,gBAAgB,IAAI,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoEjC,iBAAiB,QAAQ,QAAQ,IAAI,aAAa;;;;;;;;;;;;;iBA4ClD,qBAAqB,QAAQ,QAAQ,IAAI,aAAa;;;;;;;;;iBAiBhD,oBACpB,QAAQ,QACR,cAAc,SACb,QAAQ,YAAY;;;;;;;iBAoBD,oBACpB,QAAQ,QACR,cAAc,SACb,QAAQ,YAAY;;;;;cAkBV,qBAAkB,QACrB,QAAM,IACV,SACH;;;;;;;iBAQmB,wBACpB,QAAQ,QACR,cAAc,SACb,QAAQ,YAAY;;;;;;cAmBV,yBAAsB,QACzB,QAAM,IACV,SACH;;;;;;;;cAiBU,mBAAgB,kBAAoB;;;;;;;;;;;;;;iBAmB3B,oBACpB,QAAQ,QACR,2BACA,qCACC"}
@@ -0,0 +1,339 @@
1
+ import { activities, activityDeliveries, actors, addressing, collections, instanceMembers, instances, objects, resourceHeads, resourceRevisions, resources, revisionAddressing, revisionReceipts } from "@drfed/models/schema";
2
+ import { alias } from "drizzle-orm/pg-core";
3
+ import { PUBLIC_IRI } from "@drfed/models/resource";
4
+ import { validateUuid } from "@drfed/models/uuid";
5
+ import { and, eq, inArray, isNotNull, not, sql } from "drizzle-orm";
6
+ //#region src/readable.ts
7
+ /**
8
+ * The IRIs that address an activity or object to the public: the full one and
9
+ * the compacted forms a JSON-LD document may carry.
10
+ */
11
+ const PUBLIC_IRIS = [
12
+ PUBLIC_IRI,
13
+ "as:Public",
14
+ "Public"
15
+ ];
16
+ /**
17
+ * The local instances an account is an accepted member of; pending members,
18
+ * who have been invited but have not accepted yet, do not count.
19
+ * @returns A query of their IDs.
20
+ */
21
+ const memberInstances = (db, accountId, ...conditions) => db.select({ instanceId: instanceMembers.instanceId }).from(instanceMembers).innerJoin(instances, eq(instanceMembers.instanceId, instances.id)).where(and(eq(instanceMembers.accountId, accountId), isNotNull(instanceMembers.accepted), isNotNull(instances.localId), ...conditions));
22
+ /**
23
+ * Restricts rows to those of the local instances whose private records the
24
+ * viewer may read: every local instance for an administrator, and those the
25
+ * viewer is an accepted member of otherwise, as the `admin` and
26
+ * `localInstanceMember` scopes decide for one instance. Filtering rows by it
27
+ * before paging keeps a connection from telling of rows the viewer may not
28
+ * read.
29
+ * @param viewer The viewer and the database.
30
+ * @param instanceId The column of the instance a row belongs to.
31
+ * @returns The condition on the column.
32
+ */
33
+ function viewableInstance(viewer, instanceId) {
34
+ const { account } = viewer;
35
+ if (account == null) return sql`false`;
36
+ return account.admin ? inArray(instanceId, viewer.db.select({ id: instances.id }).from(instances).where(isNotNull(instances.localId))) : inArray(instanceId, memberInstances(viewer.db, account.id));
37
+ }
38
+ const authors = alias(actors, "readable_authors");
39
+ const remoteActivities = alias(activities, "readable_remote_activities");
40
+ const remoteObjects = alias(objects, "readable_remote_objects");
41
+ const carriers = alias(activities, "readable_carriers");
42
+ const entries = alias(addressing, "readable_addressing");
43
+ const targets = alias(resources, "readable_targets");
44
+ const deliveries = alias(activityDeliveries, "readable_deliveries");
45
+ const visibleOwners = alias(actors, "visible_owners");
46
+ const ownedObjects = alias(objects, "visible_objects");
47
+ const ownedActivities = alias(activities, "visible_activities");
48
+ const ownedCollections = alias(collections, "visible_collections");
49
+ const typedActors = alias(actors, "legacy_typed_actors");
50
+ const typedObjects = alias(objects, "legacy_typed_objects");
51
+ const typedActivities = alias(activities, "legacy_typed_activities");
52
+ const typedCollections = alias(collections, "legacy_typed_collections");
53
+ const evidenced = alias(resourceRevisions, "evidenced_revisions");
54
+ const evidencedAddressing = alias(revisionAddressing, "evidenced_addressing");
55
+ const evidencedReceipts = alias(revisionReceipts, "evidenced_receipts");
56
+ const receiptDeliveries = alias(activityDeliveries, "receipt_deliveries");
57
+ const evidencedHeads = alias(resourceHeads, "evidenced_heads");
58
+ const listedRevisions = alias(resourceRevisions, "listed_revisions");
59
+ const remoteActivity = (id) => sql`exists (
60
+ select 1 from ${activities} ${remoteActivities}
61
+ join ${actors} ${authors} on ${authors.id} = ${remoteActivities.actorId}
62
+ where ${remoteActivities.id} = ${id} and ${authors.localId} is null
63
+ )`;
64
+ const remoteObject = (id) => sql`exists (
65
+ select 1 from ${objects} ${remoteObjects}
66
+ join ${actors} ${authors} on ${authors.id} = ${remoteObjects.actorId}
67
+ where ${remoteObjects.id} = ${id} and ${authors.localId} is null
68
+ )`;
69
+ const addressedPublicly = (id) => sql`exists (
70
+ select 1 from ${addressing} ${entries}
71
+ join ${resources} ${targets} on ${targets.id} = ${entries.targetId}
72
+ where ${entries.sourceId} = ${id} and ${inArray(targets.iri, PUBLIC_IRIS)}
73
+ )`;
74
+ const received = (viewer, id, mark) => sql`exists (
75
+ select 1 from ${activityDeliveries} ${deliveries}
76
+ where ${deliveries.activityId} = ${id}
77
+ and ${deliveries.direction} = 'inbound'
78
+ and ${mark}
79
+ and ${viewableInstance(viewer, deliveries.instanceId)}
80
+ )`;
81
+ const readableActivity = (viewer, id) => sql`(not ${remoteActivity(id)} or ${addressedPublicly(id)} or ${received(viewer, id, deliveries.carriedSnapshot)})`;
82
+ const readableObject = (viewer, id) => sql`(not ${remoteObject(id)} or ${addressedPublicly(id)} or exists (
83
+ select 1 from ${objects} ${remoteObjects}
84
+ join ${activities} ${carriers} on ${carriers.id} = ${remoteObjects.activityId}
85
+ where ${remoteObjects.id} = ${id}
86
+ and (
87
+ (not ${remoteObjects.fetched} and ${addressedPublicly(carriers.id)})
88
+ or ${received(viewer, carriers.id, deliveries.carriedObjectSnapshot)}
89
+ )
90
+ ))`;
91
+ /**
92
+ * Restricts resources to those the viewer may read. Received content is an
93
+ * inbox's, which ActivityPub filters by the requester's permission (5.2) and
94
+ * opens without authentication only when addressed to the public (5.6):
95
+ *
96
+ * - An administrator reads everything.
97
+ * - An activity of a remote actor is readable when it is addressed to the
98
+ * public, or when one of the local instances the viewer is a member of
99
+ * received it, i.e. an inbound delivery to it is linked to the activity
100
+ * and carried it as stored. A delivery of the same IRI with other
101
+ * contents stays linked but opens nothing.
102
+ * - An object of a remote actor is readable when it is addressed to the
103
+ * public, when the activity that carried it embedded in it is, or when
104
+ * one of the local instances the viewer is a member of received that
105
+ * activity and the object as stored with it. An object fetched apart
106
+ * from the activity is opened neither by the activity being public nor
107
+ * by a delivery but one that fetched it as stored.
108
+ * - Everything else, including the activities and objects of local actors
109
+ * whatever their addressing, is readable.
110
+ *
111
+ * Filtering rows by it before paging keeps a connection from telling of rows
112
+ * the viewer may not read.
113
+ * @param viewer The viewer and the database.
114
+ * @param id The resource ID in an outer query.
115
+ * @returns The condition on the ID.
116
+ */
117
+ function readableResource(viewer, id) {
118
+ if (viewer.account?.admin === true) return sql`true`;
119
+ return sql`(${readableActivity(viewer, id)} and ${readableObject(viewer, id)})`;
120
+ }
121
+ /**
122
+ * Excludes deleted actors and objects, including resources authored or
123
+ * owned by deleted actors. Revisions do not use it: a deletion appends a
124
+ * whiteout, and the history before it stays readable, as
125
+ * {@link readableRevision} tells.
126
+ * @param id The resource ID in an outer query.
127
+ * @returns The condition on the ID.
128
+ */
129
+ function visibleResource(id) {
130
+ return sql`not exists (
131
+ select 1 from ${actors} ${visibleOwners}
132
+ where ${visibleOwners.id} = ${id} and ${visibleOwners.deleted} is not null
133
+ ) and not exists (
134
+ select 1 from ${objects} ${ownedObjects}
135
+ join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedObjects.actorId}
136
+ where ${ownedObjects.id} = ${id}
137
+ and (${ownedObjects.deleted} is not null or ${visibleOwners.deleted} is not null)
138
+ ) and not exists (
139
+ select 1 from ${activities} ${ownedActivities}
140
+ join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedActivities.actorId}
141
+ where ${ownedActivities.id} = ${id} and ${visibleOwners.deleted} is not null
142
+ ) and not exists (
143
+ select 1 from ${collections} ${ownedCollections}
144
+ join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedCollections.ownerActorId}
145
+ where ${ownedCollections.id} = ${id} and ${visibleOwners.deleted} is not null
146
+ )`;
147
+ }
148
+ /**
149
+ * Matches a resource with a legacy typed row, whose readability
150
+ * {@link readableResource} decides by that row. It is the only place
151
+ * {@link readableRevision} reads legacy typed tables, so that it goes when
152
+ * they do.
153
+ * @param id The resource ID in an outer query.
154
+ * @returns The condition on the ID.
155
+ */
156
+ function legacyTypedRow(id) {
157
+ return sql`(exists (
158
+ select 1 from ${actors} ${typedActors} where ${typedActors.id} = ${id}
159
+ ) or exists (
160
+ select 1 from ${objects} ${typedObjects} where ${typedObjects.id} = ${id}
161
+ ) or exists (
162
+ select 1 from ${activities} ${typedActivities} where ${typedActivities.id} = ${id}
163
+ ) or exists (
164
+ select 1 from ${collections} ${typedCollections} where ${typedCollections.id} = ${id}
165
+ ))`;
166
+ }
167
+ /**
168
+ * Restricts revisions to those the viewer may read. An administrator reads
169
+ * every revision. Anyone else reads a revision by evidence of that
170
+ * particular revision, never by the current state of its resource:
171
+ *
172
+ * - DrFed saved it for a local actor, so it is readable by anyone, with
173
+ * its blind recipients and whether published or not, as
174
+ * `Object.document` is.
175
+ * - Its own addressing is public.
176
+ * - A receipt links it to an inbound delivery to a local instance the
177
+ * viewer may read, as {@link viewableInstance} tells.
178
+ * - It is the current revision of a resource with a legacy typed row that
179
+ * {@link readableResource} lets the viewer read, since that row
180
+ * describes only its current version.
181
+ *
182
+ * What a revision embeds, and the revisions its embedded documents came
183
+ * from, never decide whether it is readable: a document recording its
184
+ * sources reads as the same document pasted without them. Only following
185
+ * a source to its revision checks that revision.
186
+ *
187
+ * A later revision being restricted, or deleted, hides no earlier one that
188
+ * was public, and a later delivery or public revision of the same IRI opens
189
+ * no earlier private one. A revision of a deleted resource, or of one
190
+ * whose author is deleted, stays readable to whoever may read it.
191
+ * @param viewer The viewer and the database.
192
+ * @param id The revision ID in an outer query.
193
+ * @returns The condition on the ID.
194
+ */
195
+ function readableRevision(viewer, id) {
196
+ if (viewer.account?.admin === true) return sql`true`;
197
+ return sql`exists (
198
+ select 1 from ${resourceRevisions} ${evidenced}
199
+ where ${evidenced.id} = ${id} and (
200
+ ${evidenced.capture} = 'local'
201
+ or exists (
202
+ select 1 from ${revisionAddressing} ${evidencedAddressing}
203
+ where ${evidencedAddressing.revisionId} = ${evidenced.id}
204
+ and ${inArray(evidencedAddressing.iri, PUBLIC_IRIS)}
205
+ )
206
+ or exists (
207
+ select 1 from ${revisionReceipts} ${evidencedReceipts}
208
+ join ${activityDeliveries} ${receiptDeliveries}
209
+ on ${receiptDeliveries.id} = ${evidencedReceipts.deliveryId}
210
+ where ${evidencedReceipts.revisionId} = ${evidenced.id}
211
+ and ${receiptDeliveries.direction} = 'inbound'
212
+ and ${viewableInstance(viewer, receiptDeliveries.instanceId)}
213
+ )
214
+ or (
215
+ exists (
216
+ select 1 from ${resourceHeads} ${evidencedHeads}
217
+ where ${evidencedHeads.resourceId} = ${evidenced.resourceId}
218
+ and ${evidencedHeads.revisionId} = ${evidenced.id}
219
+ )
220
+ and ${legacyTypedRow(evidenced.resourceId)}
221
+ and ${readableResource(viewer, evidenced.resourceId)}
222
+ )
223
+ )
224
+ )`;
225
+ }
226
+ /**
227
+ * Restricts resources to those the viewer may know of as `Resource` nodes:
228
+ * those {@link readableResource} lets the viewer read, and those with a
229
+ * revision the viewer may read, as {@link readableRevision} tells. A
230
+ * resource's current state, which its legacy typed row describes, may be
231
+ * restricted after an earlier revision was public or received; that hides
232
+ * neither the revision nor the resource it is read through. The typed
233
+ * details of the resource still follow {@link readableResource}.
234
+ * @param viewer The viewer and the database.
235
+ * @param id The resource ID in an outer query.
236
+ * @returns The condition on the ID.
237
+ */
238
+ function readableResourceNode(viewer, id) {
239
+ if (viewer.account?.admin === true) return sql`true`;
240
+ return sql`(${readableResource(viewer, id)} or exists (
241
+ select 1 from ${resourceRevisions} ${listedRevisions}
242
+ where ${listedRevisions.resourceId} = ${id}
243
+ and ${readableRevision(viewer, listedRevisions.id)}
244
+ ))`;
245
+ }
246
+ /**
247
+ * Finds the revisions among the given IDs that the viewer may not read, as
248
+ * {@link readableRevision} decides. IDs of no stored revision are never
249
+ * among them. A local write takes it to check the sources a writer
250
+ * declares, so that a writer embeds as a snapshot or derivation only what
251
+ * they may read.
252
+ * @returns The IDs the viewer may not read.
253
+ */
254
+ async function unreadableRevisions(viewer, ids) {
255
+ if (ids.length === 0 || viewer.account?.admin === true) return /* @__PURE__ */ new Set();
256
+ const rows = await viewer.db.select({ id: resourceRevisions.id }).from(resourceRevisions).where(and(inArray(resourceRevisions.id, [...new Set(ids)]), not(readableRevision(viewer, resourceRevisions.id))));
257
+ return new Set(rows.map(({ id }) => id));
258
+ }
259
+ /**
260
+ * Finds the resources among the given IDs that the viewer may not read, as
261
+ * {@link readableResource} decides. IDs of anything but a resource are never
262
+ * among them.
263
+ * @returns The IDs the viewer may not read.
264
+ */
265
+ async function unreadableResources(viewer, ids) {
266
+ if (ids.length === 0 || viewer.account?.admin === true) return /* @__PURE__ */ new Set();
267
+ const rows = await viewer.db.select({ id: resources.id }).from(resources).where(and(inArray(resources.id, [...ids]), not(readableResource(viewer, resources.id))));
268
+ return new Set(rows.map(({ id }) => id));
269
+ }
270
+ /**
271
+ * Determines whether the viewer may read the resource.
272
+ * @returns Whether {@link readableResource} holds for it.
273
+ */
274
+ const isReadableResource = async (viewer, id) => !(await unreadableResources(viewer, [id])).has(id);
275
+ /**
276
+ * Finds the resources among the given IDs that the viewer may not know of as
277
+ * `Resource` nodes, as {@link readableResourceNode} decides. IDs of anything
278
+ * but a resource are never among them.
279
+ * @returns The IDs the viewer may not know of.
280
+ */
281
+ async function unreadableResourceNodes(viewer, ids) {
282
+ if (ids.length === 0 || viewer.account?.admin === true) return /* @__PURE__ */ new Set();
283
+ const rows = await viewer.db.select({ id: resources.id }).from(resources).where(and(inArray(resources.id, [...new Set(ids)]), not(readableResourceNode(viewer, resources.id))));
284
+ return new Set(rows.map(({ id }) => id));
285
+ }
286
+ /**
287
+ * Determines whether the viewer may know of the resource as a `Resource`
288
+ * node.
289
+ * @returns Whether {@link readableResourceNode} holds for it.
290
+ */
291
+ const isReadableResourceNode = async (viewer, id) => !(await unreadableResourceNodes(viewer, [id])).has(id);
292
+ const uuidOf = (node, key) => node != null && typeof node === "object" && key in node && validateUuid(node[key]) ? node[key] : null;
293
+ /**
294
+ * Tells the resource a Relay node belongs to without being its typed detail,
295
+ * as a revision belongs to its resource. Such a node is hidden by its own
296
+ * readability, as {@link readableRevision} tells, and nothing of its
297
+ * resource's, such as a deletion, hides it.
298
+ * @returns The resource ID, or null for any other node.
299
+ */
300
+ const owningResourceOf = (node) => uuidOf(node, "resourceId");
301
+ /**
302
+ * Replaces the Relay nodes the viewer may not read by null, as if they did
303
+ * not exist. Activities, objects, and resources share their IDs, so the
304
+ * caller tells which nodes are `Resource` nodes, which are checked by
305
+ * {@link readableResourceNode}; the ID of any other node tells whether
306
+ * {@link readableResource} lets the viewer read it. A node that belongs to a
307
+ * resource, such as a revision, is checked by {@link readableRevision}
308
+ * instead, as {@link owningResourceOf} tells.
309
+ * @param viewer The viewer and the database.
310
+ * @param nodes The resolved nodes.
311
+ * @param resourceNodes Whether the node at each index is a `Resource` node.
312
+ * @returns The nodes, in the same order.
313
+ */
314
+ async function hideUnreadableNodes(viewer, nodes, resourceNodes = []) {
315
+ const kindOf = (node, index) => owningResourceOf(node) != null ? "revision" : resourceNodes[index] === true ? "resource" : "typed";
316
+ const kinds = nodes.map((node, index) => kindOf(node, index));
317
+ const idsOf = (kind) => nodes.map((node, index) => kinds[index] === kind ? uuidOf(node, "id") : null);
318
+ const ids = {
319
+ revision: idsOf("revision"),
320
+ resource: idsOf("resource"),
321
+ typed: idsOf("typed")
322
+ };
323
+ const present = (list) => list.filter((id) => id != null);
324
+ const [hiddenTyped, hiddenResources, hiddenRevisions] = await Promise.all([
325
+ unreadableResources(viewer, present(ids.typed)),
326
+ unreadableResourceNodes(viewer, present(ids.resource)),
327
+ unreadableRevisions(viewer, present(ids.revision))
328
+ ]);
329
+ return nodes.map((node, index) => {
330
+ const kind = kinds[index] ?? "typed";
331
+ const id = ids[kind][index];
332
+ if (kind === "revision") return id == null || hiddenRevisions.has(id) ? null : node;
333
+ return id != null && (kind === "resource" ? hiddenResources : hiddenTyped).has(id) ? null : node;
334
+ });
335
+ }
336
+ //#endregion
337
+ export { PUBLIC_IRIS, hideUnreadableNodes, isReadableResource, isReadableResourceNode, memberInstances, owningResourceOf, readableResource, readableResourceNode, readableRevision, unreadableResourceNodes, unreadableResources, unreadableRevisions, viewableInstance, visibleResource };
338
+
339
+ //# sourceMappingURL=readable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readable.mjs","names":[],"sources":["../src/readable.ts"],"sourcesContent":["// DrFed: A web-based platform for developing and debugging ActivityPub apps\n// Copyright (C) 2026 DrFed team\n//\n// This program is free software: you can redistribute it and/or modify\n// it under the terms of the GNU Affero General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// This program is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU Affero General Public License for more details.\n//\n// You should have received a copy of the GNU Affero General Public License\n// along with this program. If not, see <https://www.gnu.org/licenses/>.\n\nimport type { Database } from \"@drfed/models\";\nimport { PUBLIC_IRI } from \"@drfed/models/resource\";\nimport {\n type Account,\n activities,\n activityDeliveries,\n actors,\n addressing,\n collections,\n instanceMembers,\n instances,\n objects,\n resourceHeads,\n resourceRevisions,\n resources,\n revisionAddressing,\n revisionReceipts,\n} from \"@drfed/models/schema\";\nimport { type Uuid, validateUuid } from \"@drfed/models/uuid\";\nimport {\n type SQL,\n type SQLWrapper,\n and,\n eq,\n inArray,\n isNotNull,\n not,\n sql,\n} from \"drizzle-orm\";\nimport { type PgColumn, alias } from \"drizzle-orm/pg-core\";\n\n/**\n * The IRIs that address an activity or object to the public: the full one and\n * the compacted forms a JSON-LD document may carry.\n */\nexport const PUBLIC_IRIS: readonly string[] = [\n PUBLIC_IRI,\n \"as:Public\",\n \"Public\",\n];\n\n/**\n * Who reads, and the database to decide what they may read in.\n */\nexport interface Viewer {\n readonly db: Database;\n readonly account?: Account;\n}\n\n/**\n * The local instances an account is an accepted member of; pending members,\n * who have been invited but have not accepted yet, do not count.\n * @returns A query of their IDs.\n */\nexport const memberInstances = (\n db: Database,\n accountId: Uuid,\n ...conditions: SQL[]\n) =>\n db\n .select({ instanceId: instanceMembers.instanceId })\n .from(instanceMembers)\n .innerJoin(instances, eq(instanceMembers.instanceId, instances.id))\n .where(\n and(\n eq(instanceMembers.accountId, accountId),\n isNotNull(instanceMembers.accepted),\n isNotNull(instances.localId),\n ...conditions,\n ),\n );\n\n/**\n * Restricts rows to those of the local instances whose private records the\n * viewer may read: every local instance for an administrator, and those the\n * viewer is an accepted member of otherwise, as the `admin` and\n * `localInstanceMember` scopes decide for one instance. Filtering rows by it\n * before paging keeps a connection from telling of rows the viewer may not\n * read.\n * @param viewer The viewer and the database.\n * @param instanceId The column of the instance a row belongs to.\n * @returns The condition on the column.\n */\nexport function viewableInstance(viewer: Viewer, instanceId: PgColumn): SQL {\n const { account } = viewer;\n if (account == null) return sql`false`;\n return account.admin\n ? inArray(\n instanceId,\n viewer.db\n .select({ id: instances.id })\n .from(instances)\n .where(isNotNull(instances.localId)),\n )\n : inArray(instanceId, memberInstances(viewer.db, account.id));\n}\n\n// The predicates below are nested into queries on the same tables, so each\n// subquery reads an alias of its own to never capture the outer row. An\n// alias renders as its name alone, so a FROM clause names its table as well.\nconst authors = alias(actors, \"readable_authors\");\nconst remoteActivities = alias(activities, \"readable_remote_activities\");\nconst remoteObjects = alias(objects, \"readable_remote_objects\");\nconst carriers = alias(activities, \"readable_carriers\");\nconst entries = alias(addressing, \"readable_addressing\");\nconst targets = alias(resources, \"readable_targets\");\nconst deliveries = alias(activityDeliveries, \"readable_deliveries\");\nconst visibleOwners = alias(actors, \"visible_owners\");\nconst ownedObjects = alias(objects, \"visible_objects\");\nconst ownedActivities = alias(activities, \"visible_activities\");\nconst ownedCollections = alias(collections, \"visible_collections\");\nconst typedActors = alias(actors, \"legacy_typed_actors\");\nconst typedObjects = alias(objects, \"legacy_typed_objects\");\nconst typedActivities = alias(activities, \"legacy_typed_activities\");\nconst typedCollections = alias(collections, \"legacy_typed_collections\");\nconst evidenced = alias(resourceRevisions, \"evidenced_revisions\");\nconst evidencedAddressing = alias(revisionAddressing, \"evidenced_addressing\");\nconst evidencedReceipts = alias(revisionReceipts, \"evidenced_receipts\");\nconst receiptDeliveries = alias(activityDeliveries, \"receipt_deliveries\");\nconst evidencedHeads = alias(resourceHeads, \"evidenced_heads\");\nconst listedRevisions = alias(resourceRevisions, \"listed_revisions\");\n\nconst remoteActivity = (id: SQLWrapper): SQL => sql`exists (\n select 1 from ${activities} ${remoteActivities}\n join ${actors} ${authors} on ${authors.id} = ${remoteActivities.actorId}\n where ${remoteActivities.id} = ${id} and ${authors.localId} is null\n)`;\n\nconst remoteObject = (id: SQLWrapper): SQL => sql`exists (\n select 1 from ${objects} ${remoteObjects}\n join ${actors} ${authors} on ${authors.id} = ${remoteObjects.actorId}\n where ${remoteObjects.id} = ${id} and ${authors.localId} is null\n)`;\n\nconst addressedPublicly = (id: SQLWrapper): SQL => sql`exists (\n select 1 from ${addressing} ${entries}\n join ${resources} ${targets} on ${targets.id} = ${entries.targetId}\n where ${entries.sourceId} = ${id} and ${inArray(targets.iri, PUBLIC_IRIS)}\n)`;\n\n// Whether an instance the viewer may read received the activity, by one of\n// its deliveries the mark says carried what is opened.\nconst received = (viewer: Viewer, id: SQLWrapper, mark: PgColumn): SQL =>\n sql`exists (\n select 1 from ${activityDeliveries} ${deliveries}\n where ${deliveries.activityId} = ${id}\n and ${deliveries.direction} = 'inbound'\n and ${mark}\n and ${viewableInstance(viewer, deliveries.instanceId)}\n )`;\n\n// True for anything but an activity of a remote actor.\nconst readableActivity = (viewer: Viewer, id: SQLWrapper): SQL =>\n sql`(not ${remoteActivity(id)} or ${addressedPublicly(id)} or ${received(viewer, id, deliveries.carriedSnapshot)})`;\n\n// True for anything but an object of a remote actor. Only the activity that\n// carried the stored version opens it, since a later one may carry another;\n// one whose activity row is gone stays closed, since a missing row proves\n// nothing of who may read it. That activity being public opens only an\n// object embedded in it, since of one fetched apart it made public no more\n// than the IRI. Receiving that activity opens it only by a delivery that\n// carried the object as stored too, since one fetched apart from the activity\n// may differ from fetch to fetch.\nconst readableObject = (viewer: Viewer, id: SQLWrapper): SQL =>\n sql`(not ${remoteObject(id)} or ${addressedPublicly(id)} or exists (\n select 1 from ${objects} ${remoteObjects}\n join ${activities} ${carriers} on ${carriers.id} = ${remoteObjects.activityId}\n where ${remoteObjects.id} = ${id}\n and (\n (not ${remoteObjects.fetched} and ${addressedPublicly(carriers.id)})\n or ${received(viewer, carriers.id, deliveries.carriedObjectSnapshot)}\n )\n ))`;\n\n/**\n * Restricts resources to those the viewer may read. Received content is an\n * inbox's, which ActivityPub filters by the requester's permission (5.2) and\n * opens without authentication only when addressed to the public (5.6):\n *\n * - An administrator reads everything.\n * - An activity of a remote actor is readable when it is addressed to the\n * public, or when one of the local instances the viewer is a member of\n * received it, i.e. an inbound delivery to it is linked to the activity\n * and carried it as stored. A delivery of the same IRI with other\n * contents stays linked but opens nothing.\n * - An object of a remote actor is readable when it is addressed to the\n * public, when the activity that carried it embedded in it is, or when\n * one of the local instances the viewer is a member of received that\n * activity and the object as stored with it. An object fetched apart\n * from the activity is opened neither by the activity being public nor\n * by a delivery but one that fetched it as stored.\n * - Everything else, including the activities and objects of local actors\n * whatever their addressing, is readable.\n *\n * Filtering rows by it before paging keeps a connection from telling of rows\n * the viewer may not read.\n * @param viewer The viewer and the database.\n * @param id The resource ID in an outer query.\n * @returns The condition on the ID.\n */\nexport function readableResource(viewer: Viewer, id: SQLWrapper): SQL {\n if (viewer.account?.admin === true) return sql`true`;\n return sql`(${readableActivity(viewer, id)} and ${readableObject(viewer, id)})`;\n}\n\n/**\n * Excludes deleted actors and objects, including resources authored or\n * owned by deleted actors. Revisions do not use it: a deletion appends a\n * whiteout, and the history before it stays readable, as\n * {@link readableRevision} tells.\n * @param id The resource ID in an outer query.\n * @returns The condition on the ID.\n */\nexport function visibleResource(id: SQLWrapper): SQL {\n return sql`not exists (\n select 1 from ${actors} ${visibleOwners}\n where ${visibleOwners.id} = ${id} and ${visibleOwners.deleted} is not null\n ) and not exists (\n select 1 from ${objects} ${ownedObjects}\n join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedObjects.actorId}\n where ${ownedObjects.id} = ${id}\n and (${ownedObjects.deleted} is not null or ${visibleOwners.deleted} is not null)\n ) and not exists (\n select 1 from ${activities} ${ownedActivities}\n join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedActivities.actorId}\n where ${ownedActivities.id} = ${id} and ${visibleOwners.deleted} is not null\n ) and not exists (\n select 1 from ${collections} ${ownedCollections}\n join ${actors} ${visibleOwners} on ${visibleOwners.id} = ${ownedCollections.ownerActorId}\n where ${ownedCollections.id} = ${id} and ${visibleOwners.deleted} is not null\n )`;\n}\n\n/**\n * Matches a resource with a legacy typed row, whose readability\n * {@link readableResource} decides by that row. It is the only place\n * {@link readableRevision} reads legacy typed tables, so that it goes when\n * they do.\n * @param id The resource ID in an outer query.\n * @returns The condition on the ID.\n */\nfunction legacyTypedRow(id: SQLWrapper): SQL {\n return sql`(exists (\n select 1 from ${actors} ${typedActors} where ${typedActors.id} = ${id}\n ) or exists (\n select 1 from ${objects} ${typedObjects} where ${typedObjects.id} = ${id}\n ) or exists (\n select 1 from ${activities} ${typedActivities} where ${typedActivities.id} = ${id}\n ) or exists (\n select 1 from ${collections} ${typedCollections} where ${typedCollections.id} = ${id}\n ))`;\n}\n\n/**\n * Restricts revisions to those the viewer may read. An administrator reads\n * every revision. Anyone else reads a revision by evidence of that\n * particular revision, never by the current state of its resource:\n *\n * - DrFed saved it for a local actor, so it is readable by anyone, with\n * its blind recipients and whether published or not, as\n * `Object.document` is.\n * - Its own addressing is public.\n * - A receipt links it to an inbound delivery to a local instance the\n * viewer may read, as {@link viewableInstance} tells.\n * - It is the current revision of a resource with a legacy typed row that\n * {@link readableResource} lets the viewer read, since that row\n * describes only its current version.\n *\n * What a revision embeds, and the revisions its embedded documents came\n * from, never decide whether it is readable: a document recording its\n * sources reads as the same document pasted without them. Only following\n * a source to its revision checks that revision.\n *\n * A later revision being restricted, or deleted, hides no earlier one that\n * was public, and a later delivery or public revision of the same IRI opens\n * no earlier private one. A revision of a deleted resource, or of one\n * whose author is deleted, stays readable to whoever may read it.\n * @param viewer The viewer and the database.\n * @param id The revision ID in an outer query.\n * @returns The condition on the ID.\n */\nexport function readableRevision(viewer: Viewer, id: SQLWrapper): SQL {\n if (viewer.account?.admin === true) return sql`true`;\n return sql`exists (\n select 1 from ${resourceRevisions} ${evidenced}\n where ${evidenced.id} = ${id} and (\n ${evidenced.capture} = 'local'\n or exists (\n select 1 from ${revisionAddressing} ${evidencedAddressing}\n where ${evidencedAddressing.revisionId} = ${evidenced.id}\n and ${inArray(evidencedAddressing.iri, PUBLIC_IRIS)}\n )\n or exists (\n select 1 from ${revisionReceipts} ${evidencedReceipts}\n join ${activityDeliveries} ${receiptDeliveries}\n on ${receiptDeliveries.id} = ${evidencedReceipts.deliveryId}\n where ${evidencedReceipts.revisionId} = ${evidenced.id}\n and ${receiptDeliveries.direction} = 'inbound'\n and ${viewableInstance(viewer, receiptDeliveries.instanceId)}\n )\n or (\n exists (\n select 1 from ${resourceHeads} ${evidencedHeads}\n where ${evidencedHeads.resourceId} = ${evidenced.resourceId}\n and ${evidencedHeads.revisionId} = ${evidenced.id}\n )\n and ${legacyTypedRow(evidenced.resourceId)}\n and ${readableResource(viewer, evidenced.resourceId)}\n )\n )\n )`;\n}\n\n/**\n * Restricts resources to those the viewer may know of as `Resource` nodes:\n * those {@link readableResource} lets the viewer read, and those with a\n * revision the viewer may read, as {@link readableRevision} tells. A\n * resource's current state, which its legacy typed row describes, may be\n * restricted after an earlier revision was public or received; that hides\n * neither the revision nor the resource it is read through. The typed\n * details of the resource still follow {@link readableResource}.\n * @param viewer The viewer and the database.\n * @param id The resource ID in an outer query.\n * @returns The condition on the ID.\n */\nexport function readableResourceNode(viewer: Viewer, id: SQLWrapper): SQL {\n if (viewer.account?.admin === true) return sql`true`;\n return sql`(${readableResource(viewer, id)} or exists (\n select 1 from ${resourceRevisions} ${listedRevisions}\n where ${listedRevisions.resourceId} = ${id}\n and ${readableRevision(viewer, listedRevisions.id)}\n ))`;\n}\n\n/**\n * Finds the revisions among the given IDs that the viewer may not read, as\n * {@link readableRevision} decides. IDs of no stored revision are never\n * among them. A local write takes it to check the sources a writer\n * declares, so that a writer embeds as a snapshot or derivation only what\n * they may read.\n * @returns The IDs the viewer may not read.\n */\nexport async function unreadableRevisions(\n viewer: Viewer,\n ids: readonly Uuid[],\n): Promise<ReadonlySet<Uuid>> {\n if (ids.length === 0 || viewer.account?.admin === true) return new Set();\n const rows = await viewer.db\n .select({ id: resourceRevisions.id })\n .from(resourceRevisions)\n .where(\n and(\n inArray(resourceRevisions.id, [...new Set(ids)]),\n not(readableRevision(viewer, resourceRevisions.id)),\n ),\n );\n return new Set(rows.map(({ id }) => id));\n}\n\n/**\n * Finds the resources among the given IDs that the viewer may not read, as\n * {@link readableResource} decides. IDs of anything but a resource are never\n * among them.\n * @returns The IDs the viewer may not read.\n */\nexport async function unreadableResources(\n viewer: Viewer,\n ids: readonly Uuid[],\n): Promise<ReadonlySet<Uuid>> {\n if (ids.length === 0 || viewer.account?.admin === true) return new Set();\n const rows = await viewer.db\n .select({ id: resources.id })\n .from(resources)\n .where(\n and(\n inArray(resources.id, [...ids]),\n not(readableResource(viewer, resources.id)),\n ),\n );\n return new Set(rows.map(({ id }) => id));\n}\n\n/**\n * Determines whether the viewer may read the resource.\n * @returns Whether {@link readableResource} holds for it.\n */\nexport const isReadableResource = async (\n viewer: Viewer,\n id: Uuid,\n): Promise<boolean> => !(await unreadableResources(viewer, [id])).has(id);\n\n/**\n * Finds the resources among the given IDs that the viewer may not know of as\n * `Resource` nodes, as {@link readableResourceNode} decides. IDs of anything\n * but a resource are never among them.\n * @returns The IDs the viewer may not know of.\n */\nexport async function unreadableResourceNodes(\n viewer: Viewer,\n ids: readonly Uuid[],\n): Promise<ReadonlySet<Uuid>> {\n if (ids.length === 0 || viewer.account?.admin === true) return new Set();\n const rows = await viewer.db\n .select({ id: resources.id })\n .from(resources)\n .where(\n and(\n inArray(resources.id, [...new Set(ids)]),\n not(readableResourceNode(viewer, resources.id)),\n ),\n );\n return new Set(rows.map(({ id }) => id));\n}\n\n/**\n * Determines whether the viewer may know of the resource as a `Resource`\n * node.\n * @returns Whether {@link readableResourceNode} holds for it.\n */\nexport const isReadableResourceNode = async (\n viewer: Viewer,\n id: Uuid,\n): Promise<boolean> => !(await unreadableResourceNodes(viewer, [id])).has(id);\n\nconst uuidOf = (node: unknown, key: \"id\" | \"resourceId\"): Uuid | null =>\n node != null &&\n typeof node === \"object\" &&\n key in node &&\n validateUuid((node as Record<typeof key, unknown>)[key])\n ? ((node as Record<typeof key, unknown>)[key] as Uuid)\n : null;\n\n/**\n * Tells the resource a Relay node belongs to without being its typed detail,\n * as a revision belongs to its resource. Such a node is hidden by its own\n * readability, as {@link readableRevision} tells, and nothing of its\n * resource's, such as a deletion, hides it.\n * @returns The resource ID, or null for any other node.\n */\nexport const owningResourceOf = (node: unknown): Uuid | null =>\n uuidOf(node, \"resourceId\");\n\n/** How {@link hideUnreadableNodes} checks a node. */\ntype NodeKind = \"revision\" | \"resource\" | \"typed\";\n\n/**\n * Replaces the Relay nodes the viewer may not read by null, as if they did\n * not exist. Activities, objects, and resources share their IDs, so the\n * caller tells which nodes are `Resource` nodes, which are checked by\n * {@link readableResourceNode}; the ID of any other node tells whether\n * {@link readableResource} lets the viewer read it. A node that belongs to a\n * resource, such as a revision, is checked by {@link readableRevision}\n * instead, as {@link owningResourceOf} tells.\n * @param viewer The viewer and the database.\n * @param nodes The resolved nodes.\n * @param resourceNodes Whether the node at each index is a `Resource` node.\n * @returns The nodes, in the same order.\n */\nexport async function hideUnreadableNodes(\n viewer: Viewer,\n nodes: readonly unknown[],\n resourceNodes: readonly boolean[] = [],\n): Promise<unknown[]> {\n const kindOf = (node: unknown, index: number): NodeKind =>\n owningResourceOf(node) != null\n ? \"revision\"\n : resourceNodes[index] === true\n ? \"resource\"\n : \"typed\";\n const kinds = nodes.map((node, index) => kindOf(node, index));\n const idsOf = (kind: NodeKind) =>\n nodes.map((node, index) =>\n kinds[index] === kind ? uuidOf(node, \"id\") : null,\n );\n const ids = {\n revision: idsOf(\"revision\"),\n resource: idsOf(\"resource\"),\n typed: idsOf(\"typed\"),\n };\n const present = (list: readonly (Uuid | null)[]) =>\n list.filter((id) => id != null);\n const [hiddenTyped, hiddenResources, hiddenRevisions] = await Promise.all([\n unreadableResources(viewer, present(ids.typed)),\n unreadableResourceNodes(viewer, present(ids.resource)),\n unreadableRevisions(viewer, present(ids.revision)),\n ]);\n return nodes.map((node, index) => {\n const kind: NodeKind = kinds[index] ?? \"typed\";\n const id = ids[kind][index];\n if (kind === \"revision\") {\n return id == null || hiddenRevisions.has(id) ? null : node;\n }\n const hidden = kind === \"resource\" ? hiddenResources : hiddenTyped;\n return id != null && hidden.has(id) ? null : node;\n });\n}\n"],"mappings":";;;;;;;;;;AAmDA,MAAa,cAAiC;CAC5C;CACA;CACA;AACF;;;;;;AAeA,MAAa,mBACX,IACA,WACA,GAAG,eAEH,GACG,OAAO,EAAE,YAAY,gBAAgB,WAAW,CAAC,CAAC,CAClD,KAAK,eAAe,CAAC,CACrB,UAAU,WAAW,GAAG,gBAAgB,YAAY,UAAU,EAAE,CAAC,CAAC,CAClE,MACC,IAC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