forked from st8998/commitwall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
76 lines (58 loc) · 2.1 KB
/
web.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
var redis = require('redis'),
client = redis.createClient();
var jade = require('jade');
var _ = require('underscore');
function attach(app) {
client.on('error', function(err) {
console.log("Redis Error:" + err);
});
app.post('/:streamName/github', function(req, res, next) {
var pushMessage = JSON.parse(req.param('payload'));
var commits = pushMessage.commits;
req.body.messages = _.map(commits, function(commit) {
var weight = Math.log(commit.added.length + commit.removed.length + commit.modified.length)/Math.E;
weight = weight > 1 ? 1 : weight;
return {
text: commit.message,
source: 'github',
group: pushMessage.repository.name,
author: commit.author,
timestamp: commit.timestamp,
weight: weight,
url: commit.url,
extras: {
added: commit.added,
removed: commit.removed,
modified: commit.modified
}
};
});
next();
});
app.post(/^\/(\w+)\/?.*/, function(req, res) {
var streamName = req.params[0];
var messages = req.param('messages', [req.param('message')]);
console.log(messages);
_.each(messages, function(message) {
client.lpush('messages:' + streamName, JSON.stringify(message), function(err, rep) {
client.publish('messages:' + streamName, JSON.stringify(message));
});
client.ltrim('messages:' + streamName, 0, 999);
});
res.send(messages.length + ' messages was pushed to ' + streamName);
});
app.get('/:streamName.:format?', function(req, res) {
var from = req.param('from', 0),
to = req.param('to', 100),
streamName = req.param('streamName');
client.lrange('messages:' + streamName, from, to, function(err, jsonMessages) {
var messages = jsonMessages.map(function(message) {return JSON.parse(message)});
if (req.param('format') == 'json' || req.is('json')) {
res.send(messages);
} else {
res.render('wall', {messages: messages, title: streamName, globals: {stream: '/messages/' + streamName}});
}
});
});
}
exports.attach = attach;