1
1
// Setup
2
2
var application_root = __dirname ,
3
3
config = require ( "./config" ) ,
4
- express = require ( "express" ) ,
4
+ express = require ( "express" ) ,
5
5
path = require ( "path" ) ,
6
6
mongoose = require ( 'mongoose' ) ,
7
7
lessMiddleware = require ( 'less-middleware' ) ,
@@ -37,6 +37,7 @@ function inWindow (decoded, next) {
37
37
return ( ( result > 0 ) ? true : false ) ;
38
38
}
39
39
40
+ // CORS
40
41
var allowCrossDomain = function ( req , res , next ) {
41
42
res . header ( 'Access-Control-Allow-Origin' , '*' ) ;
42
43
res . header ( 'Access-Control-Expose-Headers' , 'Content-Length, Content-Type, Location' ) ;
@@ -58,7 +59,6 @@ mongoose.connect(config.mongodb.live);
58
59
59
60
// config
60
61
app . configure ( function ( ) {
61
- // app.use(tokenOK);
62
62
app . use ( allowCrossDomain ) ;
63
63
app . use ( express . bodyParser ( ) ) ;
64
64
app . use ( express . methodOverride ( ) ) ;
@@ -72,9 +72,10 @@ app.configure(function () {
72
72
app . use ( express . errorHandler ( { dumpExceptions : true , showStack : true } ) ) ;
73
73
} ) ;
74
74
75
- var Schema = mongoose . Schema ; //Schema.ObjectId
75
+ var Schema = mongoose . Schema ;
76
76
77
77
// Schemas
78
+ // Annotator Ranges
78
79
var Ranges = new Schema ( {
79
80
start : { type : String , required : true } ,
80
81
end : { type : String , required : true } ,
@@ -120,7 +121,6 @@ app.get('/api', function (req, res) {
120
121
} ) ;
121
122
122
123
// Search annotations
123
- // Auth: Token required to search
124
124
app . get ( '/api/search' , tokenOK , function ( req , res ) {
125
125
var query = AnnotationModel . find ( { 'uri' : req . query . uri } ) ;
126
126
@@ -140,7 +140,6 @@ app.get('/api/search', tokenOK, function (req, res) {
140
140
}
141
141
142
142
if ( req . query . sidebar ) {
143
- // console.log("Sidebar request: "+ JSON.stringify(req.query));
144
143
query . exec ( function ( err , annotations ) {
145
144
if ( ! err ) {
146
145
return res . send ( annotations ) ;
@@ -150,7 +149,6 @@ app.get('/api/search', tokenOK, function (req, res) {
150
149
} ) ;
151
150
}
152
151
else {
153
- // console.log("Non-sidebar request: "+ JSON.stringify(req.query));
154
152
query . exec ( function ( err , annotations ) {
155
153
if ( ! err ) {
156
154
return res . send ( { 'rows' : annotations } ) ;
@@ -159,13 +157,10 @@ app.get('/api/search', tokenOK, function (req, res) {
159
157
}
160
158
} ) ;
161
159
}
162
- // if (req.query.permissions[read]) {};
163
160
} ) ;
164
161
165
162
// GET to READ
166
163
// List annotations
167
- // Auth: Anyone can see all annotations (no check for token)
168
- // Why?
169
164
app . get ( '/api/annotations' , tokenOK , function ( req , res ) {
170
165
return AnnotationModel . find ( function ( err , annotations ) {
171
166
if ( ! err ) {
@@ -177,8 +172,6 @@ app.get('/api/annotations', tokenOK, function (req, res) {
177
172
} ) ;
178
173
179
174
// Single annotation
180
- // Auth: Anyone can see a single annotation (no check for token)
181
- // Why?
182
175
app . get ( '/api/annotations/:id' , tokenOK , function ( req , res ) {
183
176
return AnnotationModel . findById ( req . params . id , function ( err , annotation ) {
184
177
if ( ! err ) {
@@ -190,7 +183,6 @@ app.get('/api/annotations/:id', tokenOK, function (req, res) {
190
183
} ) ;
191
184
192
185
// POST to CREATE
193
- // Auth: Token required to post an annotation
194
186
app . post ( '/api/annotations' , tokenOK , function ( req , res ) {
195
187
var annotation ;
196
188
console . log ( "POST: " ) ;
@@ -212,7 +204,6 @@ app.post('/api/annotations', tokenOK, function (req, res) {
212
204
ranges : req . body . ranges ,
213
205
permissions : req . body . permissions
214
206
} ) ;
215
- // console.log(annotation.permissions.read);
216
207
217
208
annotation . save ( function ( err ) {
218
209
if ( ! err ) {
@@ -226,38 +217,7 @@ app.post('/api/annotations', tokenOK, function (req, res) {
226
217
} ) ;
227
218
228
219
// PUT to UPDATE
229
- // Bulk update: we won't really be doing this will we?
230
- // Auth: Token required to update all annotations
231
- // Permissions: users can update only their own annotations (handled by annotator)
232
- app . put ( '/api/annotations' , tokenOK , function ( req , res ) {
233
- var i , len = 0 ;
234
- console . log ( "is Array req.body.annotations" ) ;
235
- console . log ( Array . isArray ( req . body . annotations ) ) ;
236
- console . log ( "PUT: (annotations)" ) ;
237
- console . log ( req . body . annotations ) ;
238
- if ( Array . isArray ( req . body . annotations ) ) {
239
- len = req . body . annotations . length ;
240
- }
241
- for ( i = 0 ; i < len ; i ++ ) {
242
- console . log ( "UPDATE annotation by id:" ) ;
243
- for ( var id in req . body . annotations [ i ] ) {
244
- console . log ( id ) ;
245
- }
246
- AnnotationModel . update ( { "_id" : id } , req . body . annotations [ i ] [ id ] , function ( err , numAffected ) {
247
- if ( err ) {
248
- console . log ( "Error on update" ) ;
249
- console . log ( err ) ;
250
- } else {
251
- console . log ( "updated num: " + numAffected ) ;
252
- }
253
- } ) ;
254
- }
255
- return res . send ( req . body . annotations ) ;
256
- } ) ;
257
-
258
- // Single update: This is much more likely
259
- // Auth: Token required to update one annotation
260
- // Permissions: users can update only their own annotations (handled by annotator)
220
+ // Single update
261
221
app . put ( '/api/annotations/:id' , tokenOK , function ( req , res ) {
262
222
return AnnotationModel . findById ( req . params . id , function ( err , annotation ) {
263
223
annotation . _id = req . body . _id ;
@@ -290,25 +250,7 @@ app.put('/api/annotations/:id', tokenOK, function (req, res) {
290
250
} ) ;
291
251
292
252
// DELETE to DESTROY
293
- // Bulk destroy all annotations
294
- // Auth: Token required to delete all annotations
295
- // NOTE: Can't think of a good use case -- commenting out. jF 09/06/2010
296
- // Permissions: user can delete only own annotations (handled by annotator)
297
- // app.delete('/api/annotations', tokenOK, function (req, res) {
298
- // AnnotationModel.remove(function (err) {
299
- // if (!err) {
300
- // console.log("removed");
301
- // return res.send('');
302
- // } else {
303
- // console.log(err);
304
- // }
305
- // });
306
- // });
307
-
308
-
309
253
// Remove a single annotation
310
- // Auth: Token required to delete one annotation
311
- // Permissions: user can delete only own annotations (handled by annotator)
312
254
app . delete ( '/api/annotations/:id' , tokenOK , function ( req , res ) {
313
255
return AnnotationModel . findById ( req . params . id , function ( err , annotation ) {
314
256
return annotation . remove ( function ( err ) {
0 commit comments