diff --git a/src/feed.js b/src/feed.js index 349b9ca926..8e1ac6d69e 100644 --- a/src/feed.js +++ b/src/feed.js @@ -29,7 +29,9 @@ Feed.updateTopic = function(tid, cid) { var cache_time_in_seconds = 60; - topics.getTopicWithPosts(tid, 0, function(topicData) { + topics.getTopicWithPosts(tid, 0, function(err, topicData) { + if (err) console.log('Error: Problem saving topic RSS feed', err); + var location = '/topic/' + topicData.slug, xml_url = '/topic/' + tid + '.rss'; diff --git a/src/threadTools.js b/src/threadTools.js index b192b179d5..fc870d846b 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -6,6 +6,13 @@ var RDB = require('./redis.js'), notifications = require('./notifications.js'); (function(ThreadTools) { + + ThreadTools.exists = function(tid, callback) { + RDB.sismember('topics:tid', tid, function(err, ismember) { + if (err) RDB.handle(err); + callback(!!ismember || false); + }); + } ThreadTools.privileges = function(tid, uid, callback) { //todo: break early if one condition is true diff --git a/src/topics.js b/src/topics.js index 9c3cd48cb2..6235c1df52 100644 --- a/src/topics.js +++ b/src/topics.js @@ -84,47 +84,51 @@ marked.setOptions({ } Topics.getTopicWithPosts = function(tid, current_user, callback) { - - Topics.markAsRead(tid, current_user); + threadTools.exists(tid, function(exists) { + if (!exists) return callback(new Error('Topic tid \'' + tid + '\' not found')); - function getTopicData(next) { - Topics.getTopicData(tid, function(topicData) { - next(null, topicData); - }); - } + Topics.markAsRead(tid, current_user); - function getTopicPosts(next) { - Topics.getTopicPosts(tid, 0, -1, current_user, function(topicPosts, privileges) { - - next(null, topicPosts); - }); - } + function getTopicData(next) { + Topics.getTopicData(tid, function(topicData) { + next(null, topicData); + }); + } - function getPrivileges(next) { - threadTools.privileges(tid, current_user, function(privData) { - next(null, privData); - }); - } + function getTopicPosts(next) { + Topics.getTopicPosts(tid, 0, -1, current_user, function(topicPosts, privileges) { + + next(null, topicPosts); + }); + } - async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) { - var topicData = results[0], - topicPosts = results[1], - privileges = results[2]; - - var main_posts = topicPosts.splice(0, 1); - - callback({ - 'topic_name':topicData.title, - 'category_name':topicData.category_name, - 'category_slug':topicData.category_slug, - 'locked': topicData.locked, - 'deleted': topicData.deleted, - 'pinned': topicData.pinned, - 'slug': topicData.slug, - 'topic_id': tid, - 'expose_tools': privileges.editable ? 1 : 0, - 'posts': topicPosts, - 'main_posts': main_posts + function getPrivileges(next) { + threadTools.privileges(tid, current_user, function(privData) { + next(null, privData); + }); + } + + async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) { + if (err) console.log(err.message); + var topicData = results[0], + topicPosts = results[1], + privileges = results[2]; + + var main_posts = topicPosts.splice(0, 1); + + callback(null, { + 'topic_name':topicData.title, + 'category_name':topicData.category_name, + 'category_slug':topicData.category_slug, + 'locked': topicData.locked, + 'deleted': topicData.deleted, + 'pinned': topicData.pinned, + 'slug': topicData.slug, + 'topic_id': tid, + 'expose_tools': privileges.editable ? 1 : 0, + 'posts': topicPosts, + 'main_posts': main_posts + }); }); }); } diff --git a/src/webserver.js b/src/webserver.js index b8214a807d..9814c1c103 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -133,7 +133,9 @@ var express = require('express'), var topic_url = tid + (req.params.slug ? '/' + req.params.slug : ''); - topics.getTopicWithPosts(tid, ((req.user) ? req.user.uid : 0), function(topic) { + topics.getTopicWithPosts(tid, ((req.user) ? req.user.uid : 0), function(err, topic) { + if (err) return res.redirect('404'); + res.send( app.build_header(res) + '\n\t' + @@ -238,7 +240,7 @@ var express = require('express'), res.json(data); break; case 'topic' : - topics.getTopicWithPosts(req.params.id, uid, function(data) { + topics.getTopicWithPosts(req.params.id, uid, function(err, data) { res.json(data); }); break;