mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2024-11-22 12:41:11 +00:00
Refactored user methods into a controller
This commit is contained in:
parent
9618fb80ca
commit
2087638588
4 changed files with 81 additions and 63 deletions
|
@ -5,42 +5,16 @@ var path = require("path");
|
|||
var enslClient = require(path.join(__dirname, "../lib/ensl/client"))();
|
||||
var chatController = require(path.join(__dirname, "../lib/chat/controller"));
|
||||
var gatherController = require(path.join(__dirname, "../lib/gather/controller"));
|
||||
var userController = require(path.join(__dirname, "../lib/user/controller"));
|
||||
var winston = require("winston");
|
||||
|
||||
var rootNamespace;
|
||||
|
||||
var userCache = {};
|
||||
var refreshUsers = function (socket) {
|
||||
var receiver = (socket !== undefined) ? socket : rootNamespace;
|
||||
|
||||
var newCache = {};
|
||||
rootNamespace.sockets.forEach(function (socket) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
receiver.emit('userCount', {
|
||||
count: users.length,
|
||||
users: users
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = function (io) {
|
||||
rootNamespace = io.of("/");
|
||||
|
||||
var id = 2131;
|
||||
|
||||
var rootNamespace = io.of('/')
|
||||
|
||||
// Authorisation
|
||||
rootNamespace.use(function (socket, next) {
|
||||
io.use(function (socket, next) {
|
||||
enslClient.getUserById({
|
||||
id: id
|
||||
}, function (error, response, body) {
|
||||
|
@ -53,36 +27,7 @@ module.exports = function (io) {
|
|||
});
|
||||
});
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
refreshUsers();
|
||||
|
||||
socket.on('refreshUsers', refreshUsers.bind(null, socket));
|
||||
|
||||
socket.on("authorize:id", function (data) {
|
||||
var id = parseInt(data.id, 10);
|
||||
if (isNaN(id)) return;
|
||||
enslClient.getUserById({
|
||||
id: id
|
||||
}, function (error, response, body) {
|
||||
if (error || response.statusCode !== 200) {
|
||||
winston.error("An error occurred in authorising id", id);
|
||||
winston.error(error);
|
||||
winston.error("ENSL API status:", response.statusCode);
|
||||
return;
|
||||
}
|
||||
socket._user = body;
|
||||
refreshUsers();
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('disconnect', function (socket) {
|
||||
refreshUsers();
|
||||
});
|
||||
});
|
||||
|
||||
// Activate chat controller on rootNamespace namespace
|
||||
userController(rootNamespace);
|
||||
chatController(rootNamespace);
|
||||
|
||||
// Activate gather controller on rootNamespace namespace
|
||||
gatherController(rootNamespace);
|
||||
};
|
||||
|
|
|
@ -18,7 +18,7 @@ var UserCounter = React.createClass({
|
|||
var UserLogin = React.createClass({
|
||||
authorizeId: function (id) {
|
||||
id = parseInt(id, 10);
|
||||
socket.emit("authorize:id", {
|
||||
socket.emit("users:authorize", {
|
||||
id: id
|
||||
});
|
||||
},
|
||||
|
|
73
lib/user/controller.js
Normal file
73
lib/user/controller.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
"use strict";
|
||||
|
||||
/*
|
||||
* User Controller
|
||||
*
|
||||
* Server API
|
||||
* message:new - New message needs to be displayed
|
||||
* message:refresh - Reload all messages
|
||||
*
|
||||
* Client API
|
||||
* message:new - New message has been created
|
||||
* message:refresh - Retrieve most recent messages
|
||||
*
|
||||
*/
|
||||
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var enslClient = require(path.join(__dirname, "../ensl/client"))();
|
||||
var winston = require("winston");
|
||||
var userCache = {};
|
||||
|
||||
module.exports = function (namespace) {
|
||||
var refreshUsers = function (socket) {
|
||||
var receiver = (socket !== undefined) ? socket : namespace;
|
||||
|
||||
var newCache = {};
|
||||
namespace.sockets.forEach(function (socket) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
receiver.emit('userCount', {
|
||||
count: users.length,
|
||||
users: users
|
||||
});
|
||||
};
|
||||
|
||||
namespace.on('connection', function (socket) {
|
||||
refreshUsers();
|
||||
|
||||
socket.on('refreshUsers', refreshUsers.bind(null, socket));
|
||||
|
||||
socket.on("users:authorize", function (data) {
|
||||
var id = parseInt(data.id, 10);
|
||||
if (isNaN(id)) return;
|
||||
enslClient.getUserById({
|
||||
id: id
|
||||
}, function (error, response, body) {
|
||||
if (error || response.statusCode !== 200) {
|
||||
winston.error("An error occurred in authorising id", id);
|
||||
winston.error(error);
|
||||
winston.error("ENSL API status:", response.statusCode);
|
||||
return;
|
||||
}
|
||||
socket._user = body;
|
||||
refreshUsers();
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('disconnect', function (socket) {
|
||||
refreshUsers();
|
||||
});
|
||||
});
|
||||
};
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue