fixed conflicts

v1.18.x
psychobunny 12 years ago
commit 303452ef8c

@ -22,7 +22,7 @@ global.modules = modules;
config['ROOT_DIRECTORY'] = __dirname;
modules.templates.init();
modules.webserver.init();
// modules.webserver.init();
modules.websockets.init();

@ -15,7 +15,8 @@
"connect": "2.7.6",
"emailjs": "0.3.4",
"cookie": "0.0.6",
"connect-redis": "1.4.5"
"connect-redis": "1.4.5",
"path": "0.4.9"
},
"devDependencies": {},
"optionalDependencies": {},

@ -25,13 +25,11 @@
ajaxify.register_events(['user.login']);
socket.on('user.login', function(data) {
console.log(data);
if (data.status === 0) {
jQuery('#error').show(50);
jQuery('#error p').html(data.message);
} else {
jQuery('#error').hide(50);
ajaxify.go('/');
document.location.href = '/';
}
});
}());

@ -95,8 +95,8 @@ var RDB = require('./redis.js'),
}
Topics.post = function(title, content, category) {
if (global.uid === null) {
Topics.post = function(uid, title, content, category) {
if (uid === 0) {
global.socket.emit('event:alert', {
title: 'Thank you for posting',
message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.',
@ -111,8 +111,8 @@ var RDB = require('./redis.js'),
RDB.incr('global:next_topic_id', function(tid) {
// Global Topics
if (global.uid == null) global.uid = 0;
if (global.uid !== null) {
if (uid == null) uid = 0;
if (uid !== null) {
RDB.lpush('topics:tid', tid);
} else {
// need to add some unique key sent by client so we can update this with the real uid later
@ -129,7 +129,7 @@ var RDB = require('./redis.js'),
// Topic Info
RDB.set('tid:' + tid + ':title', title);
RDB.set('tid:' + tid + ':uid', global.uid);
RDB.set('tid:' + tid + ':uid', uid);
RDB.set('tid:' + tid + ':slug', slug);
RDB.set('tid:' + tid + ':timestamp', new Date().getTime());
RDB.incr('tid:' + tid + ':postcount');
@ -143,7 +143,7 @@ var RDB = require('./redis.js'),
// User Details - move this out later
RDB.lpush('uid:' + global.uid + ':topics', tid);
RDB.lpush('uid:' + uid + ':topics', tid);
global.socket.emit('event:alert', {

@ -33,25 +33,17 @@ var config = require('../config.js'),
global.uid = uid;
global.socket.emit('event:alert', {
title: 'Welcome ' + user.username,
message: 'You have successfully logged in.',
type: 'notify',
timeout: 2000
});
return global.socket.emit('user.login', {'status': 1, 'message': 'Logged in!'});
}
});
});
};
User.logout = function(callback) {
RDB.get('uid:' + global.uid + ':session', function(sessionID) {
if (sessionID) {
User.logout = function(sessionID, callback) {
User.get_uid_by_session(sessionID, function(uid) {
if (uid) {
RDB.del('sess:' + sessionID + ':uid');
RDB.del('uid:' + global.uid + ':session');
global.uid = null;
RDB.del('uid:' + uid + ':session');
callback(true);
} else callback(false);
});
@ -236,32 +228,37 @@ var config = require('../config.js'),
},
keys = [];
for(var a in active) {
keys.push('sess:' + active[a].split(':')[1] + ':uid');
}
if (active.length > 0) {
for(var a in active) {
keys.push('sess:' + active[a].split(':')[1] + ':uid');
}
RDB.mget(keys, function(uids) {
for(var u in uids) {
if (uids[u] !== null) {
if (returnObj.uids.indexOf(uids[u]) === -1) {
returnObj.users++;
returnObj.uids.push(uids[u]);
RDB.mget(keys, function(uids) {
for(var u in uids) {
if (uids[u] !== null) {
if (returnObj.uids.indexOf(uids[u]) === -1) {
returnObj.users++;
returnObj.uids.push(uids[u]);
}
} else {
returnObj.anon++;
}
} else {
returnObj.anon++;
}
}
if (callback === undefined) {
global.socket.emit('api:user.active.get', returnObj)
} else {
callback(returnObj);
}
});
if (callback === undefined) {
io.sockets.emit('api:user.active.get', returnObj)
} else {
callback(returnObj);
}
});
} else {
io.sockets.emit('api:user.active.get', returnObj)
}
});
},
register: function(sessionID) {
RDB.set('active:' + sessionID, 60*10); // Active state persists for 10 minutes
RDB.set('active:' + sessionID, '', 60*10); // Active state persists for 10 minutes
this.get();
}
}
}(exports));

@ -1,7 +1,8 @@
var express = require('express'),
WebServer = express(),
server = require('http').createServer(WebServer),
RedisStore = require('connect-redis')(express);
RedisStore = require('connect-redis')(express),
path = require('path'),
config = require('../config.js');
(function(app) {
@ -26,8 +27,10 @@ var express = require('express'),
// Middlewares
app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc)
app.use(express.static(path.join(__dirname, '../', 'public')));
app.use(express.bodyParser()); // Puts POST vars in request.body
app.use(express.cookieParser()); // If you want to parse cookies (res.cookies)
app.use(express.compress());
app.use(express.session({
store: new RedisStore({
ttl: 60*60*24*14
@ -36,20 +39,34 @@ var express = require('express'),
key: 'express.sid'
}));
app.use(function(req, res, next) {
if (global.uid === undefined) {
console.log('info: [Auth] First load, retrieving uid...');
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
global.uid = uid;
if (global.uid !== null) console.log('info: [Auth] uid ' + global.uid + ' found. Welcome back.');
else console.log('info: [Auth] No login session found.');
});
// (Re-)register the session as active
global.modules.user.active.register(req.sessionID);
} else {
console.log('info: [Auth] Ping from uid ' + global.uid);
// Don't bother with session handling for API requests
if (!/^\/api\//.test(req.url)) {
if (req.session.uid === undefined) {
console.log('info: [Auth] First load, retrieving uid...');
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
if (uid !== null) {
req.session.uid = uid;
global.socket.emit('event:alert', {
title: 'Welcome ' + user.username,
message: 'You have successfully logged in.',
type: 'notify',
timeout: 2000
});
} else req.session.uid = 0;
if (req.session.uid) console.log('info: [Auth] uid ' + req.session.uid + ' found. Welcome back.');
else console.log('info: [Auth] No login session found.');
});
} else {
// console.log('SESSION: ' + req.sessionID);
// console.log('info: [Auth] Ping from uid ' + req.session.uid);
}
}
// (Re-)register the session as active
global.modules.user.active.register(req.sessionID);
next();
});
// Dunno wtf this does
@ -64,7 +81,6 @@ var express = require('express'),
});
// need a proper way to combine these two routes together
app.get('/topics/:topic_id', function(req, res) {
global.modules.topics.generate_topic_body(function(topic_body) {
@ -88,6 +104,7 @@ var express = require('express'),
break;
default :
res.send('{}');
break;
}
});
@ -97,8 +114,11 @@ var express = require('express'),
app.get('/logout', function(req, res) {
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
global.modules.user.logout(function(logout) {
if (logout === true) req.session.destroy();
global.modules.user.logout(req.sessionID, function(logout) {
if (logout === true) {
delete(req.session.uid);
req.session.destroy();
}
});
res.send(templates['header'] + templates['logout'] + templates['footer']);
@ -124,13 +144,6 @@ var express = require('express'),
app.get('/403', function(req, res) {
res.send(templates['header'] + templates['403'] + templates['footer']);
});
module.exports.init = function() {
// todo move some of this stuff into config.json
app.configure(function() {
app.use(express.static(global.configuration.ROOT_DIRECTORY + '/public'));
});
}
}(WebServer));
server.listen(config.port);

@ -4,7 +4,8 @@ var SocketIO = require('socket.io').listen(global.server),
(function(io) {
var modules = null,
sessionID;
sessionID,
uid;
global.io = io;
module.exports.init = function() {
@ -27,7 +28,12 @@ var SocketIO = require('socket.io').listen(global.server),
// Otherwise, continue unimpeded.
sessionID = handshakeData.sessionID;
accept(null, true);
global.modules.user.get_uid_by_session(sessionID, function(session_uid) {
if (session_uid) uid = session_uid;
else uid = 0;
accept(null, true);
});
});
io.sockets.on('connection', function(socket) {
@ -80,7 +86,7 @@ var SocketIO = require('socket.io').listen(global.server),
});
socket.on('api:topics.post', function(data) {
modules.topics.post(data.title, data.content);
modules.topics.post(uid, data.title, data.content);
});
socket.on('api:user.active.get', function() {

Loading…
Cancel
Save