mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat(api): #9123, migrate /api/post/pid/:pid to Write API
This commit is contained in:
@@ -15,6 +15,31 @@ const websockets = require('../socket.io');
|
||||
|
||||
const postsAPI = module.exports;
|
||||
|
||||
postsAPI.get = async function (caller, data) {
|
||||
const [userPrivileges, post, voted] = await Promise.all([
|
||||
privileges.posts.get([data.pid], caller.uid),
|
||||
posts.getPostData(data.pid),
|
||||
posts.hasVoted(data.pid, caller.uid),
|
||||
]);
|
||||
if (!post) {
|
||||
return null;
|
||||
}
|
||||
Object.assign(post, voted);
|
||||
|
||||
const userPrivilege = userPrivileges[0];
|
||||
if (!userPrivilege.read || !userPrivilege['topics:read']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
post.ip = userPrivilege.isAdminOrMod ? post.ip : undefined;
|
||||
const selfPost = caller.uid && caller.uid === parseInt(post.uid, 10);
|
||||
if (post.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {
|
||||
post.content = '[[topic:post_is_deleted]]';
|
||||
}
|
||||
|
||||
return post;
|
||||
};
|
||||
|
||||
postsAPI.edit = async function (caller, data) {
|
||||
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
|
||||
@@ -5,13 +5,13 @@ const nconf = require('nconf');
|
||||
|
||||
const meta = require('../meta');
|
||||
const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
const topics = require('../topics');
|
||||
const categories = require('../categories');
|
||||
const privileges = require('../privileges');
|
||||
const plugins = require('../plugins');
|
||||
const translator = require('../translator');
|
||||
const languages = require('../languages');
|
||||
const api = require('../api');
|
||||
|
||||
const apiController = module.exports;
|
||||
|
||||
@@ -117,29 +117,8 @@ apiController.getConfig = async function (req, res) {
|
||||
res.json(config);
|
||||
};
|
||||
|
||||
apiController.getPostData = async function (pid, uid) {
|
||||
const [userPrivileges, post, voted] = await Promise.all([
|
||||
privileges.posts.get([pid], uid),
|
||||
posts.getPostData(pid),
|
||||
posts.hasVoted(pid, uid),
|
||||
]);
|
||||
if (!post) {
|
||||
return null;
|
||||
}
|
||||
Object.assign(post, voted);
|
||||
|
||||
const userPrivilege = userPrivileges[0];
|
||||
if (!userPrivilege.read || !userPrivilege['topics:read']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
post.ip = userPrivilege.isAdminOrMod ? post.ip : undefined;
|
||||
const selfPost = uid && uid === parseInt(post.uid, 10);
|
||||
if (post.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {
|
||||
post.content = '[[topic:post_is_deleted]]';
|
||||
}
|
||||
return post;
|
||||
};
|
||||
// TODO: Deprecate these four controllers in 1.17.0
|
||||
apiController.getPostData = async (pid, uid) => api.posts.get({ uid }, { pid });
|
||||
|
||||
apiController.getTopicData = async function (tid, uid) {
|
||||
const [userPrivileges, topic] = await Promise.all([
|
||||
|
||||
@@ -8,6 +8,10 @@ const apiHelpers = require('../../api/helpers');
|
||||
|
||||
const Posts = module.exports;
|
||||
|
||||
Posts.get = async (req, res) => {
|
||||
helpers.formatApiResponse(200, res, await api.posts.get(req, { pid: req.params.pid }));
|
||||
};
|
||||
|
||||
Posts.edit = async (req, res) => {
|
||||
const editResult = await api.posts.edit(req, {
|
||||
...req.body,
|
||||
|
||||
@@ -10,6 +10,8 @@ const setupApiRoute = routeHelpers.setupApiRoute;
|
||||
module.exports = function () {
|
||||
const middlewares = [middleware.authenticate];
|
||||
|
||||
setupApiRoute(router, 'get', '/:pid', [middleware.authenticateOrGuest], controllers.write.posts.get);
|
||||
// There is no POST route because you POST to a topic to create a new post. Intuitive, no?
|
||||
setupApiRoute(router, 'put', '/:pid', [...middlewares, middleware.checkRequired.bind(null, ['content'])], controllers.write.posts.edit);
|
||||
setupApiRoute(router, 'delete', '/:pid', [...middlewares, middleware.assert.post], controllers.write.posts.purge);
|
||||
|
||||
|
||||
@@ -10,8 +10,7 @@ const categories = require('../categories');
|
||||
const user = require('../user');
|
||||
const socketHelpers = require('./helpers');
|
||||
const utils = require('../utils');
|
||||
|
||||
const apiController = require('../controllers/api');
|
||||
const api = require('../api');
|
||||
|
||||
const sockets = require('.');
|
||||
const SocketPosts = module.exports;
|
||||
@@ -97,7 +96,8 @@ SocketPosts.getPostSummaryByIndex = async function (socket, data) {
|
||||
};
|
||||
|
||||
SocketPosts.getPost = async function (socket, pid) {
|
||||
return await apiController.getPostData(pid, socket.uid);
|
||||
sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid');
|
||||
return await api.posts.get(socket, { pid });
|
||||
};
|
||||
|
||||
SocketPosts.loadMoreBookmarks = async function (socket, data) {
|
||||
|
||||
Reference in New Issue
Block a user