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