Skip to content

Commit beeeab7

Browse files
committed
Now fetching name given a webId
1 parent 627b1fe commit beeeab7

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

bin/lib/updateIndex.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const path = require('path')
33
const cheerio = require('cheerio')
44
const LDP = require('../../lib/ldp')
55
const { URL } = require('url')
6+
const debug = require('../../lib/debug')
67

78
const { compileTemplate, writeTemplate } = require('../../lib/common/template-utils')
89
const { getAccountManager, loadConfig, loadUsernames } = require('./cli-utils')
9-
const { getWebId } = require('../../lib/common/user-utils')
10+
const { getName, getWebId } = require('../../lib/common/user-utils')
1011
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')
1112

1213
module.exports = function (program) {
@@ -22,18 +23,23 @@ module.exports = function (program) {
2223
const ldp = new LDP(config)
2324
const accountManager = getAccountManager(config, { ldp })
2425
const usernames = loadUsernames(config)
25-
const usersProcessed = usernames.map(async name => {
26-
const accountDirectory = accountManager.accountDirFor(name)
26+
const usersProcessed = usernames.map(async username => {
27+
const accountDirectory = accountManager.accountDirFor(username)
2728
const indexFilePath = path.join(accountDirectory, 'index.html')
2829
if (!isUpdateAllowed(indexFilePath)) {
2930
return
3031
}
31-
const accountUrl = getAccountUrl(name, config)
32-
const webId = await getWebId(accountDirectory, accountUrl, { ldp })
33-
writeTemplate(indexFilePath, indexTemplate, { name, webId })
32+
const accountUrl = getAccountUrl(username, config)
33+
try {
34+
const webId = await getWebId(accountDirectory, accountUrl, { ldp })
35+
const name = await getName(webId, { ldp })
36+
writeTemplate(indexFilePath, indexTemplate, { name, webId })
37+
} catch (err) {
38+
debug.errors(`Failed to create new index for ${username}: ${JSON.stringify(err, null, 2)}`)
39+
}
3440
})
3541
await Promise.all(usersProcessed)
36-
console.log(`Processed ${usersProcessed.length} users`)
42+
debug.accounts(`Processed ${usersProcessed.length} users`)
3743
})
3844
}
3945

lib/common/user-utils.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ const $rdf = require('rdflib')
33
const LDP = require('../ldp')
44

55
const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#')
6+
const VCARD = $rdf.Namespace('http://www.w3.org/2006/vcard/ns#')
67

8+
module.exports.getName = getName
79
module.exports.getWebId = getWebId
810
module.exports.isValidUsername = isValidUsername
911

12+
async function getName (webId, options = {}) {
13+
const ldp = setupLDP(options)
14+
const graph = await ldp.fetchGraph(webId)
15+
const nameNode = graph.any($rdf.sym(webId), VCARD('fn'))
16+
return nameNode.value
17+
}
18+
1019
async function getWebId (accountDirectory, accountUrl, options = {}) {
11-
if (!options.ldp && !options.config) {
12-
throw new Error('Require ldp or config set in options for getWebId')
13-
}
14-
const ldp = options.ldp || new LDP(options.config)
20+
const ldp = setupLDP(options)
1521
const metaFileUri = `${accountUrl}/${ldp.suffixMeta}`
1622
const metaData = await ldp.readContainerMeta(accountDirectory)
1723
const metaGraph = $rdf.graph()
@@ -23,3 +29,10 @@ async function getWebId (accountDirectory, accountUrl, options = {}) {
2329
function isValidUsername (username) {
2430
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(username)
2531
}
32+
33+
function setupLDP (options) {
34+
if (!options.ldp && !options.config) {
35+
throw new Error('Require ldp or config set in options for getWebId')
36+
}
37+
return options.ldp || new LDP(options.config)
38+
}

lib/ldp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class LDP {
273273
*
274274
* @return {Promise<Graph>}
275275
*/
276-
async fetchGraph (uri, options) {
276+
async fetchGraph (uri, options = {}) {
277277
const response = await fetch(uri)
278278
if (!response.ok) {
279279
const error = new Error(

0 commit comments

Comments
 (0)