Repository navigation
Expand file tree
/
Copy pathpublic-read.ts
More file actions
299 lines (296 loc) · 10.3 KB
/
Copy pathpublic-read.ts
File metadata and controls
299 lines (296 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { createHash } from "node:crypto";
import * as Y from "yjs";
import { CanvasModel, CANVAS_SCHEMA_VERSION } from "@smartdoca/canvas/model";
import { projectExlsxWorkbook } from "@smartdoca/sheet/model";
import { readDocument } from "@smartdoca/slides/core";
import type { DB, Resource } from "../../../../db/src/index.js";
import { readSnapshot } from "../../../../db/src/transactions.js";
import type { PluginRequestContext } from "@smartdoca/plugin-sdk/platform";
import type {
DocumentReadServiceV1,
LibrariesServiceV1,
PublicResource,
} from "@smartdoca/plugin-sdk/documents";
import type { Actor } from "../identity/passwords.js";
import { activeActor, authorize } from "../access/queries.js";
import { authorizeFileItem } from "../access/file-access.js";
import { fail } from "../../shared/errors.js";
import { createResourceReads } from "../resources/reads.js";
import { createResourceRunner } from "../resources/context.js";
import { queryResourcePage } from "../resources/queries.js";
import { restoreDocument } from "../collaboration/documents.js";
import { restoreMarkdown } from "./codecs/markdown.js";
import {
restoreSurface,
DEFAULT_SPREADSHEET_SCHEMA,
} from "./codecs/surfaces.js";
import { PPT_SCHEMA } from "./codecs/presentation.js";
import { documentMediaIds, textMediaIds } from "./media.js";
const formats = new Set([
"rich_text",
"markdown",
"spreadsheet",
"canvas",
"presentation",
]);
export function publicResource(
resource: Pick<
Resource,
"id" | "kind" | "title" | "format" | "parent_id" | "library_id" | "version"
>,
role: string,
): PublicResource {
return {
id: resource.id,
kind: resource.kind,
title: resource.title,
format: resource.format,
parentId: resource.parent_id,
libraryId: resource.library_id,
version: resource.version,
role,
};
}
const roles = ["none", "reader", "commenter", "editor", "manager", "owner"];
async function principal(
db: DB,
context: PluginRequestContext,
): Promise<Actor> {
context.signal.throwIfAborted();
const user = await db
.selectFrom("users")
.select(["id", "display_name", "admin"])
.where("id", "=", context.principal.id)
.where("status", "=", "active")
.executeTakeFirst();
if (!user) fail(401, "Account unavailable");
return user;
}
export function createPublicDocumentReads(db: DB): DocumentReadServiceV1 {
async function access(context: PluginRequestContext, documentId: string) {
const actor = await principal(db, context);
const result = await authorize(db, actor, documentId, 1);
if (result.resource.kind !== "document") fail(404, "Document unavailable");
return { ...result, actor };
}
return {
async get(context, input) {
const result = await access(context, input.documentId);
return publicResource(result.resource, roles[result.rank]!);
},
async capabilities(context, input) {
const { rank, resource } = await access(context, input.documentId);
return {
readSnapshot: formats.has(resource.format),
comment: rank >= 2,
edit: rank >= 3,
manage: rank >= 4,
};
},
async references(context, input) {
const { actor } = await access(context, input.documentId);
return createResourceReads(db, createResourceRunner(db)).references(
actor,
input.documentId,
);
},
async readSnapshot(context, input) {
const actor = await principal(db, context);
const read = async (tx: DB) => {
const { resource } = await authorize(tx, actor, input.documentId, 1);
if (resource.kind !== "document" || !formats.has(resource.format))
fail(409, "Native snapshot unavailable for this format");
const state = await tx
.selectFrom("document_states")
.selectAll()
.where("resource_id", "=", resource.id)
.executeTakeFirst();
// Never create a baseline as a side effect of reading.
if (!state) fail(409, "Document has not been initialized");
let content: unknown, epochId: string | null, schemaVersion: number;
if (resource.format === "rich_text") {
const epoch = await tx
.selectFrom("editor_epochs")
.select("epoch_id")
.where("resource_id", "=", resource.id)
.executeTakeFirst();
const loaded = await restoreDocument(tx, resource.id);
try {
content = loaded.runtime.getValue();
} finally {
loaded.destroy();
}
epochId = epoch ? epoch.epoch_id : null;
schemaVersion = 3;
} else if (resource.format === "markdown") {
const loaded = await restoreMarkdown(tx, resource.id);
try {
if (!loaded.epochId) fail(409, "Document epoch unavailable");
epochId = loaded.epochId;
content = loaded.doc.getText("markdown").toString();
schemaVersion = 1;
} finally {
loaded.destroy();
}
} else {
const loaded = await restoreSurface(tx, resource.id, resource.format);
epochId = loaded.epochId;
if (resource.format === "canvas") {
schemaVersion = CANVAS_SCHEMA_VERSION;
const model = CanvasModel.restore({
codec: "aidcanvas-yjs",
schemaVersion: CANVAS_SCHEMA_VERSION,
epochId,
update: loaded.update,
});
try {
content = model.getValue();
} finally {
model.dispose();
}
} else if (resource.format === "presentation") {
schemaVersion = PPT_SCHEMA;
const doc = new Y.Doc();
try {
Y.applyUpdate(doc, loaded.update);
content = readDocument(doc);
} finally {
doc.destroy();
}
} else {
schemaVersion = DEFAULT_SPREADSHEET_SCHEMA;
if (!loaded.baseline) fail(409, "Workbook baseline unavailable");
content = await projectExlsxWorkbook({
baseline: loaded.baseline,
update: loaded.update,
checkpointSeq: state.checkpoint_seq,
});
}
}
const serialized = JSON.stringify(content);
if (Buffer.byteLength(serialized) > 16 * 1024 * 1024)
fail(413, "Document snapshot exceeds 16 MiB");
const revision = createHash("sha256")
.update(
JSON.stringify([
state.codec,
schemaVersion,
epochId,
state.seq,
content,
]),
)
.digest("hex");
if (
input.expectedRevision !== undefined &&
input.expectedRevision !== revision
)
fail(409, "Document content changed");
const ids =
resource.format === "rich_text"
? documentMediaIds(content)
: textMediaIds(
typeof content === "string" ? content : JSON.stringify(content),
);
const assets: { fileId: string; name: string; mime: string }[] = [];
if (ids.size) {
const files = await tx
.selectFrom("file_items")
.select(["id", "name", "mime", "storage_object_id", "metadata"])
.where("deleted_at", "is", null)
.where((eb) =>
eb.or([
eb("id", "in", [...ids]),
eb("storage_object_id", "in", [...ids]),
]),
)
.execute();
for (const file of files) {
try {
await authorizeFileItem(tx, actor, file.id);
assets.push({
fileId: file.id,
name: file.name,
mime: file.mime,
});
} catch (error) {
if (!(
error instanceof Error &&
"status" in error &&
[403, 404].includes(Number(error.status))
))
throw error;
}
}
}
context.signal.throwIfAborted();
await activeActor(tx, actor);
return {
documentId: resource.id,
format: resource.format,
codec: state.codec,
schemaVersion,
revision,
epochId,
seq: state.seq,
content,
assets,
};
};
return db.isTransaction ? read(db) : readSnapshot(db, read);
},
};
}
export function createPublicLibraries(db: DB): LibrariesServiceV1 {
return {
async list(context, input) {
const actor = await principal(db, context);
const page = await queryResourcePage(db, actor, {
kind: "library",
scope: "all",
q: input.query,
cursor: input.cursor ?? undefined,
});
return {
items: page.items.map((row) => publicResource(row, row.role)),
nextCursor: page.nextCursor,
};
},
async children(context, input) {
const actor = await principal(db, context);
const { resource } = await authorize(db, actor, input.libraryId, 1);
if (resource.kind !== "library") fail(400, "Not a library");
if (input.parentId) {
const parent = await authorize(db, actor, input.parentId, 1);
if (parent.resource.library_id !== input.libraryId)
fail(400, "Parent is outside the library");
}
const page = await queryResourcePage(db, actor, {
libraryId: input.libraryId,
parentId: input.parentId ?? input.libraryId,
cursor: input.cursor ?? undefined,
});
context.signal.throwIfAborted();
return {
items: page.items.map((row) => publicResource(row, row.role)),
nextCursor: page.nextCursor,
};
},
async path(context, input) {
const actor = await principal(db, context);
const result: PublicResource[] = [],
seen = new Set<string>();
let id: string | null = input.resourceId;
while (id) {
context.signal.throwIfAborted();
if (seen.has(id) || seen.size >= 1000)
fail(409, "Invalid resource hierarchy");
seen.add(id);
const { resource, rank } = await authorize(db, actor, id, 1);
result.unshift(publicResource(resource, roles[rank]!));
id = resource.parent_id ?? resource.library_id;
}
return result;
},
};
}