2014-03-01 19:15:18 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-04-20 17:56:43 -04:00
|
|
|
var async = require('async'),
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2013-11-26 19:09:32 -05:00
|
|
|
posts = require('./posts'),
|
2014-05-14 17:53:23 -04:00
|
|
|
privileges = require('./privileges'),
|
2015-04-20 17:56:43 -04:00
|
|
|
cache = require('./posts/cache');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
(function(PostTools) {
|
2014-03-01 19:15:18 -05:00
|
|
|
|
2014-12-31 16:27:35 -05:00
|
|
|
PostTools.edit = function(data, callback) {
|
2015-04-20 17:56:43 -04:00
|
|
|
posts.edit(data, callback);
|
2014-03-01 19:15:18 -05:00
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-10-16 13:04:28 -04:00
|
|
|
PostTools.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
|
|
|
|
2014-04-02 16:54:57 -04:00
|
|
|
PostTools.restore = function(uid, pid, callback) {
|
|
|
|
|
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([
|
|
|
|
|
function(next) {
|
|
|
|
|
posts.getPostField(pid, 'deleted', next);
|
|
|
|
|
},
|
|
|
|
|
function(deleted, next) {
|
|
|
|
|
if(parseInt(deleted, 10) === 1 && isDelete) {
|
2014-04-09 22:26:23 -04:00
|
|
|
return next(new Error('[[error:post-already-deleted]]'));
|
2014-04-02 16:54:57 -04: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
|
|
|
|
|
|
|
|
privileges.posts.canEdit(pid, uid, next);
|
2014-04-02 16:54:57 -04:00
|
|
|
},
|
2014-05-14 17:53:23 -04:00
|
|
|
function(canEdit, next) {
|
|
|
|
|
if (!canEdit) {
|
2014-04-09 22:26:23 -04:00
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
], function(err) {
|
2014-11-05 18:59:20 -05:00
|
|
|
if (err) {
|
2014-04-02 16:54:57 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2014-10-14 22:51:48 -04:00
|
|
|
if (isDelete) {
|
2015-03-13 15:04:24 -04:00
|
|
|
cache.del(pid);
|
2014-10-14 22:51:48 -04:00
|
|
|
posts.delete(pid, callback);
|
|
|
|
|
} else {
|
|
|
|
|
posts.restore(pid, function(err, postData) {
|
2014-04-02 16:54:57 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
2014-02-25 14:03:47 -05:00
|
|
|
}
|
2015-04-20 17:56:43 -04:00
|
|
|
posts.parsePost(postData, callback);
|
2014-04-02 16:54:57 -04:00
|
|
|
});
|
2014-10-14 22:51:48 -04:00
|
|
|
}
|
2014-04-02 16:54:57 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2014-06-10 14:24:50 -04:00
|
|
|
PostTools.purge = function(uid, pid, callback) {
|
|
|
|
|
privileges.posts.canEdit(pid, uid, function(err, canEdit) {
|
|
|
|
|
if (err || !canEdit) {
|
|
|
|
|
return callback(err || new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2015-03-12 15:01:50 -04:00
|
|
|
cache.del(pid);
|
2014-06-10 16:56:55 -04:00
|
|
|
posts.purge(pid, callback);
|
2014-06-10 14:24:50 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-12 15:01:50 -04:00
|
|
|
|
2013-12-01 20:28:16 -05:00
|
|
|
}(exports));
|