-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
469 lines (425 loc) · 14.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
* @apiDefine ApiMiddleware Seguir Express Middleware
*
* This is a pre-built set of express routes that allow you to add seguir to your Node application very quickly and simply.
*
* You still create a Seguir client instance, but pass that in to this default middleware to expose the 'standard'
* collection of client endpoints within your application.
*
* This middleware expects you to pass in two additional types of middleware, but does assume that your authentication
* middleware has run first, and so will attempt to access req.user by the usual convention.
*
* Usage:
* var Seguir = require('seguir/client');
* var seguir = new Seguir(config);
* var seguirMiddleware = require('seguir-express-middleware');
* app.use('/social', seguirMiddleware(options, express, seguir, authMiddleware));
*
* authMiddleware is middleware that checks that the user is authorised to use the API and sets req.user.
*
* This middleware assumes that the user exists on 'req.user', with property 'seguirId' (can be over-ridden with options.seguirIdProperty);
*
* Express and Seguir are passed in to avoid the express and seguir dependency outside of testing.
*
*/
var _ = require('lodash');
module.exports = function (options, express, seguir, authMiddleware) {
var router = express.Router();
var u = seguir.urls; // Url helper
var defaults = {
userProperty: 'user',
seguirIdProperty: 'seguirId',
user: true,
post: true,
friend: true,
follow: true,
like: true,
feed: true
};
options = _.defaults(options, defaults);
var respondWithError = function (err, res) {
res.status(err.statusCode || 500);
res.send(err);
};
function getSeguirId (req) {
if (options.userProperty && options.seguirIdProperty && req[options.userProperty] && req[options.userProperty][options.seguirIdProperty]) {
return req[options.userProperty][options.seguirIdProperty];
}
if (options.seguirIdProperty && req[options.seguirIdProperty]) {
return req[options.seguirIdProperty];
}
return null;
}
if (options.user) {
/**
* @apiDefine ApiUsers Users
*/
/**
* @api {get} /user/:user get a user details
* @apiName GetUser
* @apiGroup ApiUsers
* @apiVersion 1.0.0
*
* @apiDescription Gets details of a user by guid
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the guid of the user to get details for
*
*/
router.get(u('getUser'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getUser(seguirId, req.params.user, function (err, user) {
if (err) { return respondWithError(err, res); }
res.send(user);
});
});
/**
* @api {get} /username/:username get a user details by username
* @apiName GetUserByName
* @apiGroup ApiUsers
* @apiVersion 1.0.0
*
* @apiDescription Gets details of a user by username
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} username the name of the user to get details for
*
*/
router.get(u('getUserByName'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getUserByName(seguirId, req.params.username, function (err, user) {
if (err) { return respondWithError(err, res); }
res.send(user);
});
});
/**
* @api {get} /useraltid/:altid get a user details by altid
* @apiName GetUserByAltid
* @apiGroup ApiUsers
* @apiVersion 1.0.0
*
* @apiDescription Gets details of a user by username
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} altid the altid of the user to get details for
*
*/
router.get(u('getUserByAltId'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getUserByAltId(seguirId, req.params.altid, function (err, user) {
if (err) { return respondWithError(err, res); }
res.send(user);
});
});
/**
* @api {get} /user/:user/relationship get a relationship details
* @apiName GetUserRelationship
* @apiGroup ApiUsers
* @apiVersion 1.0.0
*
* @apiDescription Gets details of a relationship between the logged in user and someone else
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the guid of the user to compare to the logged in user
*
*/
router.get(u('getUserRelationship'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getUserRelationship(seguirId, req.params.user, function (err, relationship) {
if (err) { return respondWithError(err, res); }
res.send(relationship);
});
});
}
if (options.post) {
/**
* @apiDefine ApiPosts Posts
*/
/**
* @api {post} /post/:post get a post
* @apiName GetPost
* @apiGroup ApiPosts
* @apiVersion 1.0.0
*
* @apiDescription Gets a post
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} post the guid of the post
*
*/
router.get(u('getPost'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getPost(seguirId, req.params.post, function (err, post) {
if (err) { return respondWithError(err, res); }
res.send(post);
});
});
/**
* @api {post} /post Add a post
* @apiName AddPost
* @apiGroup ApiPosts
* @apiVersion 1.0.0
*
* @apiDescription Creates a post
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} content the body of the post
* @apiParam {Boolean} isprivate is the post private
*
*/
router.post(u('addPost'), authMiddleware, function (req, res) {
var isprivate = req.body.isprivate === 'true' || req.body.isprivate === true;
var ispersonal = req.body.ispersonal === 'true' || req.body.ispersonal === true;
var contentType = req.body.contentType || 'text/html';
var seguirId = getSeguirId(req);
seguir.addPost(seguirId, req.body.content, contentType, Date.now(), isprivate, ispersonal, function (err, post) {
if (err) { return respondWithError(err, res); }
res.send(post);
});
});
/**
* @api {del} /post/:post Remove a post
* @apiName DeletePost
* @apiGroup ApiPosts
* @apiVersion 1.0.0
*
* @apiDescription Deletes a post
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} post the guid of the post
*
*/
router.delete(u('removePost'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.removePost(seguirId, req.params.post, function (err, result) {
if (err) { return respondWithError(err, res); }
res.send(result);
});
});
}
if (options.friend) {
/**
* @apiDefine ApiFriends Friends
*/
/**
* @api {get} /friend-request/active Get list of friend requests
* @apiName GetFriendRequests
* @apiGroup ApiFriends
* @apiVersion 1.0.0
*
* @apiDescription Gets a list of users friend requests
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
*
*/
router.get(u('getFriendRequests'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getFriendRequests(seguirId, function (err, friend_requests) {
if (err) { return respondWithError(err, res); }
res.send(friend_requests);
});
});
/**
* @api {post} /friend-request Add a friend request
* @apiName AddFriendRequest
* @apiGroup ApiFriends
* @apiVersion 1.0.0
*
* @apiDescription Creates a friend request
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the user to send a friend request to
* @apiParam {Boolean} message the message to send with the request
*
*/
router.post(u('addFriendRequest'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.addFriendRequest(seguirId, req.body.user, req.body.message, Date.now(), function (err, friend) {
if (err) { return respondWithError(err, res); }
res.send(friend);
});
});
/**
* @api {delete} /user/:user/friend/:user_friend Remove a friendship
* @apiName RemoveFriend
* @apiGroup ApiFriends
* @apiVersion 1.0.0
*
* @apiDescription Removes a friend relationship
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the user to stop being friends with
*
*/
router.delete(u('removeFriend'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.removeFriend(seguirId, req.params.user, function (err, result) {
if (err) { return respondWithError(err, res); }
res.send(result);
});
});
/**
* @api {post} /friend-request/accept Accept a friend request
* @apiName AcceptFriendRequest
* @apiGroup ApiFriends
* @apiVersion 1.0.0
*
* @apiDescription Accepts a friend request sent by another user.
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} friend_request the request guid to accept
*
*/
router.post(u('acceptFriendRequest'), authMiddleware, function (req, res) {
seguir.acceptFriendRequest(req.user.seguirId, req.body.friend_request, function (err, friend_request) {
if (err) { return respondWithError(err, res); }
res.send(friend_request);
});
});
}
if (options.follow) {
/**
* @apiDefine ApiFollow Following
*/
/**
* @api {post} /follow Follow another user
* @apiName AddFollower
* @apiGroup ApiFollow
* @apiVersion 1.0.0
*
* @apiDescription Follows another user
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the guid of the user to follow
*
*/
router.post(u('addFollower'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.followUser(seguirId, req.body.user, Date.now(), function (err, follow) {
if (err) { return respondWithError(err, res); }
res.send(follow);
});
});
/**
* @api {delete} /follow/:user Un-follow another user
* @apiName RemoveFollower
* @apiGroup ApiFollow
* @apiVersion 1.0.0
*
* @apiDescription Stops following another user
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the guid of the user to stop following
*
*/
router.delete(u('removeFollower'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.unFollowUser(seguirId, req.params.user, function (err, result) {
if (err) { return respondWithError(err, res); }
res.send(result);
});
});
/**
* @api {get} /user/:user/followers Get followers
* @apiName GetFollowers
* @apiGroup ApiFollow
* @apiVersion 1.0.0
*
* @apiDescription Stops following another user
* @apiParam {Object} req.user expects req.user to be present, with req.user.seguirId
* @apiParam {String} user the guid of the user to get followers for
*
*/
router.get(u('getFollowers'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getFollowers(seguirId, req.params.user, function (err, result) {
if (err) { return respondWithError(err, res); }
res.send(result);
});
});
}
if (options.like) {
/**
* @apiDefine ApiLikes Likes
*/
/**
* @api {post} /like Add a like
* @apiName AddLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Creates a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.post(u('addLike'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.addLike(seguirId, req.body.item, function (err, like) {
if (err) { return respondWithError(err, res); }
res.send(like);
});
});
/**
* @api {get} /like/item Check if a user likes an item
* @apiName CheckLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Checks a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.get(u('checkLike'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.checkLike(seguirId, req.params.item, function (err, like) {
if (err) { return respondWithError(err, res); }
res.send(like);
});
});
/**
* @api {del} /like/:item Add a like
* @apiName AddLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Creates a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.delete(u('removeLike'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.removeLike(seguirId, req.params.item, function (err, like) {
if (err) { return respondWithError(err, res); }
res.send(like);
});
});
}
if (options.feed) {
/**
* @api {del} /feed Get feed for logged in user
* @apiName GetFeed
* @apiGroup ApiFeeds
* @apiVersion 1.0.0
*
* @apiDescription Gets a user feed
* @apiParam {Object} user users req.user.seguirId as the user id
*
*/
router.get(u('getFeed'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getFeed(seguirId, req.params.user, 0, 50, function (err, feed) {
if (err) { return respondWithError(err, res); }
res.send(feed);
});
});
/**
* @api {del} /feed Get user feed for user
* @apiName GetUserFeed
* @apiGroup ApiFeeds
* @apiVersion 1.0.0
*
* @apiDescription Gets a user feed
* @apiParam {String} user user of feed to view
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
*
*/
router.get(u('getUserFeed'), authMiddleware, function (req, res) {
var seguirId = getSeguirId(req);
seguir.getUserFeed(seguirId, req.params.user, 0, 50, function (err, feed) {
if (err) { return respondWithError(err, res); }
res.send(feed);
});
});
}
return router;
};