Skip to content

Commit

Permalink
feat: add MapeoProject.prototype.$createdByToDeviceId (#697)
Browse files Browse the repository at this point in the history
Turns `createdAt` into `deviceId`.

Partly addresses [#136].

[#136]: #136
  • Loading branch information
EvanHahn authored Jun 25, 2024
1 parent 42bc021 commit 14f27c4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,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
)
})

0 comments on commit 14f27c4

Please sign in to comment.