mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: server-side work for #9047
- rename Thumbs.commit to Thumbs.migrate - new PUT method that calls Thumbs.migrate - `checkThumbPrivileges` now takes a single object parameter (ins. of req/res)
This commit is contained in:
@@ -37,7 +37,7 @@ postsAPI.edit = async function (caller, data) {
|
|||||||
|
|
||||||
const editResult = await posts.edit(data);
|
const editResult = await posts.edit(data);
|
||||||
if (editResult.topic.isMainPost) {
|
if (editResult.topic.isMainPost) {
|
||||||
await topics.thumbs.commit(data.uuid, editResult.topic.tid);
|
await topics.thumbs.migrate(data.uuid, editResult.topic.tid);
|
||||||
}
|
}
|
||||||
if (editResult.topic.renamed) {
|
if (editResult.topic.renamed) {
|
||||||
await events.log({
|
await events.log({
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ topicsAPI.create = async function (caller, data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await topics.post(payload);
|
const result = await topics.post(payload);
|
||||||
await topics.thumbs.commit(data.uuid, result.topicData.tid);
|
await topics.thumbs.migrate(data.uuid, result.topicData.tid);
|
||||||
|
|
||||||
socketHelpers.emitToUids('event:new_post', { posts: [result.postData] }, [caller.uid]);
|
socketHelpers.emitToUids('event:new_post', { posts: [result.postData] }, [caller.uid]);
|
||||||
socketHelpers.emitToUids('event:new_topic', result.topicData, [caller.uid]);
|
socketHelpers.emitToUids('event:new_topic', result.topicData, [caller.uid]);
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ Topics.getThumbs = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.addThumb = async (req, res) => {
|
Topics.addThumb = async (req, res) => {
|
||||||
await checkThumbPrivileges(req, res);
|
await checkThumbPrivileges({ tid: req.params.tid, uid: req.user.uid, res });
|
||||||
if (res.headersSent) {
|
if (res.headersSent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -117,8 +117,21 @@ Topics.addThumb = async (req, res) => {
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Topics.migrateThumbs = async (req, res) => {
|
||||||
|
await Promise.all([
|
||||||
|
checkThumbPrivileges({ tid: req.params.tid, uid: req.user.uid, res }),
|
||||||
|
checkThumbPrivileges({ tid: req.body.tid, uid: req.user.uid, res }),
|
||||||
|
]);
|
||||||
|
if (res.headersSent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await topics.thumbs.migrate(req.params.tid, req.body.tid);
|
||||||
|
helpers.formatApiResponse(200, res);
|
||||||
|
};
|
||||||
|
|
||||||
Topics.deleteThumb = async (req, res) => {
|
Topics.deleteThumb = async (req, res) => {
|
||||||
await checkThumbPrivileges(req, res);
|
await checkThumbPrivileges({ tid: req.params.tid, uid: req.user.uid, res });
|
||||||
if (res.headersSent) {
|
if (res.headersSent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -127,17 +140,17 @@ Topics.deleteThumb = async (req, res) => {
|
|||||||
helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));
|
helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));
|
||||||
};
|
};
|
||||||
|
|
||||||
async function checkThumbPrivileges(req, res) {
|
async function checkThumbPrivileges({ tid, uid, res }) {
|
||||||
// req.params.tid could be either a tid (pushing a new thumb to an existing topic) or a post UUID (a new topic being composed)
|
// req.params.tid could be either a tid (pushing a new thumb to an existing topic) or a post UUID (a new topic being composed)
|
||||||
const isUUID = validator.isUUID(req.params.tid);
|
const isUUID = validator.isUUID(tid);
|
||||||
|
|
||||||
// Sanity-check the tid if it's strictly not a uuid
|
// Sanity-check the tid if it's strictly not a uuid
|
||||||
if (!isUUID && (isNaN(parseInt(req.params.tid, 10)) || !await topics.exists(req.params.tid))) {
|
if (!isUUID && (isNaN(parseInt(tid, 10)) || !await topics.exists(tid))) {
|
||||||
return helpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
|
return helpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// While drafts are not protected, tids are
|
// While drafts are not protected, tids are
|
||||||
if (!isUUID && !await privileges.topics.canEdit(req.params.tid, req.user.uid)) {
|
if (!isUUID && !await privileges.topics.canEdit(tid, uid)) {
|
||||||
return helpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
|
return helpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ module.exports = function () {
|
|||||||
|
|
||||||
setupApiRoute(router, 'get', '/:tid/thumbs', [], controllers.write.topics.getThumbs);
|
setupApiRoute(router, 'get', '/:tid/thumbs', [], controllers.write.topics.getThumbs);
|
||||||
setupApiRoute(router, 'post', '/:tid/thumbs', [multipartMiddleware, middleware.validateFiles, ...middlewares], controllers.write.topics.addThumb);
|
setupApiRoute(router, 'post', '/:tid/thumbs', [multipartMiddleware, middleware.validateFiles, ...middlewares], controllers.write.topics.addThumb);
|
||||||
|
setupApiRoute(router, 'put', '/:tid/thumbs', [], controllers.write.topics.migrateThumbs);
|
||||||
setupApiRoute(router, 'delete', '/:tid/thumbs', [...middlewares, middleware.assert.path], controllers.write.topics.deleteThumb);
|
setupApiRoute(router, 'delete', '/:tid/thumbs', [...middlewares, middleware.assert.path], controllers.write.topics.deleteThumb);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ Thumbs.associate = async function (id, path) {
|
|||||||
db.sortedSetAdd(set, numThumbs, path);
|
db.sortedSetAdd(set, numThumbs, path);
|
||||||
};
|
};
|
||||||
|
|
||||||
Thumbs.commit = async function (uuid, tid) {
|
Thumbs.migrate = async function (uuid, id) {
|
||||||
// Converts the draft thumb zset to the topic zset (combines thumbs if applicable)
|
// Converts the draft thumb zset to the topic zset (combines thumbs if applicable)
|
||||||
const set = `draft:${uuid}:thumbs`;
|
const set = `draft:${uuid}:thumbs`;
|
||||||
const thumbs = await db.getSortedSetRange(set, 0, -1);
|
const thumbs = await db.getSortedSetRange(set, 0, -1);
|
||||||
await Promise.all(thumbs.map(async path => await Thumbs.associate(tid, path)));
|
await Promise.all(thumbs.map(async path => await Thumbs.associate(id, path)));
|
||||||
await db.delete(set);
|
await db.delete(set);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user