-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
293 lines (264 loc) · 9.07 KB
/
index.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
function logWrite(msg) {
console.log(msg)
hlib.getById('viewer').innerHTML = `<div class="logMessage">${msg}</div>`
}
function logAppend(msg) {
console.log(msg)
hlib.getById('viewer').innerHTML += `<div class="logMessage">${msg}</div>`
}
function setZoteroApiKey() {
hlib.setLocalStorageFromForm('zoteroApiKeyForm', 'h_zoteroApiKey')
}
function getZoteroApiKey() {
return localStorage.getItem('h_zoteroApiKey')
}
function setZoteroUserId() {
hlib.setLocalStorageFromForm('zoteroUserIdForm', 'h_zoteroUserId')
}
function getZoteroUserId() {
return localStorage.getItem('h_zoteroUserId')
}
// necessary because hlib now uses fetch, which does not allow access to custom headers,
// and zotero returns total-results in a custom header
function _httpRequest(method, url, headers) {
return new Promise(function(resolve, reject) {
const xhr = new XMLHttpRequest()
xhr.open(method, url)
for (let header of headers) {
const key = Object.keys(header)[0]
xhr.setRequestHeader(key, header[key])
}
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve({
response: xhr.response,
total: xhr.getResponseHeader('total-results')
})
} else {
reject({
status: this.status,
statusText: xhr.statusText
})
}
}
xhr.onerror = function() {
reject({
url: url,
status: this.status,
statusText: xhr.statusText
})
}
xhr.send()
})
}
// main entry point, wired to sync button
function sync() {
const offset = 0
collectZoteroItems(offset, [], [], processZoteroItems)
}
// offset: for zotero api paging
// zoteroItems: accumulator for items in the zotero library
// hypothesisNotes: subset of items that are notes imported from hypothesis
// processZoteroItems: handler called when all items collected
function collectZoteroItems(offset, zoteroItems, hypothesisNotes, processZoteroItems) {
const url = `https://www.zotero.org/api/users/${getZoteroUserId()}/items?start=${offset}&limit=50`
const headers = [ { 'Zotero-API-Key': `${getZoteroApiKey()}` }, { Authorization: `Bearer ${hlib.getToken()}` } ]
_httpRequest('get', url, headers)
.then(function(data) {
const items = JSON.parse(data.response)
const total = parseInt(data.total)
// summarize results and accumulate them into the array zoteroItems
items.forEach(item => {
const result = {
key: item.key,
version: item.version,
doi: item.data.DOI ? item.data.DOI : null,
title: item.data.title,
url: item.data.url,
itemType: item.data.itemType,
tags: item.data.tags
}
zoteroItems.push(result)
})
logWrite(`fetched ${zoteroItems.length} of ${total} zotero items`)
if (total && zoteroItems.length >= total) {
logWrite('')
// we have all the items in the zotero library
// we need to query hypothesis for items that have urls, looking for annotations on them
zoteroItems = zoteroItems.filter(x => {
let r = true
if (x.itemType === 'attachment') {
r = false // skip attachments, which have urls but are duplicative of primary types (newspaper article, blog post, etc.)
}
if (!x.url && x.itemType !== 'note') {
r = false // skip other items with no url, but keep notes so we can avoid duplicate imports
}
return r
})
// collect zotero notes that represent imported hypothesis annotations
// it's the subset of notes with tags prefixed like 'hypothesis-BvFJGPmpRd-7d6g7_sOpFg
// and suffixed with hypothesis ids that are in zotero and won't be reimported
let _hypothesisNotes = zoteroItems.filter(x => {
return x.itemType === 'note' && x.tags.length > 0 && hasHypothesisTag(x) // only zotero notes with hypothesis tags
})
hypothesisNotes = hypothesisNotes.concat(_hypothesisNotes)
let _hypothesisNoteKeys = _hypothesisNotes.map(x => {
return x.key // capture zotero keys
})
zoteroItems = zoteroItems.filter(x => {
return _hypothesisNoteKeys.indexOf(x.key) == -1 // exclude _hypothesisNotes
})
zoteroItems = zoteroItems.filter(x => {
return x.url // exclude items with no url
})
processZoteroItems(hypothesisNotes, zoteroItems)
} else {
// continue collecting until all pages of zotero api results are processed
offset += 50
collectZoteroItems(offset, zoteroItems, hypothesisNotes, processZoteroItems)
}
})
.catch((e) => {
logAppend(JSON.stringify(e))
})
}
// hypothesisNotes: zoteroItems that are child notes from hypothesis
// zoteroItems: zoteroItems that are not child notes from hypothesis
function processZoteroItems(hypothesisNotes, zoteroItems) {
logAppend(`zotero items that could be annotated: ${zoteroItems.length}`)
// spawn a worker to fetch hypothesis annotations for zotero items
const annotationFetcher = new Worker('fetchAnnotations.js')
const annotationFetchResults = {}
// listen for messages from the annotation fetcher
annotationFetcher.addEventListener('message', function(e) {
annotationFetchResults[e.data.key] = e.data
let fetchedCount = Object.keys(annotationFetchResults).length
//logWrite(`fetchWorker got response #${fetchedCount} of ${zoteroItems.length} expected`)
// expect as many messages as zotero items, if fewer, the app will time out
if (fetchedCount == zoteroItems.length) {
//logAppend(`all ${fetchedCount} messages received from annotation fetcher, calling importer`)
annotationFetcher.terminate()
// get the ids of imported hypothesis notes
let excludedIds = hypothesisNotes.map(x => {
let id = 'NoHypothesisId'
x.tags.forEach(tag => {
if (isHypothesisTag(tag)) {
id = getHypothesisIdFromZoteroTag(tag)
}
})
return id
})
let resultsToImport = []
const zoteroKeys = Object.keys(annotationFetchResults)
for (let i = 0; i < zoteroKeys.length; i++) {
const fetchedResultForZoteroKey = annotationFetchResults[zoteroKeys[i]]
if (fetchedResultForZoteroKey.hypothesisTotal == 0) {
continue
}
let candidateAnnos = fetchedResultForZoteroKey.hypothesisAnnos
// exclude replies
candidateAnnos = candidateAnnos.filter(x => {
return !x.references
})
// filter out the excluded rows
const importAnnos = candidateAnnos.filter(x => {
return excludedIds.indexOf(x.id) == -1
})
fetchedResultForZoteroKey.hypothesisAnnos = importAnnos
if (importAnnos.length) {
resultsToImport.push(fetchedResultForZoteroKey)
}
}
logAppend(`zotero items with new annotations to import: ${resultsToImport.length}`)
if (resultsToImport.length) {
importer(resultsToImport)
} else {
logAppend('done')
}
}
})
// message the worker once per zotero item
zoteroItems.forEach(zoteroItem => {
annotationFetcher.postMessage({
zoteroItem: zoteroItem,
token: hlib.getToken() // hypothesis api token so worker can read private/group annotations
})
})
}
function getHypothesisIdFromZoteroTag(tag) {
return tag['tag'].slice(11)
}
function isHypothesisTag(tag) {
return tag['tag'].slice(0, 11) === 'hypothesis-'
}
function hasHypothesisTag(zoteroItem) {
let hasHypothesisTag = false
zoteroItem.tags.forEach(tag => {
if (isHypothesisTag(tag)) {
hasHypothesisTag = true
}
})
return hasHypothesisTag
}
// a web worker called with a list of objects that contain a merge of
// zotero item info and hypothesis api search results
function importer(resultsToImport) {
const importWorker = new Worker('postNotes.js')
const objectKeys = Object.keys(resultsToImport)
const expectedResponses = {}
objectKeys.forEach(key => {
const resultToImport = resultsToImport[key]
expectedResponses[resultToImport.key] = resultToImport.hypothesisAnnos.length
})
importWorker.addEventListener('message', function(e) {
if (e.data.zoteroKey) {
expectedResponses[e.data.zoteroKey] -= 1
} else {
logAppend(e.data)
}
let done = true
Object.keys(expectedResponses).forEach(zoteroKey => {
if (expectedResponses[zoteroKey]) {
done = false // we haven't yet received all the messages for this key
}
})
if (done) {
logAppend('done')
importWorker.terminate()
}
})
objectKeys.forEach(key => {
// ask the worker to import annotations for a zotero item
importWorker.postMessage({
zoteroUserId: getZoteroUserId(),
zoteroApiKey: getZoteroApiKey(),
zoteroItemKey: key,
annotationsToImport: resultsToImport[key]
})
})
}
const tokenContainer = hlib.getById('tokenContainer')
hlib.createApiTokenInputForm(tokenContainer)
const userArgs = {
element: hlib.getById('zoteroUserContainer'),
name: 'Zotero numeric user ID',
id: 'zoteroUserId',
value: getZoteroUserId(),
onchange: setZoteroUserId,
type: '',
msg: `Zotero numeric user id from
<a href="https://www.zotero.org/settings/keys">https://www.zotero.org/settings/keys</a>`
}
hlib.createNamedInputForm(userArgs)
const apiKeyArgs = {
element: hlib.getById('zoteroApiKeyContainer'),
name: 'Zotero API key',
id: 'zoteroApiKey',
value: getZoteroApiKey(),
onchange: setZoteroApiKey,
type: 'password',
msg: `Zotero API key from
<a href="https://www.zotero.org/settings/keys">https://www.zotero.org/settings/keys</a>`
}
hlib.createNamedInputForm(apiKeyArgs)
const viewer = document.getElementById('viewer')