2014-03-01 19:15:18 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-02-23 13:08:38 +02:00
|
|
|
var async = require('async');
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2016-02-23 13:08:38 +02:00
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
var cache = require('./cache');
|
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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.tools.delete = function (uid, pid, callback) {
|
2014-04-02 16:54:57 -04:00
|
|
|
togglePostDelete(uid, pid, true, callback);
|
|
|
|
|
};
|
2014-02-19 21:06:30 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.tools.restore = function (uid, pid, callback) {
|
2014-04-02 16:54:57 -04:00
|
|
|
togglePostDelete(uid, pid, false, callback);
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2014-04-02 16:54:57 -04:00
|
|
|
function togglePostDelete(uid, pid, isDelete, callback) {
|
|
|
|
|
async.waterfall([
|
2015-10-02 16:44:48 -04:00
|
|
|
function (next) {
|
|
|
|
|
Posts.exists(pid, next);
|
|
|
|
|
},
|
|
|
|
|
function (exists, next) {
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return next(new Error('[[error:no-post]]'));
|
|
|
|
|
}
|
2015-09-17 14:58:58 -04:00
|
|
|
Posts.getPostField(pid, 'deleted', next);
|
2014-04-02 16:54:57 -04:00
|
|
|
},
|
2015-10-02 16:44:48 -04:00
|
|
|
function (deleted, next) {
|
2015-09-17 14:58:58 -04:00
|
|
|
if (parseInt(deleted, 10) === 1 && isDelete) {
|
2014-04-09 22:26:23 -04:00
|
|
|
return next(new Error('[[error:post-already-deleted]]'));
|
2017-02-18 01:52:56 -07:00
|
|
|
} else if (parseInt(deleted, 10) !== 1 && !isDelete) {
|
2014-04-09 22:26:23 -04:00
|
|
|
return next(new Error('[[error:post-already-restored]]'));
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
2014-05-14 17:53:23 -04:00
|
|
|
|
2016-06-28 12:34:09 +03:00
|
|
|
privileges.posts.canDelete(pid, uid, next);
|
2014-04-02 16:54:57 -04:00
|
|
|
},
|
2016-06-28 12:34:09 +03:00
|
|
|
function (canDelete, next) {
|
2016-08-10 23:55:49 +03:00
|
|
|
if (!canDelete.flag) {
|
|
|
|
|
return next(new Error(canDelete.message));
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2016-02-23 13:08:38 +02:00
|
|
|
if (isDelete) {
|
|
|
|
|
cache.del(pid);
|
|
|
|
|
Posts.delete(pid, uid, next);
|
|
|
|
|
} else {
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.restore(pid, uid, function (err, postData) {
|
2016-02-23 13:08:38 +02:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
Posts.parsePost(postData, next);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-02-23 13:08:38 +02:00
|
|
|
], callback);
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.tools.purge = function (uid, pid, callback) {
|
2015-09-16 08:35:40 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
privileges.posts.canPurge(pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canPurge, next) {
|
|
|
|
|
if (!canPurge) {
|
2015-09-16 08:36:51 -04:00
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
2015-09-16 08:35:40 -04:00
|
|
|
}
|
|
|
|
|
cache.del(pid);
|
2016-02-23 13:08:38 +02:00
|
|
|
Posts.purge(pid, uid, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-09-16 08:35:40 -04:00
|
|
|
], callback);
|
2014-06-10 14:24:50 -04:00
|
|
|
};
|
|
|
|
|
|
2015-09-17 14:58:58 -04:00
|
|
|
};
|
2015-03-12 15:01:50 -04:00
|
|
|
|