From 3df70da23c813cbb95ac24b2d0068fb3c836fb55 Mon Sep 17 00:00:00 2001 From: nickreynolds Date: Wed, 3 Apr 2024 10:33:24 -0400 Subject: [PATCH] feat: create brainshare posts for canvas files (#221) --- scripts/upload-docs.js | 87 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/scripts/upload-docs.js b/scripts/upload-docs.js index a402862..849547c 100644 --- a/scripts/upload-docs.js +++ b/scripts/upload-docs.js @@ -53,7 +53,23 @@ function getFiles(dir, files_) { return files_; } +function getCanvasFiles(dir, files_) { + files_ = files_ || []; + console.log("starting canvas files: ", files_) + const files = fs.readdirSync(dir); + for (const i in files) { + const name = dir + '/' + files[i]; + if (fs.statSync(name).isDirectory()) { + getCanvasFiles(name, files_); + } else if (name.endsWith('.canvas')) { + files_.push(name); + } + } + return files_; +} + const files = getFiles(docsPath); +const canvasFiles = getCanvasFiles(docsPath) // find all links to other files in markdown // create a graph of links @@ -74,6 +90,24 @@ files.forEach((file) => { }); }); +let canvasObjects = new Map() +canvasFiles.forEach((file) => { + const fileContent = fs.readFileSync(file, 'utf8'); + const data = JSON.parse(fileContent) + for(var node of data.nodes) { + if (node.type === 'file') { + const fileSplit1 = node.file.split('/') + const fileSplit2 = fileSplit1[fileSplit1.length -1].split('.')[0] + node.file = `${process.env.AUTHOR_DID}/${fileSplit2}` + node.type = 'Credential' + } + } + + const fileName1 = file.split('.')[0] + const fileName1Split = fileName1.split('/') + canvasObjects.set(fileName1Split[fileName1Split.length - 1], data) +}); + // replace links with brainshare urls const updatedFiles = files.map((file) => { let fileContent = fs.readFileSync(file, 'utf8'); @@ -141,10 +175,61 @@ const createPost = async (did, post, title) => { } -// create post for each file + +const createCanvasPost = async (did, canvas, title) => { + + try { + const credentialSubject = { + title, + isPublic: true, + canvas + } + + const getPrevious = await agent.dataStoreORMGetVerifiableCredentialsByClaims({ + where: [{ column: 'type', value: ['title'] }], + where: [{ column: 'value', value: [title]}], + order: [{ column: 'issuanceDate', direction: 'DESC' }], + take: 1 + }) + + if (getPrevious.length > 0) { + if (getPrevious[0].verifiableCredential.credentialSubject.canvas === canvas) { + return + } + } + + console.log("create new revision for post: ", title) + const credential = await agent.createVerifiableCredential({ + save: true, + proofFormat: 'jwt', + credential: { + '@context': ['https://www.w3.org/2018/credentials/v1'], + type: ['VerifiableCredential', 'BrainSharePost'], + issuer: { id: did }, + issuanceDate: new Date().toISOString(), + credentialSubject, + }, + }) + + if (credential) { + const hash = await agent.dataStoreSaveVerifiableCredential({verifiableCredential: credential}) + return hash + } + } catch (e) { + console.error(e) + } + +} + + +// // create post for each file for (const file of updatedFiles) { const hash = await createPost(process.env.AUTHOR_DID, file.content, file.title) // file['hash'] = hash } +for (const [title, canvas] of Object.entries(canvasObjects)) { + await createCanvasPost(process.env.AUTHOR_DID, canvas, title) +} + console.log(`\n\nDone creating posts`) \ No newline at end of file