Skip to content

Node.js based forum software built for the modern web

exposing thread tools to users in the "administrators" set, fixing up Topics methods to call the "editable" method first instead of just checking rep thresholds.

Julian Lam1a38644eb824
diff --git a/src/posts.js b/src/posts.js
index 85103b827f..ab87b5ba63 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -15,7 +15,7 @@ marked.setOptions({
Posts.get = function(callback, tid, current_user, start, end) {
if (start == null) start = 0;
- if (end == null) end = start + 10;
+ if (end == null) end = -1;//start + 10;
var post_data, user_data, thread_data, vote_data, viewer_data;
@@ -30,8 +30,11 @@ marked.setOptions({
var posts = [],
main_posts = [],
- manage_content = viewer_data.reputation >= config.privilege_thresholds.manage_content;
-
+ manage_content = (
+ viewer_data.reputation >= config.privilege_thresholds.manage_content ||
+ viewer_data.isModerator ||
+ viewer_data.isAdministrator
+ );
for (var i=0, ii= post_data.pid.length; i<ii; i++) {
var uid = post_data.uid[i],
@@ -70,7 +73,7 @@ marked.setOptions({
'deleted': parseInt(thread_data.deleted) || 0,
'pinned': parseInt(thread_data.pinned) || 0,
'topic_id': tid,
- 'expose_tools': (manage_content || viewer_data.isModerator) ? 1 : 0,
+ 'expose_tools': manage_content ? 1 : 0,
'posts': posts,
'main_posts': main_posts
});
@@ -174,8 +177,15 @@ marked.setOptions({
callback(null);
});
})
+ },
+ function(callback) {
+ user.isAdministrator(current_user, function(isAdmin) {
+ viewer_data = viewer_data || {};
+ viewer_data.isAdministrator = isAdmin;
+ callback(null);
+ });
}
- ], function(err, results) {
+ ], function(err) {
generateThread();
});
}
@@ -201,6 +211,10 @@ marked.setOptions({
});
});
});
+ }, function(next) {
+ user.isAdministrator(uid, function(err, isAdmin) {
+ next(null, isAdmin);
+ });
}
], function(err, results) {
// If any return true, allow the edit
diff --git a/src/topics.js b/src/topics.js
index 3413d1631d..37be5dbcb3 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -2,7 +2,7 @@ var RDB = require('./redis.js'),
posts = require('./posts.js'),
utils = require('./utils.js'),
user = require('./user.js'),
- configs = require('../config.js'),
+ config = require('../config.js'),
categories = require('./categories.js'),
marked = require('marked'),
async = require('async');
@@ -169,6 +169,30 @@ marked.setOptions({
});
}
+ Topics.editable = function(tid, uid, callback) {
+ async.parallel([
+ function(next) {
+ user.getUserField(uid, 'reputation', function(reputation) {
+ next(null, reputation >= config.privilege_thresholds.manage_thread);
+ });
+ },
+ function(next) {
+ Topics.get_cid_by_tid(tid, function(cid) {
+ user.isModerator(uid, cid, function(isMod) {
+ next(null, isMod);
+ });
+ });
+ }, function(next) {
+ user.isAdministrator(uid, function(isAdmin) {
+ next(null, isAdmin);
+ });
+ }
+ ], function(err, results) {
+ // If any return true, allow the edit
+ if (results.indexOf(true) !== -1) callback(true);
+ });
+ }
+
Topics.get_topic = function(tid, uid, callback) {
var topicData = {};
@@ -223,7 +247,7 @@ marked.setOptions({
}
Topics.get_cid_by_tid = function(tid, callback) {
- RDB.get('tid:' + pid + ':cid', function(err, cid) {
+ RDB.get('tid:' + tid + ':cid', function(err, cid) {
if (cid && parseInt(cid) > 0) callback(cid);
else callback(false);
});
@@ -359,8 +383,8 @@ marked.setOptions({
};
Topics.lock = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as locked
RDB.set('tid:' + tid + ':locked', 1);
@@ -375,8 +399,8 @@ marked.setOptions({
}
Topics.unlock = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as unlocked
RDB.del('tid:' + tid + ':locked');
@@ -391,8 +415,8 @@ marked.setOptions({
}
Topics.delete = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as deleted
RDB.set('tid:' + tid + ':deleted', 1);
Topics.lock(tid, uid);
@@ -408,8 +432,8 @@ marked.setOptions({
}
Topics.restore = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as restored
RDB.del('tid:' + tid + ':deleted');
Topics.unlock(tid, uid);
@@ -425,8 +449,8 @@ marked.setOptions({
}
Topics.pin = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as pinned
RDB.set('tid:' + tid + ':pinned', 1);
@@ -441,8 +465,8 @@ marked.setOptions({
}
Topics.unpin = function(tid, uid, socket) {
- user.getUserField(uid, 'reputation', function(rep) {
- if (rep >= configs.privilege_thresholds.manage_thread) {
+ Topics.editable(tid, uid, function(editable) {
+ if (editable) {
// Mark thread as unpinned
RDB.del('tid:' + tid + ':pinned');
diff --git a/src/user.js b/src/user.js
index 399f0ebb08..37d4577d3a 100644
--- a/src/user.js
+++ b/src/user.js
@@ -264,7 +264,7 @@ var config = require('../config.js'),
User.exists(username, function(exists) {
RDB.incr('global:next_user_id', function(err, uid) {
- RDB.handle(err);
+ RDB.handfle(err);
var gravatar = User.createGravatarURLFromEmail(email);
@@ -524,7 +524,13 @@ var config = require('../config.js'),
User.isModerator = function(uid, cid, callback) {
RDB.sismember('cid:' + cid + ':moderators', uid, function(err, exists) {
- callback(exists);
+ callback(!!exists);
+ });
+ }
+
+ User.isAdministrator = function(uid, callback) {
+ RDB.sismember('administrators', uid, function(err, exists) {
+ callback(!!exists);
});
}