|
6 | 6 | groupPagesListQuerySchema, |
7 | 7 | pageBacklinkCreateRequestSchema, |
8 | 8 | pageBumpRequestSchema, |
| 9 | + pageMoveRequestSchema, |
9 | 10 | pageIdPathSchema, |
10 | 11 | pageSnapshotCreateResponseSchema, |
11 | 12 | pageSnapshotSaveRequestSchema, |
@@ -33,6 +34,7 @@ import { |
33 | 34 | } from "@deepnotes/api"; |
34 | 35 | import type { ContentfulStatusCode } from "hono/utils/http-status"; |
35 | 36 | import { Hono } from "hono"; |
| 37 | +import type { PageMoveBody } from "@deepnotes/session"; |
36 | 38 |
|
37 | 39 | import { getDbForConnectionString } from "./db-pool.js"; |
38 | 40 | import { readCookieHeader } from "./cookies.js"; |
@@ -1371,6 +1373,113 @@ app.post("/api/groups/:groupId/pages", async (c) => { |
1371 | 1373 | } |
1372 | 1374 | }); |
1373 | 1375 |
|
| 1376 | +app.post("/api/pages/:pageId/move", async (c) => { |
| 1377 | + const sessionEnv = getSessionEnv(c.env); |
| 1378 | + if (sessionEnv == null) { |
| 1379 | + return c.json(serviceUnavailableBody, 503); |
| 1380 | + } |
| 1381 | + const hyper = c.env.HYPERDRIVE; |
| 1382 | + if (hyper == null) { |
| 1383 | + return c.json( |
| 1384 | + { |
| 1385 | + code: "SERVICE_UNAVAILABLE" as const, |
| 1386 | + message: "HYPERDRIVE binding is not configured.", |
| 1387 | + }, |
| 1388 | + 503, |
| 1389 | + ); |
| 1390 | + } |
| 1391 | + |
| 1392 | + let bodyJson: unknown; |
| 1393 | + try { |
| 1394 | + bodyJson = await c.req.json(); |
| 1395 | + } catch { |
| 1396 | + return c.json({ code: "BAD_REQUEST", message: "Expected JSON body." }, 400); |
| 1397 | + } |
| 1398 | + |
| 1399 | + const pParams = pageIdPathSchema.safeParse({ pageId: c.req.param("pageId") }); |
| 1400 | + if (!pParams.success) { |
| 1401 | + return c.json( |
| 1402 | + { code: "VALIDATION_ERROR", message: pParams.error.message }, |
| 1403 | + 400, |
| 1404 | + ); |
| 1405 | + } |
| 1406 | + const parsed = pageMoveRequestSchema.safeParse(bodyJson); |
| 1407 | + if (!parsed.success) { |
| 1408 | + return c.json( |
| 1409 | + { |
| 1410 | + code: "VALIDATION_ERROR", |
| 1411 | + message: parsed.error.flatten().formErrors.join("; "), |
| 1412 | + }, |
| 1413 | + 400, |
| 1414 | + ); |
| 1415 | + } |
| 1416 | + |
| 1417 | + const d = parsed.data; |
| 1418 | + const moveBody: PageMoveBody = { |
| 1419 | + destGroupId: d.destGroupId, |
| 1420 | + setAsMainPage: d.setAsMainPage, |
| 1421 | + groupCreation: |
| 1422 | + d.groupCreation == null |
| 1423 | + ? undefined |
| 1424 | + : { |
| 1425 | + groupEncryptedName: d.groupCreation.groupEncryptedName, |
| 1426 | + groupPasswordHash: d.groupCreation.groupPasswordHash, |
| 1427 | + groupIsPublic: d.groupCreation.groupIsPublic, |
| 1428 | + groupAccessKeyring: d.groupCreation.groupAccessKeyring, |
| 1429 | + groupEncryptedInternalKeyring: |
| 1430 | + d.groupCreation.groupEncryptedInternalKeyring, |
| 1431 | + groupEncryptedContentKeyring: d.groupCreation.groupEncryptedContentKeyring, |
| 1432 | + groupPublicKeyring: d.groupCreation.groupPublicKeyring, |
| 1433 | + groupEncryptedPrivateKeyring: d.groupCreation.groupEncryptedPrivateKeyring, |
| 1434 | + groupOwnerEncryptedName: d.groupCreation.groupOwnerEncryptedName, |
| 1435 | + }, |
| 1436 | + reencrypt: |
| 1437 | + d.reencrypt == null |
| 1438 | + ? undefined |
| 1439 | + : { |
| 1440 | + pageEncryptedSymmetricKeyring: |
| 1441 | + d.reencrypt.pageEncryptedSymmetricKeyring, |
| 1442 | + pageEncryptedRelativeTitle: d.reencrypt.pageEncryptedRelativeTitle, |
| 1443 | + pageEncryptedAbsoluteTitle: d.reencrypt.pageEncryptedAbsoluteTitle, |
| 1444 | + pageEncryptedUpdate: d.reencrypt.pageEncryptedUpdate, |
| 1445 | + pageEncryptedSnapshots: Object.fromEntries( |
| 1446 | + Object.entries(d.reencrypt.pageEncryptedSnapshots).map( |
| 1447 | + ([id, snap]) => [ |
| 1448 | + id, |
| 1449 | + { |
| 1450 | + encryptedSymmetricKey: snap.encryptedSymmetricKey, |
| 1451 | + encryptedData: snap.encryptedData, |
| 1452 | + }, |
| 1453 | + ], |
| 1454 | + ), |
| 1455 | + ), |
| 1456 | + }, |
| 1457 | + }; |
| 1458 | + |
| 1459 | + const db = getDbForConnectionString(hyper.connectionString); |
| 1460 | + const cookieHeader = c.req.header("Cookie"); |
| 1461 | + try { |
| 1462 | + const { performPageMove } = await import("@deepnotes/session"); |
| 1463 | + await performPageMove({ |
| 1464 | + db, |
| 1465 | + env: sessionEnv, |
| 1466 | + accessCookie: readCookieHeader(cookieHeader, "accessToken"), |
| 1467 | + pageId: pParams.data.pageId, |
| 1468 | + body: moveBody, |
| 1469 | + }); |
| 1470 | + return c.body(null, 204); |
| 1471 | + } catch (e) { |
| 1472 | + const { SessionError } = await import("@deepnotes/session"); |
| 1473 | + if (e instanceof SessionError) { |
| 1474 | + return c.json( |
| 1475 | + { code: e.code, message: e.message }, |
| 1476 | + e.status as ContentfulStatusCode, |
| 1477 | + ); |
| 1478 | + } |
| 1479 | + throw e; |
| 1480 | + } |
| 1481 | +}); |
| 1482 | + |
1374 | 1483 | app.post("/api/pages/:pageId/bump", async (c) => { |
1375 | 1484 | const sessionEnv = getSessionEnv(c.env); |
1376 | 1485 | if (sessionEnv == null) { |
|
0 commit comments