-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivityController.js
56 lines (42 loc) · 1.48 KB
/
activityController.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
var utils = require('./utilities.js');
var activityController = function activityController(activityLog, connectionPool, plugins) {
this.update = function update(request, response) {
var entries = null;
if (request.query.since === undefined) {
entries = activityLog.findAllEntries();
} else {
var since = request.query.since;
entries = activityLog.findEntries(since);
}
entries = entries.map(applyPlugins);
if (entries.length)
utils.jsonResponse(entries, response);
else
connectionPool.add(response, since);
};
var applyPlugins = function applyPlugins(entry) {
var clone = utils.clone(entry);
for (var name in plugins)
clone = tryApply(plugins[name], clone);
return clone;
};
var tryApply = function tryApply(plugin, entry) {
if (!plugin.apply)
return entry;
try {
return plugin.apply(entry);
} catch (err) {
console.log(err.message);
}
};
var onActivity = function onActivity() {
connectionPool.endAll(writeEntries);
};
var writeEntries = function writeEntries(response, since) {
var entries = activityLog.findEntries(since)
.map(applyPlugins);
utils.jsonResponse(entries, response);
};
activityLog.on('activity', onActivity);
};
module.exports = activityController;