Files
NodeBB/src/postTools.js

250 lines
6.3 KiB
JavaScript
Raw Normal View History

2013-11-26 19:09:32 -05:00
var RDB = require('./redis'),
posts = require('./posts'),
topics = require('./topics'),
2013-11-26 19:09:32 -05:00
threadTools = require('./threadTools'),
user = require('./user'),
websockets = require('./websockets'),
async = require('async'),
nconf = require('nconf'),
validator = require('validator'),
2013-08-23 13:14:36 -04:00
utils = require('../public/src/utils'),
plugins = require('./plugins'),
reds = require('reds'),
2013-08-08 11:40:31 -04:00
postSearch = reds.createSearch('nodebbpostsearch'),
2013-08-13 16:00:24 -04:00
topicSearch = reds.createSearch('nodebbtopicsearch'),
2013-08-23 13:14:36 -04:00
winston = require('winston'),
2013-11-26 19:09:32 -05:00
meta = require('./meta'),
Feed = require('./feed');
(function(PostTools) {
PostTools.isMain = function(pid, tid, callback) {
RDB.lrange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
2013-11-29 23:14:28 -05:00
if(err) {
return callback(err);
}
callback(null, pids[0] === pid);
});
}
PostTools.privileges = function(pid, uid, callback) {
2013-11-01 13:45:24 -04:00
if(uid == 0) {
callback({
editable: false,
view_deleted: false
});
return;
}
2013-08-23 13:14:36 -04:00
function getThreadPrivileges(next) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'tid', function(err, tid) {
threadTools.privileges(tid, uid, next);
2013-07-03 13:54:47 -04:00
});
}
function isOwnPost(next) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'uid', function(err, author) {
next(null, parseInt(author, 10) === parseInt(uid, 10));
2013-07-03 13:54:47 -04:00
});
}
function hasEnoughRep(next) {
2013-08-23 14:55:25 -04:00
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));
});
}
async.parallel([getThreadPrivileges, isOwnPost, hasEnoughRep], function(err, results) {
callback({
2013-10-26 19:00:34 -04:00
editable: results[0].editable || results[1] || results[2],
2013-10-25 16:02:18 -04:00
view_deleted: results[0].view_deleted || results[1] || results[2]
});
});
}
2013-09-17 13:42:07 -04:00
2013-09-17 13:37:03 -04:00
PostTools.edit = function(uid, pid, title, content) {
var success = function() {
async.waterfall([
function(next) {
posts.setPostField(pid, 'edited', Date.now());
next(null);
},
function(next) {
posts.setPostField(pid, 'editor', uid);
next(null);
},
function(next) {
posts.setPostField(pid, 'content', content);
next(null);
}
]);
2013-08-08 11:40:31 -04:00
postSearch.remove(pid, function() {
postSearch.index(content, pid);
});
async.parallel([
function(next) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'tid', function(err, tid) {
2013-11-29 23:14:28 -05:00
PostTools.isMain(pid, tid, function(err, isMainPost) {
if (isMainPost) {
topics.setTopicField(tid, 'title', title);
topicSearch.remove(tid, function() {
topicSearch.index(title, tid);
});
}
2013-09-17 13:09:37 -04:00
next(null, {
tid: tid,
isMainPost: isMainPost
});
2013-08-08 11:40:31 -04:00
});
});
},
function(next) {
2013-09-22 13:54:21 -04:00
PostTools.parse(content, next);
}
], function(err, results) {
2013-11-26 19:09:32 -05:00
websockets.in('topic_' + results[0].tid).emit('event:post_edited', {
pid: pid,
title: validator.sanitize(title).escape(),
2013-09-17 13:37:03 -04:00
isMainPost: results[0].isMainPost,
content: results[1]
});
});
};
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
plugins.fireHook('filter:post.save', content, function(err, parsedContent) {
if (!err) content = parsedContent;
success();
});
}
});
}
2013-10-16 13:04:28 -04:00
PostTools.delete = function(uid, pid, callback) {
var success = function() {
posts.setPostField(pid, 'deleted', 1);
2013-10-16 13:04:28 -04:00
RDB.decr('totalpostcount');
2013-08-08 11:40:31 -04:00
postSearch.remove(pid);
2013-11-15 14:57:50 -05:00
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
2013-09-17 13:09:37 -04:00
RDB.hincrby('topic:' + postData.tid, 'postcount', -1);
user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) {
RDB.zadd('users:postcount', postcount, postData.uid);
});
2013-08-23 13:14:36 -04:00
// Delete the thread if it is the last undeleted post
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
if (err && err.message === 'no-undeleted-pids-found') {
threadTools.delete(postData.tid, function(err) {
if (err) {
winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack);
}
});
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);
});
}
});
Feed.updateTopic(postData.tid);
2013-11-22 11:42:42 -05:00
Feed.updateRecent();
2013-10-16 13:04:28 -04:00
callback(null);
});
};
posts.getPostField(pid, 'deleted', function(err, deleted) {
if(deleted === '1') {
return callback(new Error('Post already deleted!'));
}
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
success();
}
});
});
}
2013-10-16 13:04:28 -04:00
PostTools.restore = function(uid, pid, callback) {
var success = function() {
2013-09-17 13:09:37 -04:00
posts.setPostField(pid, 'deleted', 0);
2013-10-16 13:04:28 -04:00
RDB.incr('totalpostcount');
2013-11-15 14:57:50 -05:00
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
2013-09-17 13:09:37 -04:00
RDB.hincrby('topic:' + postData.tid, 'postcount', 1);
2013-09-17 13:09:37 -04:00
user.incrementUserFieldBy(postData.uid, 'postcount', 1);
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-08-23 13:14:36 -04:00
});
2013-09-17 13:09:37 -04:00
});
2013-09-17 13:09:37 -04:00
// Restore topic if it is the only post
topics.getTopicField(postData.tid, 'postcount', function(err, count) {
if (count === '1') {
threadTools.restore(postData.tid, uid);
}
});
2013-08-28 11:00:34 -04:00
2013-09-17 13:09:37 -04:00
Feed.updateTopic(postData.tid);
2013-11-22 11:42:42 -05:00
Feed.updateRecent();
2013-09-17 13:09:37 -04:00
postSearch.index(postData.content, pid);
2013-10-16 13:04:28 -04:00
callback();
2013-09-17 13:09:37 -04:00
});
};
posts.getPostField(pid, 'deleted', function(err, deleted) {
if(deleted === '0') {
return callback(new Error('Post already restored'));
}
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
success();
}
});
});
}
2013-09-22 13:54:21 -04:00
PostTools.parse = function(raw, callback) {
2013-08-30 15:07:58 -04:00
raw = raw || '';
async.waterfall([
function(next) {
console.log('parsing');
plugins.fireHook('filter:post.parse', raw, next);
},
function(parsed, next) {
console.log('footering');
plugins.fireHook('filter:post.buildFooter', parsed, next);
}
], function(err, parsed) {
console.log('doneing');
callback(null, !err ? parsed : raw);
});
}
PostTools.parseSignature = function(raw, callback) {
raw = raw || '';
plugins.fireHook('filter:post.parseSignature', raw, function(err, parsedSignature) {
callback(null, !err ? parsedSignature : raw);
});
}
}(exports));