forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.ts
More file actions
185 lines (165 loc) · 5.79 KB
/
data.ts
File metadata and controls
185 lines (165 loc) · 5.79 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import '@babel/polyfill'
import $rdf, { IndexedFormula, NamedNode, Statement, Node } from 'rdflib'
import vocab from 'solid-namespace'
import { InitialisationFunction } from './types'
const ns = vocab($rdf)
/* istanbul ignore next [Side effects are contained to initialise(), so ignore just that for test coverage] */
export const initialise: InitialisationFunction = async (store, user) => {
const creationDate = new Date()
const [pad, initialisationAdditions] = getInitialisationStatements(creationDate, store, user)
const [_setContentDeletions, setContentAdditions] = getSetContentsStatements('', creationDate, pad, store, user)
const statementsToAdd = initialisationAdditions.concat(setContentAdditions)
if (store.updater) {
await store.updater.put(pad, statementsToAdd, 'text/turtle', () => undefined)
}
return pad
}
export function isPad (pad: NamedNode, store: IndexedFormula): boolean {
const [padStatement] = store.statementsMatching(
pad,
ns.rdf('type'),
ns.pad('Notepad'),
pad.doc(),
true
)
return !!padStatement
}
export function getInitialisationStatements (
creationDate: Date,
store: IndexedFormula,
user?: NamedNode
): [NamedNode, Statement[]] {
const storeNamespaces = store.namespaces
const padName = creationDate.getTime()
const pad = store.sym(storeNamespaces.pub + padName + '/index.ttl#this')
const statementsToAdd = [
$rdf.st(pad, ns.rdf('type'), ns.pad('Notepad'), pad.doc()),
$rdf.st(pad, ns.dc('title'), `Scratchpad (${creationDate.toLocaleDateString()})`, pad.doc()),
$rdf.st(pad, ns.dc('created'), creationDate, pad.doc())
]
if (user) {
statementsToAdd.push(
$rdf.st(pad, ns.dc('author'), user, pad.doc())
)
}
return [pad, statementsToAdd]
}
// Potential improvement: get current content, generate a diff
export function getSetContentsStatements (
contents: string,
creationDate: Date,
pad: NamedNode,
store: IndexedFormula,
user?: NamedNode
): [Statement[], Statement[]] {
const lines = contents.split('\n')
const statementsToAdd = lines.reduce(
(statementsToAdd, lineContents, lineNr) => {
const line = store.sym(pad.uri + `_line${lineNr}`)
const prevLine = (lineNr === 0) ? pad : statementsToAdd[statementsToAdd.length - 1].subject
statementsToAdd.push(
$rdf.st(prevLine, ns.pad('next'), line, pad.doc()),
$rdf.st(line, ns.sioc('content'), lineContents, pad.doc()),
$rdf.st(line, ns.dc('created'), creationDate, pad.doc())
)
if (user) {
statementsToAdd.push($rdf.st(line, ns.dc('author'), user, pad.doc()))
}
return statementsToAdd
},
[] as $rdf.Statement[]
)
const lastLine = statementsToAdd[statementsToAdd.length - 1].subject
statementsToAdd.push($rdf.st(lastLine, ns.pad('next'), pad, pad.doc()))
const oldLines = store.statementsMatching(null as any, ns.pad('next'), null as any, pad.doc(), false)
.map(statement => statement.object)
.filter(line => line.value !== pad.value)
const statementsPerOldLine = oldLines.map(oldLine => {
return store.statementsMatching(oldLine, null as any, null as any, pad.doc(), false)
})
const statementsToDelete = statementsPerOldLine.reduce(
(statementsToDelete, oldLineStatements) => {
statementsToDelete.push(...oldLineStatements)
return statementsToDelete
},
[] as $rdf.Statement[]
)
const [startingLink] = store.statementsMatching(pad, ns.pad('next'), null as any, pad.doc(), true)
if (startingLink) {
statementsToDelete.push(startingLink)
}
return [statementsToDelete, statementsToAdd]
}
export function getTitle (
store: IndexedFormula,
pad: NamedNode
): string {
const [titleStatement] = store.statementsMatching(pad, ns.dc('title'), null, pad.doc(), true)
return titleStatement.object.value
}
export function getContents (
store: IndexedFormula,
pad: NamedNode
): string {
const [firstLineStatement] = store.statementsMatching(pad, ns.pad('next'), null, pad.doc(), true)
let prevLine: Node = firstLineStatement.object
const lines = []
while (prevLine.value !== pad.value) {
const [currentLineStatement] = store.statementsMatching(prevLine, ns.pad('next'), null, pad.doc(), true)
const [lineContentStatement] = store.statementsMatching(
currentLineStatement.subject,
ns.sioc('content'),
null,
pad.doc(),
true
)
if (lineContentStatement) {
lines.push(lineContentStatement.object.value)
}
prevLine = currentLineStatement.object
}
return lines.join('\n')
}
export function getLatestAuthor (
store: IndexedFormula,
pad: NamedNode
): NamedNode | null {
const [firstLineStatement] = store.statementsMatching(pad, ns.pad('next'), null, pad.doc(), true)
let prevLine: Node = firstLineStatement.object
const datesAndAuthors = []
while (prevLine.value !== pad.value) {
const [currentLineStatement] = store.statementsMatching(prevLine, ns.pad('next'), null, pad.doc(), true)
const [lineDateStatement] = store.statementsMatching(
currentLineStatement.subject,
ns.dc('created'),
null,
pad.doc(),
true
)
const [lineAuthorStatement] = store.statementsMatching(
currentLineStatement.subject,
ns.dc('author'),
null,
pad.doc(),
true
)
if (lineDateStatement && lineAuthorStatement) {
datesAndAuthors.push({
created: new Date(lineDateStatement.object.value),
author: lineAuthorStatement.object
})
}
prevLine = currentLineStatement.object
}
if (datesAndAuthors.length === 0) {
return null
}
const latestAuthor = datesAndAuthors.reduce(
(latestAuthor, lineDateAndAuthor) => {
return (latestAuthor.created.getTime() < lineDateAndAuthor.created.getTime())
? lineDateAndAuthor
: latestAuthor
}
)
return latestAuthor.author as NamedNode
}