diff --git a/src/redis.js b/src/redis.js index 6689158938..d55bd57dd5 100644 --- a/src/redis.js +++ b/src/redis.js @@ -58,6 +58,7 @@ */ RedisDB.hmgetObject = function(key, fields, callback) { RedisDB.hmget(key, fields, function(err, data) { + if(err) { return callback(err, null); } diff --git a/src/topics.js b/src/topics.js index 9a24db2d36..e086903b99 100644 --- a/src/topics.js +++ b/src/topics.js @@ -18,6 +18,100 @@ var RDB = require('./redis.js'), (function(Topics) { + Topics.post = function(uid, title, content, category_id, callback) { + if (!category_id) + throw new Error('Attempted to post without a category_id'); + + if (content) + content = content.trim(); + if (title) + title = title.trim(); + + if (!uid) { + callback(new Error('not-logged-in'), null); + return; + } else if (!title || title.length < meta.config.minimumTitleLength) { + callback(new Error('title-too-short'), null); + return; + } else if (!content || content.length < meta.config.miminumPostLength) { + callback(new Error('content-too-short'), null); + return; + } + + user.getUserField(uid, 'lastposttime', function(err, lastposttime) { + if (err) lastposttime = 0; + if (Date.now() - lastposttime < meta.config.postDelay * 1000) { + callback(new Error('too-many-posts'), null); + return; + } + + RDB.incr('next_topic_id', function(err, tid) { + RDB.handle(err); + + // Global Topics + if (uid == null) uid = 0; + if (uid !== null) { + RDB.sadd('topics:tid', tid); + } else { + // need to add some unique key sent by client so we can update this with the real uid later + RDB.lpush('topics:queued:tid', tid); + } + + var slug = tid + '/' + utils.slugify(title); + var timestamp = Date.now(); + RDB.hmset('topic:' + tid, { + 'tid': tid, + 'uid': uid, + 'cid': category_id, + 'title': title, + 'slug': slug, + 'timestamp': timestamp, + 'lastposttime': 0, + 'postcount': 0, + 'viewcount': 0, + 'locked': 0, + 'deleted': 0, + 'pinned': 0 + }); + + topicSearch.index(title, tid); + + user.addTopicIdToUser(uid, tid); + + // let everyone know that there is an unread topic in this category + RDB.del('cid:' + category_id + ':read_by_uid', function(err, data) { + Topics.markAsRead(tid, uid); + }); + + + // in future it may be possible to add topics to several categories, so leaving the door open here. + RDB.zadd('categories:' + category_id + ':tid', timestamp, tid); + RDB.hincrby('category:' + category_id, 'topic_count', 1); + RDB.incr('totaltopiccount'); + + feed.updateCategory(category_id); + + posts.create(uid, tid, content, function(err, postData) { + if(err) { + return callback(err, null); + } else if(!postData) { + return callback(new Error('invalid-post'), null); + } + + // Auto-subscribe the post creator to the newly created topic + threadTools.toggleFollow(tid, uid); + + Topics.getTopicForCategoryView(tid, uid, function(topicData) { + callback(null, { + topicData: topicData, + postData: postData + }); + }); + }); + }); + }); + }; + Topics.getTopicData = function(tid, callback) { RDB.hgetall('topic:' + tid, function(err, data) { if(err) { @@ -678,104 +772,6 @@ var RDB = require('./redis.js'), }); } - Topics.post = function(uid, title, content, category_id, callback) { - if (!category_id) - throw new Error('Attempted to post without a category_id'); - - if (content) - content = content.trim(); - if (title) - title = title.trim(); - - if (uid === 0) { - callback(new Error('not-logged-in'), null); - return; - } else if (!title || title.length < meta.config.minimumTitleLength) { - callback(new Error('title-too-short'), null); - return; - } else if (!content || content.length < meta.config.miminumPostLength) { - callback(new Error('content-too-short'), null); - return; - } - - user.getUserField(uid, 'lastposttime', function(err, lastposttime) { - if (err) lastposttime = 0; - if (Date.now() - lastposttime < meta.config.postDelay * 1000) { - callback(new Error('too-many-posts'), null); - return; - } - - RDB.incr('next_topic_id', function(err, tid) { - RDB.handle(err); - - // Global Topics - if (uid == null) uid = 0; - if (uid !== null) { - RDB.sadd('topics:tid', tid); - } else { - // need to add some unique key sent by client so we can update this with the real uid later - RDB.lpush('topics:queued:tid', tid); - } - - var slug = tid + '/' + utils.slugify(title); - var timestamp = Date.now(); - RDB.hmset('topic:' + tid, { - 'tid': tid, - 'uid': uid, - 'cid': category_id, - 'title': title, - 'slug': slug, - 'timestamp': timestamp, - 'lastposttime': 0, - 'postcount': 0, - 'viewcount': 0, - 'locked': 0, - 'deleted': 0, - 'pinned': 0 - }); - - topicSearch.index(title, tid); - - user.addTopicIdToUser(uid, tid); - - // let everyone know that there is an unread topic in this category - RDB.del('cid:' + category_id + ':read_by_uid', function(err, data) { - Topics.markAsRead(tid, uid); - }); - - - // in future it may be possible to add topics to several categories, so leaving the door open here. - RDB.zadd('categories:' + category_id + ':tid', timestamp, tid); - RDB.hincrby('category:' + category_id, 'topic_count', 1); - RDB.incr('totaltopiccount'); - - feed.updateCategory(category_id); - - posts.create(uid, tid, content, function(err, postData) { - if(err) { - return callback(err, null); - } else if(!postData) { - return callback(new Error('invalid-post'), null); - } - - // Auto-subscribe the post creator to the newly created topic - threadTools.toggleFollow(tid, uid); - - // Notify any users looking at the category that a new topic has arrived - Topics.getTopicForCategoryView(tid, uid, function(topicData) { - io.sockets.in('category_' + category_id).emit('event:new_topic', topicData); - io.sockets.in('recent_posts').emit('event:new_topic', topicData); - io.sockets.in('user/' + uid).emit('event:new_post', { - posts: postData - }); - }); - - callback(null, postData); - }); - }); - }); - }; - Topics.getTopicField = function(tid, field, callback) { RDB.hget('topic:' + tid, field, callback); } diff --git a/src/websockets.js b/src/websockets.js index 15193ea45c..326757cc2b 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -380,6 +380,12 @@ module.exports.init = function(io) { } if (result) { + io.sockets.in('category_' + data.category_id).emit('event:new_topic', topicData); + io.sockets.in('recent_posts').emit('event:new_topic', topicData); + io.sockets.in('user/' + uid).emit('event:new_post', { + posts: postData + }); + posts.getTopicPostStats(); socket.emit('event:alert', { diff --git a/tests/topics.js b/tests/topics.js new file mode 100644 index 0000000000..3b7655fbbf --- /dev/null +++ b/tests/topics.js @@ -0,0 +1,85 @@ + + + +var winston = require('winston'); + +process.on('uncaughtException', function (err) { + winston.error('Encountered error while running test suite: ' + err.message); +}); + +var assert = require('assert'), + RDB = require('../mocks/redismock'); + +// Reds is not technically used in this test suite, but its invocation is required to stop the included +// libraries from trying to connect to the default Redis host/port +var reds = require('reds'); +reds.createClient = function () { + return reds.client || (reds.client = RDB); +}; + +var Topics = require('../src/topics'); + +describe('Topics', function() { + var newTopic; + var newPost; + var userInfo; + + describe('.post', function() { + it('should post a new topic', function(done) { + var uid = 1, + cid = 1, + title = 'Test Topic Title', + content = 'The content of test topic'; + + Topics.post(uid, title, content, cid, function(err, result) { + assert.equal(err, null, 'was created with error'); + assert.ok(result); + + newTopic = result.topicData; + newPost = result.postData; + + done(); + }); + }); + + it('should fail posting a topic', function(done) { + var uid = null, + cid = 1, + title = 'Test Topic Title', + content = 'The content of test topic'; + + Topics.post(uid, title, content, cid, function(err, result) { + assert.equal(err.message, 'not-logged-in'); + done(); + }); + }); + }); + + describe('.getTopicData', function() { + it('should get Topic data', function(done) { + Topics.getTopicData(newTopic.tid, function(err, result) { + done.apply(this.arguments); + }); + }); + }); + + + describe('.getTopicDataWithUser', function() { + it('should get Topic data with user info', function(done) { + Topics.getTopicDataWithUser(newTopic.tid, function(err, result) { + + done.apply(this.arguments); + }); + }); + }); + + + after(function() { + RDB.send_command('flushdb', [], function(error){ + if(error){ + winston.error(error); + throw new Error(error); + } + }); + }); +}); \ No newline at end of file