Skip to content

Node.js based forum software built for the modern web

user has read feature - turns post count badge red if there is a new post and grey if the user has seen this post already

psychobunnyf218e0f3a72f
diff --git a/public/templates/category.tpl b/public/templates/category.tpl
index b0bf52d5a3..4bda45c74f 100644
--- a/public/templates/category.tpl
+++ b/public/templates/category.tpl
@@ -21,9 +21,9 @@
<img src="http://www.gravatar.com/avatar/91050ce0072697b53380c6a03a1bc12a?s=60" class="img-polaroid" />
</div>
<div>
- <h3><span class="badge badge-important">3</span> {topics.title} <small>24<i class="icon-star"></i></small></h3>
+ <h3><span class="badge {topics.badgeclass}">{topics.post_count}</span> {topics.title} <small>24<i class="icon-star"></i></small></h3>
<p> Posted {topics.relativeTime} ago by
- <span class="username">{topics.username}</span>. {topics.post_count} posts.</p>
+ <span class="username">{topics.username}</span>.</p>
</div>
</div>
</div>
diff --git a/src/posts.js b/src/posts.js
index 0c92061829..c85da614e6 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -2,6 +2,7 @@ var RDB = require('./redis.js'),
utils = require('./utils.js'),
marked = require('marked'),
user = require('./user.js'),
+ topics = require('./topics.js'),
config = require('../config.js');
(function(Posts) {
@@ -12,6 +13,7 @@ var RDB = require('./redis.js'),
var post_data, user_data, thread_data, vote_data, viewer_data;
+ topics.markAsRead(tid, current_user);
//compile thread after all data is asynchronously called
function generateThread() {
@@ -123,6 +125,8 @@ var RDB = require('./redis.js'),
if (pid > 0) {
RDB.rpush('tid:' + tid + ':posts', pid);
+ RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post
+
socket.emit('event:alert', {
title: 'Reply Successful',
message: 'You have successfully replied. Click here to view your reply.',
diff --git a/src/topics.js b/src/topics.js
index 89e6c9ab26..54911f672d 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -12,8 +12,7 @@ var RDB = require('./redis.js'),
}
-
- Topics.get = function(callback, category_id, start, end) {
+ Topics.get = function(callback, category_id, current_user, start, end) {
if (start == null) start = 0;
if (end == null) end = start + 10;
@@ -70,15 +69,18 @@ var RDB = require('./redis.js'),
locked = replies[6];
deleted = replies[7];
pinned = replies[8];
-
- user.get_usernames_by_uids(uid, function(userNames) {
-
- for (var i=0, ii=title.length; i<ii; i++) {
-
+
+ var usernames,
+ has_read;
+
+ function generate_topic() {
+ if (!usernames || !has_read) return;
+
+ for (var i=0, ii=title.length; i<ii; i++) {
topics.push({
'title' : title[i],
'uid' : uid[i],
- 'username': userNames[i],
+ 'username': usernames[i],
'timestamp' : timestamp[i],
'relativeTime': utils.relativeTime(timestamp[i]),
'slug' : slug[i],
@@ -86,7 +88,8 @@ var RDB = require('./redis.js'),
'lock-icon': locked[i] === '1' ? 'icon-lock' : 'none',
'deleted': deleted[i],
'pinned': parseInt(pinned[i] || 0), // For sorting purposes
- 'pin-icon': pinned[i] === '1' ? 'icon-pushpin' : 'none'
+ 'pin-icon': pinned[i] === '1' ? 'icon-pushpin' : 'none',
+ 'badgeclass' : (has_read[i] && current_user !=0) ? '' : 'badge-important'
});
}
@@ -101,8 +104,17 @@ var RDB = require('./redis.js'),
'category_id': category_id || 0,
'topics': topics
});
+ }
+ user.get_usernames_by_uids(uid, function(userNames) {
+ usernames = userNames;
+ generate_topic();
});
+
+ Topics.hasReadTopics(tids, current_user, function(hasRead) {
+ has_read = hasRead;
+ generate_topic();
+ });
}
else {
callback({
@@ -119,6 +131,23 @@ var RDB = require('./redis.js'),
});
}
+
+ Topics.markAsRead = function(tid, uid) {
+ RDB.sadd('tid:' + tid + ':read_by_uid', uid);
+ }
+
+ Topics.hasReadTopics = function(tids, uid, callback) {
+ var batch = RDB.multi();
+
+ for (var i=0, ii=tids.length; i<ii; i++) {
+ batch.sismember('tid:' + tids[i] + ':read_by_uid', uid);
+ }
+
+ batch.exec(function(err, hasRead) {
+ callback(hasRead);
+ });
+ }
+
Topics.post = function(socket, uid, title, content, category_id) {
if (!category_id) throw new Error('Attempted to post without a category_id');
diff --git a/src/webserver.js b/src/webserver.js
index 151b179ca1..e289f6218a 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -131,7 +131,7 @@ var express = require('express'),
case 'category' :
global.modules.topics.get(function(data) {
res.send(JSON.stringify(data));
- }, req.params.id);
+ }, req.params.id, (req.user) ? req.user.uid : 0);
break;
case 'latest' :
global.modules.topics.get(function(data) {