1
1
// Setup
2
+
3
+ const uriFormat = require ( 'mongodb-uri' )
4
+ function encodeMongoURI ( urlString ) {
5
+ if ( urlString ) {
6
+ let parsed = uriFormat . parse ( urlString )
7
+ urlString = uriFormat . format ( parsed ) ;
8
+ }
9
+ return urlString ;
10
+ } ;
11
+
12
+
2
13
var application_root = __dirname ,
3
14
secret = process . env . SECRET ,
4
15
port = process . env . PORT ,
5
- db = process . env . DB ,
16
+ db = encodeMongoURI ( process . env . DB ) ,
6
17
consumer = process . env . CONSUMER ,
7
18
version = process . env . VERSION ,
8
19
path = require ( "path" ) ,
@@ -30,6 +41,7 @@ var allowCrossDomain = function(req, res, next) {
30
41
}
31
42
} ;
32
43
44
+
33
45
// Schemas
34
46
var Schema = mongoose . Schema ;
35
47
@@ -131,7 +143,7 @@ var Annotation = new Schema({
131
143
required : false
132
144
} ,
133
145
groups : [ String ] ,
134
- subgroups : [ String ] ,
146
+ group_ids : [ Number ] ,
135
147
ranges : [ Ranges ] ,
136
148
tags : [ String ] ,
137
149
permissions : {
@@ -144,11 +156,33 @@ var Annotation = new Schema({
144
156
sort_position : {
145
157
type : String ,
146
158
required : false
159
+ } ,
160
+ doc_title : {
161
+ type : String ,
162
+ required : false
163
+ } ,
164
+ doc_author : {
165
+ type : String ,
166
+ required : false
167
+ } ,
168
+ doc_date : {
169
+ type : String ,
170
+ required : false
171
+ } ,
172
+ doc_publisher : {
173
+ type : String ,
174
+ required : false
175
+ } ,
176
+ doc_edition : {
177
+ type : String ,
178
+ required : false
179
+ } ,
180
+ doc_source : {
181
+ type : String ,
182
+ required : false
147
183
}
148
184
} ) ;
149
185
150
- var AnnotationModel = mongoose . model ( 'Annotation' , Annotation ) ;
151
-
152
186
// DB
153
187
mongoose . connect ( db ) ;
154
188
@@ -159,13 +193,27 @@ app.use(express.urlencoded({
159
193
} ) ) ;
160
194
app . use ( express . json ( ) ) ;
161
195
app . use ( methodOverride ( ) ) ;
162
-
196
+ app . use ( lessMiddleware ( __dirname + '/public' , {
197
+ render :{
198
+ compress : true
199
+ }
200
+ } ) ) ;
201
+
202
+ app . use ( express . static ( path . join ( application_root , "public" ) ) ) ;
203
+ app . use ( errorhandler ( {
204
+ dumpExceptions : true ,
205
+ showStack : true
206
+ } ) ) ;
207
+
163
208
164
209
Annotation . pre ( 'save' , function ( next ) {
165
210
this . id = this . _id ;
166
211
next ( ) ;
167
212
} ) ;
168
213
214
+ var AnnotationModel = mongoose . model ( 'Annotation' , Annotation ) ;
215
+
216
+
169
217
// ROUTES
170
218
app . get ( '/api' , function ( req , res ) {
171
219
res . send ( 'Annotations API is running' ) ;
@@ -209,18 +257,6 @@ app.get('/api/search', tokenOK, function(req, res) {
209
257
case 'user' :
210
258
query . where ( 'user' ) . equals ( req . query . user ) ;
211
259
break ;
212
- case 'group' :
213
- if ( req . query . subgroups ) {
214
- var clean_subgroups = req . query . subgroups ;
215
- var index = clean_subgroups . indexOf ( "" ) ;
216
- if ( index > - 1 ) clean_subgroups . splice ( index , 1 ) ;
217
- query . where ( 'subgroups' ) . in ( clean_subgroups ) ;
218
- }
219
- else {
220
- query . where ( 'subgroups' ) . in ( [ ] ) ;
221
- }
222
- query . $where ( 'this.permissions.read.length < 1' ) ;
223
- break ;
224
260
case 'class' :
225
261
if ( req . query . groups ) {
226
262
var clean_groups = req . query . groups ;
@@ -233,6 +269,15 @@ app.get('/api/search', tokenOK, function(req, res) {
233
269
}
234
270
query . $where ( 'this.permissions.read.length < 1' ) ;
235
271
break ;
272
+ case 'groupId' :
273
+ if ( req . query . group_ids ) {
274
+ query . where ( 'group_ids' ) . in ( req . query . group_ids ) ;
275
+ }
276
+ else {
277
+ query . where ( 'group_ids' ) . in ( [ ] ) ;
278
+ }
279
+ query . $where ( 'this.permissions.read.length < 1' ) ;
280
+ break ;
236
281
case 'admin' :
237
282
query . $where ( 'this.permissions.read.length < 1' ) ;
238
283
break ;
@@ -270,7 +315,6 @@ app.get('/api/search', tokenOK, function(req, res) {
270
315
else {
271
316
query . exec ( function ( err , annotations ) {
272
317
if ( ! err ) {
273
- // console.info(annotations);
274
318
if ( annotations . length > 0 ) {
275
319
return res . send ( {
276
320
'rows' : annotations
@@ -328,14 +372,20 @@ app.post('/api/annotations', tokenOK, function(req, res) {
328
372
quote : req . body . quote ,
329
373
tags : req . body . tags ,
330
374
groups : req . body . groups ,
331
- subgroups : req . body . subgroups ,
375
+ group_ids : req . body . group_ids ,
332
376
uuid : req . body . uuid ,
333
377
parentIndex : req . body . parentIndex ,
334
378
ranges : req . body . ranges ,
335
379
shapes : req . body . shapes ,
336
380
permissions : req . body . permissions ,
337
381
annotation_categories : req . body . annotation_categories ,
338
- sort_position : req . body . sort_position
382
+ sort_position : req . body . sort_position ,
383
+ doc_title : req . body . doc_title ,
384
+ doc_author : req . body . doc_author ,
385
+ doc_date : req . body . doc_date ,
386
+ doc_publisher : req . body . doc_publisher ,
387
+ doc_edition : req . body . doc_edition ,
388
+ doc_source : req . body . doc_source
339
389
} ) ;
340
390
341
391
annotation . save ( function ( err ) {
@@ -375,13 +425,19 @@ app.put('/api/annotations/:id', tokenOK, function(req, res) {
375
425
annotation . quote = req . body . quote ;
376
426
annotation . tags = req . body . tags ;
377
427
annotation . groups = req . body . groups ;
378
- annotation . subgroups = req . body . subgroups ;
428
+ annotation . group_ids = req . body . group_ids ;
379
429
annotation . uuid = req . body . uuid ;
380
430
annotation . parentIndex = req . body . parentIndex ;
381
431
annotation . ranges = req . body . ranges ;
382
432
annotation . permissions = req . body . permissions ;
383
433
annotation . annotation_categories = req . body . annotation_categories ;
384
434
annotation . sort_position = req . body . sort_position ;
435
+ annotation . doc_title = req . body . doc_title ;
436
+ annotation . doc_author = req . body . doc_author ;
437
+ annotation . doc_date = req . body . doc_date ;
438
+ annotation . doc_publisher = req . body . doc_publisher ;
439
+ annotation . doc_edition = req . body . doc_edition ;
440
+ annotation . doc_source = req . body . doc_source ;
385
441
386
442
return annotation . save ( function ( err ) {
387
443
if ( ! err ) {
@@ -408,18 +464,6 @@ app.delete('/api/annotations/:id', tokenOK, function(req, res) {
408
464
} ) ;
409
465
} ) ;
410
466
411
- // Middleware config
412
- app . use ( lessMiddleware ( __dirname + '/public' , {
413
- render :{
414
- compress : true
415
- }
416
- } ) ) ;
417
-
418
- app . use ( express . static ( path . join ( application_root , "public" ) ) ) ;
419
- app . use ( errorhandler ( {
420
- dumpExceptions : true ,
421
- showStack : true
422
- } ) ) ;
423
467
424
468
// Authentication
425
469
function tokenOK ( req , res , next ) {
0 commit comments