Files
NodeBB/src/postTools.js

246 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-03-01 19:15:18 -05:00
'use strict';
2013-12-21 19:42:07 -05:00
var winston = require('winston'),
async = require('async'),
nconf = require('nconf'),
validator = require('validator'),
db = require('./database'),
2013-11-26 19:09:32 -05:00
posts = require('./posts'),
topics = require('./topics'),
2013-11-26 19:09:32 -05:00
threadTools = require('./threadTools'),
privileges = require('./privileges'),
2013-11-26 19:09:32 -05:00
user = require('./user'),
utils = require('../public/src/utils'),
plugins = require('./plugins'),
2013-12-21 19:42:07 -05:00
events = require('./events'),
2014-02-09 20:34:11 +00:00
meta = require('./meta');
(function(PostTools) {
2014-03-01 19:15:18 -05:00
PostTools.isMain = function(pid, tid, callback) {
2014-06-07 11:38:43 -04:00
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));
2013-11-29 23:14:28 -05:00
});
2014-03-01 19:15:18 -05:00
};
2014-03-01 19:15:18 -05:00
PostTools.edit = function(uid, pid, title, content, options, callback) {
2014-03-01 19:18:15 -05:00
options = options || {};
2014-02-20 02:05:49 -05:00
2014-03-01 19:15:18 -05:00
function success(postData) {
posts.setPostFields(pid, {
edited: Date.now(),
editor: uid,
content: postData.content
});
2014-03-01 19:15:18 -05:00
events.logPostEdit(uid, pid);
2013-12-23 13:59:12 -05:00
2014-03-01 19:15:18 -05:00
async.parallel({
topic: function(next) {
var tid = postData.tid;
PostTools.isMain(pid, tid, function(err, isMainPost) {
if (err) {
return next(err);
}
if (isMainPost) {
title = title.trim();
var slug = tid + '/' + utils.slugify(title);
2014-03-01 19:15:18 -05:00
topics.setTopicField(tid, 'title', title);
topics.setTopicField(tid, 'slug', slug);
2014-02-21 23:15:25 -05:00
2014-03-01 19:15:18 -05:00
topics.setTopicField(tid, 'thumb', options.topic_thumb);
topics.updateTags(tid, options.tags);
2014-03-01 19:15:18 -05:00
plugins.fireHook('action:topic.edit', tid);
}
2014-03-01 19:15:18 -05:00
plugins.fireHook('action:post.edit', postData);
2014-02-19 21:06:30 -05:00
2014-03-01 19:15:18 -05:00
next(null, {
tid: tid,
title: validator.escape(title),
isMainPost: isMainPost
2013-08-08 11:40:31 -04:00
});
});
2014-03-01 19:15:18 -05:00
},
content: function(next) {
PostTools.parse(postData.content, next);
}
}, callback);
}
privileges.posts.canEdit(pid, uid, function(err, canEdit) {
if (err || !canEdit) {
2014-04-09 22:26:23 -04:00
return callback(err || new Error('[[error:no-privileges]]'));
}
2014-03-01 19:15:18 -05:00
posts.getPostData(pid, function(err, postData) {
if (err) {
return callback(err);
}
postData.content = content;
plugins.fireHook('filter:post.save', postData, function(err, postData) {
if (err) {
return callback(err);
}
success(postData);
});
});
});
2014-03-01 19:15:18 -05: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);
};
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
}
privileges.posts.canEdit(pid, uid, next);
2014-04-02 16:54:57 -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) {
if(err) {
return callback(err);
}
2013-12-21 19:42:07 -05:00
2014-04-02 16:54:57 -04:00
posts.setPostField(pid, 'deleted', isDelete ? 1 : 0, function(err) {
if (err) {
return callback(err);
}
2014-04-02 16:54:57 -04:00
events[isDelete ? 'logPostDelete' : 'logPostRestore'](uid, pid);
2013-08-23 13:14:36 -04:00
2014-04-02 16:54:57 -04:00
db.incrObjectFieldBy('global', 'postCount', isDelete ? -1 : 1);
2014-05-27 14:53:47 -04:00
posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content'], function(err, postData) {
2014-04-02 16:54:57 -04:00
if (err) {
return callback(err);
2014-02-25 14:03:47 -05:00
}
2014-04-02 16:54:57 -04:00
if (isDelete) {
plugins.fireHook('action:post.delete', pid);
2013-07-18 15:14:06 -04:00
} else {
2014-04-02 16:54:57 -04:00
plugins.fireHook('action:post.restore', postData);
}
2014-04-02 16:54:57 -04:00
async.parallel([
function(next) {
topics[isDelete ? 'decreasePostCount' : 'increasePostCount'](postData.tid, next);
},
function(next) {
user.incrementUserPostCountBy(postData.uid, isDelete ? -1 : 1, next);
},
function(next) {
updateTopicTimestamp(postData.tid, next);
},
function(next) {
addOrRemoveFromCategoryRecentPosts(pid, postData.tid, isDelete, next);
}
2014-05-27 14:53:47 -04:00
], function(err) {
if (!isDelete) {
PostTools.parse(postData.content, function(err, parsed) {
postData.content = parsed;
callback(err, postData);
});
return;
}
callback(err, postData);
});
2014-04-02 16:54:57 -04:00
});
});
2014-04-02 16:54:57 -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]]'));
}
posts.delete(pid, callback);
});
};
2014-04-02 16:54:57 -04:00
function updateTopicTimestamp(tid, callback) {
2014-04-24 20:05:05 -04:00
topics.getLatestUndeletedPid(tid, function(err, pid) {
2014-04-02 16:54:57 -04:00
if(err || !pid) {
return callback(err);
}
2014-04-02 16:54:57 -04:00
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if (err) {
return callback(err);
}
2013-08-28 11:00:34 -04:00
2014-04-02 16:54:57 -04:00
if (timestamp) {
topics.updateTimestamp(tid, timestamp);
}
2013-10-16 13:04:28 -04:00
callback();
2013-09-17 13:09:37 -04:00
});
2014-04-02 16:54:57 -04:00
});
}
2014-04-02 16:54:57 -04:00
function addOrRemoveFromCategoryRecentPosts(pid, tid, isDelete, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
2014-04-02 16:54:57 -04:00
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if (err) {
return callback(err);
}
if (isDelete) {
db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, callback);
} else {
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid, callback);
}
});
});
2014-04-02 16:54:57 -04:00
}
2013-09-22 13:54:21 -04:00
PostTools.parse = function(raw, callback) {
2014-04-04 12:55:44 -04:00
parse('filter:post.parse', raw, callback);
2014-03-01 19:15:18 -05:00
};
PostTools.parseSignature = function(raw, callback) {
2014-04-04 12:55:44 -04:00
parse('filter:post.parseSignature', raw, callback);
};
function parse(hook, raw, callback) {
raw = raw || '';
2014-04-04 12:55:44 -04:00
plugins.fireHook(hook, raw, function(err, parsed) {
callback(null, !err ? parsed : raw);
});
2014-04-04 12:55:44 -04:00
}
2014-03-01 19:15:18 -05:00
}(exports));