2014-03-01 19:15:18 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-07-17 19:05:55 -04:00
|
|
|
const privileges = require('../privileges');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Posts) {
|
2015-09-17 14:58:58 -04:00
|
|
|
Posts.tools = {};
|
2014-03-01 19:15:18 -05:00
|
|
|
|
2019-07-17 19:05:55 -04:00
|
|
|
Posts.tools.delete = async function (uid, pid) {
|
|
|
|
|
return await togglePostDelete(uid, pid, true);
|
2014-04-02 16:54:57 -04:00
|
|
|
};
|
2014-02-19 21:06:30 -05:00
|
|
|
|
2019-07-17 19:05:55 -04:00
|
|
|
Posts.tools.restore = async function (uid, pid) {
|
|
|
|
|
return await togglePostDelete(uid, pid, false);
|
2014-04-02 16:54:57 -04:00
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2019-07-17 19:05:55 -04:00
|
|
|
async function togglePostDelete(uid, pid, isDelete) {
|
|
|
|
|
const [postData, canDelete] = await Promise.all([
|
|
|
|
|
Posts.getPostData(pid),
|
|
|
|
|
privileges.posts.canDelete(pid, uid),
|
|
|
|
|
]);
|
|
|
|
|
if (!postData) {
|
|
|
|
|
throw new Error('[[error:no-post]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (postData.deleted && isDelete) {
|
|
|
|
|
throw new Error('[[error:post-already-deleted]]');
|
|
|
|
|
} else if (!postData.deleted && !isDelete) {
|
|
|
|
|
throw new Error('[[error:post-already-restored]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!canDelete.flag) {
|
|
|
|
|
throw new Error(canDelete.message);
|
|
|
|
|
}
|
|
|
|
|
let post;
|
|
|
|
|
if (isDelete) {
|
|
|
|
|
require('./cache').del(pid);
|
|
|
|
|
post = await Posts.delete(pid, uid);
|
|
|
|
|
} else {
|
|
|
|
|
post = await Posts.restore(pid, uid);
|
|
|
|
|
post = await Posts.parsePost(post);
|
|
|
|
|
}
|
|
|
|
|
return post;
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2019-07-17 19:05:55 -04:00
|
|
|
Posts.tools.purge = async function (uid, pid) {
|
|
|
|
|
const canPurge = await privileges.posts.canPurge(pid, uid);
|
|
|
|
|
if (!canPurge) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
require('./cache').del(pid);
|
|
|
|
|
await Posts.purge(pid, uid);
|
2014-06-10 14:24:50 -04:00
|
|
|
};
|
2015-09-17 14:58:58 -04:00
|
|
|
};
|