-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathincoming-activity.js
More file actions
105 lines (92 loc) · 2.57 KB
/
incoming-activity.js
File metadata and controls
105 lines (92 loc) · 2.57 KB
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
'use strict';
/**
* Module Dependencies
*/
var async = require('async'),
stream = require('getstream'),
streamUtils = require('./../lib/stream_utils');
/**
* Get incoming activity for a specific user
* URL: /incoming-activity
* Method: GET
* Auth Required: Yes
* @param {string} user_id This required param specifies the user id to filter by
* @returns {object} Returns a 200 status code with an array of search objects
*/
server.get('/incoming-activity', function(req, res, next) {
// extract query params
var params = req.params || {};
// async waterfall (see: https://github.com/caolan/async)
async.waterfall(
[
// connect to stream
function(cb) {
// instantiate a new client (server side)
var streamClient = stream.connect(
config.stream.key,
config.stream.secret,
);
// instantiate a feed using feed class 'notification' and user id from params
var notificationFeed = streamClient.feed(
'notification',
params.user_id,
);
cb(null, notificationFeed);
},
// get and loop through activities
function(notificationFeed, cb) {
// empty array to hold activities
var arr = [];
// get activities from stream
notificationFeed
.get({ limit: 100 })
.then(function(stream) {
// length of activity results
var ln = stream.results.length;
// if length is empty, return
if (!ln) {
res.send(204);
return next();
}
/*
* Activities stored in Stream reference user ids and upload ids
* We need the full object, not just the reference for the template
* The steps below query the DB to translate user:2 into
* Something like {'username': 'Nick', 'img':...}
*/
var references = streamUtils.referencesFromActivities(
stream.results,
);
streamUtils.loadReferencedObjects(
references,
params.user_id,
function(referencedObjects) {
streamUtils.enrichActivities(
stream.results,
referencedObjects,
);
// return the enriched activities
cb(null, stream.results);
},
);
})
.catch(function(error) {
cb(error);
});
},
// final cb function
],
function(err, result) {
// catch all errors
if (err) {
// use global logger to log to console
log.error(err);
// return error message to client
return next(new restify.InternalError(err.message));
}
// send response to client
res.send(200, result);
return next();
},
);
});