getTopicWithPosts change, fix feeds

v1.18.x
barisusakli 9 years ago
parent 07c55ee859
commit dbc04cb4a2

@ -36,12 +36,12 @@ topicsController.get = function(req, res, callback) {
user.getSettings(req.uid, next);
},
topic: function(next) {
topics.getTopicFields(tid, ['slug', 'postcount', 'deleted'], next);
topics.getTopicData(tid, next);
}
}, next);
},
function (results, next) {
if (!results.topic.slug) {
if (!results.topic) {
return callback();
}
@ -106,26 +106,19 @@ topicsController.get = function(req, res, callback) {
currentPage = Math.max(1, Math.ceil(index / settings.postsPerPage));
}
var start = (currentPage - 1) * settings.postsPerPage + postIndex,
stop = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(tid, set, req.uid, start, stop, reverse, function (err, topicData) {
if (err && err.message === '[[error:no-topic]]' && !topicData) {
return callback();
}
if (err && !topicData) {
return next(err);
}
var start = (currentPage - 1) * settings.postsPerPage + postIndex;
var stop = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(results.topic, set, req.uid, start, stop, reverse, next);
},
function (topicData, next) {
if (topicData.category.disabled) {
return callback();
}
topics.modifyByPrivilege(topicData.posts, results.privileges);
topics.modifyByPrivilege(topicData.posts, userPrivileges);
plugins.fireHook('filter:controllers.topic.get', {topicData: topicData, uid: req.uid}, next);
});
},
function (data, next) {

@ -1,62 +1,52 @@
"use strict";
var async = require('async'),
rss = require('rss'),
nconf = require('nconf'),
posts = require('../posts'),
topics = require('../topics'),
user = require('../user'),
categories = require('../categories'),
meta = require('../meta'),
helpers = require('../controllers/helpers'),
privileges = require('../privileges');
function hasTopicPrivileges(req, res, next) {
var tid = req.params.topic_id;
var async = require('async');
var rss = require('rss');
var nconf = require('nconf');
hasPrivileges(privileges.topics.can, tid, req, res, next);
}
var posts = require('../posts');
var topics = require('../topics');
var user = require('../user');
var categories = require('../categories');
var meta = require('../meta');
var helpers = require('../controllers/helpers');
var privileges = require('../privileges');
function hasCategoryPrivileges(req, res, next) {
var cid = req.params.category_id;
hasPrivileges(privileges.categories.can, cid, req, res, next);
function generateForTopic(req, res, callback) {
if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) {
return callback();
}
function hasPrivileges(method, id, req, res, next) {
method('read', id, req.uid, function(err, canRead) {
if (err) {
return next(err);
var tid = req.params.topic_id;
var userPrivileges;
async.waterfall([
function (next) {
async.parallel({
privileges: function(next) {
privileges.topics.get(tid, req.uid, next);
},
topic: function(next) {
topics.getTopicData(tid, next);
}
if (!canRead) {
return helpers.notAllowed(req, res);
}, next);
},
function (results, next) {
if (!results.topic) {
return callback();
}
return next();
});
if (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted) {
return callback();
}
function generateForTopic(req, res, next) {
if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) {
return next();
if (!results.privileges.read) {
return helpers.notAllowed(req, res);
}
var tid = req.params.topic_id;
privileges.topics.get(tid, req.uid, function(err, userPrivileges) {
if (err) {
return next(err);
userPrivileges = results.privileges;
topics.getTopicWithPosts(results.topic, 'tid:' + tid + ':posts', req.uid, 0, 25, false, next);
}
topics.getTopicWithPosts(tid, 'tid:' + tid + ':posts', req.uid, 0, 25, false, function (err, topicData) {
], function(err, topicData) {
if (err) {
return next(err);
}
if (topicData.deleted && !userPrivileges.view_deleted) {
return next();
return callback(err);
}
topics.modifyByPrivilege(topicData.posts, userPrivileges);
@ -96,12 +86,11 @@ function generateForTopic(req, res, next) {
sendFeed(feed, res);
});
});
}
function generateForUserTopics(req, res, next) {
function generateForUserTopics(req, res, callback) {
if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) {
return next();
return callback();
}
var userslug = req.params.userslug;
@ -111,6 +100,9 @@ function generateForUserTopics(req, res, next) {
user.getUidByUserslug(userslug, next);
},
function (uid, next) {
if (!uid) {
return callback();
}
user.getUserFields(uid, ['uid', 'username'], next);
}
], function(err, userData) {
@ -134,6 +126,13 @@ function generateForCategory(req, res, next) {
}
var cid = req.params.category_id;
async.waterfall([
function (next) {
async.parallel({
privileges: function(next) {
privileges.categories.get(cid, req.uid, next);
},
category: function(next) {
categories.getCategoryById({
cid: cid,
set: 'cid:' + cid + ':tids',
@ -141,24 +140,28 @@ function generateForCategory(req, res, next) {
start: 0,
stop: 25,
uid: req.uid
}, function (err, categoryData) {
if (err) {
return next(err);
}, next);
}
}, next);
},
function (results, next) {
if (!results.privileges.read) {
return helpers.notAllowed(req, res);
}
generateTopicsFeed({
uid: req.uid,
title: categoryData.name,
description: categoryData.description,
title: results.category.name,
description: results.category.description,
feed_url: '/category/' + cid + '.rss',
site_url: '/category/' + categoryData.cid,
}, categoryData.topics, function(err, feed) {
site_url: '/category/' + results.category.cid,
}, results.category.topics, next);
}
], function(err, feed) {
if (err) {
return next(err);
}
sendFeed(feed, res);
});
});
}
function generateForRecent(req, res, next) {
@ -300,6 +303,9 @@ function generateForCategoryRecentPosts(req, res, next) {
var cid = req.params.category_id;
async.parallel({
privileges: function(next) {
privileges.categories.get(cid, req.uid, next);
},
category: function(next) {
categories.getCategoryData(cid, next);
},
@ -310,16 +316,20 @@ function generateForCategoryRecentPosts(req, res, next) {
if (err) {
return next(err);
}
if (!results.category) {
return next();
}
var category = results.category;
var posts = results.posts;
if (!results.privileges.read) {
return helpers.notAllowed(req, res);
}
var feed = generateForPostsFeed({
title: category.name + ' Recent Posts',
description: 'A list of recent posts from ' + category.name,
title: results.category.name + ' Recent Posts',
description: 'A list of recent posts from ' + results.category.name,
feed_url: '/category/' + cid + '/recentposts.rss',
site_url: '/category/' + cid + '/recentposts'
}, posts);
}, results.posts);
sendFeed(feed, res);
});
@ -355,12 +365,12 @@ function sendFeed(feed, res) {
}
module.exports = function(app, middleware, controllers){
app.get('/topic/:topic_id.rss', hasTopicPrivileges, generateForTopic);
app.get('/category/:category_id.rss', hasCategoryPrivileges, generateForCategory);
app.get('/topic/:topic_id.rss', generateForTopic);
app.get('/category/:category_id.rss', generateForCategory);
app.get('/recent.rss', generateForRecent);
app.get('/popular.rss', generateForPopular);
app.get('/popular/:term.rss', generateForPopular);
app.get('/recentposts.rss', generateForRecentPosts);
app.get('/category/:category_id/recentposts.rss', hasCategoryPrivileges, generateForCategoryRecentPosts);
app.get('/category/:category_id/recentposts.rss', generateForCategoryRecentPosts);
app.get('/user/:userslug/topics.rss', generateForUserTopics);
};

@ -170,25 +170,16 @@ var async = require('async'),
], callback);
};
Topics.getTopicWithPosts = function(tid, set, uid, start, stop, reverse, callback) {
var topicData;
Topics.getTopicWithPosts = function(topicData, set, uid, start, stop, reverse, callback) {
async.waterfall([
function (next) {
Topics.getTopicData(tid, next);
},
function(_topicData, next) {
topicData = _topicData;
if (!topicData) {
return callback(new Error('[[error:no-topic]]'));
}
async.parallel({
posts: async.apply(getMainPostAndReplies, topicData, set, uid, start, stop, reverse),
category: async.apply(Topics.getCategoryData, tid),
category: async.apply(Topics.getCategoryData, topicData.tid),
threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', {topic: topicData, uid: uid, tools: []}),
tags: async.apply(Topics.getTopicTagsObjects, tid),
isFollowing: async.apply(Topics.isFollowing, [tid], uid),
bookmark: async.apply(Topics.getUserBookmark, tid, uid)
tags: async.apply(Topics.getTopicTagsObjects, topicData.tid),
isFollowing: async.apply(Topics.isFollowing, [topicData.tid], uid),
bookmark: async.apply(Topics.getUserBookmark, topicData.tid, uid)
}, next);
},
function (results, next) {

Loading…
Cancel
Save