2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2016-05-05 20:24:03 +03:00
|
|
|
|
2024-04-08 14:46:02 -04:00
|
|
|
const nconf = require('nconf');
|
2019-11-05 19:51:01 -05:00
|
|
|
const querystring = require('querystring');
|
|
|
|
|
|
2024-04-08 14:46:02 -04:00
|
|
|
const meta = require('../meta');
|
2019-08-12 21:56:09 -04:00
|
|
|
const posts = require('../posts');
|
|
|
|
|
const privileges = require('../privileges');
|
2024-05-06 15:54:45 -04:00
|
|
|
const activitypub = require('../activitypub');
|
2024-03-05 14:24:13 -05:00
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
2019-08-12 21:56:09 -04:00
|
|
|
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) {
|
2024-03-05 14:24:13 -05:00
|
|
|
const pid = utils.isNumber(req.params.pid) ? parseInt(req.params.pid, 10) : req.params.pid;
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-06 15:54:45 -04:00
|
|
|
// Kickstart note assertion if applicable
|
2024-06-12 00:18:29 -04:00
|
|
|
if (!utils.isNumber(pid) && req.uid && meta.config.activitypubEnabled) {
|
2024-05-06 15:54:45 -04:00
|
|
|
const exists = await posts.exists(pid);
|
|
|
|
|
if (!exists) {
|
|
|
|
|
await activitypub.notes.assert(req.uid, pid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2024-04-08 14:46:02 -04:00
|
|
|
if (meta.config.activitypubEnabled) {
|
|
|
|
|
// Include link header for richer parsing
|
|
|
|
|
res.set('Link', `<${nconf.get('url')}/post/${req.params.pid}>; rel="alternate"; type="application/activity+json"`);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 19:51:01 -05:00
|
|
|
const qs = querystring.stringify(req.query);
|
2023-08-16 19:35:23 -04:00
|
|
|
helpers.redirect(res, qs ? `${path}?${qs}` : path, true);
|
2016-05-05 20:24:03 +03:00
|
|
|
};
|
|
|
|
|
|
2019-08-12 21:56:09 -04:00
|
|
|
postsController.getRecentPosts = async function (req, res) {
|
2021-02-01 21:38:26 -05:00
|
|
|
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
|
|
|
};
|