Merge branch 'master' of github.com:designcreateplay/NodeBB

v1.18.x
Julian Lam
commit 92aaeca899

@ -42,11 +42,11 @@
</div> </div>
</li></a> </li></a>
<!-- END topics --> <!-- END topics -->
</ul>
<hr /> <hr />
</ul>
<button id="new_post" class="btn btn-primary btn-large {show_category_features}">New Topic</button> <button id="new_post" class="btn btn-primary btn-large {show_category_features}">New Topic</button>
</div> </div>
<div class="span3 {show_category_features}"> <div class="span3 {show_category_features} category-sidebar">
<div class="sidebar-block img-polaroid"> <div class="sidebar-block img-polaroid">
<div class="block-header"> <div class="block-header">
Recent Replies Recent Replies
@ -100,7 +100,7 @@
]); ]);
if (jQuery('.category-item').length == 0) { if (jQuery('.category-item').length == 0) {
jQuery('.category.row').hide(); jQuery('#topics-container, .category-sidebar').hide();
jQuery('#category-no-topics').show(); jQuery('#category-no-topics').show();
} }

@ -1,378 +1,378 @@
var RDB = require('./redis.js') var RDB = require('./redis.js')
schema = require('./schema.js'), schema = require('./schema.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
utils = require('./../public/src/utils.js'), utils = require('./../public/src/utils.js'),
user = require('./user.js'), user = require('./user.js'),
categories = require('./categories.js'), categories = require('./categories.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
marked = require('marked'), marked = require('marked'),
threadTools = require('./threadTools.js'), threadTools = require('./threadTools.js'),
async = require('async'); async = require('async');
marked.setOptions({ marked.setOptions({
breaks: true breaks: true
}); });
(function(Topics) { (function(Topics) {
Topics.getTopicById = function(tid, current_user, callback) { Topics.getTopicById = function(tid, current_user, callback) {
function getTopicData(next) { function getTopicData(next) {
RDB.multi() RDB.multi()
.get(schema.topics(tid).title) .get(schema.topics(tid).title)
.get(schema.topics(tid).locked) .get(schema.topics(tid).locked)
.get(schema.topics(tid).category_name) .get(schema.topics(tid).category_name)
.get(schema.topics(tid).category_slug) .get(schema.topics(tid).category_slug)
.get(schema.topics(tid).deleted) .get(schema.topics(tid).deleted)
.get(schema.topics(tid).pinned) .get(schema.topics(tid).pinned)
.get(schema.topics(tid).slug) .get(schema.topics(tid).slug)
.exec(function(err, replies) { .exec(function(err, replies) {
next(null, { next(null, {
topic_name: replies[0], topic_name: replies[0],
locked: replies[1] || 0, locked: replies[1] || 0,
category_name: replies[2], category_name: replies[2],
category_slug: replies[3], category_slug: replies[3],
deleted: replies[4] || 0, deleted: replies[4] || 0,
pinned: replies[5] || 0, pinned: replies[5] || 0,
slug: replies[6] slug: replies[6]
}); });
}); });
} }
function getTopicPosts(next) { function getTopicPosts(next) {
posts.getPostsByTid(tid, current_user, 0, 10, function(postData) { posts.getPostsByTid(tid, current_user, 0, 10, function(postData) {
next(null, postData); next(null, postData);
}); });
} }
function getPrivileges(next) { function getPrivileges(next) {
threadTools.privileges(tid, current_user, function(privData) { threadTools.privileges(tid, current_user, function(privData) {
next(null, privData); next(null, privData);
}); });
} }
async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) { async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) {
var retrieved_posts = [], var retrieved_posts = [],
main_posts = []; main_posts = [];
var topicData = results[0], var topicData = results[0],
postData = results[1].postData, postData = results[1].postData,
userData = results[1].userData, userData = results[1].userData,
voteData = results[1].voteData, voteData = results[1].voteData,
privileges = results[2]; privileges = results[2];
if (!postData) { if (!postData) {
callback(false); callback(false);
return; return;
} }
for (var i=0, ii= postData.pid.length; i<ii; i++) { for (var i=0, ii= postData.pid.length; i<ii; i++) {
var uid = postData.uid[i], var uid = postData.uid[i],
pid = postData.pid[i]; pid = postData.pid[i];
// ############ to be moved into posts.getPostsByTid ############ // ############ to be moved into posts.getPostsByTid ############
if (postData.deleted[i] === null || (postData.deleted[i] === '1' && privileges.view_deleted) || current_user === uid) { if (postData.deleted[i] === null || (postData.deleted[i] === '1' && privileges.view_deleted) || current_user === uid) {
var post_obj = { var post_obj = {
'pid' : pid, 'pid' : pid,
'uid' : uid, 'uid' : uid,
'content' : marked(postData.content[i] || ''), 'content' : marked(postData.content[i] || ''),
'post_rep' : postData.reputation[i] || 0, 'post_rep' : postData.reputation[i] || 0,
'timestamp' : postData.timestamp[i], 'timestamp' : postData.timestamp[i],
'relativeTime': utils.relativeTime(postData.timestamp[i]), 'relativeTime': utils.relativeTime(postData.timestamp[i]),
'username' : userData[uid].username || 'anonymous', 'username' : userData[uid].username || 'anonymous',
'user_rep' : userData[uid].reputation || 0, 'user_rep' : userData[uid].reputation || 0,
'gravatar' : userData[uid].picture || 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e', 'gravatar' : userData[uid].picture || 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e',
'signature' : marked(userData[uid].signature || ''), 'signature' : marked(userData[uid].signature || ''),
'fav_star_class' : voteData[pid] ? 'icon-star' : 'icon-star-empty', 'fav_star_class' : voteData[pid] ? 'icon-star' : 'icon-star-empty',
'display_moderator_tools': (uid == current_user || privileges.editable) ? 'show' : 'none', 'display_moderator_tools': (uid == current_user || privileges.editable) ? 'show' : 'none',
'edited-class': postData.editor[i] !== null ? '' : 'none', 'edited-class': postData.editor[i] !== null ? '' : 'none',
'editor': postData.editor[i] !== null ? userData[postData.editor[i]].username : '', 'editor': postData.editor[i] !== null ? userData[postData.editor[i]].username : '',
'relativeEditTime': postData.editTime !== null ? utils.relativeTime(postData.editTime[i]) : '', 'relativeEditTime': postData.editTime !== null ? utils.relativeTime(postData.editTime[i]) : '',
'deleted': postData.deleted[i] || '0' 'deleted': postData.deleted[i] || '0'
}; };
if (i == 0) { if (i == 0) {
main_posts.push(post_obj); main_posts.push(post_obj);
} else { } else {
retrieved_posts.push(post_obj); retrieved_posts.push(post_obj);
} }
} }
// ########## end to be moved into posts.getPostsByTid ############ // ########## end to be moved into posts.getPostsByTid ############
} }
callback({ callback({
'topic_name':topicData.topic_name, 'topic_name':topicData.topic_name,
'category_name':topicData.category_name, 'category_name':topicData.category_name,
'category_slug':topicData.category_slug, 'category_slug':topicData.category_slug,
'locked': parseInt(topicData.locked) || 0, 'locked': parseInt(topicData.locked) || 0,
'deleted': parseInt(topicData.deleted) || 0, 'deleted': parseInt(topicData.deleted) || 0,
'pinned': parseInt(topicData.pinned) || 0, 'pinned': parseInt(topicData.pinned) || 0,
'slug': topicData.slug, 'slug': topicData.slug,
'topic_id': tid, 'topic_id': tid,
'expose_tools': privileges.editable ? 1 : 0, 'expose_tools': privileges.editable ? 1 : 0,
'posts': retrieved_posts, 'posts': retrieved_posts,
'main_posts': main_posts 'main_posts': main_posts
}); });
}); });
} }
Topics.get_topic = function(tid, uid, callback) { Topics.get_topic = function(tid, uid, callback) {
var topicData = {}; var topicData = {};
function get_topic_data(next) { function get_topic_data(next) {
RDB.mget([ RDB.mget([
schema.topics(tid).title, schema.topics(tid).title,
schema.topics(tid).uid, schema.topics(tid).uid,
schema.topics(tid).timestamp, schema.topics(tid).timestamp,
schema.topics(tid).slug, schema.topics(tid).slug,
schema.topics(tid).postcount, schema.topics(tid).postcount,
schema.topics(tid).locked, schema.topics(tid).locked,
schema.topics(tid).pinned, schema.topics(tid).pinned,
schema.topics(tid).deleted schema.topics(tid).deleted
], function(err, topic) { ], function(err, topic) {
if (err) { if (err) {
throw new Error(err); throw new Error(err);
} }
topicData.title = topic[0]; topicData.title = topic[0];
topicData.uid = topic[1]; topicData.uid = topic[1];
topicData.timestamp = topic[2]; topicData.timestamp = topic[2];
topicData.relativeTime = utils.relativeTime(topic[2]), topicData.relativeTime = utils.relativeTime(topic[2]),
topicData.slug = topic[3]; topicData.slug = topic[3];
topicData.post_count = topic[4]; topicData.post_count = topic[4];
topicData.locked = topic[5]; topicData.locked = topic[5];
topicData.pinned = topic[6]; topicData.pinned = topic[6];
topicData.deleted = topic[7]; topicData.deleted = topic[7];
user.getUserField(topic[1], 'username', function(username) { user.getUserField(topic[1], 'username', function(username) {
topicData.username = username; topicData.username = username;
next(); next();
}); });
}); });
} }
function get_read_status(next) { function get_read_status(next) {
// posts.create calls this function - should be an option to skip this because its always true // posts.create calls this function - should be an option to skip this because its always true
if (uid && parseInt(uid) > 0) { if (uid && parseInt(uid) > 0) {
RDB.sismember(schema.topics(tid).read_by_uid, uid, function(err, read) { RDB.sismember(schema.topics(tid).read_by_uid, uid, function(err, read) {
topicData.badgeclass = read ? '' : 'badge-important'; topicData.badgeclass = read ? '' : 'badge-important';
next(); next();
}); });
} else { } else {
next(); next();
} }
} }
function get_teaser(next) { function get_teaser(next) {
Topics.get_teaser(tid, function(teaser) { Topics.get_teaser(tid, function(teaser) {
topicData.teaser_text = teaser.text; topicData.teaser_text = teaser.text;
topicData.teaser_username = teaser.username; topicData.teaser_username = teaser.username;
next(); next();
}); });
} }
async.parallel([get_topic_data, get_read_status, get_teaser], function(err) { async.parallel([get_topic_data, get_read_status, get_teaser], function(err) {
if (err) { if (err) {
throw new Error(err); throw new Error(err);
} }
callback(topicData); callback(topicData);
}); });
} }
Topics.get_cid_by_tid = function(tid, callback) { Topics.get_cid_by_tid = function(tid, callback) {
RDB.get(schema.topics(tid).cid, function(err, cid) { RDB.get(schema.topics(tid).cid, function(err, cid) {
if (cid && parseInt(cid) > 0) { if (cid && parseInt(cid) > 0) {
callback(cid); callback(cid);
} else { } else {
callback(false); callback(false);
} }
}); });
} }
Topics.markAsRead = function(tid, uid) { 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 // 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(schema.topics(tid).read_by_uid, uid); RDB.sadd(schema.topics(tid).read_by_uid, uid);
Topics.get_cid_by_tid(tid, function(cid) { Topics.get_cid_by_tid(tid, function(cid) {
RDB.sadd('cid:' + cid + ':read_by_uid', uid); RDB.sadd('cid:' + cid + ':read_by_uid', uid);
}); });
} }
Topics.hasReadTopics = function(tids, uid, callback) { Topics.hasReadTopics = function(tids, uid, callback) {
var batch = RDB.multi(); var batch = RDB.multi();
for (var i=0, ii=tids.length; i<ii; i++) { for (var i=0, ii=tids.length; i<ii; i++) {
batch.sismember(schema.topics(tids[i]).read_by_uid, uid); batch.sismember(schema.topics(tids[i]).read_by_uid, uid);
} }
batch.exec(function(err, hasRead) { batch.exec(function(err, hasRead) {
callback(hasRead); callback(hasRead);
}); });
} }
Topics.get_teasers = function(tids, callback) { Topics.get_teasers = function(tids, callback) {
var requests = []; var requests = [];
if (Array.isArray(tids)) { if (Array.isArray(tids)) {
for(x=0,numTids=tids.length;x<numTids;x++) { for(x=0,numTids=tids.length;x<numTids;x++) {
(function(tid) { (function(tid) {
requests.push(function(next) { requests.push(function(next) {
Topics.get_teaser(tid, function(teaser_info) { Topics.get_teaser(tid, function(teaser_info) {
next(null, teaser_info); next(null, teaser_info);
}); });
}); });
})(tids[x]); })(tids[x]);
} }
async.parallel(requests, function(err, teasers) { async.parallel(requests, function(err, teasers) {
callback(teasers); callback(teasers);
}); });
} else { } else {
callback([]); callback([]);
} }
} }
// start: probably should be moved into posts // start: probably should be moved into posts
Topics.get_latest_undeleted_pid = function(tid, callback) { Topics.get_latest_undeleted_pid = function(tid, callback) {
RDB.lrange(schema.topics(tid).posts, 0, -1, function(err, pids) { RDB.lrange(schema.topics(tid).posts, 0, -1, function(err, pids) {
var pidKeys = [], var pidKeys = [],
numPids = pids.length; numPids = pids.length;
if (numPids === 0) return callback(null); if (numPids === 0) return callback(null);
for(var x=0,numPids=pids.length;x<numPids;x++) { for(var x=0,numPids=pids.length;x<numPids;x++) {
pidKeys.push('pid:' + pids[x] + ':deleted'); pidKeys.push('pid:' + pids[x] + ':deleted');
} }
RDB.mget(pidKeys, function(err, posts) { RDB.mget(pidKeys, function(err, posts) {
var numPosts = posts.length; var numPosts = posts.length;
while(numPosts--) { while(numPosts--) {
if (posts[numPosts] !== '1') { if (posts[numPosts] !== '1') {
callback(pids[numPosts]); callback(pids[numPosts]);
break; break;
} }
} }
}); });
}); });
} }
Topics.get_teaser = function(tid, callback) { Topics.get_teaser = function(tid, callback) {
Topics.get_latest_undeleted_pid(tid, function(pid) { Topics.get_latest_undeleted_pid(tid, function(pid) {
if (pid !== null) { if (pid !== null) {
RDB.mget([ RDB.mget([
'pid:' + pid + ':content', 'pid:' + pid + ':content',
'pid:' + pid + ':uid', 'pid:' + pid + ':uid',
'pid:' + pid + ':timestamp' 'pid:' + pid + ':timestamp'
], function(err, content) { ], function(err, content) {
user.getUserField(content[1], 'username', function(username) { user.getUserField(content[1], 'username', function(username) {
var stripped = content[0], var stripped = content[0],
timestamp = content[2]; timestamp = content[2];
if(content[0]) if(content[0])
stripped = utils.strip_tags(marked(content[0])); stripped = utils.strip_tags(marked(content[0]));
callback({ callback({
"text": stripped, "text": stripped,
"username": username, "username": username,
"timestamp" : timestamp "timestamp" : timestamp
}); });
}); });
}); });
} }
}); });
} }
// end: probably should be moved into posts // end: probably should be moved into posts
Topics.post = function(socket, uid, title, content, category_id) { Topics.post = function(socket, uid, title, content, category_id) {
if (!category_id) throw new Error('Attempted to post without a category_id'); if (!category_id) throw new Error('Attempted to post without a category_id');
if (uid === 0) { if (uid === 0) {
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Thank you for posting', title: 'Thank you for posting',
message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.', message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.',
type: 'warning', type: 'warning',
timeout: 7500, timeout: 7500,
clickfn: function() { clickfn: function() {
ajaxify.go('register'); ajaxify.go('register');
} }
}); });
return; // for now, until anon code is written. return; // for now, until anon code is written.
} }
user.getUserField(uid, 'lastposttime', function(lastposttime) { user.getUserField(uid, 'lastposttime', function(lastposttime) {
if(new Date().getTime() - lastposttime < config.post_delay) { if(new Date().getTime() - lastposttime < config.post_delay) {
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Too many posts!', title: 'Too many posts!',
message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.', message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.',
type: 'error', type: 'error',
timeout: 2000 timeout: 2000
}); });
return; return;
} }
RDB.incr(schema.global().next_topic_id, function(err, tid) { RDB.incr(schema.global().next_topic_id, function(err, tid) {
RDB.handle(err); RDB.handle(err);
// Global Topics // Global Topics
if (uid == null) uid = 0; if (uid == null) uid = 0;
if (uid !== null) { if (uid !== null) {
RDB.sadd(schema.topics().tid, tid); RDB.sadd(schema.topics().tid, tid);
} else { } else {
// need to add some unique key sent by client so we can update this with the real uid later // need to add some unique key sent by client so we can update this with the real uid later
RDB.lpush(schema.topics().queued_tids, tid); RDB.lpush(schema.topics().queued_tids, tid);
} }
var slug = tid + '/' + utils.slugify(title); var slug = tid + '/' + utils.slugify(title);
// Topic Info // Topic Info
RDB.set(schema.topics(tid).title, title); RDB.set(schema.topics(tid).title, title);
RDB.set(schema.topics(tid).uid, uid); RDB.set(schema.topics(tid).uid, uid);
RDB.set(schema.topics(tid).slug, slug); RDB.set(schema.topics(tid).slug, slug);
RDB.set(schema.topics(tid).timestamp, new Date().getTime()); RDB.set(schema.topics(tid).timestamp, new Date().getTime());
RDB.set(schema.topics().slug(slug).tid, tid); RDB.set(schema.topics().slug(slug).tid, tid);
// Posts // Posts
posts.create(uid, tid, content, function(pid) { posts.create(uid, tid, content, function(pid) {
if (pid > 0) { if (pid > 0) {
RDB.lpush(schema.topics(tid).posts, pid); RDB.lpush(schema.topics(tid).posts, pid);
// Notify any users looking at the category that a new topic has arrived // Notify any users looking at the category that a new topic has arrived
Topics.get_topics(tid, uid, function(topicData) { Topics.get_topic(tid, uid, function(topicData) {
io.sockets.in('category_' + category_id).emit('event:new_topic', topicData); io.sockets.in('category_' + category_id).emit('event:new_topic', topicData);
}); });
} }
}); });
Topics.markAsRead(tid, uid); Topics.markAsRead(tid, uid);
// User Details - move this out later // User Details - move this out later
RDB.lpush('uid:' + uid + ':topics', tid); RDB.lpush('uid:' + uid + ':topics', tid);
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Thank you for posting', title: 'Thank you for posting',
message: 'You have successfully posted. Click here to view your post.', message: 'You have successfully posted. Click here to view your post.',
type: 'notify', type: 'notify',
timeout: 2000 timeout: 2000
}); });
// let everyone know that there is an unread topic in this category // let everyone know that there is an unread topic in this category
RDB.del('cid:' + category_id + ':read_by_uid'); RDB.del('cid:' + category_id + ':read_by_uid');
RDB.zadd(schema.topics().recent, (new Date()).getTime(), tid); RDB.zadd(schema.topics().recent, (new Date()).getTime(), tid);
//RDB.zadd('topics:active', tid); //RDB.zadd('topics:active', tid);
// in future it may be possible to add topics to several categories, so leaving the door open here. // 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.sadd('categories:' + category_id + ':tid', tid);
RDB.set(schema.topics(tid).cid, category_id); RDB.set(schema.topics(tid).cid, category_id);
categories.getCategories([category_id], function(data) { categories.getCategories([category_id], function(data) {
RDB.set(schema.topics(tid).category_name, data.categories[0].name); RDB.set(schema.topics(tid).category_name, data.categories[0].name);
RDB.set(schema.topics(tid).category_slug, data.categories[0].slug); RDB.set(schema.topics(tid).category_slug, data.categories[0].slug);
}); });
RDB.incr('cid:' + category_id + ':topiccount'); RDB.incr('cid:' + category_id + ':topiccount');
}); });
}); });
}; };
}(exports)); }(exports));
Loading…
Cancel
Save