diff --git a/src/api/posts.js b/src/api/posts.js index ca77e1ce33..c01b377340 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -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]]'); diff --git a/src/controllers/api.js b/src/controllers/api.js index ac6313152f..f6748dfb81 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -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([ diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js index 1a73cb7b1b..7988439a1f 100644 --- a/src/controllers/write/posts.js +++ b/src/controllers/write/posts.js @@ -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, diff --git a/src/routes/write/posts.js b/src/routes/write/posts.js index f445873a1a..837f8def8c 100644 --- a/src/routes/write/posts.js +++ b/src/routes/write/posts.js @@ -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); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index c8e30962f4..e9fc770338 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -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) {