Skip to content

Commit

Permalink
feat: add assignRole() method to MemberApi (#423)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregor MacLennan <[email protected]>
  • Loading branch information
achou11 and gmaclennan authored Jan 5, 2024
1 parent 06fe6bb commit 0d22a83
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 169 deletions.
26 changes: 21 additions & 5 deletions src/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,27 @@ export class Capabilities {
if (!ownCapabilities.roleAssignment.includes(roleId)) {
throw new Error('No capability to assign role ' + roleId)
}
await this.#dataType[kCreateWithDocId](deviceId, {
schemaName: 'role',
roleId,
fromIndex,
})

const existingRoleDoc = await this.#dataType
.getByDocId(deviceId)
.catch(() => null)

if (existingRoleDoc) {
await this.#dataType.update(
[existingRoleDoc.versionId, ...existingRoleDoc.forks],
{
schemaName: 'role',
roleId,
fromIndex,
}
)
} else {
await this.#dataType[kCreateWithDocId](deviceId, {
schemaName: 'role',
roleId,
fromIndex,
})
}
}

async #isProjectCreator() {
Expand Down
8 changes: 4 additions & 4 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const CORESTORE_STORAGE_FOLDER_NAME = 'corestore'
const INDEXER_STORAGE_FOLDER_NAME = 'indexer'
export const kCoreManager = Symbol('coreManager')
export const kCoreOwnership = Symbol('coreOwnership')
export const kCapabilities = Symbol('capabilities')
export const kSetOwnDeviceInfo = Symbol('kSetOwnDeviceInfo')
export const kBlobStore = Symbol('blobStore')
export const kProjectReplicate = Symbol('replicate project')
export const kDataTypes = Symbol('dataTypes')
const EMPTY_PROJECT_SETTINGS = Object.freeze({})

/**
Expand Down Expand Up @@ -349,10 +349,10 @@ export class MapeoProject extends TypedEmitter {
}

/**
* Capabilities instance, used for tests
* DataTypes object mappings, used for tests
*/
get [kCapabilities]() {
return this.#capabilities
get [kDataTypes]() {
return this.#dataTypes
}

get [kBlobStore]() {
Expand Down
9 changes: 9 additions & 0 deletions src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ export class MemberApi extends TypedEmitter {
})
)
}

/**
* @param {string} deviceId
* @param {import('./capabilities.js').RoleId} roleId
* @returns {Promise<void>}
*/
async assignRole(deviceId, roleId) {
return this.#capabilities.assignRole(deviceId, roleId)
}
}
159 changes: 0 additions & 159 deletions test-e2e/capabilities.js

This file was deleted.

Loading

0 comments on commit 0d22a83

Please sign in to comment.