forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateFolder.js
More file actions
165 lines (152 loc) · 5.5 KB
/
Copy pathdateFolder.js
File metadata and controls
165 lines (152 loc) · 5.5 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
/**
* Contains the [[DateFolder]] class
* @packageDocumentation
*/
/* global $rdf */
const kb = require('../store.js')
const ns = require('../ns.js')
/**
* Track back through the YYYY/MM/DD tree to find the previous/next day
*/
module.exports = class DateFolder {
constructor (rootThing, leafFileName, membershipProperty) {
this.root = rootThing
this.rootFolder = rootThing.dir()
this.leafFileName = leafFileName || 'index.ttl' // typically chat.ttl
this.membershipProperty = membershipProperty || ns.wf('leafObject')
}
/* Generate the leaf document (rdf object) from date
* @returns: <NamedNode> - document
*/
leafDocumentFromDate (date) {
// console.log('incoming date: ' + date)
const isoDate = date.toISOString() // Like "2018-05-07T17:42:46.576Z"
var path = isoDate.split('T')[0].replace(/-/g, '/') // Like "2018/05/07"
path = this.root.dir().uri + path + '/' + this.leafFileName
return kb.sym(path)
}
/* Generate a date object from the leaf file name
*/
dateFromLeafDocument (doc) {
const head = this.rootFolder.uri.length
const str = doc.uri.slice(head, head + 10).replace(/\//g, '-')
// let date = new Date(str + 'Z') // GMT - but fails in FF - invalid format :-(
var date = new Date(str) // not explicitly UTC but is assumed so in spec
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
console.log('Date for ' + doc + ':' + date.toISOString())
return date
}
async loadPrevious (date, backwards) {
const thisDateFolder = this
async function previousPeriod (file, level) {
function younger (x) {
if (backwards ? x.uri >= file.uri : x.uri <= file.uri) return false // later than we want or same -- looking for different
return true
}
function suitable (x) {
const tail = x.uri
.slice(0, -1)
.split('/')
.slice(-1)[0]
if (!'0123456789'.includes(tail[0])) return false // not numeric
return true
}
async function lastNonEmpty (siblings) {
siblings = siblings.filter(suitable)
siblings.sort() // chronological order
if (!backwards) siblings.reverse()
if (level !== 3) return siblings.pop() // only length chck final leverl
while (siblings.length) {
const folder = siblings.pop()
const leafDocument = kb.sym(folder.uri + thisDateFolder.leafFileName)
await kb.fetcher.load(leafDocument)
// files can have seealso links. skip ones with no leafObjects with a date
if (
kb.statementsMatching(null, ns.dct('created'), null, leafDocument)
.length > 0
) {
return folder
}
}
return null
}
// console.log(' previousPeriod level' + level + ' file ' + file)
const parent = file.dir()
await kb.fetcher.load(parent)
var siblings = kb.each(parent, ns.ldp('contains'))
siblings = siblings.filter(younger)
const folder = await lastNonEmpty(siblings)
if (folder) return folder
if (level === 0) return null // 3:day, 2:month, 1: year 0: no
const uncle = await previousPeriod(parent, level - 1)
if (!uncle) return null // reached first ever
await kb.fetcher.load(uncle)
var cousins = kb.each(uncle, ns.ldp('contains'))
const result = await lastNonEmpty(cousins)
return result
} // previousPeriod
const folder = this.leafDocumentFromDate(date).dir()
const found = await previousPeriod(folder, 3)
if (found) {
const doc = kb.sym(found.uri + this.leafFileName)
return this.dateFromLeafDocument(doc)
}
return null
} // loadPrevious
async firstLeaf (backwards) {
// backwards -> last leafObject
var folderStore = $rdf.graph()
var folderFetcher = new $rdf.Fetcher(folderStore)
async function earliestSubfolder (parent) {
function suitable (x) {
const tail = x.uri
.slice(0, -1)
.split('/')
.slice(-1)[0]
if (!'0123456789'.includes(tail[0])) return false // not numeric
return true
}
console.log(' parent ' + parent)
delete folderFetcher.requested[parent.uri]
// try {
await folderFetcher.load(parent, { force: true }) // Force fetch as will have changed
// }catch (err) {
// }
var kids = folderStore.each(parent, ns.ldp('contains'))
kids = kids.filter(suitable)
if (kids.length === 0) {
throw new Error(' @@@ No children to parent2 ' + parent)
}
kids.sort()
if (backwards) kids.reverse()
return kids[0]
}
const y = await earliestSubfolder(this.root.dir())
const month = await earliestSubfolder(y)
const d = await earliestSubfolder(month)
const leafDocument = $rdf.sym(d.uri + 'chat.ttl')
await folderFetcher.load(leafDocument)
const leafObjects = folderStore.each(
this.root,
this.membershipProperty,
null,
leafDocument
)
if (leafObjects.length === 0) {
const msg =
' INCONSISTENCY -- no chat leafObject in file ' + leafDocument
console.trace(msg)
throw new Error(msg)
}
const sortMe = leafObjects.map(leafObject => [
folderStore.any(leafObject, ns.dct('created')),
leafObject
])
sortMe.sort()
if (backwards) sortMe.reverse()
console.log(
(backwards ? 'Latest' : 'Earliest') + ' leafObject is ' + sortMe[0][1]
)
return sortMe[0][1]
} // firstleafObject
} // class