Skip to content

Commit

Permalink
feat: support user-provided JSON-LD context (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan authored Jun 19, 2019
1 parent bb5d351 commit f9d3746
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/api/works/CreateWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { WorksController } from '../../modules/Works/Works.controller'
import { Vault } from '../../utils/Vault/Vault'

export const CreateWorkSchema = () => ({
'@context': Joi.alternatives(
Joi.string(),
Joi.object(),
).optional(),
name: Joi.string().required(),
datePublished: Joi.string()
.required()
Expand All @@ -29,12 +33,15 @@ export const CreateWork = (poetUrl: string, testPoetUrl: string) => async (ctx:
const { user, tokenData } = ctx.state
const { WorkError } = errors

const newWork = ctx.request.body
const { '@context': context = {}, ...newWork } = ctx.request.body

logger.info({ context, newWork }, 'Creating Work')

const privateKey = await Vault.decrypt(user.privateKey)
const nodeNetwork = isLiveNetwork(tokenData.data.meta.network) ? poetUrl : testPoetUrl

const work = new WorksController(ctx.logger, nodeNetwork)
const claim = await work.generateClaim(user.issuer, privateKey, newWork)
const claim = await work.generateClaim(user.issuer, privateKey, newWork, context)

try {
await work.create(claim)
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Works/Works.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class WorksController {
this.logger = createLogger(__dirname)
}

async generateClaim(issuer: string, privateKey: string, work: WorkAttributes) {
async generateClaim(issuer: string, privateKey: string, work: WorkAttributes, context: any) {
const createAndSignClaim = pipeP(
configureCreateVerifiableClaim({ issuer, context: legacyContext }),
configureCreateVerifiableClaim({ issuer, context: { ...legacyContext, ...context} }),
getVerifiableClaimSigner().configureSignVerifiableClaim({ privateKey }),
)
const { archiveUrl, hash } = (await this.uploadContent(work.content))[0]
Expand Down
21 changes: 20 additions & 1 deletion test/Integration/works/getWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ describe('Works', async function() {

describe('When a newly created work with content has been sucessfully created', function() {
it('Should return the work with a hash and archiveUrl of the content', async function() {
const user = await createUserVerified(mail, frost)
const { token } = user
const { apiToken } = await frost.createApiToken(token, Network.LIVE)
const { workId } = await frost.createWork(apiToken, createWork())
const work = await frost.getWork(apiToken, workId)
expect(work).to.have.all.keys(
'name',
'datePublished',
'dateCreated',
'author',
'tags',
'hash',
'archiveUrl',
)
expect(work).to.not.have.key('content')
expect(work.author).to.eql(createWork().author)
})
})
describe('When a newly created work with content has been successfully created', function() {
it.skip('Should be able to download the archiveUrl', async function() {
const user = await createUserVerified(mail, frost)
const { token } = user
const { apiToken } = await frost.createApiToken(token, Network.LIVE)
Expand All @@ -57,7 +77,6 @@ describe('Works', async function() {
expect(work.hash).to.eq('QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm')
})
})

describe('When a newly createrd work with an archiveUrl and a hash has been successfully created', function() {
it('Should return the work with the provided hash and archiveUrl', async function() {
const user = await createUserVerified(mail, frost)
Expand Down

0 comments on commit f9d3746

Please sign in to comment.