redis updates for sets: added sadd, srem, and sismember; finished basic favouriting/up/down votes - still need to add socket support in future

v1.18.x
psychobunny 12 years ago
parent b5622690e0
commit 5e48b4cdc5

@ -11,17 +11,17 @@
<div class="span1 profile-image-block">
<!--<i class="icon-spinner icon-spin icon-2x pull-left"></i>-->
<img src="{posts.gravatar}" align="left" />
<i class="icon-star"></i>2432
<i class="icon-star"></i><span id="user_rep_{posts.uid}">{posts.user_rep}</span>
</div>
<div class="span11">
<div class="post-block">
<div id="content_{posts.pid}" class="post-content">{posts.content}</div>
<!--<p>Posted {posts.relativeTime} by user {posts.uid}.</p>-->
<div class="profile-block">
posted by <strong>{posts.userName}</strong> {posts.relativeTime}
posted by <strong>{posts.username}</strong> {posts.relativeTime}
<span class="post-buttons">
<div id="quote_{posts.pid}" class="quote"><i class="icon-quote-left"></i></div>
<div id="favs_{posts.pid}" class="favourite"><span id="favs_counter_{posts.pid}">0</span><i class="icon-star-empty"></i></div>
<div id="favs_{posts.pid}_{posts.uid}" class="favourite"><span id="post_rep_{posts.pid}">{posts.post_rep}</span><i class="{posts.fav_star_class}"></i></div>
<div class="post_reply">Reply <i class="icon-reply"></i></div>
</span>
</div>
@ -47,20 +47,33 @@ jQuery('.quote').click(function() {
});
jQuery('.favourite').click(function() {
var pid = this.id.replace('favs_', ''),
favs_counter = document.getElementById('favs_counter_' + pid);
var ids = this.id.replace('favs_', '').split('_'),
pid = ids[0],
uid = ids[1],
post_rep = document.getElementById('post_rep_' + pid),
user_rep = document.getElementById('user_rep_' + uid);
var total = parseInt(favs_counter.innerHTML, 10);
var ptotal = parseInt(post_rep.innerHTML, 10),
utotal = parseInt(user_rep.innerHTML, 10);
if (this.children[1].className == 'icon-star-empty') {
this.children[1].className = 'icon-star';
total++;
favs_counter.innerHTML = total;
ptotal++;
utotal++;
post_rep.innerHTML = ptotal;
user_rep.innerHTML = utotal;
socket.emit('api:posts.favourite', {pid: pid});
}
else {
this.children[1].className = 'icon-star-empty';
total--;
favs_counter.innerHTML = total;
ptotal--;
utotal--;
post_rep.innerHTML = ptotal;
user_rep.innerHTML = utotal;
socket.emit('api:posts.unfavourite', {pid: pid});
}
})
</script>

@ -23,12 +23,14 @@ var RDB = require('./redis.js'),
var content = [],
uid = [],
timestamp = [],
pid = [];
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]);
}
@ -37,27 +39,41 @@ var RDB = require('./redis.js'),
.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_usernames_by_uids(uid, function(userNames) {
user.get_user_postdetails(uid, function(user_details) {
user.get_gravatars_by_uids(uid, 80, function(gravatars) {
var posts = [];
var callbacks = content.length;
for (var i=0, ii=content.length; i<ii; i++) {
posts.push({
'pid' : pid[i],
'content' : marked(content[i] || ''),
'uid' : uid[i],
'userName' : userNames[i] || 'anonymous',
'gravatar' : gravatars[i],
'timestamp' : timestamp[i],
'relativeTime': utils.relativeTime(timestamp[i])
});
(function(i) {
Posts.hasFavourited(pid[i], uid[i], 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'
});
callbacks--;
if (callbacks == 0) {
callback({'topic_name':topic_name, 'topic_id': tid, 'posts': posts});
}
});
}(i));
}
callback({'topic_name':topic_name, 'topic_id': tid, 'posts': posts});
});
});
});
@ -103,4 +119,36 @@ var RDB = require('./redis.js'),
}
Posts.favourite = function(socket, pid) {
RDB.get('pid:' + pid + ':uid', function(uid) {
Posts.hasFavourited(pid, uid, function(hasFavourited) {
if (hasFavourited == false) {
RDB.sadd('pid:' + pid + ':users_favourited', uid);
RDB.incr('uid:' + uid + ':rep');
RDB.incr('pid:' + pid + ':rep');
}
});
});
}
Posts.unfavourite = function(socket, pid) {
RDB.get('pid:' + pid + ':uid', function(uid) {
Posts.hasFavourited(pid, uid, function(hasFavourited) {
if (hasFavourited == true) {
RDB.srem('pid:' + pid + ':users_favourited', uid);
RDB.decr('uid:' + uid + ':rep');
RDB.decr('pid:' + pid + ':rep');
}
});
});
}
Posts.hasFavourited = function(pid, uid, callback) {
RDB.sismember('pid:' + pid + ':users_favourited', uid, function(hasFavourited) {
callback(hasFavourited);
});
}
}(exports));

@ -86,4 +86,19 @@
});
}
// Sets
RedisDB.sadd = function(key, item) {
db.sadd(key, item);
};
RedisDB.srem = function(key, item) {
db.srem(key, item);
};
RedisDB.sismember = function(key, item, callback, error_handler) {
db.sismember(key, item, function(error, data) {
return_handler(error, data, callback, error_handler);
});
};
}(exports));

@ -302,6 +302,26 @@ var config = require('../config.js'),
});
};
User.get_user_postdetails = function(uids, callback) {
var username = [],
rep = [];
for(var i=0, ii=uids.length; i<ii; i++) {
username.push('uid:' + uids[i] + ':username');
rep.push('uid:' + uids[i] + ':rep');
}
RDB.multi()
.mget(username)
.mget(rep)
.exec(function(err, replies) {
callback({
'username': replies[0],
'rep' : replies[1]
});
});
}
User.get_uid_by_email = function(email, callback) {
RDB.get('email:' + email, callback)
};

@ -126,7 +126,11 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
socket.on('api:posts.favourite', function(data) {
modules.posts.favourite(socket, data.pid);
})
});
socket.on('api:posts.unfavourite', function(data) {
modules.posts.unfavourite(socket, data.pid);
});
socket.on('api:user.active.get_record', function() {
modules.user.active.get_record(socket);

Loading…
Cancel
Save