2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2016-05-05 20:24:03 +03:00
|
|
|
|
2019-11-05 19:51:01 -05:00
|
|
|
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;
|
2016-05-05 20:24:03 +03:00
|
|
|
|
2019-08-12 21:56:09 -04:00
|
|
|
postsController.redirectToPost = async function (req, res, next) {
|
|
|
|
|
const pid = parseInt(req.params.pid, 10);
|
2016-05-05 20:24:03 +03:00
|
|
|
if (!pid) {
|
2017-03-02 16:11:11 +03:00
|
|
|
return next();
|
2016-05-05 20:24:03 +03:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2019-11-05 19:51:01 -05:00
|
|
|
|
|
|
|
|
const qs = querystring.stringify(req.query);
|
|
|
|
|
helpers.redirect(res, qs ? path + '?' + qs : path);
|
2016-05-05 20:24:03 +03:00
|
|
|
};
|
|
|
|
|
|
2019-08-12 21:56:09 -04:00
|
|
|
postsController.getRecentPosts = async function (req, res) {
|
|
|
|
|
const data = await posts.getRecentPosts(req.uid, 0, 19, req.params.term);
|
|
|
|
|
res.json(data);
|
2017-03-02 16:11:11 +03:00
|
|
|
};
|