refactor(api): post move to write API

This commit is contained in:
Julian Lam
2021-01-18 15:31:14 -05:00
parent 0fa4c11ea6
commit 966c4117ec
9 changed files with 93 additions and 43 deletions

View File

@@ -13,6 +13,7 @@ const events = require('../events');
const privileges = require('../privileges');
const apiHelpers = require('./helpers');
const websockets = require('../socket.io');
const socketHelpers = require('../socket.io/helpers');
const postsAPI = module.exports;
@@ -195,6 +196,27 @@ async function isMainAndLastPost(pid) {
};
}
postsAPI.move = async function (caller, data) {
const canMove = await Promise.all([
privileges.topics.isAdminOrMod(data.tid, caller.uid),
privileges.posts.canMove(data.pid, caller.uid),
]);
if (!canMove.every(Boolean)) {
throw new Error('[[error:no-privileges]]');
}
await topics.movePostToTopic(caller.uid, data.pid, data.tid);
const [postDeleted, topicDeleted] = await Promise.all([
posts.getPostField(data.pid, 'deleted'),
topics.getTopicField(data.tid, 'deleted'),
]);
if (!postDeleted && !topicDeleted) {
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, 'move', 'notifications:moved_your_post');
}
};
postsAPI.upvote = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
};