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'),
|
2013-06-06 20:39:45 -04:00
|
|
|
topics = require('./topics'),
|
2013-11-26 19:09:32 -05:00
|
|
|
threadTools = require('./threadTools'),
|
2014-05-14 17:53:23 -04:00
|
|
|
privileges = require('./privileges'),
|
2013-11-26 19:09:32 -05:00
|
|
|
user = require('./user'),
|
2013-07-28 02:24:41 -04:00
|
|
|
utils = require('../public/src/utils'),
|
2013-08-03 20:54:16 -04:00
|
|
|
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');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
(function(PostTools) {
|
2014-03-01 19:15:18 -05:00
|
|
|
|
2013-06-05 13:34:44 -04:00
|
|
|
PostTools.isMain = function(pid, tid, callback) {
|
2014-01-07 17:30:29 -05:00
|
|
|
db.getSortedSetRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
|
2013-11-29 23:14:28 -05:00
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 19:15:18 -05:00
|
|
|
if(!Array.isArray(pids) || !pids.length) {
|
2014-05-02 00:57:05 +02:00
|
|
|
return callback(null, false);
|
2014-03-01 19:15:18 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-07 17:30:29 -05:00
|
|
|
callback(null, parseInt(pids[0], 10) === parseInt(pid, 10));
|
2013-11-29 23:14:28 -05:00
|
|
|
});
|
2014-03-01 19:15:18 -05:00
|
|
|
};
|
2013-05-23 12:52:16 -04: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
|
|
|
|
|
});
|
2013-11-01 13:04:15 -04:00
|
|
|
|
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-01-09 20:13:17 -05:00
|
|
|
|
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);
|
2014-01-09 20:13:17 -05:00
|
|
|
|
2014-05-21 16:13:46 -04:00
|
|
|
topics.updateTags(tid, options.tags);
|
|
|
|
|
|
2014-03-01 19:15:18 -05:00
|
|
|
plugins.fireHook('action:topic.edit', tid);
|
|
|
|
|
}
|
2014-01-09 20:13:17 -05:00
|
|
|
|
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
|
|
|
});
|
2013-05-24 11:18:28 -04:00
|
|
|
});
|
2014-03-01 19:15:18 -05:00
|
|
|
|
|
|
|
|
},
|
|
|
|
|
content: function(next) {
|
|
|
|
|
PostTools.parse(postData.content, next);
|
|
|
|
|
}
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2014-05-14 17:53:23 -04:00
|
|
|
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]]'));
|
2013-05-24 11:18:28 -04:00
|
|
|
}
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
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) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
2013-07-19 10:59:24 -04:00
|
|
|
|
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);
|
2013-12-10 15:33:35 -05:00
|
|
|
|
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);
|
2013-07-15 15:25:31 -04:00
|
|
|
}
|
2013-09-10 12:34:48 -04:00
|
|
|
|
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
|
|
|
});
|
2013-07-03 13:08:32 -04:00
|
|
|
});
|
2014-04-02 16:54:57 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
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);
|
2013-05-24 11:18:28 -04:00
|
|
|
}
|
2013-11-25 17:20:44 -05:00
|
|
|
|
2014-04-02 16:54:57 -04:00
|
|
|
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
2013-11-24 13:58:06 -05:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
}
|
2013-05-23 12:52:16 -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);
|
2013-11-25 17:20:44 -05:00
|
|
|
}
|
|
|
|
|
|
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);
|
2013-11-24 13:58:06 -05:00
|
|
|
}
|
|
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2014-04-02 16:54:57 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -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
|
|
|
};
|
2013-07-22 12:44:50 -04:00
|
|
|
|
2013-12-01 19:59:17 -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) {
|
2013-12-01 19:59:17 -05:00
|
|
|
raw = raw || '';
|
2013-06-26 12:37:35 -04:00
|
|
|
|
2014-04-04 12:55:44 -04:00
|
|
|
plugins.fireHook(hook, raw, function(err, parsed) {
|
|
|
|
|
callback(null, !err ? parsed : raw);
|
2013-12-01 19:59:17 -05:00
|
|
|
});
|
2014-04-04 12:55:44 -04:00
|
|
|
}
|
2014-03-01 19:15:18 -05:00
|
|
|
|
2013-12-01 20:28:16 -05:00
|
|
|
}(exports));
|