Skip to content

Commit

Permalink
repair prefs script
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Nov 8, 2023
1 parent aa7a337 commit c806ebb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
27 changes: 7 additions & 20 deletions packages/pds/src/migrate-script/migrate-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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')
Expand Down
45 changes: 45 additions & 0 deletions packages/pds/src/migrate-script/repair-prefs.ts
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions packages/pds/src/migrate-script/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c806ebb

Please sign in to comment.