refactor: deprecate socket method posts.getPidIndex

This commit is contained in:
Julian Lam
2023-04-13 16:23:26 -04:00
parent d814e281a0
commit ee9f53f1ff
7 changed files with 71 additions and 32 deletions

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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) {