-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (112 loc) · 2.85 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
require('dotenv').load() // Load env variables
var express = require('express')
, cache = new (require("node-cache"))()
, path = require('path')
, port = process.env.PORT || 3000
, app = express()
, badger = require('./lib/badger')
, slack = require('./lib/slack')
, linkUtils = require('./lib/link-utils.js')
var ttls = {
users: 604800, // one week
presence: 900 // 15 minutes
}
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
function isEmpty(obj) {
return Object.keys(obj).length === 0
}
function fetchUsers(cb) {
var users = cache.get('users').users
if (users) {
cb(null, users)
} else {
slack.user.list(function(err, users) {
if (err) {
cb(err, null)
} else {
cache.set('users', users, ttls.users)
cb(null, users)
}
})
}
}
function fetchUser(username, cb) {
function findUser(name, list) {
return list.filter(function(u) { return u.name === name }).pop()
}
fetchUsers(function(err, users) {
if (err) {
cb(err, null)
} else {
cb(null, findUser(username, users))
}
})
}
function fetchPresence(id, cb) {
var key = id + ':presence'
, val = cache.get(key)[key]
if (val) {
cb(null, val)
} else {
slack.user.presence(id, function(err, presence) {
if (err) {
cb(err, null)
} else {
cache.set(key, presence, ttls.presence)
cb(null, presence)
}
})
}
}
function getUser(req, res, next) {
fetchUser(req.params.name, function(err, user) {
if (!err && !user) {
err = new Error('User "'+req.params.name+'" could not be found')
}
res.user = user
next(err)
})
}
function getPresence(req, res, next) {
fetchPresence(res.user.id, function(err, presence) {
res.user.presence = presence
next(err)
})
}
function drawUser(req, res, next) {
badger.draw(res.user, {width: 350, height: 60}, function(err, canvas) {
res.canvas = canvas
next(err)
})
}
app.get('/', function(req, res) {
var paulUser = {
name: 'paulsweeney',
presence: 'active',
profile: {
first_name: 'Paul',
last_name: 'Sweeney'
}
}
res.render('index', {
anchorExample: linkUtils.userAnchor({name: '<username>'}),
mdExample: linkUtils.userMarkdown({name: '<username>'}),
badge: linkUtils.userAnchor(paulUser)
})
})
app.get('/users/:name.json', getUser, getPresence, function(req, res) {
res.send({
anchorBadge: linkUtils.userAnchor(res.user),
markdownBadge: linkUtils.userMarkdown(res.user),
})
})
app.get('/users/:name.png', getUser, getPresence, drawUser, function(req, res) {
res.setHeader('content-type', 'image/png')
res.canvas.pngStream().pipe(res)
})
// Boot our server
app.listen(port, function() {
console.log('Listening for requests on port', port)
})