Skip to content

Commit

Permalink
feat: create brainshare posts for canvas files (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreynolds committed Apr 3, 2024
1 parent e8a37eb commit 3df70da
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion scripts/upload-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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');
Expand Down Expand Up @@ -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`)

0 comments on commit 3df70da

Please sign in to comment.