-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
50 lines (46 loc) · 1.51 KB
/
io.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
var session = require('client-sessions');
var cookieParser = require('cookie');
var mysession = require('./config');
var db = require('./models/index.js');
var io = null;
var this_socket = null;
module.exports = {
init: function(server) {
io = require('socket.io')(server);
io.on('connection', function(socket) {
this_socket = socket;
var cookie = socket.request.headers.cookie;
cookie = cookieParser.parse(cookie);
cookie = session.util.decode(mysession, cookie.session);
if (cookie) {
//join the socket rooms that this user is friends with
db.sequelize.query(
'SELECT ALL followed FROM "FollowerFollowed" WHERE ((FOLLOWER = :local_id OR FOLLOWED = :local_id) AND FRIENDS = true)', {
replacements: {
local_id: cookie.content.user.id,
},
type: db.sequelize.QueryTypes.SELECT
}
).then(function(friend_ids) {
//join your own socket
socket.join(cookie.content.user.id);
if (friend_ids.length > 0) { // user never longer than 1 beacuse of username uniqueness
for (var i = 0; i < friend_ids.length; i++) {
socket.join(friend_ids[i].followed);
}
}
});
} else {
//user does not exist
console.log('H E k, user does not exist');
res.status(500).send('user cookie could not be found');
}
});
},
instance: function() {
return io;
},
socket: function() {
return this_socket;
}
};