mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: #7743 finish topics controller
This commit is contained in:
@@ -1,22 +1,20 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const nconf = require('nconf');
|
||||||
|
const winston = require('winston');
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../user');
|
||||||
var nconf = require('nconf');
|
const meta = require('../meta');
|
||||||
var winston = require('winston');
|
const topics = require('../topics');
|
||||||
|
const posts = require('../posts');
|
||||||
|
const privileges = require('../privileges');
|
||||||
|
const plugins = require('../plugins');
|
||||||
|
const helpers = require('./helpers');
|
||||||
|
const pagination = require('../pagination');
|
||||||
|
const utils = require('../utils');
|
||||||
|
const analytics = require('../analytics');
|
||||||
|
|
||||||
var user = require('../user');
|
const topicsController = module.exports;
|
||||||
var meta = require('../meta');
|
|
||||||
var topics = require('../topics');
|
|
||||||
var posts = require('../posts');
|
|
||||||
var privileges = require('../privileges');
|
|
||||||
var plugins = require('../plugins');
|
|
||||||
var helpers = require('./helpers');
|
|
||||||
var pagination = require('../pagination');
|
|
||||||
var utils = require('../utils');
|
|
||||||
var analytics = require('../analytics');
|
|
||||||
|
|
||||||
var topicsController = module.exports;
|
|
||||||
|
|
||||||
topicsController.get = async function getTopic(req, res, callback) {
|
topicsController.get = async function getTopic(req, res, callback) {
|
||||||
const tid = req.params.topic_id;
|
const tid = req.params.topic_id;
|
||||||
@@ -37,7 +35,7 @@ topicsController.get = async function getTopic(req, res, callback) {
|
|||||||
user.auth.getFeedToken(req.uid),
|
user.auth.getFeedToken(req.uid),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var currentPage = parseInt(req.query.page, 10) || 1;
|
let currentPage = parseInt(req.query.page, 10) || 1;
|
||||||
const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));
|
const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));
|
||||||
if (!topicData || userPrivileges.disabled || (settings.usePagination && (currentPage < 1 || currentPage > pageCount))) {
|
if (!topicData || userPrivileges.disabled || (settings.usePagination && (currentPage < 1 || currentPage > pageCount))) {
|
||||||
return callback();
|
return callback();
|
||||||
@@ -113,7 +111,7 @@ function calculatePageFromIndex(postIndex, settings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function calculateStartStop(page, postIndex, settings) {
|
function calculateStartStop(page, postIndex, settings) {
|
||||||
var startSkip = 0;
|
let startSkip = 0;
|
||||||
|
|
||||||
if (!settings.usePagination) {
|
if (!settings.usePagination) {
|
||||||
if (postIndex !== 0) {
|
if (postIndex !== 0) {
|
||||||
@@ -152,7 +150,7 @@ function markAsRead(req, tid) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function buildBreadcrumbs(topicData) {
|
async function buildBreadcrumbs(topicData) {
|
||||||
var breadcrumbs = [
|
const breadcrumbs = [
|
||||||
{
|
{
|
||||||
text: topicData.category.name,
|
text: topicData.category.name,
|
||||||
url: nconf.get('relative_path') + '/category/' + topicData.category.slug,
|
url: nconf.get('relative_path') + '/category/' + topicData.category.slug,
|
||||||
@@ -287,67 +285,55 @@ function addOGImageTag(res, image) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
topicsController.teaser = function (req, res, next) {
|
topicsController.teaser = async function (req, res, next) {
|
||||||
var tid = req.params.topic_id;
|
const tid = req.params.topic_id;
|
||||||
|
|
||||||
if (!utils.isNumber(tid)) {
|
if (!utils.isNumber(tid)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
const canRead = await privileges.topics.can('topics:read', tid, req.uid);
|
||||||
async.waterfall([
|
if (!canRead) {
|
||||||
function (next) {
|
return res.status(403).json('[[error:no-privileges]]');
|
||||||
privileges.topics.can('topics:read', tid, req.uid, next);
|
}
|
||||||
},
|
const pid = await topics.getLatestUndeletedPid(tid);
|
||||||
function (canRead, next) {
|
if (!pid) {
|
||||||
if (!canRead) {
|
return res.status(404).json('not-found');
|
||||||
return res.status(403).json('[[error:no-privileges]]');
|
}
|
||||||
}
|
const postData = await posts.getPostSummaryByPids([pid], req.uid, { stripTags: false });
|
||||||
topics.getLatestUndeletedPid(tid, next);
|
if (!postData.length) {
|
||||||
},
|
return res.status(404).json('not-found');
|
||||||
function (pid, next) {
|
}
|
||||||
if (!pid) {
|
res.json(postData[0]);
|
||||||
return res.status(404).json('not-found');
|
|
||||||
}
|
|
||||||
posts.getPostSummaryByPids([pid], req.uid, { stripTags: false }, next);
|
|
||||||
},
|
|
||||||
function (posts) {
|
|
||||||
if (!posts.length) {
|
|
||||||
return res.status(404).json('not-found');
|
|
||||||
}
|
|
||||||
res.json(posts[0]);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
topicsController.pagination = function (req, res, callback) {
|
topicsController.pagination = async function (req, res, callback) {
|
||||||
var tid = req.params.topic_id;
|
const tid = req.params.topic_id;
|
||||||
var currentPage = parseInt(req.query.page, 10) || 1;
|
const currentPage = parseInt(req.query.page, 10) || 1;
|
||||||
|
|
||||||
if (!utils.isNumber(tid)) {
|
if (!utils.isNumber(tid)) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel({
|
const [userPrivileges, settings, topic] = await Promise.all([
|
||||||
privileges: async.apply(privileges.topics.get, tid, req.uid),
|
privileges.topics.get(tid, req.uid),
|
||||||
settings: async.apply(user.getSettings, req.uid),
|
user.getSettings(req.uid),
|
||||||
topic: async.apply(topics.getTopicData, tid),
|
topics.getTopicData(tid),
|
||||||
}, function (err, results) {
|
]);
|
||||||
if (err || !results.topic) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!results.privileges.read || (results.topic.deleted && !results.privileges.view_deleted)) {
|
if (!topic) {
|
||||||
return helpers.notAllowed(req, res);
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
var postCount = results.topic.postcount;
|
if (!userPrivileges.read || (topic.deleted && !userPrivileges.view_deleted)) {
|
||||||
var pageCount = Math.max(1, Math.ceil(postCount / results.settings.postsPerPage));
|
return helpers.notAllowed(req, res);
|
||||||
|
}
|
||||||
|
|
||||||
var paginationData = pagination.create(currentPage, pageCount);
|
var postCount = topic.postcount;
|
||||||
paginationData.rel.forEach(function (rel) {
|
var pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage));
|
||||||
rel.href = nconf.get('url') + '/topic/' + results.topic.slug + rel.href;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.json(paginationData);
|
var paginationData = pagination.create(currentPage, pageCount);
|
||||||
|
paginationData.rel.forEach(function (rel) {
|
||||||
|
rel.href = nconf.get('url') + '/topic/' + topic.slug + rel.href;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
res.json(paginationData);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user