refactor: deprecate socket method posts.getPidIndex

isekai-main
Julian Lam 2 years ago
parent d814e281a0
commit ee9f53f1ff

@ -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:

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

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

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

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

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

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

Loading…
Cancel
Save