solid-panes
Version:
Solid-compatible Panes: applets and views for the mashlib and databrowser
84 lines (80 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getName = getName;
exports.getNameOfPodOwner = getNameOfPodOwner;
exports.loadProfileFromURI = loadProfileFromURI;
var _rdflib = require("rdflib");
var _solidUi = require("solid-ui");
/*
* Utility functions to help load the profile
* especially when I am not logged in
*/
const DEFAULT_PROFILE_PATH = 'profile/card#me';
async function loadProfileFromURI(uri, store, fetcher) {
const pod = uri.site().uri;
// TODO: This is a hack - we cannot assume that the profile is at this document, but we will live with it for now
const webId = (0, _rdflib.sym)(`${pod}${DEFAULT_PROFILE_PATH}`);
try {
await fetcher.load(webId);
return webId;
} catch (err) {
// continue
}
// we try a prefixed pod structure
try {
const uriUrl = new URL(uri.uri);
const pathSegments = uriUrl.pathname.split('/').filter(Boolean);
if (pathSegments.length > 0) {
const derivedPod = `${uriUrl.origin}/${pathSegments[0]}/`;
const derivedWebId = (0, _rdflib.sym)(`${derivedPod}${DEFAULT_PROFILE_PATH}`);
await fetcher.load(derivedWebId);
return derivedWebId;
}
} catch (err) {
// continue
}
try {
await fetcher.load(uri);
} catch (err) {
return uri;
}
const primaryTopic = store.any(uri, _solidUi.ns.foaf('primaryTopic'), null, uri.doc());
if (primaryTopic && primaryTopic.termType === 'NamedNode') {
try {
await fetcher.load(primaryTopic);
return primaryTopic;
} catch (err) {
return uri;
}
}
return uri;
}
async function getNameOfPodOwner(pod, store, fetcher) {
// TODO: This is a hack - we cannot assume that the profile is at this document, but we will live with it for now
const webId = (0, _rdflib.sym)(`${pod.uri}${DEFAULT_PROFILE_PATH}`);
try {
await fetcher.load(webId);
return getName(store, webId);
} catch (err) {
// continue
}
// we try a prefixed pod structure
try {
const uriUrl = new URL(pod.uri);
const pathSegments = uriUrl.pathname.split('/').filter(Boolean);
if (pathSegments.length > 0) {
const derivedPod = `${uriUrl.origin}/${pathSegments[0]}/`;
const derivedWebId = (0, _rdflib.sym)(`${derivedPod}${DEFAULT_PROFILE_PATH}`);
await fetcher.load(derivedWebId);
return getName(store, derivedWebId);
}
} catch (err) {
// continue
}
return '';
}
function getName(store, ownersProfile) {
return store.anyValue(ownersProfile, _solidUi.ns.vcard('fn'), null, ownersProfile.doc()) || store.anyValue(ownersProfile, _solidUi.ns.foaf('name'), null, ownersProfile.doc()) || new URL(ownersProfile.uri).host.split('.')[0];
}