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

v1.18.x
psychobunny 12 years ago
parent a4e8f9e706
commit f218e0f3a7

@ -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>

@ -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.',

@ -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');

@ -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) {

Loading…
Cancel
Save