Graphical PDS migrator for AT Protocol
1import { getSessionAgent } from "../../../lib/sessions.ts";
2import { define } from "../../../utils.ts";
3
4export const handler = define.handlers({
5 async GET(ctx) {
6 let nextStep = null;
7 const oldAgent = await getSessionAgent(ctx.req);
8 const newAgent = await getSessionAgent(ctx.req, new Response(), true);
9
10 if (!newAgent) return Response.json({ nextStep: 1, completed: false });
11 if (!oldAgent) return new Response("Unauthorized", { status: 401 });
12
13 const oldStatus = await oldAgent.com.atproto.server.checkAccountStatus();
14 const newStatus = await newAgent.com.atproto.server.checkAccountStatus();
15 if (!oldStatus.data || !newStatus.data) {
16 return new Response("Could not verify status", { status: 500 });
17 }
18
19 // Check conditions in sequence to determine the next step
20 if (!newStatus.data) {
21 nextStep = 1;
22 } else if (
23 !(newStatus.data.repoCommit &&
24 newStatus.data.indexedRecords === oldStatus.data.indexedRecords &&
25 newStatus.data.privateStateValues ===
26 oldStatus.data.privateStateValues &&
27 newStatus.data.expectedBlobs === newStatus.data.importedBlobs &&
28 newStatus.data.importedBlobs === oldStatus.data.importedBlobs)
29 ) {
30 nextStep = 2;
31 } else if (!newStatus.data.validDid) {
32 nextStep = 3;
33 } else if (
34 !(newStatus.data.activated === true && oldStatus.data.activated === false)
35 ) {
36 nextStep = 4;
37 }
38
39 return Response.json({
40 nextStep,
41 completed: nextStep === null,
42 currentStatus: {
43 activated: newStatus.data.activated,
44 validDid: newStatus.data.validDid,
45 repoCommit: newStatus.data.repoCommit,
46 indexedRecords: newStatus.data.indexedRecords,
47 privateStateValues: newStatus.data.privateStateValues,
48 importedBlobs: newStatus.data.importedBlobs,
49 },
50 });
51 },
52});