From c806ebba075afe0401827364e4c21184a1c28307 Mon Sep 17 00:00:00 2001 From: dholms Date: Wed, 8 Nov 2023 15:37:08 -0600 Subject: [PATCH] repair prefs script --- .../pds/src/migrate-script/migrate-all.ts | 27 +++-------- .../pds/src/migrate-script/repair-prefs.ts | 45 +++++++++++++++++++ packages/pds/src/migrate-script/util.ts | 19 ++++++++ 3 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 packages/pds/src/migrate-script/repair-prefs.ts diff --git a/packages/pds/src/migrate-script/migrate-all.ts b/packages/pds/src/migrate-script/migrate-all.ts index 2d7462f0dc2..523384af8f8 100644 --- a/packages/pds/src/migrate-script/migrate-all.ts +++ b/packages/pds/src/migrate-script/migrate-all.ts @@ -9,7 +9,13 @@ import { envToCfg, envToSecrets, readEnv } from '../config' import AppContext from '../context' import { FailedTakedown, MigrateDb, Status, TransferPhase, getDb } from './db' import PQueue from 'p-queue' -import { AdminHeaders, PdsInfo, makeAdminHeaders, repairBlob } from './util' +import { + AdminHeaders, + PdsInfo, + makeAdminHeaders, + repairBlob, + transferPreferences, +} from './util' dotenv.config() export const runScript = async () => { @@ -310,25 +316,6 @@ const updatePdsOnEntryway = async ( }) } -const transferPreferences = async ( - ctx: AppContext, - pds: PdsInfo, - did: string, -) => { - const accessToken = await ctx.services - .auth(ctx.db) - .createAccessToken({ did: did, pdsDid: pds.did }) - - const prefs = await ctx.services.account(ctx.db).getPreferences(did) - await pds.agent.api.app.bsky.actor.putPreferences( - { preferences: prefs }, - { - headers: { authorization: `Bearer ${accessToken}` }, - encoding: 'application/json', - }, - ) -} - const logFailedBlob = async (db: MigrateDb, did: string, cid: string) => { await db .insertInto('failed_blob') diff --git a/packages/pds/src/migrate-script/repair-prefs.ts b/packages/pds/src/migrate-script/repair-prefs.ts new file mode 100644 index 00000000000..6705a5f1ecb --- /dev/null +++ b/packages/pds/src/migrate-script/repair-prefs.ts @@ -0,0 +1,45 @@ +import dotenv from 'dotenv' +import AtpAgent from '@atproto/api' +import { envToCfg, envToSecrets, readEnv } from '../config' +import AppContext from '../context' +import { getDb } from './db' +import { transferPreferences } from './util' + +dotenv.config() + +export const runScript = async () => { + const db = getDb() + const env = readEnv() + const cfg = envToCfg(env) + const secrets = envToSecrets(env) + const ctx = await AppContext.fromConfig(cfg, secrets) + const pdsRes = await ctx.db.db.selectFrom('pds').selectAll().execute() + const pdsInfos = pdsRes.map((row) => ({ + id: row.id, + did: row.did, + url: `https://${row.host}`, + agent: new AtpAgent({ service: `https://${row.host}` }), + })) + const failed = await db + .selectFrom('failed_pref') + .innerJoin('status', 'status.did', 'failed_pref.did') + .selectAll() + .execute() + let count = 0 + for (const pref of failed) { + const pdsInfo = pdsInfos.find((info) => info.id === pref.pdsId) + if (!pdsInfo) { + throw new Error(`could not find pds with id: ${pref.pdsId}`) + } + try { + await transferPreferences(ctx, pdsInfo, pref.did) + } catch (err) { + console.log(err) + } + count++ + console.log(`${count}/${failed.length}`) + } + console.log('DONE WITH ALL') +} + +runScript() diff --git a/packages/pds/src/migrate-script/util.ts b/packages/pds/src/migrate-script/util.ts index 7f5353f53b5..8de51daaf8c 100644 --- a/packages/pds/src/migrate-script/util.ts +++ b/packages/pds/src/migrate-script/util.ts @@ -27,6 +27,25 @@ export const makeAdminHeaders = (secrets: ServerSecrets): AdminHeaders => { } } +export const transferPreferences = async ( + ctx: AppContext, + pds: PdsInfo, + did: string, +) => { + const accessToken = await ctx.services + .auth(ctx.db) + .createAccessToken({ did: did, pdsDid: pds.did }) + + const prefs = await ctx.services.account(ctx.db).getPreferences(did) + await pds.agent.api.app.bsky.actor.putPreferences( + { preferences: prefs }, + { + headers: { authorization: `Bearer ${accessToken}` }, + encoding: 'application/json', + }, + ) +} + export const repairBlob = async ( ctx: AppContext, db: MigrateDb,