diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml index 5a4d77c94e..7866cc8e5e 100644 --- a/public/openapi/write.yaml +++ b/public/openapi/write.yaml @@ -144,6 +144,8 @@ paths: $ref: 'write/topics/tid/events/eventId.yaml' /posts/{pid}: $ref: 'write/posts/pid.yaml' + /posts/{pid}/index: + $ref: 'write/posts/pid/index.yaml' /posts/{pid}/raw: $ref: 'write/posts/pid/raw.yaml' /posts/{pid}/summary: diff --git a/public/openapi/write/posts/pid/index.yaml b/public/openapi/write/posts/pid/index.yaml new file mode 100644 index 0000000000..fed5ae66c6 --- /dev/null +++ b/public/openapi/write/posts/pid/index.yaml @@ -0,0 +1,28 @@ +get: + tags: + - posts + summary: get post index + description: This operation retrieves a post's index relative to its topic + parameters: + - in: path + name: pid + schema: + type: string + required: true + description: a valid post id + example: 2 + responses: + '200': + description: Post index successfully retrieved. + content: + application/json: + schema: + type: object + properties: + status: + $ref: ../../../components/schemas/Status.yaml#/Status + response: + type: object + properties: + index: + type: number diff --git a/src/api/posts.js b/src/api/posts.js index 68e28fb32a..4bba610aed 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -41,6 +41,16 @@ postsAPI.get = async function (caller, data) { return post; }; +postsAPI.getIndex = async (caller, { pid, sort }) => { + const tid = await posts.getPostField(pid, 'tid'); + const topicPrivileges = await privileges.topics.get(tid, caller.uid); + if (!topicPrivileges.read || !topicPrivileges['topics:read']) { + return null; + } + + return await posts.getPidIndex(pid, tid, sort); +}; + postsAPI.getSummary = async (caller, { pid }) => { const tid = await posts.getPostField(pid, 'tid'); const topicPrivileges = await privileges.topics.get(tid, caller.uid); diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js index 12c816e7c3..d2a2cb9b9d 100644 --- a/src/controllers/write/posts.js +++ b/src/controllers/write/posts.js @@ -44,6 +44,18 @@ Posts.get = async (req, res) => { helpers.formatApiResponse(200, res, post); }; +Posts.getIndex = async (req, res) => { + const { pid } = req.params; + const { sort } = req.body; + + const index = await api.posts.getIndex(req, { pid, sort }); + if (index === null) { + return helpers.formatApiResponse(404, res, new Error('[[error:no-post]]')); + } + + helpers.formatApiResponse(200, res, { index }); +}; + Posts.getSummary = async (req, res) => { const post = await api.posts.getSummary(req, { pid: req.params.pid }); if (!post) { diff --git a/src/routes/write/posts.js b/src/routes/write/posts.js index 634ba23cdd..ed6bd62ad8 100644 --- a/src/routes/write/posts.js +++ b/src/routes/write/posts.js @@ -15,6 +15,7 @@ module.exports = function () { setupApiRoute(router, 'put', '/:pid', [middleware.ensureLoggedIn, middleware.checkRequired.bind(null, ['content'])], controllers.write.posts.edit); setupApiRoute(router, 'delete', '/:pid', middlewares, controllers.write.posts.purge); + setupApiRoute(router, 'get', '/:pid/index', [middleware.assert.post], controllers.write.posts.getIndex); setupApiRoute(router, 'get', '/:pid/raw', [middleware.assert.post], controllers.write.posts.getRaw); setupApiRoute(router, 'get', '/:pid/summary', [middleware.assert.post], controllers.write.posts.getSummary); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index ceb85d9ca4..b297bb9579 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -28,6 +28,8 @@ SocketPosts.getRawPost = async function (socket, pid) { }; SocketPosts.getPostSummaryByIndex = async function (socket, data) { + sockets.warnDeprecated(socket, 'GET /api/v3/posts/byIndex/:index/summary?tid=:tid'); + if (data.index < 0) { data.index = 0; } @@ -42,14 +44,7 @@ SocketPosts.getPostSummaryByIndex = async function (socket, data) { return 0; } - const topicPrivileges = await privileges.topics.get(data.tid, socket.uid); - if (!topicPrivileges['topics:read']) { - throw new Error('[[error:no-privileges]]'); - } - - const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false }); - posts.modifyPostByPrivilege(postsData[0], topicPrivileges); - return postsData[0]; + return await api.posts.getSummary(socket, { pid }); }; SocketPosts.getPostTimestampByIndex = async function (socket, data) { @@ -83,10 +78,16 @@ SocketPosts.getCategory = async function (socket, pid) { }; SocketPosts.getPidIndex = async function (socket, data) { + sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/index'); + if (!data) { throw new Error('[[error:invalid-data]]'); } - return await posts.getPidIndex(data.pid, data.tid, data.topicPostSort); + + return await api.posts.getIndex(socket, { + pid: data.pid, + sort: data.topicPostSort, + }); }; SocketPosts.getReplies = async function (socket, pid) { diff --git a/test/posts.js b/test/posts.js index e7d046c48c..bec4590adb 100644 --- a/test/posts.js +++ b/test/posts.js @@ -879,35 +879,20 @@ describe('Post\'s', () => { }); }); - it('should error with invalid data', (done) => { - socketPosts.getPidIndex({ uid: voterUid }, null, (err) => { - assert.equal(err.message, '[[error:invalid-data]]'); - done(); - }); - }); - - it('should get pid index', (done) => { - socketPosts.getPidIndex({ uid: voterUid }, { pid: pid, tid: topicData.tid, topicPostSort: 'oldest_to_newest' }, (err, index) => { - assert.ifError(err); - assert.equal(index, 4); - done(); - }); + it('should get pid index', async () => { + const index = await apiPosts.getIndex({ uid: voterUid }, { pid: pid, sort: 'oldest_to_newest' }); + assert.strictEqual(index, 4); }); - it('should get pid index in reverse', (done) => { - topics.reply({ + it('should get pid index in reverse', async () => { + const postData = await topics.reply({ uid: voterUid, tid: topicData.tid, content: 'raw content', - }, (err, postData) => { - assert.ifError(err); - - socketPosts.getPidIndex({ uid: voterUid }, { pid: postData.pid, tid: topicData.tid, topicPostSort: 'newest_to_oldest' }, (err, index) => { - assert.ifError(err); - assert.equal(index, 1); - done(); - }); }); + + const index = await apiPosts.getIndex({ uid: voterUid }, { pid: postData.pid, sort: 'newest_to_oldest' }); + assert.equal(index, 1); }); });