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'),
|
|
|
|
|
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'),
|
2013-11-26 19:09:32 -05:00
|
|
|
meta = require('./meta'),
|
2013-09-10 12:34:48 -04:00
|
|
|
Feed = require('./feed');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
(function(PostTools) {
|
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-01-07 17:30:29 -05:00
|
|
|
callback(null, parseInt(pids[0], 10) === parseInt(pid, 10));
|
2013-11-29 23:14:28 -05:00
|
|
|
});
|
2013-06-05 13:34:44 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
PostTools.privileges = function(pid, uid, callback) {
|
2014-01-14 19:01:12 -05:00
|
|
|
async.parallel({
|
|
|
|
|
topicPrivs: function(next) {
|
|
|
|
|
posts.getPostField(pid, 'tid', function(err, tid) {
|
|
|
|
|
threadTools.privileges(tid, uid, next);
|
2013-12-08 10:49:42 -05:00
|
|
|
});
|
2014-01-14 19:01:12 -05:00
|
|
|
},
|
|
|
|
|
isOwner: function(next) {
|
|
|
|
|
posts.getPostField(pid, 'uid', function(err, author) {
|
|
|
|
|
next(null, parseInt(author, 10) === parseInt(uid, 10));
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
hasEnoughRep: function(next) {
|
|
|
|
|
if (parseInt(meta.config['privileges:disabled'], 10)) {
|
|
|
|
|
return next(null, false);
|
|
|
|
|
} else {
|
|
|
|
|
user.getUserField(uid, 'reputation', function(err, reputation) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(null, false);
|
|
|
|
|
}
|
|
|
|
|
next(null, parseInt(reputation, 10) >= parseInt(meta.config['privileges:manage_content'], 10));
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-12-08 10:49:42 -05:00
|
|
|
}
|
2014-01-14 19:01:12 -05:00
|
|
|
// [getThreadPrivileges, isOwnPost, hasEnoughRep]
|
|
|
|
|
}, function(err, results) {
|
2014-01-16 22:06:23 -05:00
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, {
|
2014-01-14 19:01:12 -05:00
|
|
|
read: results.topicPrivs.read,
|
|
|
|
|
editable: results.topicPrivs.editable || results.isOwner || results.hasEnoughRep,
|
2014-01-19 17:28:08 -05:00
|
|
|
view_deleted: results.topicPrivs.view_deleted || results.isOwner || results.hasEnoughRep,
|
|
|
|
|
move: results.topicPrivs.admin || results.topicPrivs.moderator
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 13:42:07 -04:00
|
|
|
|
2013-09-17 13:37:03 -04:00
|
|
|
PostTools.edit = function(uid, pid, title, content) {
|
2014-01-09 20:13:17 -05:00
|
|
|
var websockets = require('./socket.io'),
|
|
|
|
|
success = function() {
|
|
|
|
|
posts.setPostFields(pid, {
|
|
|
|
|
edited: Date.now(),
|
|
|
|
|
editor: uid,
|
|
|
|
|
content: content
|
|
|
|
|
});
|
2013-11-01 13:04:15 -04:00
|
|
|
|
2014-01-09 20:13:17 -05:00
|
|
|
events.logPostEdit(uid, pid);
|
2013-12-23 13:59:12 -05:00
|
|
|
|
2014-01-09 20:13:17 -05:00
|
|
|
db.searchRemove('post', pid, function() {
|
|
|
|
|
db.searchIndex('post', content, pid);
|
|
|
|
|
});
|
2013-12-23 13:59:12 -05:00
|
|
|
|
2014-01-09 20:13:17 -05:00
|
|
|
async.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
posts.getPostField(pid, 'tid', function(err, tid) {
|
|
|
|
|
PostTools.isMain(pid, tid, function(err, isMainPost) {
|
|
|
|
|
if (isMainPost) {
|
|
|
|
|
title = title.trim();
|
|
|
|
|
var slug = tid + '/' + utils.slugify(title);
|
|
|
|
|
|
|
|
|
|
topics.setTopicField(tid, 'title', title);
|
|
|
|
|
topics.setTopicField(tid, 'slug', slug);
|
|
|
|
|
|
|
|
|
|
db.searchRemove('topic', tid, function() {
|
|
|
|
|
db.searchIndex('topic', title, tid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(null, {
|
|
|
|
|
tid: tid,
|
|
|
|
|
isMainPost: isMainPost
|
2013-08-27 12:14:27 -04:00
|
|
|
});
|
2013-09-17 13:09:37 -04:00
|
|
|
});
|
2013-08-08 11:40:31 -04:00
|
|
|
});
|
2014-01-09 20:13:17 -05:00
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
PostTools.parse(content, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err, results) {
|
|
|
|
|
websockets.in('topic_' + results[0].tid).emit('event:post_edited', {
|
|
|
|
|
pid: pid,
|
|
|
|
|
title: validator.sanitize(title).escape(),
|
|
|
|
|
isMainPost: results[0].isMainPost,
|
|
|
|
|
content: results[1]
|
2013-05-24 11:18:28 -04:00
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2014-01-09 20:13:17 -05:00
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2014-01-16 22:06:23 -05:00
|
|
|
PostTools.privileges(pid, uid, function(err, privileges) {
|
2013-05-24 11:18:28 -04:00
|
|
|
if (privileges.editable) {
|
2013-09-29 20:33:35 -04:00
|
|
|
plugins.fireHook('filter:post.save', content, function(err, parsedContent) {
|
|
|
|
|
if (!err) content = parsedContent;
|
2013-07-28 02:24:41 -04:00
|
|
|
success();
|
|
|
|
|
});
|
2013-05-24 11:18:28 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 13:04:28 -04:00
|
|
|
PostTools.delete = function(uid, pid, callback) {
|
2013-07-20 16:59:53 -04:00
|
|
|
var success = function() {
|
2013-07-03 13:08:32 -04:00
|
|
|
posts.setPostField(pid, 'deleted', 1);
|
2013-12-02 21:20:55 -05:00
|
|
|
db.decrObjectField('global', 'postCount');
|
2013-12-05 20:06:36 -05:00
|
|
|
db.searchRemove('post', pid);
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-12-21 19:42:07 -05:00
|
|
|
events.logPostDelete(uid, pid);
|
|
|
|
|
|
2013-11-15 14:57:50 -05:00
|
|
|
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
|
2014-01-03 21:33:41 -05:00
|
|
|
topics.decreasePostCount(postData.tid);
|
2013-07-19 10:59:24 -04:00
|
|
|
|
2013-08-12 14:32:56 -04:00
|
|
|
user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) {
|
2013-12-02 21:20:55 -05:00
|
|
|
db.sortedSetAdd('users:postcount', postcount, postData.uid);
|
2013-08-12 14:32:56 -04:00
|
|
|
});
|
2013-08-23 13:14:36 -04:00
|
|
|
|
2013-12-10 15:33:35 -05:00
|
|
|
topics.getTopicField(postData.tid, 'cid', function(err, cid) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-15 15:25:31 -04:00
|
|
|
// Delete the thread if it is the last undeleted post
|
2013-09-22 13:33:05 -04:00
|
|
|
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
|
2013-07-15 15:25:31 -04:00
|
|
|
if (err && err.message === 'no-undeleted-pids-found') {
|
2014-01-01 14:15:53 -05:00
|
|
|
threadTools.delete(postData.tid, uid, function(err) {
|
2013-11-24 13:58:06 -05:00
|
|
|
if (err) {
|
|
|
|
|
winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack);
|
|
|
|
|
}
|
2013-07-15 15:25:31 -04:00
|
|
|
});
|
2013-07-18 15:14:06 -04:00
|
|
|
} else {
|
2013-11-15 14:57:50 -05:00
|
|
|
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
|
2013-08-23 13:14:36 -04:00
|
|
|
topics.updateTimestamp(postData.tid, timestamp);
|
|
|
|
|
});
|
2013-07-15 15:25:31 -04:00
|
|
|
}
|
|
|
|
|
});
|
2013-09-10 12:34:48 -04:00
|
|
|
|
|
|
|
|
Feed.updateTopic(postData.tid);
|
2013-11-22 11:42:42 -05:00
|
|
|
Feed.updateRecent();
|
2013-10-16 13:04:28 -04:00
|
|
|
|
2013-11-24 13:58:06 -05:00
|
|
|
callback(null);
|
2013-07-03 13:08:32 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-11-24 13:58:06 -05:00
|
|
|
posts.getPostField(pid, 'deleted', function(err, deleted) {
|
2013-12-05 13:11:27 -05:00
|
|
|
if(parseInt(deleted, 10) === 1) {
|
2013-11-25 17:20:44 -05:00
|
|
|
return callback(new Error('Post already deleted!'));
|
2013-05-24 11:18:28 -04:00
|
|
|
}
|
2013-11-25 17:20:44 -05:00
|
|
|
|
2014-01-16 22:06:23 -05:00
|
|
|
PostTools.privileges(pid, uid, function(err, privileges) {
|
2013-11-24 13:58:06 -05:00
|
|
|
if (privileges.editable) {
|
|
|
|
|
success();
|
|
|
|
|
}
|
2013-11-25 17:20:44 -05:00
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2013-11-24 13:58:06 -05:00
|
|
|
|
2013-05-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
2013-10-16 13:04:28 -04:00
|
|
|
PostTools.restore = function(uid, pid, callback) {
|
2013-07-20 16:59:53 -04:00
|
|
|
var success = function() {
|
2013-09-17 13:09:37 -04:00
|
|
|
posts.setPostField(pid, 'deleted', 0);
|
2013-12-02 21:20:55 -05:00
|
|
|
db.incrObjectField('global', 'postCount');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-12-21 19:42:07 -05:00
|
|
|
events.logPostRestore(uid, pid);
|
|
|
|
|
|
2013-11-15 14:57:50 -05:00
|
|
|
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
|
2014-01-03 21:33:41 -05:00
|
|
|
topics.increasePostCount(postData.tid);
|
2013-07-19 10:59:24 -04:00
|
|
|
|
2013-09-17 13:09:37 -04:00
|
|
|
user.incrementUserFieldBy(postData.uid, 'postcount', 1);
|
2013-07-19 10:59:24 -04:00
|
|
|
|
2013-09-22 13:33:05 -04:00
|
|
|
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
|
2013-11-15 14:57:50 -05:00
|
|
|
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
|
2013-09-17 13:09:37 -04:00
|
|
|
topics.updateTimestamp(postData.tid, timestamp);
|
2013-12-10 15:33:35 -05:00
|
|
|
|
|
|
|
|
topics.getTopicField(postData.tid, 'cid', function(err, cid) {
|
|
|
|
|
if(!err) {
|
|
|
|
|
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-08-23 13:14:36 -04:00
|
|
|
});
|
2013-09-17 13:09:37 -04:00
|
|
|
});
|
2013-08-03 20:54:16 -04:00
|
|
|
|
2014-01-22 23:58:21 -05:00
|
|
|
Feed.updateTopic(postData.tid);
|
|
|
|
|
Feed.updateRecent();
|
|
|
|
|
|
|
|
|
|
db.searchIndex('post', postData.content, pid);
|
|
|
|
|
|
2013-09-17 13:09:37 -04:00
|
|
|
// Restore topic if it is the only post
|
|
|
|
|
topics.getTopicField(postData.tid, 'postcount', function(err, count) {
|
2013-12-05 13:11:27 -05:00
|
|
|
if (parseInt(count, 10) === 1) {
|
2014-01-22 23:58:21 -05:00
|
|
|
threadTools.restore(postData.tid, uid, function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
winston.err(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-09-17 13:09:37 -04:00
|
|
|
}
|
|
|
|
|
});
|
2013-08-28 11:00:34 -04:00
|
|
|
|
2013-10-16 13:04:28 -04:00
|
|
|
callback();
|
2013-09-17 13:09:37 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-11-24 13:58:06 -05:00
|
|
|
posts.getPostField(pid, 'deleted', function(err, deleted) {
|
2013-12-05 13:11:27 -05:00
|
|
|
if(parseInt(deleted, 10) === 0) {
|
2013-11-24 13:58:06 -05:00
|
|
|
return callback(new Error('Post already restored'));
|
2013-11-25 17:20:44 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-16 22:06:23 -05:00
|
|
|
PostTools.privileges(pid, uid, function(err, privileges) {
|
2013-11-24 13:58:06 -05:00
|
|
|
if (privileges.editable) {
|
|
|
|
|
success();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 13:54:21 -04:00
|
|
|
PostTools.parse = function(raw, callback) {
|
2013-08-30 15:07:58 -04:00
|
|
|
raw = raw || '';
|
2013-07-23 03:07:27 +08:00
|
|
|
|
2013-12-01 21:04:10 -05:00
|
|
|
plugins.fireHook('filter:post.parse', raw, function(err, parsed) {
|
2013-09-29 20:33:35 -04:00
|
|
|
callback(null, !err ? parsed : raw);
|
2013-08-27 12:50:15 -04:00
|
|
|
});
|
2013-07-22 12:44:50 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-01 19:59:17 -05:00
|
|
|
PostTools.parseSignature = function(raw, callback) {
|
|
|
|
|
raw = raw || '';
|
2013-06-26 12:37:35 -04:00
|
|
|
|
2013-12-01 19:59:17 -05:00
|
|
|
plugins.fireHook('filter:post.parseSignature', raw, function(err, parsedSignature) {
|
|
|
|
|
callback(null, !err ? parsedSignature : raw);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-12-01 20:28:16 -05:00
|
|
|
}(exports));
|