Skip to content

Commit

Permalink
update implementation based on suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Aug 24, 2023
1 parent 530b8ce commit 5851f5c
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,41 +185,35 @@ export class MapeoManager {
}

/**
* @param {string} projectId
* @returns {Promise<MapeoProject>}
* @param {import('./generated/rpc.js').Invite} invite
* @returns {Promise<string>}
*/
async addProject(projectId) {
async addProject({ projectKey, encryptionKeys }) {
const projectId = projectKey.toString('hex')

const activeProject = this.#activeProjects.get(projectId)

if (activeProject) return activeProject
if (activeProject) {
throw new Error(`Project with ID ${projectId} already exists`)
}

const existingKeysResult = this.#db
.select({
keysCipher: projectKeysTable.keysCipher,
})
.from(projectKeysTable)
.where(eq(projectKeysTable.projectId, projectId))
const existingProjectFromDb = this.#db
.select()
.from(projectTable)
.where(eq(projectTable.docId, projectId))
.get()

/** @type {ProjectKeys} */
let projectKeys

if (existingKeysResult) {
projectKeys = this.#decodeProjectKeysCipher(
existingKeysResult.keysCipher,
projectId
)
} else {
projectKeys = {
projectKey: Buffer.from(projectId, 'hex'),
encryptionKeys: {
// TODO: How do we get the auth key in this case?
auth: randomBytes(32),
},
}
this.#saveProjectKeys(projectId, projectKeys)
if (existingProjectFromDb) {
throw new Error(`Project with ID ${projectId} already exists`)
}

const projectKeys = {
projectKey: Buffer.from(projectId, 'hex'),
encryptionKeys,
}

this.#saveProjectKeys(projectId, projectKeys)

const project = new MapeoProject({
...projectKeys,
storagePath: this.#storagePath,
Expand All @@ -228,8 +222,10 @@ export class MapeoManager {
sharedIndexWriter: this.#projectSettingsIndexWriter,
})

// TODO: Use invite.projectInfo to update project settings?

this.#activeProjects.set(projectId, project)

return project
return projectId
}
}

0 comments on commit 5851f5c

Please sign in to comment.