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

273 lines
6.8 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'),
user = require('./user'),
utils = require('../public/src/utils'),
plugins = require('./plugins'),
11 years ago
events = require('./events'),
meta = require('./meta');
(function(PostTools) {
11 years ago
PostTools.isMain = function(pid, tid, callback) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
if(err) {
return callback(err);
}
11 years ago
if(!Array.isArray(pids) || !pids.length) {
callback(null, false);
}
callback(null, parseInt(pids[0], 10) === parseInt(pid, 10));
});
11 years ago
};
PostTools.privileges = function(pid, uid, callback) {
11 years ago
async.parallel({
topicPrivs: function(next) {
posts.getPostField(pid, 'tid', function(err, tid) {
threadTools.privileges(tid, uid, next);
});
11 years ago
},
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));
});
}
}
11 years ago
}, function(err, results) {
11 years ago
if(err) {
return callback(err);
}
callback(null, {
11 years ago
read: results.topicPrivs.read,
editable: results.topicPrivs.editable || results.isOwner || results.hasEnoughRep,
view_deleted: results.topicPrivs.view_deleted || results.isOwner || results.hasEnoughRep,
move: results.topicPrivs.admin || results.topicPrivs.moderator
});
});
11 years ago
};
12 years ago
11 years ago
PostTools.edit = function(uid, pid, title, content, options, callback) {
11 years ago
options = options || {};
11 years ago
function success(postData) {
posts.setPostFields(pid, {
edited: Date.now(),
editor: uid,
content: postData.content
});
11 years ago
events.logPostEdit(uid, pid);
11 years ago
11 years ago
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);
11 years ago
topics.setTopicField(tid, 'title', title);
topics.setTopicField(tid, 'slug', slug);
11 years ago
topics.setTopicField(tid, 'thumb', options.topic_thumb);
11 years ago
plugins.fireHook('action:topic.edit', tid);
}
11 years ago
plugins.fireHook('action:post.edit', postData);
11 years ago
11 years ago
next(null, {
tid: tid,
title: validator.escape(title),
isMainPost: isMainPost
});
});
11 years ago
},
content: function(next) {
PostTools.parse(postData.content, next);
}
}, callback);
}
11 years ago
PostTools.privileges(pid, uid, function(err, privileges) {
11 years ago
if (err || !privileges.editable) {
return callback(err || new Error('not-privileges-to-edit'));
}
11 years ago
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);
});
});
});
11 years ago
};
11 years ago
PostTools.delete = function(uid, pid, callback) {
var success = function() {
posts.setPostField(pid, 'deleted', 1);
11 years ago
db.decrObjectField('global', 'postCount');
11 years ago
plugins.fireHook('action:post.delete', pid);
11 years ago
events.logPostDelete(uid, pid);
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
topics.decreasePostCount(postData.tid);
user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) {
11 years ago
db.sortedSetAdd('users:postcount', postcount, postData.uid);
});
topics.getTopicField(postData.tid, 'cid', function(err, cid) {
if(!err) {
db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid);
}
});
// Delete the thread if it is the last undeleted post
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
if(err) {
return winston.error(err.message);
}
if (!pid) {
threadTools.delete(postData.tid, uid, function(err) {
if (err) {
winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack);
}
});
12 years ago
} else {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
topics.updateTimestamp(postData.tid, timestamp);
});
}
});
callback(null);
});
};
posts.getPostField(pid, 'deleted', function(err, deleted) {
11 years ago
if(parseInt(deleted, 10) === 1) {
return callback(new Error('Post already deleted!'));
}
11 years ago
PostTools.privileges(pid, uid, function(err, privileges) {
if (privileges.editable) {
success();
}
});
});
11 years ago
};
11 years ago
PostTools.restore = function(uid, pid, callback) {
var success = function() {
posts.setPostField(pid, 'deleted', 0);
11 years ago
db.incrObjectField('global', 'postCount');
11 years ago
events.logPostRestore(uid, pid);
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
topics.increasePostCount(postData.tid);
user.incrementUserFieldBy(postData.uid, 'postcount', 1);
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
topics.updateTimestamp(postData.tid, timestamp);
topics.getTopicField(postData.tid, 'cid', function(err, cid) {
if(!err) {
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
}
});
});
});
11 years ago
11 years ago
plugins.fireHook('action:post.restore', postData);
11 years ago
// Restore topic if it is the only post
topics.getTopicField(postData.tid, 'postcount', function(err, count) {
11 years ago
if (parseInt(count, 10) === 1) {
11 years ago
threadTools.restore(postData.tid, uid, function(err) {
if(err) {
winston.err(err);
}
});
}
});
11 years ago
callback();
});
};
posts.getPostField(pid, 'deleted', function(err, deleted) {
11 years ago
if(parseInt(deleted, 10) === 0) {
return callback(new Error('Post already restored'));
}
11 years ago
PostTools.privileges(pid, uid, function(err, privileges) {
if (privileges.editable) {
success();
}
});
});
11 years ago
};
PostTools.parse = function(raw, callback) {
12 years ago
raw = raw || '';
plugins.fireHook('filter:post.parse', raw, function(err, parsed) {
callback(null, !err ? parsed : raw);
});
11 years ago
};
PostTools.parseSignature = function(raw, callback) {
raw = raw || '';
plugins.fireHook('filter:post.parseSignature', raw, function(err, parsedSignature) {
callback(null, !err ? parsedSignature : raw);
});
11 years ago
};
}(exports));