refactored topic locking pinning and deleting (and its inverses) so that the privilege check is done not in the method, but in the socket call

This commit is contained in:
Julian Lam
2013-11-25 17:20:44 -05:00
parent 561ee9e4f1
commit b2fb9aa99f
3 changed files with 129 additions and 130 deletions

View File

@@ -142,7 +142,7 @@ var RDB = require('./redis.js'),
// Delete the thread if it is the last undeleted post // Delete the thread if it is the last undeleted post
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) { threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
if (err && err.message === 'no-undeleted-pids-found') { if (err && err.message === 'no-undeleted-pids-found') {
threadTools.delete(postData.tid, -1, function(err) { threadTools.delete(postData.tid, function(err) {
if (err) { if (err) {
winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack); winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack);
} }
@@ -163,14 +163,14 @@ var RDB = require('./redis.js'),
posts.getPostField(pid, 'deleted', function(err, deleted) { posts.getPostField(pid, 'deleted', function(err, deleted) {
if(deleted === '1') { if(deleted === '1') {
return callback(new Error('Post already deleted!')); return callback(new Error('Post already deleted!'));
} }
PostTools.privileges(pid, uid, function(privileges) { PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) { if (privileges.editable) {
success(); success();
} }
}); });
}); });
} }
@@ -210,8 +210,8 @@ var RDB = require('./redis.js'),
posts.getPostField(pid, 'deleted', function(err, deleted) { posts.getPostField(pid, 'deleted', function(err, deleted) {
if(deleted === '0') { if(deleted === '0') {
return callback(new Error('Post already restored')); return callback(new Error('Post already restored'));
} }
PostTools.privileges(pid, uid, function(privileges) { PostTools.privileges(pid, uid, function(privileges) {
if (privileges.editable) { if (privileges.editable) {
success(); success();

View File

@@ -47,136 +47,111 @@ var RDB = require('./redis.js'),
}); });
} }
ThreadTools.lock = function(tid, uid, socket) { ThreadTools.lock = function(tid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.setTopicField(tid, 'locked', 1);
if (privileges.editable) {
topics.setTopicField(tid, 'locked', 1);
if (socket) { if (socket) {
io.sockets. in ('topic_' + tid).emit('event:topic_locked', { io.sockets.in('topic_' + tid).emit('event:topic_locked', {
tid: tid, tid: tid,
status: 'ok' status: 'ok'
}); });
socket.emit('api:topic.lock', { socket.emit('api:topic.lock', {
status: 'ok', status: 'ok',
tid: tid tid: tid
}); });
} }
}
});
} }
ThreadTools.unlock = function(tid, uid, socket) { ThreadTools.unlock = function(tid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.setTopicField(tid, 'locked', 0);
if (privileges.editable) {
topics.setTopicField(tid, 'locked', 0);
if (socket) { if (socket) {
io.sockets. in ('topic_' + tid).emit('event:topic_unlocked', { io.sockets.in('topic_' + tid).emit('event:topic_unlocked', {
tid: tid, tid: tid,
status: 'ok' status: 'ok'
}); });
socket.emit('api:topic.unlock', { socket.emit('api:topic.unlock', {
status: 'ok', status: 'ok',
tid: tid tid: tid
}); });
} }
}
});
} }
ThreadTools.delete = function(tid, uid, callback) { ThreadTools.delete = function(tid, callback) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.delete(tid);
if (privileges.editable || uid === -1) {
topics.delete(tid); RDB.decr('totaltopiccount');
RDB.decr('totaltopiccount'); ThreadTools.lock(tid);
ThreadTools.lock(tid, uid); topicSearch.remove(tid);
topicSearch.remove(tid); io.sockets.in('topic_' + tid).emit('event:topic_deleted', {
tid: tid,
io.sockets. in ('topic_' + tid).emit('event:topic_deleted', { status: 'ok'
tid: tid,
status: 'ok'
});
callback(null);
} else callback(new Error('not-enough-privs'));
}); });
if (callback) {
callback(null);
}
} }
ThreadTools.restore = function(tid, uid, socket, callback) { ThreadTools.restore = function(tid, socket, callback) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.restore(tid);
if (privileges.editable) { RDB.incr('totaltopiccount');
ThreadTools.unlock(tid);
topics.restore(tid); io.sockets.in('topic_' + tid).emit('event:topic_restored', {
RDB.incr('totaltopiccount'); tid: tid,
ThreadTools.unlock(tid, uid); status: 'ok'
io.sockets. in ('topic_' + tid).emit('event:topic_restored', {
tid: tid,
status: 'ok'
});
topics.getTopicField(tid, 'title', function(err, title) {
topicSearch.index(title, tid);
});
if(callback)
callback(null);
}
}); });
topics.getTopicField(tid, 'title', function(err, title) {
topicSearch.index(title, tid);
});
if(callback) {
callback(null);
}
} }
ThreadTools.pin = function(tid, uid, socket) { ThreadTools.pin = function(tid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.setTopicField(tid, 'pinned', 1);
if (privileges.editable) { topics.getTopicField(tid, 'cid', function(err, cid) {
RDB.zadd('categories:' + cid + ':tid', Math.pow(2, 53), tid);
topics.setTopicField(tid, 'pinned', 1);
topics.getTopicField(tid, 'cid', function(err, cid) {
RDB.zadd('categories:' + cid + ':tid', Math.pow(2, 53), tid);
});
if (socket) {
io.sockets. in ('topic_' + tid).emit('event:topic_pinned', {
tid: tid,
status: 'ok'
});
socket.emit('api:topic.pin', {
status: 'ok',
tid: tid
});
}
}
}); });
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_pinned', {
tid: tid,
status: 'ok'
});
socket.emit('api:topic.pin', {
status: 'ok',
tid: tid
});
}
} }
ThreadTools.unpin = function(tid, uid, socket) { ThreadTools.unpin = function(tid, socket) {
ThreadTools.privileges(tid, uid, function(privileges) { topics.setTopicField(tid, 'pinned', 0);
if (privileges.editable) { topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
RDB.zadd('categories:' + topicData.cid + ':tid', topicData.lastposttime, tid);
topics.setTopicField(tid, 'pinned', 0);
topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
RDB.zadd('categories:' + topicData.cid + ':tid', topicData.lastposttime, tid);
});
if (socket) {
io.sockets. in ('topic_' + tid).emit('event:topic_unpinned', {
tid: tid,
status: 'ok'
});
socket.emit('api:topic.unpin', {
status: 'ok',
tid: tid
});
}
}
}); });
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_unpinned', {
tid: tid,
status: 'ok'
});
socket.emit('api:topic.unpin', {
status: 'ok',
tid: tid
});
}
} }
ThreadTools.move = function(tid, cid, socket) { ThreadTools.move = function(tid, cid, socket) {
@@ -213,7 +188,7 @@ var RDB = require('./redis.js'),
status: 'ok' status: 'ok'
}); });
io.sockets. in ('topic_' + tid).emit('event:topic_moved', { io.sockets.in('topic_' + tid).emit('event:topic_moved', {
tid: tid tid: tid
}); });
} else { } else {

View File

@@ -491,42 +491,66 @@ module.exports.init = function(io) {
}); });
socket.on('api:topic.delete', function(data) { socket.on('api:topic.delete', function(data) {
threadTools.delete(data.tid, uid, function(err) { threadTools.privileges(data.tid, uid, function(privileges) {
if (!err) { if (privileges.editable) {
posts.getTopicPostStats(); threadTools.delete(data.tid, function(err) {
socket.emit('api:topic.delete', { if (!err) {
status: 'ok', posts.getTopicPostStats();
tid: data.tid socket.emit('api:topic.delete', {
status: 'ok',
tid: data.tid
});
}
}); });
} }
}); });
}); });
socket.on('api:topic.restore', function(data) { socket.on('api:topic.restore', function(data) {
threadTools.restore(data.tid, uid, socket, function(err) { threadTools.privileges(data.tid, uid, function(privileges) {
posts.getTopicPostStats(); if (privileges.editable) {
threadTools.restore(data.tid, socket, function(err) {
posts.getTopicPostStats();
socket.emit('api:topic.restore', { socket.emit('api:topic.restore', {
status: 'ok', status: 'ok',
tid: data.tid tid: data.tid
}); });
});
}
}); });
}); });
socket.on('api:topic.lock', function(data) { socket.on('api:topic.lock', function(data) {
threadTools.lock(data.tid, uid, socket); threadTools.privileges(data.tid, uid, function(privileges) {
if (privileges.editable) {
threadTools.lock(data.tid, socket);
}
});
}); });
socket.on('api:topic.unlock', function(data) { socket.on('api:topic.unlock', function(data) {
threadTools.unlock(data.tid, uid, socket); threadTools.privileges(data.tid, uid, function(privileges) {
if (privileges.editable) {
threadTools.unlock(data.tid, socket);
}
});
}); });
socket.on('api:topic.pin', function(data) { socket.on('api:topic.pin', function(data) {
threadTools.pin(data.tid, uid, socket); threadTools.privileges(data.tid, uid, function(privileges) {
if (privileges.editable) {
threadTools.pin(data.tid, socket);
}
});
}); });
socket.on('api:topic.unpin', function(data) { socket.on('api:topic.unpin', function(data) {
threadTools.unpin(data.tid, uid, socket); threadTools.privileges(data.tid, uid, function(privileges) {
if (privileges.editable) {
threadTools.unpin(data.tid, socket);
}
});
}); });
socket.on('api:topic.move', function(data) { socket.on('api:topic.move', function(data) {