2013-05-23 12:52:16 -04:00
|
|
|
var RDB = require('./redis.js'),
|
|
|
|
|
posts = require('./posts.js'),
|
2013-06-06 20:39:45 -04:00
|
|
|
topics = require('./topics'),
|
2013-05-23 12:52:16 -04:00
|
|
|
threadTools = require('./threadTools.js'),
|
|
|
|
|
user = require('./user.js'),
|
|
|
|
|
async = require('async'),
|
2013-07-22 12:44:50 -04:00
|
|
|
|
2013-07-28 02:24:41 -04:00
|
|
|
utils = require('../public/src/utils'),
|
2013-08-03 20:54:16 -04:00
|
|
|
plugins = require('./plugins'),
|
|
|
|
|
reds = require('reds'),
|
2013-08-08 11:40:31 -04:00
|
|
|
postSearch = reds.createSearch('nodebbpostsearch'),
|
|
|
|
|
topicSearch = reds.createSearch('nodebbtopicsearch');
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
(function(PostTools) {
|
2013-06-05 13:34:44 -04:00
|
|
|
PostTools.isMain = function(pid, tid, callback) {
|
|
|
|
|
RDB.lrange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
|
|
|
|
|
if (pids[0] === pid) callback(true);
|
|
|
|
|
else callback(false);
|
|
|
|
|
})
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
PostTools.privileges = function(pid, uid, callback) {
|
|
|
|
|
//todo: break early if one condition is true
|
|
|
|
|
|
|
|
|
|
function getThreadPrivileges(next) {
|
2013-07-02 19:46:58 -04:00
|
|
|
posts.getPostField(pid, 'tid', function(tid) {
|
2013-07-03 13:54:47 -04:00
|
|
|
threadTools.privileges(tid, uid, function(privileges) {
|
|
|
|
|
next(null, privileges);
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2013-07-03 13:54:47 -04:00
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isOwnPost(next) {
|
2013-07-03 13:54:47 -04:00
|
|
|
posts.getPostField(pid, 'uid', function(author) {
|
|
|
|
|
if (author && parseInt(author) > 0) {
|
|
|
|
|
next(null, author === uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasEnoughRep(next) {
|
|
|
|
|
user.getUserField(uid, 'reputation', function(reputation) {
|
2013-05-27 14:02:57 -04:00
|
|
|
next(null, reputation >= global.config['privileges:manage_content']);
|
|
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([getThreadPrivileges, isOwnPost, hasEnoughRep], function(err, results) {
|
|
|
|
|
callback({
|
|
|
|
|
editable: results[0].editable || (results.slice(1).indexOf(true) !== -1 ? true : false),
|
|
|
|
|
view_deleted: results[0].view_deleted || (results.slice(1).indexOf(true) !== -1 ? true : false)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 14:29:35 -04:00
|
|
|
PostTools.edit = function(uid, pid, title, content) {
|
2013-07-03 13:54:47 -04:00
|
|
|
|
2013-05-23 12:52:16 -04:00
|
|
|
var success = function() {
|
2013-07-03 13:08:32 -04:00
|
|
|
posts.setPostField(pid, 'content', content);
|
|
|
|
|
posts.setPostField(pid, 'edited', Date.now());
|
|
|
|
|
posts.setPostField(pid, 'editor', uid);
|
|
|
|
|
|
2013-08-08 11:40:31 -04:00
|
|
|
postSearch.remove(pid, function() {
|
|
|
|
|
postSearch.index(content, pid);
|
2013-08-03 20:54:16 -04:00
|
|
|
});
|
|
|
|
|
|
2013-07-03 13:54:47 -04:00
|
|
|
posts.getPostField(pid, 'tid', function(tid) {
|
2013-07-03 13:08:32 -04:00
|
|
|
PostTools.isMain(pid, tid, function(isMainPost) {
|
2013-08-08 11:40:31 -04:00
|
|
|
if (isMainPost) {
|
2013-07-03 13:08:32 -04:00
|
|
|
topics.setTopicField(tid, 'title', title);
|
2013-08-08 11:40:31 -04:00
|
|
|
topicSearch.remove(tid, function() {
|
|
|
|
|
topicSearch.index(title, tid);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-07-03 13:08:32 -04:00
|
|
|
|
|
|
|
|
io.sockets.in('topic_' + tid).emit('event:post_edited', {
|
|
|
|
|
pid: pid,
|
|
|
|
|
title: title,
|
2013-07-22 12:58:10 -04:00
|
|
|
content: PostTools.markdownToHTML(content)
|
2013-05-24 11:18:28 -04:00
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2013-07-03 13:08:32 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
PostTools.privileges(pid, uid, function(privileges) {
|
2013-05-24 11:18:28 -04:00
|
|
|
if (privileges.editable) {
|
2013-07-28 02:24:41 -04:00
|
|
|
plugins.fireHook('filter:save_post_content', content, function(parsedContent) {
|
|
|
|
|
content = parsedContent;
|
|
|
|
|
success();
|
|
|
|
|
});
|
2013-05-24 11:18:28 -04:00
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PostTools.delete = function(uid, pid) {
|
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-07-19 10:59:24 -04:00
|
|
|
|
2013-08-08 11:40:31 -04:00
|
|
|
postSearch.remove(pid);
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-07-19 10:59:24 -04:00
|
|
|
posts.getPostFields(pid, ['tid', 'uid'], function(postData) {
|
|
|
|
|
|
2013-07-19 10:59:52 -04:00
|
|
|
user.decrementUserFieldBy(postData.uid, 'postcount', 1);
|
2013-07-19 10:59:24 -04:00
|
|
|
|
|
|
|
|
io.sockets.in('topic_' + postData.tid).emit('event:post_deleted', {
|
2013-07-03 13:08:32 -04:00
|
|
|
pid: pid
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2013-07-15 15:25:31 -04:00
|
|
|
|
|
|
|
|
// Delete the thread if it is the last undeleted post
|
2013-07-19 10:59:24 -04:00
|
|
|
threadTools.get_latest_undeleted_pid(postData.tid, function(err, pid) {
|
2013-07-15 15:25:31 -04:00
|
|
|
if (err && err.message === 'no-undeleted-pids-found') {
|
2013-07-19 10:59:24 -04:00
|
|
|
threadTools.delete(postData.tid, -1, function(err) {
|
|
|
|
|
if (err) console.log('Error: Could not delete topic (tid: ' + postData.tid + ')');
|
2013-07-15 15:25:31 -04:00
|
|
|
});
|
2013-07-18 15:14:06 -04:00
|
|
|
} else {
|
|
|
|
|
posts.getPostField(pid, 'timestamp', function(timestamp) {
|
2013-07-19 10:59:24 -04:00
|
|
|
topics.updateTimestamp(postData.tid, timestamp);
|
2013-07-18 15:14:06 -04:00
|
|
|
});
|
2013-07-15 15:25:31 -04:00
|
|
|
}
|
|
|
|
|
});
|
2013-07-03 13:08:32 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
PostTools.privileges(pid, uid, function(privileges) {
|
2013-05-24 11:18:28 -04:00
|
|
|
if (privileges.editable) {
|
|
|
|
|
success();
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PostTools.restore = function(uid, pid) {
|
2013-07-20 16:59:53 -04:00
|
|
|
var success = function() {
|
2013-07-03 13:58:04 -04:00
|
|
|
posts.setPostField(pid, 'deleted', 0);
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2013-08-03 20:54:16 -04:00
|
|
|
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(postData) {
|
2013-07-19 10:59:24 -04:00
|
|
|
|
|
|
|
|
user.incrementUserFieldBy(postData.uid, 'postcount', 1);
|
|
|
|
|
|
|
|
|
|
io.sockets.in('topic_' + postData.tid).emit('event:post_restored', {
|
2013-07-03 13:58:04 -04:00
|
|
|
pid: pid
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2013-07-20 16:59:53 -04:00
|
|
|
|
|
|
|
|
threadTools.get_latest_undeleted_pid(postData.tid, function(err, pid) {
|
|
|
|
|
posts.getPostField(pid, 'timestamp', function(timestamp) {
|
|
|
|
|
topics.updateTimestamp(postData.tid, timestamp);
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-08-03 20:54:16 -04:00
|
|
|
|
2013-08-08 11:40:31 -04:00
|
|
|
postSearch.index(postData.content, pid);
|
2013-07-03 13:58:04 -04:00
|
|
|
});
|
|
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
PostTools.privileges(pid, uid, function(privileges) {
|
2013-05-24 11:18:28 -04:00
|
|
|
if (privileges.editable) {
|
|
|
|
|
success();
|
|
|
|
|
}
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 03:34:45 +08:00
|
|
|
PostTools.markdownToHTML = function(md, isSignature) {
|
2013-07-22 12:44:50 -04:00
|
|
|
var marked = require('marked'),
|
|
|
|
|
cheerio = require('cheerio');
|
|
|
|
|
|
|
|
|
|
marked.setOptions({
|
|
|
|
|
breaks: true
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-24 13:19:36 -04:00
|
|
|
if (md && md.length > 0) {
|
2013-07-22 12:44:50 -04:00
|
|
|
var parsedContentDOM = cheerio.load(marked(md));
|
2013-07-23 03:15:17 +08:00
|
|
|
var domain = global.nconf.get('url');
|
|
|
|
|
|
2013-07-22 15:03:33 -04:00
|
|
|
parsedContentDOM('a').each(function() {
|
2013-07-23 03:15:17 +08:00
|
|
|
this.attr('rel', 'nofollow');
|
|
|
|
|
var href = this.attr('href');
|
2013-07-23 03:07:27 +08:00
|
|
|
|
2013-07-23 03:15:17 +08:00
|
|
|
if (href && !href.match(domain)) {
|
2013-08-08 15:04:22 -04:00
|
|
|
this.attr('href', domain + 'outgoing?url=' + encodeURIComponent(href));
|
2013-07-23 03:34:45 +08:00
|
|
|
if (!isSignature) this.append(' <i class="icon-external-link"></i>');
|
2013-07-23 03:15:17 +08:00
|
|
|
}
|
2013-07-22 15:03:33 -04:00
|
|
|
});
|
2013-07-23 03:15:17 +08:00
|
|
|
|
2013-07-23 03:07:27 +08:00
|
|
|
|
2013-07-22 12:44:50 -04:00
|
|
|
html = parsedContentDOM.html();
|
2013-07-23 03:07:27 +08:00
|
|
|
} else {
|
|
|
|
|
html = '<p></p>';
|
|
|
|
|
}
|
2013-07-22 12:44:50 -04:00
|
|
|
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 12:37:35 -04:00
|
|
|
|
2013-05-23 12:52:16 -04:00
|
|
|
}(exports));
|