-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (153 loc) · 4.49 KB
/
server.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
/* eslint no-console: 0 */
const path = require('path');
const express = require('express');
const Redis = require('ioredis');
const Twit = require('twit');
const config = require('./twitter.json');
const app = express();
const redis = new Redis();
const sub = new Redis();
const twitter = new Twit(config);
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const port = 1337;
server.listen(port);
console.log(`server started on port ${port}`);
redis.on('error', error => console.error('Redis error: ', error.toString()));
// Serve static files
app.use(express.static(path.join(__dirname, 'static/')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/views/index.html'));
});
function filterUserData(user) {
return {
id: user.id_str,
name: user.screen_name,
};
}
app.get('/api/users/search', (req, res) => {
twitter.get('users/search', { q: req.query.q, count: 10 })
.then(response => response.data)
.then(data => data.map(filterUserData))
.then(data => res.json(data))
.catch(error => res.status(500).json(error));
});
app.get('/api/users/show', (req, res) => {
console.log('user/show');
twitter.get('users/show', { screen_name: req.query.screen_name })
.then(response => response.data)
.then(data => filterUserData(data))
.then(data => res.json(data))
.catch(error => res.status(500).json(error));
});
app.get('/api/users/lookup', (req, res) => {
twitter.get('users/lookup', { user_id: req.query.user_id })
.then(response => response.data)
.then(data => data.map(filterUserData))
.then(data => res.json(data))
.catch(error => res.status(500).json(error));
});
function getSettings() {
return redis.pipeline()
.smembers('settings:blacklist:words')
.smembers('settings:blacklist:users')
.smembers('settings:filter:track')
.smembers('settings:filter:follow')
.smembers('settings:status:languages')
.getbit('settings:status:retweets', 0)
.getbit('settings:status:sensitive', 0)
.exec()
.then(result => ({
blacklist: {
words: result[0][1],
users: result[1][1],
},
filter: {
track: result[2][1],
follow: result[3][1],
},
status: {
languages: result[4][1],
retweets: !!result[5][1],
sensitive: !!result[6][1],
},
}));
}
function getData() {
return redis.pipeline()
.lrange('data:tweets:removed', 0, -1)
.smembers('data:analysis:keywords')
.exec()
.then(result => ({
removed: result[0][1].map(JSON.parse),
keywords: result[1][1],
}));
}
function updateSetting(key, action, value) {
switch (key) {
case 'settings:blacklist:words':
case 'settings:blacklist:users':
case 'settings:filter:track':
case 'settings:filter:follow':
case 'settings:status:languages':
if (action === 'add') {
redis.sadd(key, value);
} else if (action === 'remove') {
redis.srem(key, value);
} else {
console.error('Unknown action.', 'key', key, 'action', action, 'value', value);
}
break;
case 'settings:status:retweets':
case 'settings:status:sensitive':
if (action === 'set') {
redis.setbit(key, 0, value ? 1 : 0);
} else {
console.error('Unknown action.', 'key', key, 'action', action, 'value', value);
}
break;
default:
console.error('Unknown key.', 'key', key, 'action', action, 'value', value);
return;
}
redis.publish('update-cache', 'all');
}
function updateData(key, action, value) {
switch (key) {
case 'data:analysis:keywords':
if (action === 'remove') {
redis.srem(key, value);
} else {
console.error('Unknown action.', 'key', key, 'action', action, 'value', value);
}
break;
default:
console.error('Unknown key.', 'key', key, 'action', action, 'value', value);
return;
}
getData().then(data => io.sockets.emit('update-data', data));
}
// Communicate with clients
io.on('connect', (socket) => {
getSettings().then(settings => socket.emit('update-settings', settings));
socket.on('alter-setting', data => updateSetting(data.key, data.action, data.value));
socket.on('alter-data', data => updateData(data.key, data.action, data.value));
socket.on('update-filter', () => redis.publish('update-filter', 'all'));
getData().then(data => socket.emit('update-data', data));
setInterval(() => {
getData().then(data => socket.emit('update-data', data));
}, 5000);
});
sub.subscribe('update-cache');
sub.on('message', (channel, message) => {
console.log(channel, message);
switch (channel) {
case 'update-cache':
getSettings().then((settings) => {
io.sockets.emit('update-settings', settings);
});
break;
default:
return;
}
});