Skip to content

Commit

Permalink
mismatched roots fix (bluesky-social#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms authored Apr 28, 2022
1 parent 30aa032 commit 07d7fcf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/setup/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default cmd({
required: true,
default: false,
})
if (!isSure) {
if (!isSure.question) {
console.log('Exiting without deleting')
return
}
Expand Down
29 changes: 16 additions & 13 deletions server/src/routes/data/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,44 @@ router.post('/:did', async (req, res) => {
const { did } = util.checkReqBody(req.params, postRepoReq)
const bytes = await util.readReqBytes(req)

const maybeRepo = await util.maybeLoadRepo(res, did)
const db = util.getDB(res)

// check to see if we have their username in DB, for indexed queries
const haveUsername = await db.isDidRegistered(did)
if (!haveUsername) {
const username = await service.getUsernameFromDidNetwork(did)
if (username) {
const [name, host] = username.split('@')
await db.registerDid(name, did, host)
}
}

const maybeRepo = await util.maybeLoadRepo(res, did)
const isNewRepo = maybeRepo === null
let repo: Repo
const evts: delta.Event[] = []

// @TODO: we should do these on a temp in-memory blockstore before merging down to our on-disk one
if (maybeRepo) {
if (!isNewRepo) {
repo = maybeRepo
await repo.loadAndVerifyDiff(bytes, async (evt) => {
evts.push(evt)
})
await db.updateRepoRoot(did, repo.cid)
} else {
const blockstore = util.getBlockstore(res)
repo = await Repo.fromCarFile(bytes, blockstore, async (evt) => {
evts.push(evt)
})
await db.createRepoRoot(did, repo.cid)
}

for (const evt of evts) {
await processEvent(db, util.getOwnHost(req), repo, did, evt)
}
await subscriptions.notifySubscribers(db, repo)

// check to see if we have their username in DB, for indexed queries
const haveUsername = await db.isDidRegistered(did)
if (!haveUsername) {
const username = await service.getUsernameFromDidNetwork(did)
if (username) {
const [name, host] = username.split('@')
await db.registerDid(name, did, host)
}
}
if (isNewRepo) {
await db.createRepoRoot(did, repo.cid)
} else [await db.updateRepoRoot(did, repo.cid)]

res.status(200).send()
})
Expand Down

0 comments on commit 07d7fcf

Please sign in to comment.