Baris Soner Usakli 12 years ago
commit 47782df9cb

@ -26,7 +26,8 @@
"less-middleware": "0.1.11",
"marked": "0.2.8",
"bcrypt": "0.7.5",
"node-gyp": "0.9.5"
"node-gyp": "0.9.5",
"async": "0.2.8"
},
"devDependencies": {},
"optionalDependencies": {},

@ -260,3 +260,16 @@ footer.footer {
}
}
}
#thread_active_users {
float: right;
color: rgb(153,153,153);
}
#thread_active_users strong {
color: rgb(100,100,100);
font-weight: 600;
cursor: pointer;
}

@ -2,9 +2,8 @@
<ul class="breadcrumb">
<li><a href="/">Home</a> <span class="divider">/</span></li>
<li class="active">{topic_name}</li>
<div id="thread_active_users"></div>
</ul>
<div id="thread_active_users"></div><br />
</div>
<ul id="post-container" class="post-container container">
@ -48,7 +47,24 @@ jQuery('document').ready(function() {
ajaxify.register_events(['event:rep_up', 'event:rep_down', 'event:new_post', 'api:get_users_in_room']);
socket.on('api:get_users_in_room', function(users) {
document.getElementById('thread_active_users').innerHTML = (users.uids.join(', ')) + ' are browsing this thread';
var anonymous = users.anonymous,
usernames = users.usernames,
usercount = usernames.length;
for (var i = 0, ii=usercount; i<ii; i++) {
usernames[i] = '<strong>' + usernames[i] + '</strong>';
}
// headexplosion.gif for fun, to see if I could do this in one line of code. feel free to refactor haha
var active =
((usercount === 1) ? usernames[0] : '')
+ ((usercount === 2 && anonymous === 0) ? usernames[0] + ' and ' + usernames[1] : '')
+ ((usercount > 2 && anonymous === 0) ? usernames.join(', ').replace(/,([^,]*)$/, ", and$1") : '')
+ (usercount > 1 && anonymous > 0 ? usernames.join(', ') : '')
+ ((anonymous > 0) ? (usercount > 0 ? ' and ': '') + anonymous + ' guest' + (anonymous.length > 1 ? 's are': ' is') : '')
+ (anonymous === 0 ? (usercount > 1 ? ' are' : ' is') : '') + ' browsing this thread';
document.getElementById('thread_active_users').innerHTML = active;
});
socket.on('event:rep_up', function(data) {

@ -1,7 +1,8 @@
var RDB = require('./redis.js'),
utils = require('./utils.js'),
marked = require('marked'),
user = require('./user.js');
user = require('./user.js'),
async = require('async');
(function(Posts) {
//data structure
@ -18,73 +19,134 @@ var RDB = require('./redis.js'),
if (start == null) start = 0;
if (end == null) end = start + 10;
RDB.get('tid:' + tid + ':title', function(topic_name) { //do these asynch later
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
var content = [],
uid = [],
timestamp = [],
pid = [],
post_rep = [];
for (var i=0, ii=pids.length; i<ii; i++) {
content.push('pid:' + pids[i] + ':content');
uid.push('pid:' + pids[i] + ':uid');
timestamp.push('pid:' + pids[i] + ':timestamp');
post_rep.push('pid:' + pids[i] + ':rep');
pid.push(pids[i]);
}
async.parallel({
details: function(callback) {
RDB.get('tid:' + tid + ':title', function(topic_name) {
callback(null, {
'topic_name': topic_name
});
});
},
posts: function(callback) {
var participant_uids = [],
post_calls = [];
async.waterfall([
function(next) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
var content = [],
uids = [],
participants = [],
timestamp = [],
pid = [],
post_rep = [];
for (var i=0, ii=pids.length; i<ii; i++) {
content.push('pid:' + pids[i] + ':content');
uids.push('pid:' + pids[i] + ':uid');
timestamp.push('pid:' + pids[i] + ':timestamp');
post_rep.push('pid:' + pids[i] + ':rep');
pid.push(pids[i]);
}
if (pids.length > 0) {
RDB.multi()
.mget(content)
.mget(uid)
.mget(timestamp)
.mget(post_rep)
.exec(function(err, replies) {
content = replies[0];
uid = replies[1];
timestamp = replies[2];
post_rep = replies[3];
user.get_user_postdetails(uid, function(user_details) {
user.get_gravatars_by_uids(uid, '', function(gravatars) {
var posts = [];
var callbacks = content.length;
for (var i=0, ii=content.length; i<ii; i++) {
(function(i) {
Posts.hasFavourited(pid[i], current_user, function(hasFavourited) {
posts.push({
'pid' : pid[i],
'content' : marked(content[i] || ''),
'uid' : uid[i],
'username' : user_details.username[i] || 'anonymous',
'user_rep' : user_details.rep[i] || 0,
'post_rep' : post_rep[i] || 0,
'gravatar' : gravatars[i],
'timestamp' : timestamp[i],
'relativeTime': utils.relativeTime(timestamp[i]),
'fav_star_class' : hasFavourited ? 'icon-star' : 'icon-star-empty',
'display_moderator_tools' : uid[i] === current_user ? 'show' : 'hide'
});
callbacks--;
if (callbacks == 0) {
callback({'topic_name':topic_name, 'topic_id': tid, 'posts': posts});
}
});
}(i));
if (pids.length > 0) {
RDB.multi()
.mget(content)
.mget(uids)
.mget(timestamp)
.mget(post_rep)
.exec(function(err, replies) {
// Populate uids array
for(var x=0,numReplies=replies[1].length;x<numReplies;x++) {
var uid = parseInt(replies[1][x]);
if (participants.indexOf(uid) === -1) participants.push(uid);
}
// Construct return object
next(null, {
replies: replies,
pids: pids,
participants: participants
});
}
);
}
});
}, function(returnObj, next) {
// Get user details
var details = {},
calls = [];
for(var x=0,numParticipants=returnObj.participants.length;x<numParticipants;x++) {
(function(uid) {
calls.push(function(next) {
// Get individual participant's details
user.getUserData(uid, function(userData) {
next(null, userData);
})
});
});
})(returnObj.participants[x]);
}
async.parallel(calls, function(err, results) {
for(var x=0,numResults=results.length;x<numResults;x++) {
details[returnObj.participants[x]] = results[x];
}
returnObj.participants = details;
next(null, returnObj);
});
} else {
callback({});
}
}, function(returnObj, next) {
// Favourited?
var calls = [],
numPosts = returnObj.pids.length;
for(var x=0;x<numPosts;x++) {
(function(pid) {
calls.push(function(callback) {
Posts.hasFavourited(pid, current_user, function(hasFavourited) {
callback(null, hasFavourited);
});
});
})(returnObj.pids[x]);
}
async.parallel(calls, function(err, results) {
returnObj.favourites = results;
next(null, returnObj);
});
}
], function(err, returnObj) {
callback(null, returnObj);
});
}
}, function(err, results) {
// Construct posts array
var posts = [],
participant_details = results.posts.participants;
for(var x=0,numPosts=results.posts.pids.length;x<numPosts;x++) {
var uid = results.posts.replies[1][x],
participant = participant_details[uid];
posts.push({
pid: results.posts.pids[x],
content: marked(results.posts.replies[0][x] || ''),
uid: uid,
timestamp: results.posts.replies[2][x],
relativeTime: utils.relativeTime(results.posts.replies[2][x]),
post_rep: results.posts.replies[3][x],
display_moderator_tools: results.posts.replies[1][x] === current_user ? 'show' : 'hide',
username: participant.username,
gravatar: participant.picture,
user_rep: participant.reputation,
fav_star_class: results.posts.favourites[x] ? 'icon-star' : 'icon-star-empty',
});
}
// Construct return object
callback({
'topic_name': results.details.topic_name,
'topic_id': tid,
posts: posts,
uids: results.posts.participants
});
});
}

@ -59,7 +59,6 @@ var config = require('../config.js'),
for(var i=0, ii=uids.length; i<ii; ++i) {
User.getUserField(uids[i], 'picture', function(picture) {
console.log(picture);
gravatars.push(picture);
if(gravatars.length >= uids.length)
callback(gravatars);
@ -239,7 +238,7 @@ var config = require('../config.js'),
User.createGravatarURLFromEmail = function(email) {
if(email) {
var md5sum = crypto.createHash('md5');
md5sum.update(email.toLowerCase());
md5sum.update((email || '').toLowerCase());
var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex');
return gravatarURL;
}

@ -337,6 +337,12 @@ passport.deserializeUser(function(uid, done) {
res.send('User profile for uid: ' + uid);
});
});
app.get('/test', function(req, res) {
global.modules.posts.get(function(data) {
res.send('<pre>' + JSON.stringify(data) + '</pre>');
}, 1, 1);
});
}(WebServer));
server.listen(config.port);

@ -6,7 +6,10 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
(function(io) {
var modules = null,
users = {},
rooms = {};
rooms = {
'users' : {},
'anonymous' : {}
};
global.io = io;
module.exports.init = function() {
@ -70,25 +73,39 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
socket.on('event:enter_room', function(data) {
socket.on('event:enter_room', function(data) {
if (data.leave !== null) socket.leave (data.leave);
socket.join(data.enter);
rooms[data.enter] = rooms[data.enter] || {};
rooms.users[data.enter] = rooms.users[data.enter] || {};
if (uid) {
rooms[data.enter][uid] = true;
if (rooms[data.leave]) {
delete rooms[data.leave][uid];
rooms.users[data.enter][uid] = true;
if (rooms.users[data.leave]) {
delete rooms.users[data.leave][uid];
}
} else {
rooms[data.enter].anonymous = rooms[data.enter].anonymous ? rooms[data.enter].anonymous + 1 : 1;
rooms[data.leave].anonymous = rooms[data.leave].anonymous ? rooms[data.enter].anonymous - 1 : 0;
rooms.anonymous[data.enter] = (rooms.anonymous[data.enter] || 0) + 1;
rooms.anonymous[data.leave] = rooms.anonymous[data.leave] || 0;
}
socket.emit('api:get_users_in_room', {
uids: Object.keys(rooms[data.enter] || {}),
anonymous: rooms[data.enter] ? rooms[data.enter].anonymous : 0
var uids = Object.keys(rooms.users[data.enter] || {});
if (uids.length == 0) {
socket.emit('api:get_users_in_room', {
usernames: [],
uids: [],
anonymous: rooms.anonymous[data.enter] || 0
});
}
modules.user.get_usernames_by_uids(uids, function(usernames) {
socket.emit('api:get_users_in_room', {
usernames: usernames,
uids: uids,
anonymous: rooms.anonymous[data.enter] || 0
});
});
});
// BEGIN: API calls (todo: organize)

Loading…
Cancel
Save