-
Notifications
You must be signed in to change notification settings - Fork 0
/
paperhive-adapter.js
143 lines (112 loc) · 3.96 KB
/
paperhive-adapter.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
'use strict'
var DocLoopAdapter = require('docloop').DocloopAdapter,
DocloopError = require('docloop').DocloopError,
PaperhiveSource = require('./paperhive-source.js'),
request = require('request-promise-native').defaults({json:true}),
Promise = require('bluebird')
/**
* TODO: description!
* @module paperhiveAdapter
* @license GPL-3.0
*
*/
/**
* Adapter to harvest public annotations from paperhive.
*
* @alias PaperhiveAdapter
* @memberof module:paperhiveAdapter
*
* @extends {DocloopAdapter}
*
* @param {DocloopCore} core
* @param {Object} config Configuration object
* @param {String} config.home Website of the used paperhive instance.
* @param {String} config.contentLink Client Url of paperhive document. Use %s in this string to indicate the positin of the document's id and %t for comment id.
* @param {String} config.documentItemsById Api Url of paperhive document items. Use %s in this string to indicate the positin of the document's id.
* @param {String} config.documentItemByItemId Api Url of paperhive document item. Use %s in this string to indicate the positin of the document's item id.
*
* @param {Boolean} config.extraEndpoints True iff there are addtional non privileged endpoints.
* @param {Number} scanningInterval Time between two scans in milliseconds.
*
* @property {Object} endpointDefaultConfig
* @property {Object} endpointDefaultConfig.includePastAnnotations If set to true, the adapter will collect all annotations from a source,
* as soon as a link is established. This might be undesirable for documents with a
* large amount of annotations.
*/
class PaperhiveAdapter extends DocLoopAdapter {
constructor(core, config){
super(core, {
...config,
id: 'paperhive',
type: 'source',
endpointClass: PaperhiveSource,
endpointDefaultConfig: {
includePastAnnotations: true,
}
})
this.id = 'paperhive'
this.config = config
//May generelize for all adapters
var mandatory_config = {
name: true,
home: true,
contentLink: true,
discussionsLink: true,
documentItemsById: true,
documentItemByItemId: true,
scanningInterval: true
}
for(var option in mandatory_config){
if(this.config[option] === undefined) throw new Error("PaperhiveAdapter.constructor(): missing config: "+option)
}
this.core.on('link-established', this.handleLinkEstablishedEvent.bind(this) )
this.core.on('link-removed', link => {
console.log('### link removed', link.id)
})
this.core.ready
.then( () => {
setInterval(this.scanSources.bind(this), this.config.scanningInterval || 6*60*60*1000)
})
}
/**
* If the source matches this adapter it will be scanned for annotations and replies.
* @param {DocloopLink.skeleton}
* @return undefined
* @listens link-established
*/
async handleLinkEstablishedEvent(link){
if(!link || !link.source || !link.source.adapter == this.id) return null
var source = await this.getStoredEndpoint(link.source.id)
source.scan()
}
/**
* Scan all stored source for new annotations or replies
* @return undefined
*/
async scanSources(){
//TODO: spread events? Dont handle all of them at the same time...
var raw_sources = await this.endpoints.find({}).toArray()
return Promise.all(
raw_sources
.map( data => this.newEndpoint(data))
.map( source => source.scan() )
)
}
/**
* There are no privileged endpoints. This Adapter only uses public paperhive documents.
*
* @return {Array} []
*/
async getEndpoints(){
return []
}
/**
* There are no privileged endpoints. This Adapter only uses public paperhive documents.
*
* @return {Array} []
*/
async getStoredEndpoints(session_data){
return []
}
}
module.exports = {PaperhiveAdapter, PaperhiveSource}