forked from FGRibreau/filesync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (120 loc) · 3.65 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
'use strict';
var io = require('socket.io');
var express = require('express');
var path = require('path');
var app = express();
var _ = require('lodash');
var logger = require('winston');
var config = require('./config')(logger);
var passport = require('passport');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var currentUser;
require('./oauth/passport')(passport);
app.use(cookieParser());
app.use(session({ secret: 'someSecretKeyHere' }));
app.use(passport.initialize());
app.use(passport.session());
require('./oauth/route.js')(app, passport);
function isAuthenticated(req, res, next) {
if(req.hasOwnProperty('user')){
if (req.user.authenticated)
return next();
}
res.redirect('/login');
}
app.get('/', isAuthenticated, function(req, res) {
currentUser = req.user;
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static(path.resolve(__dirname, './public')));
var server = app.listen(config.server.port, function() {
logger.info('Server listening on %s', config.server.port);
});
var sio = io(server);
sio.set('authorization', function(handshakeData, accept) {
// @todo use something else than a private `query`
handshakeData.isAdmin = handshakeData._query.access_token === config.auth.token;
accept(null, true);
});
function Viewers(sio) {
var data = [];
function notifyChanges() {
sio.emit('viewers:updated', data);
}
return {
add: function add(nickname) {
var idx = data.map(function(e) { return e.id; }).indexOf(nickname.id);
var count = 1;
if (idx != -1) {
count++;
data[idx].connectionNumbers = count;
}
else{
data.push({id: nickname.id, name: nickname.name, email: nickname.email, authType: nickname.authType, connectionNumbers: count});
}
notifyChanges();
},
remove: function remove(nickname) {
var idx = data.map(function(e) { return e.id; }).indexOf(nickname);
if (idx > -1) {
if(data[idx].connectionNumbers == 1){
data.splice(idx, 1);
}
else {
data[idx].connectionNumbers--;
}
}
notifyChanges();
console.log('-->', data);
}
};
}
function Messages(sio) {
var data = [];
function notifyChanges() {
sio.emit('social:message', data);
}
return {
add: function add(message) {
data.push(message);
notifyChanges();
}
};
}
var viewers = Viewers(sio);
var messages = Messages(sio);
// @todo extract in its own
sio.on('connection', function(socket) {
// console.log('nouvelle connexion', socket.id);
socket.on('viewer:new', function(nickname) {
socket.nickname = currentUser.id;
viewers.add(currentUser);
console.log('new viewer with nickname %s', socket.nickname, viewers);
});
socket.on('message:send', function(message) {
console.log(message);
messages.add(message);
});
socket.on('disconnect', function() {
viewers.remove(socket.nickname);
console.log('viewer disconnected %s\nremaining:', socket.nickname, viewers);
});
socket.on('file:changed', function() {
if (!socket.conn.request.isAdmin) {
// if the user is not admin
// skip this
return socket.emit('error:auth', 'Unauthorized :)');
}
// forward the event to everyone
sio.emit.apply(sio, ['file:changed'].concat(_.toArray(arguments)));
});
socket.visibility = 'visible';
socket.on('user-visibility:changed', function(state) {
socket.visibility = state;
sio.emit('users:visibility-states', getVisibilityCounts());
});
});
function getVisibilityCounts() {
return _.chain(sio.sockets.sockets).values().countBy('visibility').value();
}