post counts + read status working on homepage category listing. fixed posts.get_cid_by_pid

v1.18.x
psychobunny 12 years ago
parent effb26c856
commit 34133eef8c

@ -12,7 +12,7 @@
<!-- BEGIN categories -->
<div class="span3">
<a href="category/{categories.slug}">
<h4>{categories.name} <span class="badge badge-important">3</span></h4>
<h4>{categories.name} <span class="badge {categories.badgeclass}">{categories.topic_count}</span></h4>
<!-- {categories.description} -->
<div class="category-icon {categories.blockclass}">
<i class="{categories.icon} icon-4x"></i>

@ -56,10 +56,10 @@ var RDB = require('./redis.js'),
// just a reminder to self that name + slugs are stored into topics data as well.
};
Categories.get = function(callback) {
Categories.get = function(callback, current_user) {
RDB.lrange('categories:cid', 0, -1, function(err, cids) {
RDB.handle(err);
Categories.get_category(cids, callback);
Categories.get_category(cids, callback, current_user);
});
}
@ -79,12 +79,29 @@ var RDB = require('./redis.js'),
});
}
Categories.get_category = function(cids, callback) {
Categories.hasReadCategories = function(cids, uid, callback) {
var batch = RDB.multi();
for (var i=0, ii=cids.length; i<ii; i++) {
batch.sismember('cid:' + cids[i] + ':read_by_uid', uid);
}
batch.exec(function(err, hasRead) {
callback(hasRead);
});
}
Categories.get_category = function(cids, callback, current_user) {
var name = [],
description = [],
icon = [],
blockclass = [],
slug = [];
slug = [],
topic_count = [],
has_read = {};
for (var i=0, ii=cids.length; i<ii; i++) {
name.push('cid:' + cids[i] + ':name');
@ -92,6 +109,7 @@ var RDB = require('./redis.js'),
icon.push('cid:' + cids[i] + ':icon');
blockclass.push('cid:' + cids[i] + ':blockclass');
slug.push('cid:' + cids[i] + ':slug');
topic_count.push('cid:' + cids[i] + ':topiccount');
}
if (cids.length > 0) {
@ -101,26 +119,39 @@ var RDB = require('./redis.js'),
.mget(icon)
.mget(blockclass)
.mget(slug)
.mget(topic_count)
.exec(function(err, replies) {
name = replies[0];
description = replies[1];
icon = replies[2];
blockclass = replies[3];
slug = replies[4];
topic_count = replies[5];
var categories = [];
for (var i=0, ii=cids.length; i<ii; i++) {
categories.push({
'name' : name[i],
'cid' : cids[i],
'slug' : slug[i],
'description' : description[i],
'blockclass' : blockclass[i],
'icon' : icon[i]
});
function generateCategories() {
var categories = [];
for (var i=0, ii=cids.length; i<ii; i++) {
categories.push({
'name' : name[i],
'cid' : cids[i],
'slug' : slug[i],
'description' : description[i],
'blockclass' : blockclass[i],
'icon' : icon[i],
'badgeclass' : (!topic_count[i] || (has_read[i] && current_user !=0)) ? '' : 'badge-important',
'topic_count' : topic_count[i] || 0
});
}
callback({'categories': categories});
}
callback({'categories': categories});
Categories.hasReadCategories(cids, current_user, function(read_data) {
has_read = read_data;
generateCategories();
});
});
} else callback({'categories' : []});
};

@ -196,7 +196,7 @@ marked.setOptions({
}
Posts.get_cid_by_pid = function(pid, callback) {
Posts.get_tid(pid, function(tid) {
Posts.get_tid_by_pid(pid, function(tid) {
if (tid) topics.get_cid_by_tid(tid, function(cid) {
if (cid) callback(cid);
else callback(false);
@ -220,6 +220,9 @@ marked.setOptions({
RDB.rpush('tid:' + tid + ':posts', pid);
RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post
Posts.get_cid_by_pid(pid, function(cid) {
RDB.del('cid:' + cid + ':read_by_uid');
});
// Re-add the poster, so he/she does not get an "unread" flag on this topic
topics.markAsRead(tid, uid);

@ -262,7 +262,11 @@ marked.setOptions({
}
Topics.markAsRead = function(tid, uid) {
// there is an issue with this fn. if you read a topic that is previously read you will mark the category as read anyways - there is no check
RDB.sadd('tid:' + tid + ':read_by_uid', uid);
Topics.get_cid_by_tid(tid, function(cid) {
RDB.sadd('cid:' + cid + ':read_by_uid', uid);
});
}
Topics.hasReadTopics = function(tids, uid, callback) {
@ -397,6 +401,9 @@ marked.setOptions({
timeout: 2000
});
// let everyone know that there is an unread topic in this category
RDB.del('cid:' + category_id + ':read_by_uid');
// in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.sadd('categories:' + category_id + ':tid', tid);
RDB.set('tid:' + tid + ':cid', category_id);
@ -405,6 +412,7 @@ marked.setOptions({
RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug);
});
RDB.incr('cid:' + category_id + ':topiccount');
});
};

@ -116,7 +116,7 @@ var express = require('express'),
case 'home' :
categories.get(function(data) {
res.send(JSON.stringify(data));
});
}, (req.user) ? req.user.uid : 0);
break;
case 'login' :
var data = {},

Loading…
Cancel
Save