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

120 lines
2.9 KiB
JavaScript

var RDB = require('./redis.js'),
posts = require('./posts.js'),
topics = require('./topics'),
threadTools = require('./threadTools.js'),
user = require('./user.js'),
async = require('async'),
marked = require('marked'),
utils = require('../public/src/utils');
marked.setOptions({
breaks: true
});
(function(PostTools) {
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);
})
}
PostTools.privileges = function(pid, uid, callback) {
//todo: break early if one condition is true
function getThreadPrivileges(next) {
12 years ago
posts.getPostField(pid, 'tid', function(tid) {
12 years ago
threadTools.privileges(tid, uid, function(privileges) {
next(null, privileges);
});
12 years ago
});
}
function isOwnPost(next) {
12 years ago
posts.getPostField(pid, 'uid', function(author) {
if (author && parseInt(author) > 0) {
next(null, author === uid);
}
});
}
function hasEnoughRep(next) {
user.getUserField(uid, 'reputation', function(reputation) {
next(null, reputation >= global.config['privileges:manage_content']);
});
}
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)
});
});
}
PostTools.edit = function(uid, pid, title, content) {
12 years ago
var success = function() {
posts.setPostField(pid, 'content', content);
posts.setPostField(pid, 'edited', Date.now());
posts.setPostField(pid, 'editor', uid);
12 years ago
posts.getPostField(pid, 'tid', function(tid) {
PostTools.isMain(pid, tid, function(isMainPost) {
if (isMainPost)
topics.setTopicField(tid, 'title', title);
io.sockets.in('topic_' + tid).emit('event:post_edited', {
pid: pid,
title: title,
content: marked(content || '')
});
});
});
};
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
success();
}
});
}
PostTools.delete = function(uid, pid) {
var success = function() {
posts.setPostField(pid, 'deleted', 1);
12 years ago
posts.getPostField(pid, 'tid', function(tid) {
io.sockets.in('topic_' + tid).emit('event:post_deleted', {
pid: pid
});
});
};
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
success();
}
});
}
PostTools.restore = function(uid, pid) {
var success = function() {
12 years ago
posts.setPostField(pid, 'deleted', 0);
12 years ago
posts.getPostField(pid, 'tid', function(tid) {
io.sockets.in('topic_' + tid).emit('event:post_restored', {
pid: pid
});
12 years ago
});
};
PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) {
success();
}
});
}
}(exports));