Skip to content

Commit

Permalink
chore: replace queries opt in MemberAPI with dataType (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 authored Sep 18, 2023
1 parent 350ccc5 commit 479f96f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
7 changes: 1 addition & 6 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,9 @@ export class MapeoProject {
encryptionKeys,
projectKey,
rpc,
queries: {
getProjectInfo: async () => {
const settings = await this.$getProjectSettings()
return { name: settings.name }
},
},
dataTypes: {
deviceInfo: this.#dataTypes.deviceInfo,
project: this.#dataTypes.project,
},
})

Expand Down
22 changes: 8 additions & 14 deletions src/member-api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { TypedEmitter } from 'tiny-typed-emitter'
import { InviteResponse_Decision } from './generated/rpc.js'
import { projectKeyToId } from './utils.js'

/** @typedef {import('./datatype/index.js').DataType<import('./datastore/index.js').DataStore<'config'>, typeof import('./schema/project.js').deviceInfoTable, "deviceInfo", import('@mapeo/schema').DeviceInfo, import('@mapeo/schema').DeviceInfoValue>} DeviceInfoDataType */
/** @typedef {import('./datatype/index.js').DataType<import('./datastore/index.js').DataStore<'config'>, typeof import('./schema/client.js').projectTable, "project", import('@mapeo/schema').Project, import('@mapeo/schema').ProjectValue>} ProjectDataType */
/** @typedef {{ deviceId: string, name: import('@mapeo/schema').DeviceInfo['name'] }} MemberInfo */

export class MemberApi extends TypedEmitter {
Expand All @@ -10,31 +12,21 @@ export class MemberApi extends TypedEmitter {
#projectKey
#rpc
#dataTypes
#queries

/**
* @param {Object} opts
* @param {import('./capabilities.js').Capabilities} opts.capabilities
* @param {import('./generated/keys.js').EncryptionKeys} opts.encryptionKeys
* @param {Buffer} opts.projectKey
* @param {import('./rpc/index.js').MapeoRPC} opts.rpc
* @param {Object} opts.queries
* @param {() => Promise<import('./generated/rpc.js').Invite_ProjectInfo>} opts.queries.getProjectInfo
* @param {Object} opts.dataTypes
* @param {Pick<DeviceInfoDataType, 'getByDocId' | 'getMany'>} opts.dataTypes.deviceInfo
* @param {Pick<ProjectDataType, 'getByDocId'>} opts.dataTypes.project
*/
constructor({
capabilities,
encryptionKeys,
projectKey,
rpc,
queries,
dataTypes,
}) {
constructor({ capabilities, encryptionKeys, projectKey, rpc, dataTypes }) {
super()
this.#capabilities = capabilities
this.#encryptionKeys = encryptionKeys
this.#queries = queries
this.#projectKey = projectKey
this.#rpc = rpc
this.#dataTypes = dataTypes
Expand All @@ -50,12 +42,14 @@ export class MemberApi extends TypedEmitter {
* @returns {Promise<import('./generated/rpc.js').InviteResponse_Decision>}
*/
async invite(deviceId, { roleId, timeout }) {
const projectInfo = await this.#queries.getProjectInfo()
const projectId = projectKeyToId(this.#projectKey)

const project = await this.#dataTypes.project.getByDocId(projectId)

const response = await this.#rpc.invite(deviceId, {
projectKey: this.#projectKey,
encryptionKeys: this.#encryptionKeys,
projectInfo,
projectInfo: { name: project.name },
timeout,
})

Expand Down
26 changes: 21 additions & 5 deletions tests/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ test('invite() sends expected project-related details', async (t) => {
encryptionKeys,
projectKey,
rpc: r1,
queries: { getProjectInfo: async () => projectInfo },
dataTypes: {
project: {
async getByDocId() {
return projectInfo
},
},
},
})

r1.on('peers', async (peers) => {
const response = await memberApi.invite(peers[0].id, {
roleId: randomBytes(8).toString('hex'),
Expand Down Expand Up @@ -69,7 +74,13 @@ test('invite() assigns role to invited device after invite accepted', async (t)
encryptionKeys: { auth: randomBytes(32) },
projectKey: KeyManager.generateProjectKeypair().publicKey,
rpc: r1,
queries: { getProjectInfo: async () => {} },
dataTypes: {
project: {
async getByDocId() {
return { name: 'mapeo' }
},
},
},
})

r1.on('peers', async (peers) => {
Expand Down Expand Up @@ -118,7 +129,13 @@ test('invite() does not assign role to invited device if invite is not accepted'
encryptionKeys: { auth: randomBytes(32) },
projectKey: KeyManager.generateProjectKeypair().publicKey,
rpc: r1,
queries: { getProjectInfo: async () => {} },
dataTypes: {
project: {
async getByDocId() {
return { name: 'mapeo' }
},
},
},
})

r1.on('peers', async (peers) => {
Expand Down Expand Up @@ -158,7 +175,6 @@ test('getById() works', async (t) => {
encryptionKeys,
projectKey,
rpc,
queries: { getProjectInfo: async () => {} },
dataTypes: {
deviceInfo: {
async getByDocId(deviceId) {
Expand Down

0 comments on commit 479f96f

Please sign in to comment.