2015-07-22 16:38:58 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User Controller
|
|
|
|
*
|
|
|
|
* Server API
|
2015-07-29 10:46:30 +00:00
|
|
|
* users:update - Update list of users
|
2015-07-22 16:38:58 +00:00
|
|
|
*
|
|
|
|
* Client API
|
2015-07-29 10:46:30 +00:00
|
|
|
* users:online - User is on the page
|
|
|
|
* users:away - User is away from page
|
2015-08-18 09:56:35 +00:00
|
|
|
* users:authorize - Sign on with arbitary user ID (to be enabled for admin only)
|
2015-07-29 10:46:30 +00:00
|
|
|
* users:refresh - Request new user list
|
2015-07-22 16:38:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var userCache = {};
|
2015-07-22 23:30:14 +00:00
|
|
|
var User = require("./user");
|
|
|
|
var winston = require("winston");
|
2015-08-18 09:56:35 +00:00
|
|
|
var mongoose = require("mongoose");
|
|
|
|
var Session = mongoose.model("Session");
|
2015-07-22 23:30:14 +00:00
|
|
|
var enslClient = require("../ensl/client")();
|
2015-07-29 13:50:39 +00:00
|
|
|
var _ = require("lodash");
|
2015-07-22 16:38:58 +00:00
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
module.exports = namespace => {
|
|
|
|
var refreshUsers = socket => {
|
2015-07-27 11:55:36 +00:00
|
|
|
var receivers = (socket !== undefined) ? [socket] : namespace.sockets;
|
2015-07-22 16:38:58 +00:00
|
|
|
|
|
|
|
var newCache = {};
|
2015-08-18 09:56:35 +00:00
|
|
|
namespace.sockets.forEach(socket => {
|
2015-07-22 16:38:58 +00:00
|
|
|
var user = socket._user;
|
|
|
|
newCache[user.id] = user;
|
|
|
|
});
|
|
|
|
userCache = newCache;
|
|
|
|
|
|
|
|
var users = [];
|
|
|
|
|
|
|
|
for (var id in userCache) {
|
|
|
|
if (userCache.hasOwnProperty(id)) {
|
|
|
|
users.push(userCache[id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
receivers.forEach(socket => {
|
2015-07-27 11:55:36 +00:00
|
|
|
socket.emit('users:update', {
|
|
|
|
count: users.length,
|
|
|
|
users: users,
|
|
|
|
currentUser: socket._user
|
|
|
|
});
|
|
|
|
});
|
2015-07-22 16:38:58 +00:00
|
|
|
};
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
namespace.on('connection', socket => {
|
2015-07-22 16:38:58 +00:00
|
|
|
refreshUsers();
|
|
|
|
|
2015-07-27 11:55:36 +00:00
|
|
|
socket.on('users:refresh', refreshUsers.bind(null, socket));
|
2015-07-22 16:38:58 +00:00
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
socket.on('users:online', () => {
|
2015-07-29 10:46:30 +00:00
|
|
|
socket._user.online = true;
|
|
|
|
});
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
socket.on('users:away', () => {
|
2015-07-29 10:46:30 +00:00
|
|
|
socket._user.online = false;
|
|
|
|
});
|
|
|
|
|
2015-08-28 20:28:50 +00:00
|
|
|
socket.on('users:update:profile', data => {
|
|
|
|
if (socket._user.id !== data.id) return;
|
|
|
|
socket._user.updateProfile(data.profile, function (error) {
|
|
|
|
if (error) {
|
|
|
|
winston.error(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
refreshUsers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
socket.on("users:authorize", data => {
|
|
|
|
if (!socket._user.admin) return;
|
2015-07-22 16:38:58 +00:00
|
|
|
var id = parseInt(data.id, 10);
|
|
|
|
if (isNaN(id)) return;
|
2015-09-14 17:28:22 +00:00
|
|
|
User.find(id, (error, user) => {
|
|
|
|
if (error) {
|
2015-07-22 16:38:58 +00:00
|
|
|
winston.error(error);
|
2015-09-14 17:28:22 +00:00
|
|
|
return next(new Error("Authentication failed"));
|
2015-07-22 16:38:58 +00:00
|
|
|
}
|
2015-09-14 17:28:22 +00:00
|
|
|
socket._user = user;
|
|
|
|
winston.info("Logged in:", user.username, user.id);
|
2015-07-22 16:38:58 +00:00
|
|
|
refreshUsers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
socket.on('disconnect', socket => { refreshUsers(); });
|
2015-07-22 16:38:58 +00:00
|
|
|
});
|
|
|
|
};
|