Files
NodeBB/src/controllers/posts.js

40 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
const querystring = require('querystring');
2019-08-12 21:56:09 -04:00
const posts = require('../posts');
const privileges = require('../privileges');
const helpers = require('./helpers');
2017-03-02 16:11:11 +03:00
2019-08-12 21:56:09 -04:00
const postsController = module.exports;
2019-08-12 21:56:09 -04:00
postsController.redirectToPost = async function (req, res, next) {
const pid = parseInt(req.params.pid, 10);
if (!pid) {
2017-03-02 16:11:11 +03:00
return next();
}
2019-08-12 21:56:09 -04:00
const [canRead, path] = await Promise.all([
privileges.posts.can('topics:read', pid, req.uid),
posts.generatePostPath(pid, req.uid),
]);
if (!path) {
return next();
}
if (!canRead) {
return helpers.notAllowed(req, res);
}
const qs = querystring.stringify(req.query);
2021-02-03 23:59:08 -07:00
helpers.redirect(res, qs ? `${path}?${qs}` : path);
};
2019-08-12 21:56:09 -04:00
postsController.getRecentPosts = async function (req, res) {
const page = parseInt(req.query.page, 10) || 1;
const postsPerPage = 20;
const start = Math.max(0, (page - 1) * postsPerPage);
const stop = start + postsPerPage - 1;
const data = await posts.getRecentPosts(req.uid, start, stop, req.params.term);
2019-08-12 21:56:09 -04:00
res.json(data);
2017-03-02 16:11:11 +03:00
};