|
| 1 | +/* |
| 2 | + Copied from mashlib/src/global/metadata.ts |
| 3 | + */ |
| 4 | +import { IndexedFormula, LiveStore, NamedNode, parse, sym } from 'rdflib' |
| 5 | +import ns from '../lib/ns' |
| 6 | + |
| 7 | +/* @ts-ignore no-console */ |
| 8 | +type ThrottleOptions = { |
| 9 | + leading?: boolean; |
| 10 | + throttling?: boolean; |
| 11 | + trailing?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * @ignore exporting this only for the unit test |
| 16 | + */ |
| 17 | +export function getPod (): NamedNode { |
| 18 | + const { origin, pathname } = document.location |
| 19 | + const isDatabrowserShell = document.body?.dataset?.appShell === 'databrowser' |
| 20 | + const segments = pathname.split('/').filter(Boolean) |
| 21 | + const lastSegment = segments[segments.length - 1] || '' |
| 22 | + const looksLikeFile = /\.[^/]+$/.test(lastSegment) |
| 23 | + |
| 24 | + if (isDatabrowserShell && segments.length > 0 && !looksLikeFile) { |
| 25 | + return sym(`${origin}/${segments[0]}/`) |
| 26 | + } |
| 27 | + |
| 28 | + // Root-hosted pods and static databrowser pages still use the site root. |
| 29 | + return sym(origin).site() |
| 30 | +} |
| 31 | +/** |
| 32 | + */ |
| 33 | +export async function getPodOwner (pod: NamedNode, store: LiveStore): Promise<NamedNode | null> { |
| 34 | + // This is a massive guess. In future |
| 35 | + // const podOwner = sym(`${pod.uri}profile/card#me`) |
| 36 | + |
| 37 | + try { |
| 38 | + // load turtle Container representation |
| 39 | + if (!store.any(pod, null, ns.ldp('Container'), pod)) { |
| 40 | + const response = await store.fetcher.webOperation('GET', pod.uri, store.fetcher.initFetchOptions(pod.uri, { headers: { accept: 'text/turtle' } })) |
| 41 | + const containerTurtle = response.responseText |
| 42 | + parse(containerTurtle as string, store, pod.uri, 'text/turtle') |
| 43 | + } |
| 44 | + } catch (err) { |
| 45 | + console.error('Error loading pod ' + pod + ': ' + err) |
| 46 | + return null |
| 47 | + } |
| 48 | + if (!store.holds(pod, ns.rdf('type'), ns.space('Storage'), pod)) { |
| 49 | + console.warn('Pod ' + pod + ' does not declare itself as a space:Storage') |
| 50 | + return null |
| 51 | + } |
| 52 | + const podOwner = store.any(pod, ns.solid('owner'), null, pod) || |
| 53 | + store.any(null, ns.space('storage'), pod, pod) |
| 54 | + if (podOwner) { |
| 55 | + try { |
| 56 | + await store.fetcher.load((podOwner as NamedNode).doc()) |
| 57 | + } catch (_err) { |
| 58 | + console.warn('Unable to load profile of pod owner ' + podOwner) |
| 59 | + return null |
| 60 | + } |
| 61 | + if (!store.holds(podOwner, ns.space('storage'), pod, (podOwner as NamedNode).doc())) { |
| 62 | + console.warn(`Pod owner ${podOwner} does NOT list pod ${pod} as their storage`) |
| 63 | + } |
| 64 | + return podOwner as NamedNode// Success! |
| 65 | + } else { // pod owner not declared in pod |
| 66 | + // @@ TODO: This is given the structure that NSS provides |
| 67 | + // This is a massive guess. For old pods which don't have owner link |
| 68 | + const guess = sym(`${pod.uri}profile/card#me`) |
| 69 | + try { |
| 70 | + // @ts-ignore LiveStore always has fetcher |
| 71 | + await store.fetcher.load(guess) |
| 72 | + } catch (_err) { |
| 73 | + console.error('Ooops. Guessed wrong pod owner webid {$guess} : can\'t load it.') |
| 74 | + return null |
| 75 | + } |
| 76 | + if (store.holds(guess, ns.space('storage'), pod, guess.doc())) { |
| 77 | + console.warn('Using guessed pod owner webid but it links back.') |
| 78 | + return guess |
| 79 | + } |
| 80 | + return null |
| 81 | + } |
| 82 | +} |
| 83 | +/** |
| 84 | + * @ignore exporting this only for the unit test |
| 85 | + */ |
| 86 | +export function getName (store: IndexedFormula, user: NamedNode): string { |
| 87 | + return store.anyValue(user, ns.vcard('fn'), null, user.doc()) || |
| 88 | + store.anyValue(user, ns.foaf('name'), null, user.doc()) || |
| 89 | + user.uri |
| 90 | +} |
| 91 | +/** |
| 92 | + * @ignore exporting this only for the unit test |
| 93 | + */ |
| 94 | +export function throttle (func: Function, wait: number, options: ThrottleOptions = {}): (...args: any[]) => any { |
| 95 | + let context: any, |
| 96 | + args: any, |
| 97 | + result: any |
| 98 | + let timeout: any = null |
| 99 | + let previous = 0 |
| 100 | + const later = function () { |
| 101 | + previous = !options.leading ? 0 : Date.now() |
| 102 | + timeout = null |
| 103 | + result = func.apply(context, args) |
| 104 | + if (!timeout) context = args = null |
| 105 | + } |
| 106 | + return function () { |
| 107 | + const now = Date.now() |
| 108 | + if (!previous && !options.leading) previous = now |
| 109 | + const remaining = wait - (now - previous) |
| 110 | + // @ts-ignore |
| 111 | + context = this |
| 112 | + args = arguments |
| 113 | + if (remaining <= 0 || remaining > wait) { |
| 114 | + if (timeout) { |
| 115 | + clearTimeout(timeout) |
| 116 | + timeout = null |
| 117 | + } |
| 118 | + previous = now |
| 119 | + result = func.apply(context, args) |
| 120 | + if (!timeout) context = args = null |
| 121 | + } else if (!timeout && options.trailing !== false) { |
| 122 | + timeout = setTimeout(later, remaining) |
| 123 | + } |
| 124 | + return result |
| 125 | + } |
| 126 | +} |
0 commit comments