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/postTools.js

224 lines
5.3 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var winston = require('winston'),
async = require('async'),
nconf = require('nconf'),
validator = require('validator'),
db = require('./database'),
11 years ago
posts = require('./posts'),
topics = require('./topics'),
11 years ago
threadTools = require('./threadTools'),
privileges = require('./privileges'),
11 years ago
user = require('./user'),
utils = require('../public/src/utils'),
plugins = require('./plugins'),
11 years ago
events = require('./events'),
meta = require('./meta'),
LRU = require('lru-cache');
var cache = LRU({
max: 1048576,
length: function (n) { return n.length; },
maxAge: 1000 * 60 * 60
});
(function(PostTools) {
11 years ago
PostTools.edit = function(data, callback) {
var options = data.options || {},
title = data.title.trim();
async.waterfall([
function (next) {
privileges.posts.canEdit(data.pid, data.uid, next);
},
function(canEdit, next) {
if (!canEdit) {
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostData(data.pid, next);
},
function(postData, next) {
postData.content = data.content;
10 years ago
plugins.fireHook('filter:post.edit', {post: postData, uid: data.uid}, next);
}
10 years ago
], function(err, result) {
if (err) {
return callback(err);
}
10 years ago
var postData = result.post;
11 years ago
async.parallel({
post: function(next) {
10 years ago
var d = {
edited: Date.now(),
editor: data.uid,
content: postData.content
10 years ago
};
if (data.handle) {
d.handle = data.handle;
}
posts.setPostFields(data.pid, d, next);
},
11 years ago
topic: function(next) {
var tid = postData.tid;
async.parallel({
cid: function(next) {
topics.getTopicField(tid, 'cid', next);
},
isMain: function(next) {
posts.isMain(data.pid, next);
}
}, function(err, results) {
11 years ago
if (err) {
return next(err);
}
11 years ago
11 years ago
options.tags = options.tags || [];
if (!results.isMain) {
11 years ago
return next(null, {
tid: tid,
cid: results.cid,
11 years ago
isMainPost: false
});
11 years ago
}
11 years ago
var topicData = {
10 years ago
tid: tid,
cid: results.cid,
uid: postData.uid,
mainPid: data.pid,
11 years ago
title: title,
slug: tid + '/' + utils.slugify(title)
};
if (options.topic_thumb) {
topicData.thumb = options.topic_thumb;
11 years ago
}
11 years ago
db.setObject('topic:' + tid, topicData, function(err) {
10 years ago
plugins.fireHook('action:topic.edit', topicData);
});
11 years ago
11 years ago
topics.updateTags(tid, options.tags, function(err) {
if (err) {
return next(err);
}
topics.getTopicTagsObjects(tid, function(err, tags) {
next(err, {
tid: tid,
cid: results.cid,
uid: postData.uid,
11 years ago
title: validator.escape(title),
isMainPost: results.isMain,
11 years ago
tags: tags
});
});
});
});
11 years ago
},
postData: function(next) {
cache.del(postData.pid);
PostTools.parsePost(postData, next);
11 years ago
}
}, function(err, results) {
if (err) {
return callback(err);
}
postData.cid = results.topic.cid;
results.content = results.postData.content;
plugins.fireHook('action:post.edit', postData);
callback(null, results);
});
});
11 years ago
};
11 years ago
PostTools.delete = function(uid, pid, callback) {
11 years ago
togglePostDelete(uid, pid, true, callback);
};
11 years ago
11 years ago
PostTools.restore = function(uid, pid, callback) {
togglePostDelete(uid, pid, false, callback);
};
11 years ago
function togglePostDelete(uid, pid, isDelete, callback) {
async.waterfall([
function(next) {
posts.getPostField(pid, 'deleted', next);
},
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);
}
PostTools.parsePost(postData, callback);
11 years ago
});
}
11 years ago
});
}
11 years ago
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]]'));
}
cache.del(pid);
11 years ago
posts.purge(pid, callback);
11 years ago
});
};
PostTools.parsePost = function(postData, callback) {
postData.content = postData.content || '';
var cachedContent = cache.get(postData.pid);
if (cachedContent) {
postData.content = cachedContent;
return callback(null, postData);
}
plugins.fireHook('filter:parse.post', {postData: postData}, function(err, data) {
if (err) {
return callback(err);
}
cache.set(data.postData.pid, data.postData.content);
callback(null, data.postData);
});
11 years ago
};
PostTools.parseSignature = function(userData, uid, callback) {
userData.signature = userData.signature || '';
plugins.fireHook('filter:parse.signature', {userData: userData, uid: uid}, callback);
};
11 years ago
PostTools.resetCache = function() {
cache.reset();
};
}(exports));