-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathservice.ts
More file actions
76 lines (71 loc) · 2.57 KB
/
Copy pathservice.ts
File metadata and controls
76 lines (71 loc) · 2.57 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
import $rdf, { NamedNode, IndexedFormula, Statement } from 'rdflib'
import { Namespaces } from 'solid-namespace'
import { Mode, TrustedApplication } from './model'
export function getStatementsToDelete (
origin: NamedNode,
person: NamedNode,
kb: IndexedFormula,
ns: Namespaces
) {
const applicationStatements = kb.statementsMatching(null, ns.acl('origin'), origin, null)
const statementsToDelete = applicationStatements.reduce(
(memo, st) => {
return memo
.concat(kb.statementsMatching(person, ns.acl('trustedApp'), st.subject, null, false))
.concat(kb.statementsMatching(st.subject, null, null, null, false))
},
[] as Statement[]
)
return statementsToDelete
}
export function getStatementsToAdd (
origin: NamedNode,
nodeName: string,
modes: Mode[],
person: NamedNode,
ns: Namespaces
) {
var application = new $rdf.BlankNode(`bn_${nodeName}`)
return [
$rdf.st(person, ns.acl('trustedApp'), application, person.doc()),
$rdf.st(application, ns.acl('origin'), origin, person.doc()),
...modes
.map(mode => {
return ns.acl(mode)
})
.map(mode => $rdf.st(application, ns.acl('mode'), mode, person.doc()))
]
}
/* istanbul ignore next [This executes the actual HTTP requests, which is too much effort to test.] */
export function fetchTrustedApps (
store: $rdf.IndexedFormula,
subject: $rdf.NamedNode,
ns: Namespaces
): TrustedApplication[] {
return (store.each(subject, ns.acl('trustedApp'), undefined, undefined) as any)
.flatMap((app: $rdf.NamedNode) => {
return store.each(app, ns.acl('origin'), undefined, undefined)
.map((origin) => {
const modes = store.each(app, ns.acl('mode'), undefined, undefined)
const trustedApp: TrustedApplication = {
origin: origin.value,
subject: subject.value,
modes: modes.map((mode) => deserialiseMode(mode as $rdf.NamedNode, ns))
}
return trustedApp
})
})
.sort((appA: TrustedApplication, appB: TrustedApplication) => (appA.origin > appB.origin) ? 1 : -1)
}
/**
* @param serialisedMode The full IRI of a mode
* @returns A plain text string representing that mode, i.e. 'read', 'append', 'write' or 'control'
*/
export function deserialiseMode (serialisedMode: $rdf.NamedNode, ns: Namespaces): Mode {
const deserialisedMode = serialisedMode.value
.replace(ns.acl('read').value, 'read')
.replace(ns.acl('append').value, 'append')
.replace(ns.acl('write').value, 'write')
.replace(ns.acl('control').value, 'control')
return deserialisedMode as Mode
}