Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MapeoProject.prototype.$createdByToDeviceId #697

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,20 @@ export class MapeoProject extends TypedEmitter {
return this.#roles.getRole(this.#deviceId)
}

/**
* @param {string} createdBy The `createdBy` value from a document.
* @returns {Promise<string>} The device ID for this creator.
* @throws When device ID cannot be found.
*/
async $createdByToDeviceId(createdBy) {
const discoveryKey = Buffer.from(createdBy, 'hex')
const coreId = this.#coreManager
.getCoreByDiscoveryKey(discoveryKey)
?.key.toString('hex')
if (!coreId) throw new Error('NotFound')
return this.#coreOwnership.getOwner(coreId)
}

/**
* Replicate a project to a @hyperswarm/secret-stream. Invites will not
* function because the RPC channel is not connected for project replication,
Expand Down
53 changes: 53 additions & 0 deletions test-e2e/created_by_to_device_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
connectPeers,
createManagers,
invite,
waitForPeers,
waitForSync,
} from './utils.js'

test('$createdByToDeviceId', async (t) => {
const managers = await createManagers(2, t)

const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, member] = managers
const projectId = await creator.createProject({ name: 'mapeo' })

await invite({
invitor: creator,
invitees: [member],
projectId,
})

const projects = await Promise.all(
managers.map((m) => m.getProject(projectId))
)
const [creatorProject, memberProject] = projects

creatorProject.$sync.start()
memberProject.$sync.start()

const observation = await creatorProject.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
refs: [],
metadata: {},
})

await waitForSync(projects, 'full')

assert.equal(
await creatorProject.$createdByToDeviceId(observation.createdBy),
creator.deviceId
)
assert.equal(
await memberProject.$createdByToDeviceId(observation.createdBy),
creator.deviceId
)
})
Loading