You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/posts/tools.js

81 lines
1.7 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
11 years ago
privileges = require('../privileges'),
cache = require('./cache');
module.exports = function(Posts) {
Posts.tools = {};
11 years ago
Posts.tools.delete = function(uid, pid, callback) {
11 years ago
togglePostDelete(uid, pid, true, callback);
};
11 years ago
Posts.tools.restore = function(uid, pid, callback) {
11 years ago
togglePostDelete(uid, pid, false, callback);
};
11 years ago
function togglePostDelete(uid, pid, isDelete, callback) {
async.waterfall([
function (next) {
Posts.exists(pid, next);
},
function (exists, next) {
if (!exists) {
return next(new Error('[[error:no-post]]'));
}
Posts.getPostField(pid, 'deleted', next);
11 years ago
},
function (deleted, next) {
if (parseInt(deleted, 10) === 1 && isDelete) {
return next(new Error('[[error:post-already-deleted]]'));
11 years ago
} else if(parseInt(deleted, 10) !== 1 && !isDelete) {
return next(new Error('[[error:post-already-restored]]'));
11 years ago
}
privileges.posts.canEdit(pid, uid, next);
11 years ago
},
function (canEdit, next) {
if (!canEdit) {
return next(new Error('[[error:no-privileges]]'));
11 years ago
}
next();
}
], function (err) {
if (err) {
11 years ago
return callback(err);
}
11 years ago
if (isDelete) {
10 years ago
cache.del(pid);
Posts.delete(pid, callback);
} else {
Posts.restore(pid, function(err, postData) {
11 years ago
if (err) {
return callback(err);
}
Posts.parsePost(postData, callback);
11 years ago
});
}
11 years ago
});
}
Posts.tools.purge = function(uid, pid, callback) {
10 years ago
async.waterfall([
function (next) {
privileges.posts.canPurge(pid, uid, next);
},
function (canPurge, next) {
if (!canPurge) {
10 years ago
return next(new Error('[[error:no-privileges]]'));
10 years ago
}
cache.del(pid);
Posts.purge(pid, next);
11 years ago
}
10 years ago
], callback);
11 years ago
};
};