redis wrapper removed. pretty safe to say that we didn't really need it in the first place.

v1.18.x
psychobunny
parent d77ab9048d
commit fb83913670

@ -1,90 +1,93 @@
var RDB = require('./redis.js'), var RDB = require('./redis.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
utils = require('./utils.js'), utils = require('./utils.js'),
user = require('./user.js'); user = require('./user.js');
(function(Categories) { (function(Categories) {
// An admin-only function. Seeing how we have no control panel yet ima leave this right here. sit pretty, you // An admin-only function. Seeing how we have no control panel yet ima leave this right here. sit pretty, you
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
RDB.incr('global:next_category_id', function(cid) { RDB.incr('global:next_category_id', function(err, cid) {
var slug = cid + '/' + utils.slugify(data.name); RDB.handle(err);
RDB.rpush('categories:cid', cid);
var slug = cid + '/' + utils.slugify(data.name);
// Topic Info RDB.rpush('categories:cid', cid);
RDB.set('cid:' + cid + ':name', data.name);
RDB.set('cid:' + cid + ':description', data.description); // Topic Info
RDB.set('cid:' + cid + ':icon', data.icon); RDB.set('cid:' + cid + ':name', data.name);
RDB.set('cid:' + cid + ':blockclass', data.blockclass); RDB.set('cid:' + cid + ':description', data.description);
RDB.set('cid:' + cid + ':slug', slug); RDB.set('cid:' + cid + ':icon', data.icon);
RDB.set('cid:' + cid + ':blockclass', data.blockclass);
RDB.set('category:slug:' + slug + ':cid', cid); RDB.set('cid:' + cid + ':slug', slug);
if (callback) callback({'status': 1}); RDB.set('category:slug:' + slug + ':cid', cid);
});
}; if (callback) callback({'status': 1});
});
Categories.edit = function(data, callback) { };
// just a reminder to self that name + slugs are stored into topics data as well.
}; Categories.edit = function(data, callback) {
// just a reminder to self that name + slugs are stored into topics data as well.
Categories.get = function(callback) { };
RDB.lrange('categories:cid', 0, -1, function(cids) {
Categories.get_category(cids, callback); Categories.get = function(callback) {
}); RDB.lrange('categories:cid', 0, -1, function(err, cids) {
} RDB.handle(err);
Categories.get_category(cids, callback);
Categories.get_category = function(cids, callback) { });
var name = [], }
description = [],
icon = [], Categories.get_category = function(cids, callback) {
blockclass = [], var name = [],
slug = []; description = [],
icon = [],
for (var i=0, ii=cids.length; i<ii; i++) { blockclass = [],
name.push('cid:' + cids[i] + ':name'); slug = [];
description.push('cid:' + cids[i] + ':description');
icon.push('cid:' + cids[i] + ':icon'); for (var i=0, ii=cids.length; i<ii; i++) {
blockclass.push('cid:' + cids[i] + ':blockclass'); name.push('cid:' + cids[i] + ':name');
slug.push('cid:' + cids[i] + ':slug'); description.push('cid:' + cids[i] + ':description');
} icon.push('cid:' + cids[i] + ':icon');
blockclass.push('cid:' + cids[i] + ':blockclass');
if (cids.length > 0) { slug.push('cid:' + cids[i] + ':slug');
RDB.multi() }
.mget(name)
.mget(description) if (cids.length > 0) {
.mget(icon) RDB.multi()
.mget(blockclass) .mget(name)
.mget(slug) .mget(description)
.exec(function(err, replies) { .mget(icon)
name = replies[0]; .mget(blockclass)
description = replies[1]; .mget(slug)
icon = replies[2]; .exec(function(err, replies) {
blockclass = replies[3]; name = replies[0];
slug = replies[4]; description = replies[1];
icon = replies[2];
var categories = []; blockclass = replies[3];
for (var i=0, ii=cids.length; i<ii; i++) { slug = replies[4];
categories.push({
'name' : name[i], var categories = [];
'cid' : cids[i], for (var i=0, ii=cids.length; i<ii; i++) {
'slug' : slug[i], categories.push({
'description' : description[i], 'name' : name[i],
'blockclass' : blockclass[i], 'cid' : cids[i],
'icon' : icon[i], 'slug' : slug[i],
/*'topics' : [0,1], later 'description' : description[i],
'latest_post' : { 'blockclass' : blockclass[i],
'uid' : 1, 'icon' : icon[i],
'pid' : 1, /*'topics' : [0,1], later
timestamp and shit 'latest_post' : {
}*/ 'uid' : 1,
}); 'pid' : 1,
} timestamp and shit
}*/
callback({'categories': categories}); });
}); }
} else callback({'categories' : []});
}; callback({'categories': categories});
});
} else callback({'categories' : []});
};
}(exports)); }(exports));

@ -1,248 +1,261 @@
var RDB = require('./redis.js'), var RDB = require('./redis.js'),
utils = require('./utils.js'), utils = require('./utils.js'),
marked = require('marked'), marked = require('marked'),
user = require('./user.js'), user = require('./user.js'),
config = require('../config.js'); config = require('../config.js');
(function(Posts) { (function(Posts) {
Posts.get = function(callback, tid, current_user, start, end) { Posts.get = function(callback, tid, current_user, start, end) {
if (start == null) start = 0; if (start == null) start = 0;
if (end == null) end = start + 10; if (end == null) end = start + 10;
var post_data, user_data, thread_data, vote_data, viewer_data; var post_data, user_data, thread_data, vote_data, viewer_data;
//compile thread after all data is asynchronously called //compile thread after all data is asynchronously called
function generateThread() { function generateThread() {
if (!post_data ||! user_data || !thread_data || !vote_data || !viewer_data) return; if (!post_data ||! user_data || !thread_data || !vote_data || !viewer_data) return;
var posts = []; var posts = [];
for (var i=0, ii= post_data.pid.length; i<ii; i++) { for (var i=0, ii= post_data.pid.length; i<ii; i++) {
var uid = post_data.uid[i], var uid = post_data.uid[i],
pid = post_data.pid[i]; pid = post_data.pid[i];
posts.push({ posts.push({
'pid' : pid, 'pid' : pid,
'uid' : uid, 'uid' : uid,
'content' : marked(post_data.content[i] || ''), 'content' : marked(post_data.content[i] || ''),
'post_rep' : post_data.reputation[i] || 0, 'post_rep' : post_data.reputation[i] || 0,
'timestamp' : post_data.timestamp[i], 'timestamp' : post_data.timestamp[i],
'relativeTime': utils.relativeTime(post_data.timestamp[i]), 'relativeTime': utils.relativeTime(post_data.timestamp[i]),
'username' : user_data[uid].username || 'anonymous', 'username' : user_data[uid].username || 'anonymous',
'user_rep' : user_data[uid].reputation || 0, 'user_rep' : user_data[uid].reputation || 0,
'gravatar' : user_data[uid].picture, 'gravatar' : user_data[uid].picture,
'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty', 'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty',
'display_moderator_tools' : uid == current_user ? 'show' : 'none' 'display_moderator_tools' : uid == current_user ? 'show' : 'none'
}); });
} }
callback({ callback({
'topic_name':thread_data.topic_name, 'topic_name':thread_data.topic_name,
'category_name':thread_data.category_name, 'category_name':thread_data.category_name,
'category_slug':thread_data.category_slug, 'category_slug':thread_data.category_slug,
'locked': parseInt(thread_data.locked) || 0, 'locked': parseInt(thread_data.locked) || 0,
'deleted': parseInt(thread_data.deleted) || 0, 'deleted': parseInt(thread_data.deleted) || 0,
'pinned': parseInt(thread_data.pinned) || 0, 'pinned': parseInt(thread_data.pinned) || 0,
'topic_id': tid, 'topic_id': tid,
'expose_tools': viewer_data.reputation >= config.privilege_thresholds.manage_thread ? 1 : 0, 'expose_tools': viewer_data.reputation >= config.privilege_thresholds.manage_thread ? 1 : 0,
'posts': posts 'posts': posts
}); });
} }
// get all data for thread in asynchronous fashion // get all data for thread in asynchronous fashion
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) { RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
var content = [], uid = [], timestamp = [], pid = [], post_rep = []; RDB.handle(err);
for (var i=0, ii=pids.length; i<ii; i++) { var content = [], uid = [], timestamp = [], pid = [], post_rep = [];
content.push('pid:' + pids[i] + ':content');
uid.push('pid:' + pids[i] + ':uid'); for (var i=0, ii=pids.length; i<ii; i++) {
timestamp.push('pid:' + pids[i] + ':timestamp'); content.push('pid:' + pids[i] + ':content');
post_rep.push('pid:' + pids[i] + ':rep'); uid.push('pid:' + pids[i] + ':uid');
pid.push(pids[i]); timestamp.push('pid:' + pids[i] + ':timestamp');
} post_rep.push('pid:' + pids[i] + ':rep');
pid.push(pids[i]);
}
Posts.getFavouritesByPostIDs(pids, current_user, function(fav_data) {
vote_data = fav_data;
generateThread(); Posts.getFavouritesByPostIDs(pids, current_user, function(fav_data) {
}); vote_data = fav_data;
generateThread();
});
RDB.multi()
.mget(content)
.mget(uid) RDB.multi()
.mget(timestamp) .mget(content)
.mget(post_rep) .mget(uid)
.get('tid:' + tid + ':title') .mget(timestamp)
.get('tid:' + tid + ':locked') .mget(post_rep)
.get('tid:' + tid + ':category_name') .get('tid:' + tid + ':title')
.get('tid:' + tid + ':category_slug') .get('tid:' + tid + ':locked')
.get('tid:' + tid + ':deleted') .get('tid:' + tid + ':category_name')
.get('tid:' + tid + ':pinned') .get('tid:' + tid + ':category_slug')
.exec(function(err, replies) { .get('tid:' + tid + ':deleted')
post_data = { .get('tid:' + tid + ':pinned')
pid: pids, .exec(function(err, replies) {
content: replies[0], post_data = {
uid: replies[1], pid: pids,
timestamp: replies[2], content: replies[0],
reputation: replies[3] uid: replies[1],
}; timestamp: replies[2],
reputation: replies[3]
thread_data = { };
topic_name: replies[4],
locked: replies[5] || 0, thread_data = {
category_name: replies[6], topic_name: replies[4],
category_slug: replies[7], locked: replies[5] || 0,
deleted: replies[8] || 0, category_name: replies[6],
pinned: replies[9] || 0 category_slug: replies[7],
}; deleted: replies[8] || 0,
pinned: replies[9] || 0
user.getMultipleUserFields(post_data.uid, ['username','reputation','picture'], function(user_details){ };
user_data = user_details;
generateThread(); user.getMultipleUserFields(post_data.uid, ['username','reputation','picture'], function(user_details){
}); user_data = user_details;
}); generateThread();
}); });
});
user.getUserField(current_user, 'reputation', function(reputation){ });
viewer_data = {
reputation: reputation user.getUserField(current_user, 'reputation', function(reputation){
}; viewer_data = {
generateThread(); reputation: reputation
}); };
} generateThread();
});
}
Posts.reply = function(socket, tid, uid, content) {
Posts.create(uid, tid, content, function(pid) {
if (pid > 0) { Posts.reply = function(socket, tid, uid, content) {
RDB.rpush('tid:' + tid + ':posts', pid); Posts.create(uid, tid, content, function(pid) {
if (pid > 0) {
socket.emit('event:alert', { RDB.rpush('tid:' + tid + ':posts', pid);
title: 'Reply Successful',
message: 'You have successfully replied. Click here to view your reply.', socket.emit('event:alert', {
type: 'notify', title: 'Reply Successful',
timeout: 2000 message: 'You have successfully replied. Click here to view your reply.',
}); type: 'notify',
timeout: 2000
});
user.getUserFields(uid, ['username','reputation','picture'], function(data){
var timestamp = new Date().getTime(); user.getUserFields(uid, ['username','reputation','picture'], function(data){
io.sockets.in('topic_' + tid).emit('event:new_post', { var timestamp = new Date().getTime();
'posts' : [
{ io.sockets.in('topic_' + tid).emit('event:new_post', {
'pid' : pid, 'posts' : [
'content' : marked(content || ''), {
'uid' : uid, 'pid' : pid,
'username' : data.username || 'anonymous', 'content' : marked(content || ''),
'user_rep' : data.reputation || 0, 'uid' : uid,
'post_rep' : 0, 'username' : data.username || 'anonymous',
'gravatar' : data.picture, 'user_rep' : data.reputation || 0,
'timestamp' : timestamp, 'post_rep' : 0,
'relativeTime': utils.relativeTime(timestamp), 'gravatar' : data.picture,
'fav_star_class' :'icon-star-empty' 'timestamp' : timestamp,
} 'relativeTime': utils.relativeTime(timestamp),
] 'fav_star_class' :'icon-star-empty'
}); }
}); ]
} else { });
socket.emit('event:alert', { });
title: 'Reply Unsuccessful', } else {
message: 'Your reply could not be posted at this time. Please try again later.', socket.emit('event:alert', {
type: 'notify', title: 'Reply Unsuccessful',
timeout: 2000 message: 'Your reply could not be posted at this time. Please try again later.',
}); type: 'notify',
} timeout: 2000
}); });
}; }
});
Posts.create = function(uid, tid, content, callback) { };
if (uid === null) return;
Posts.create = function(uid, tid, content, callback) {
RDB.get('tid:' + tid + ':locked', function(locked) { if (uid === null) return;
if (!locked || locked === '0') {
RDB.incr('global:next_post_id', function(pid) { RDB.get('tid:' + tid + ':locked', function(err, locked) {
// Posts Info RDB.handle(err);
RDB.set('pid:' + pid + ':content', content);
RDB.set('pid:' + pid + ':uid', uid); if (!locked || locked === '0') {
RDB.set('pid:' + pid + ':timestamp', new Date().getTime()); RDB.incr('global:next_post_id', function(err, pid) {
RDB.set('pid:' + pid + ':rep', 0); RDB.handle(err);
RDB.incr('tid:' + tid + ':postcount'); // Posts Info
RDB.set('pid:' + pid + ':content', content);
// User Details - move this out later RDB.set('pid:' + pid + ':uid', uid);
RDB.lpush('uid:' + uid + ':posts', pid); RDB.set('pid:' + pid + ':timestamp', new Date().getTime());
RDB.set('pid:' + pid + ':rep', 0);
user.incrementUserFieldBy(uid, 'postcount', 1);
RDB.incr('tid:' + tid + ':postcount');
if (callback)
callback(pid); // User Details - move this out later
}); RDB.lpush('uid:' + uid + ':posts', pid);
} else {
callback(-1); user.incrementUserFieldBy(uid, 'postcount', 1);
}
}); if (callback)
} callback(pid);
});
} else {
Posts.favourite = function(io, pid, room_id, uid) { callback(-1);
RDB.get('pid:' + pid + ':uid', function(uid_of_poster) { }
Posts.hasFavourited(pid, uid, function(hasFavourited) { });
if (hasFavourited == false) { }
RDB.sadd('pid:' + pid + ':users_favourited', uid);
user.incrementUserFieldBy(uid_of_poster, 'reputation', 1); Posts.favourite = function(io, pid, room_id, uid) {
RDB.get('pid:' + pid + ':uid', function(err, uid_of_poster) {
RDB.incr('pid:' + pid + ':rep'); RDB.handle(err);
if (room_id) { Posts.hasFavourited(pid, uid, function(hasFavourited) {
io.sockets.in(room_id).emit('event:rep_up', {uid: uid_of_poster, pid: pid}); if (hasFavourited == false) {
} RDB.sadd('pid:' + pid + ':users_favourited', uid);
}
}); user.incrementUserFieldBy(uid_of_poster, 'reputation', 1);
});
} RDB.incr('pid:' + pid + ':rep');
Posts.unfavourite = function(io, pid, room_id, uid) { if (room_id) {
RDB.get('pid:' + pid + ':uid', function(uid_of_poster) { io.sockets.in(room_id).emit('event:rep_up', {uid: uid_of_poster, pid: pid});
Posts.hasFavourited(pid, uid, function(hasFavourited) { }
if (hasFavourited == true) { }
});
RDB.srem('pid:' + pid + ':users_favourited', uid); });
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1); }
RDB.decr('pid:' + pid + ':rep');
Posts.unfavourite = function(io, pid, room_id, uid) {
if (room_id) { RDB.get('pid:' + pid + ':uid', function(err, uid_of_poster) {
io.sockets.in(room_id).emit('event:rep_down', {uid: uid_of_poster, pid: pid}); RDB.handle(err);
}
} Posts.hasFavourited(pid, uid, function(hasFavourited) {
}); if (hasFavourited == true) {
});
} RDB.srem('pid:' + pid + ':users_favourited', uid);
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1);
Posts.hasFavourited = function(pid, uid, callback) { RDB.decr('pid:' + pid + ':rep');
RDB.sismember('pid:' + pid + ':users_favourited', uid, function(hasFavourited) {
callback(hasFavourited); if (room_id) {
}); io.sockets.in(room_id).emit('event:rep_down', {uid: uid_of_poster, pid: pid});
} }
}
Posts.getFavouritesByPostIDs = function(pids, uid, callback) { });
var loaded = 0; });
var data = {}; }
for (var i=0, ii=pids.length; i<ii; i++) { Posts.hasFavourited = function(pid, uid, callback) {
(function(post_id) { RDB.sismember('pid:' + pid + ':users_favourited', uid, function(err, hasFavourited) {
Posts.hasFavourited(post_id, uid, function(hasFavourited){ RDB.handle(err);
data[post_id] = hasFavourited; callback(hasFavourited);
loaded ++; });
if (loaded == pids.length) callback(data); }
});
}(pids[i])) Posts.getFavouritesByPostIDs = function(pids, uid, callback) {
} var loaded = 0;
} var data = {};
for (var i=0, ii=pids.length; i<ii; i++) {
(function(post_id) {
Posts.hasFavourited(post_id, uid, function(err, hasFavourited) {
RDB.handle(err);
data[post_id] = hasFavourited;
loaded ++;
if (loaded == pids.length) callback(data);
});
}(pids[i]))
}
}
}(exports)); }(exports));

@ -1,106 +1,106 @@
(function(RedisDB) { (function(RedisDB) {
var PRODUCTION = false, var PRODUCTION = false,
ERROR_LOGS = true, ERROR_LOGS = true,
redis = require('redis'), redis = require('redis'),
config = require('../config.js'), config = require('../config.js'),
db = redis.createClient(config.redis.port, config.redis.host, config.redis.options); RedisDB = redis.createClient(config.redis.port, config.redis.host, config.redis.options);
RedisDB.db = db; //RedisDB.db = db;
// todo (holy cow): append,auth,bgrewriteaof,bgsave,bitcount,bitop,blpop,brpop,brpoplpush,client kill,client list,client getname,client setname,config get,config set,config resetstat,dbsize,debug object,debug segfault,decrby,discard,dump,echo,eval,evalsha,exec,exists,expireat,flushall,flushdb,getbit,getrange,getset,hdel,hexists,hget,hgetall,hincrby,hincrbyfloat,hkeys,hlen,hmget,hmset,hset,hsetnx,hvals,incrby,incrbyfloat,info,lastsave,lindex,linsert,llen,lpop,lpushx,lrem,lset,ltrim,migrate,monitor,move,mset,msetnx,object,persist,pexpire,pexpireat,ping,psetex,psubscribe,pttl,publish,punsubscribe,quit,randomkey,rename,renamenx,restore,rpop,rpoplpush,rpush,rpushx,sadd,save,scard,script exists,script flush,script kill,script load,sdiff,sdiffstore,select,setbit,setex,setnx,setrange,shutdown,sinter,sinterstore,sismember,slaveof,slowlog,smembers,smove,sort,spop,srandmember,srem,strlen,subscribe,sunion,sunionstore,sync,time,ttl,type,unsubscribe,unwatch,watch,zadd,zcard,zcount,zincrby,zinterstore,zrange,zrangebyscore,zrank,zrem,zremrangebyrank,zremrangebyscore,zrevrange,zrevrangebyscore,zrevrank,zscore,zunionstore // todo (holy cow): append,auth,bgrewriteaof,bgsave,bitcount,bitop,blpop,brpop,brpoplpush,client kill,client list,client getname,client setname,config get,config set,config resetstat,dbsize,debug object,debug segfault,decrby,discard,dump,echo,eval,evalsha,exec,exists,expireat,flushall,flushdb,getbit,getrange,getset,hdel,hexists,hget,hgetall,hincrby,hincrbyfloat,hkeys,hlen,hmget,hmset,hset,hsetnx,hvals,incrby,incrbyfloat,info,lastsave,lindex,linsert,llen,lpop,lpushx,lrem,lset,ltrim,migrate,monitor,move,mset,msetnx,object,persist,pexpire,pexpireat,ping,psetex,psubscribe,pttl,publish,punsubscribe,quit,randomkey,rename,renamenx,restore,rpop,rpoplpush,rpush,rpushx,sadd,save,scard,script exists,script flush,script kill,script load,sdiff,sdiffstore,select,setbit,setex,setnx,setrange,shutdown,sinter,sinterstore,sismember,slaveof,slowlog,smembers,smove,sort,spop,srandmember,srem,strlen,subscribe,sunion,sunionstore,sync,time,ttl,type,unsubscribe,unwatch,watch,zadd,zcard,zcount,zincrby,zinterstore,zrange,zrangebyscore,zrank,zrem,zremrangebyrank,zremrangebyscore,zrevrange,zrevrangebyscore,zrevrank,zscore,zunionstore
// done: get, set, incr, decr, del, mget, multi, expire, lpush, lrange, keys // done: get, set, incr, decr, del, mget, multi, expire, lpush, lrange, keys
function return_handler(error, data, callback, error_handler) { function return_handler(error, data, callback, error_handler) {
if (error !== null) { if (error !== null) {
if (error_handler !== null) { if (error_handler !== null) {
error_handler(error); error_handler(error);
} else if (PRODUCTION === false) { } else if (PRODUCTION === false) {
throw new Exception('RedisDB Error: ' + error); throw new Exception('RedisDB Error: ' + error);
} else if (ERROR_LOGS === true) { } else if (ERROR_LOGS === true) {
console.log('RedisDB Error: ' + error); console.log('RedisDB Error: ' + error);
} }
} else { } else {
callback(data); callback(data);
} }
} }
RedisDB.set = function(key, value, expiry) { RedisDB.set = function(key, value, expiry) {
db.set(key, value); db.set(key, value);
if (expiry !== undefined) RedisDB.expire(key, expiry); if (expiry !== undefined) RedisDB.expire(key, expiry);
}; };
RedisDB.get = function(key, callback, error_handler) { RedisDB.get = function(key, callback, error_handler) {
db.get(key, function(error, data) { db.get(key, function(error, data) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
}); });
}; };
RedisDB.mget = function(keys, callback, error_handler) { RedisDB.mget = function(keys, callback, error_handler) {
db.mget(keys, function(error, data) { db.mget(keys, function(error, data) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
}); });
}; };
RedisDB.multi = function() { RedisDB.multi = function() {
return db.multi(); return db.multi();
} }
RedisDB.keys = function(pattern, callback, error_handler) { RedisDB.keys = function(pattern, callback, error_handler) {
return db.keys(pattern, function(error, data) { return db.keys(pattern, function(error, data) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
}); });
} }
RedisDB.del = function(key, callback) { RedisDB.del = function(key, callback) {
db.del(key); db.del(key);
} }
RedisDB.expire = function(key, expiry) { RedisDB.expire = function(key, expiry) {
db.expire(key, expiry); db.expire(key, expiry);
} }
// Atomic Operations // Atomic Operations
RedisDB.incr = function(key, callback, error_handler) { RedisDB.incr = function(key, callback, error_handler) {
db.incr(key, function(error, data) { db.incr(key, function(error, data) {
if (callback) { if (callback) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
} }
}); });
}; };
RedisDB.decr = function(key) { RedisDB.decr = function(key) {
db.decr(key); db.decr(key);
}; };
// Lists // Lists
RedisDB.lpush = function(key, item) { RedisDB.lpush = function(key, item) {
db.lpush(key, item); db.lpush(key, item);
} }
RedisDB.rpush = function(key, item) { RedisDB.rpush = function(key, item) {
db.rpush(key, item); db.rpush(key, item);
} }
RedisDB.lrange = function(key, start, end, callback, error_handler) { RedisDB.lrange = function(key, start, end, callback, error_handler) {
db.lrange(key, start, end, function(error, data) { db.lrange(key, start, end, function(error, data) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
}); });
} }
// Sets // Sets
RedisDB.sadd = function(key, item) { RedisDB.sadd = function(key, item) {
db.sadd(key, item); db.sadd(key, item);
}; };
RedisDB.srem = function(key, item) { RedisDB.srem = function(key, item) {
db.srem(key, item); db.srem(key, item);
}; };
RedisDB.sismember = function(key, item, callback, error_handler) { RedisDB.sismember = function(key, item, callback, error_handler) {
db.sismember(key, item, function(error, data) { db.sismember(key, item, function(error, data) {
return_handler(error, data, callback, error_handler); return_handler(error, data, callback, error_handler);
}); });
}; };
}(exports)); }(exports));

@ -1,303 +1,306 @@
var RDB = require('./redis.js'), var RDB = require('./redis.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
utils = require('./utils.js'), utils = require('./utils.js'),
user = require('./user.js'), user = require('./user.js'),
configs = require('../config.js'), configs = require('../config.js'),
categories = require('./categories.js'); categories = require('./categories.js');
(function(Topics) { (function(Topics) {
Topics.get_by_category = function(callback, category, start, end) { Topics.get_by_category = function(callback, category, start, end) {
} }
Topics.get = function(callback, category_id, start, end) { Topics.get = function(callback, category_id, start, end) {
if (start == null) start = 0; if (start == null) start = 0;
if (end == null) end = start + 10; if (end == null) end = start + 10;
//build a proper wrapper for this and move it into above function later //build a proper wrapper for this and move it into above function later
var range_var = (category_id) ? 'categories:' + category_id + ':tid' : 'topics:tid'; var range_var = (category_id) ? 'categories:' + category_id + ':tid' : 'topics:tid';
RDB.db.smembers(range_var, function(err, tids) { RDB.smembers(range_var, function(err, tids) {
var title = [], var title = [],
uid = [], uid = [],
timestamp = [], timestamp = [],
slug = [], slug = [],
postcount = [], postcount = [],
locked = [], locked = [],
deleted = [], deleted = [],
pinned = []; pinned = [];
for (var i=0, ii=tids.length; i<ii; i++) { for (var i=0, ii=tids.length; i<ii; i++) {
title.push('tid:' + tids[i] + ':title'); title.push('tid:' + tids[i] + ':title');
uid.push('tid:' + tids[i] + ':uid'); uid.push('tid:' + tids[i] + ':uid');
timestamp.push('tid:' + tids[i] + ':timestamp'); timestamp.push('tid:' + tids[i] + ':timestamp');
slug.push('tid:' + tids[i] + ':slug'); slug.push('tid:' + tids[i] + ':slug');
postcount.push('tid:' + tids[i] + ':postcount'); postcount.push('tid:' + tids[i] + ':postcount');
locked.push('tid:' + tids[i] + ':locked'); locked.push('tid:' + tids[i] + ':locked');
deleted.push('tid:' + tids[i] + ':deleted'); deleted.push('tid:' + tids[i] + ':deleted');
pinned.push('tid:' + tids[i] + ':pinned'); pinned.push('tid:' + tids[i] + ':pinned');
} }
var multi = RDB.multi() var multi = RDB.multi()
.get('cid:' + category_id + ':name'); .get('cid:' + category_id + ':name');
if (tids.length > 0) { if (tids.length > 0) {
multi multi
.mget(title) .mget(title)
.mget(uid) .mget(uid)
.mget(timestamp) .mget(timestamp)
.mget(slug) .mget(slug)
.mget(postcount) .mget(postcount)
.mget(locked) .mget(locked)
.mget(deleted) .mget(deleted)
.mget(pinned) .mget(pinned)
} }
multi.exec(function(err, replies) { multi.exec(function(err, replies) {
category_name = replies[0]; category_name = replies[0];
var topics = []; var topics = [];
if (tids.length > 0) { if (tids.length > 0) {
title = replies[1]; title = replies[1];
uid = replies[2]; uid = replies[2];
timestamp = replies[3]; timestamp = replies[3];
slug = replies[4]; slug = replies[4];
postcount = replies[5]; postcount = replies[5];
locked = replies[6]; locked = replies[6];
deleted = replies[7]; deleted = replies[7];
pinned = replies[8]; pinned = replies[8];
user.get_usernames_by_uids(uid, function(userNames) { user.get_usernames_by_uids(uid, function(userNames) {
for (var i=0, ii=title.length; i<ii; i++) { for (var i=0, ii=title.length; i<ii; i++) {
topics.push({ topics.push({
'title' : title[i], 'title' : title[i],
'uid' : uid[i], 'uid' : uid[i],
'username': userNames[i], 'username': userNames[i],
'timestamp' : timestamp[i], 'timestamp' : timestamp[i],
'relativeTime': utils.relativeTime(timestamp[i]), 'relativeTime': utils.relativeTime(timestamp[i]),
'slug' : slug[i], 'slug' : slug[i],
'post_count' : postcount[i], 'post_count' : postcount[i],
'lock-icon': locked[i] === '1' ? 'icon-lock' : 'none', 'lock-icon': locked[i] === '1' ? 'icon-lock' : 'none',
'deleted': deleted[i], 'deleted': deleted[i],
'pinned': parseInt(pinned[i] || 0), // For sorting purposes 'pinned': parseInt(pinned[i] || 0), // For sorting purposes
'pin-icon': pinned[i] === '1' ? 'icon-pushpin' : 'none' 'pin-icon': pinned[i] === '1' ? 'icon-pushpin' : 'none'
}); });
} }
// Float pinned topics to the top // Float pinned topics to the top
topics = topics.sort(function(a, b) { topics = topics.sort(function(a, b) {
return b.pinned - a.pinned; return b.pinned - a.pinned;
}); });
callback({ callback({
'category_name' : category_id ? category_name : 'Recent', 'category_name' : category_id ? category_name : 'Recent',
'show_topic_button' : category_id ? 'show' : 'hidden', 'show_topic_button' : category_id ? 'show' : 'hidden',
'category_id': category_id || 0, 'category_id': category_id || 0,
'topics': topics 'topics': topics
}); });
}); });
} }
else { else {
callback({ callback({
'category_name' : category_id ? category_name : 'Recent', 'category_name' : category_id ? category_name : 'Recent',
'show_topic_button' : category_id ? 'show' : 'hidden', 'show_topic_button' : category_id ? 'show' : 'hidden',
'category_id': category_id || 0, 'category_id': category_id || 0,
'topics': [] 'topics': []
}); });
} }
}); });
}); });
} }
Topics.post = function(socket, uid, title, content, category_id) { Topics.post = function(socket, uid, title, content, category_id) {
if (!category_id) throw new Error('Attempted to post without a category_id'); if (!category_id) throw new Error('Attempted to post without a category_id');
if (uid === 0) { if (uid === 0) {
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Thank you for posting', title: 'Thank you for posting',
message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.', message: 'Since you are unregistered, your post is awaiting approval. Click here to register now.',
type: 'warning', type: 'warning',
timeout: 7500, timeout: 7500,
clickfn: function() { clickfn: function() {
ajaxify.go('register'); ajaxify.go('register');
} }
}); });
return; // for now, until anon code is written. return; // for now, until anon code is written.
} }
RDB.incr('global:next_topic_id', function(tid) { RDB.incr('global:next_topic_id', function(err, tid) {
RDB.handle(err);
// Global Topics
if (uid == null) uid = 0; // Global Topics
if (uid !== null) { if (uid == null) uid = 0;
RDB.db.sadd('topics:tid', tid); if (uid !== null) {
} else { RDB.sadd('topics:tid', tid);
// need to add some unique key sent by client so we can update this with the real uid later } else {
RDB.lpush('topics:queued:tid', tid); // need to add some unique key sent by client so we can update this with the real uid later
} RDB.lpush('topics:queued:tid', tid);
}
var slug = tid + '/' + utils.slugify(title);
var slug = tid + '/' + utils.slugify(title);
// Topic Info
RDB.set('tid:' + tid + ':title', title); // Topic Info
RDB.set('tid:' + tid + ':uid', uid); RDB.set('tid:' + tid + ':title', title);
RDB.set('tid:' + tid + ':slug', slug); RDB.set('tid:' + tid + ':uid', uid);
RDB.set('tid:' + tid + ':timestamp', new Date().getTime()); RDB.set('tid:' + tid + ':slug', slug);
RDB.set('tid:' + tid + ':timestamp', new Date().getTime());
RDB.set('topic:slug:' + slug + ':tid', tid);
RDB.set('topic:slug:' + slug + ':tid', tid);
// Posts
posts.create(uid, tid, content, function(pid) { // Posts
if (pid > 0) RDB.lpush('tid:' + tid + ':posts', pid); posts.create(uid, tid, content, function(pid) {
}); if (pid > 0) RDB.lpush('tid:' + tid + ':posts', pid);
});
// User Details - move this out later
RDB.lpush('uid:' + uid + ':topics', tid); // User Details - move this out later
RDB.lpush('uid:' + uid + ':topics', tid);
socket.emit('event:alert', {
title: 'Thank you for posting', socket.emit('event:alert', {
message: 'You have successfully posted. Click here to view your post.', title: 'Thank you for posting',
type: 'notify', message: 'You have successfully posted. Click here to view your post.',
timeout: 2000 type: 'notify',
}); timeout: 2000
});
// in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.db.sadd('categories:' + category_id + ':tid', tid); // in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.set('tid:' + tid + ':cid', category_id); RDB.sadd('categories:' + category_id + ':tid', tid);
categories.get_category([category_id], function(data) { RDB.set('tid:' + tid + ':cid', category_id);
RDB.set('tid:' + tid + ':category_name', data.categories[0].name); categories.get_category([category_id], function(data) {
RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); RDB.set('tid:' + tid + ':category_name', data.categories[0].name);
}); RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug);
});
});
}; });
};
Topics.lock = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.lock = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as locked if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.set('tid:' + tid + ':locked', 1); // Mark thread as locked
RDB.set('tid:' + tid + ':locked', 1);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_locked', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_locked', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.unlock = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.unlock = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as locked if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.del('tid:' + tid + ':locked'); // Mark thread as locked
RDB.del('tid:' + tid + ':locked');
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_unlocked', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_unlocked', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.delete = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.delete = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as deleted if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.set('tid:' + tid + ':deleted', 1); // Mark thread as deleted
Topics.lock(tid, uid); RDB.set('tid:' + tid + ':deleted', 1);
Topics.lock(tid, uid);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_deleted', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_deleted', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.restore = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.restore = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as deleted if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.del('tid:' + tid + ':deleted'); // Mark thread as deleted
Topics.unlock(tid, uid); RDB.del('tid:' + tid + ':deleted');
Topics.unlock(tid, uid);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_restored', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_restored', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.pin = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.pin = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as deleted if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.set('tid:' + tid + ':pinned', 1); // Mark thread as deleted
RDB.set('tid:' + tid + ':pinned', 1);
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_pinned', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_pinned', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.unpin = function(tid, uid, socket) {
user.getUserField(uid, 'reputation', function(rep) { Topics.unpin = function(tid, uid, socket) {
if (rep >= configs.privilege_thresholds.manage_thread) { user.getUserField(uid, 'reputation', function(rep) {
// Mark thread as deleted if (rep >= configs.privilege_thresholds.manage_thread) {
RDB.del('tid:' + tid + ':pinned'); // Mark thread as deleted
RDB.del('tid:' + tid + ':pinned');
if (socket) {
io.sockets.in('topic_' + tid).emit('event:topic_unpinned', { if (socket) {
tid: tid, io.sockets.in('topic_' + tid).emit('event:topic_unpinned', {
status: 'ok' tid: tid,
}); status: 'ok'
} });
} }
}); }
} });
}
Topics.move = function(tid, cid, socket) {
RDB.get('tid:' + tid + ':cid', function(oldCid) { Topics.move = function(tid, cid, socket) {
RDB.db.smove('categories:' + oldCid + ':tid', 'categories:' + cid + ':tid', tid, function(err, result) { RDB.get('tid:' + tid + ':cid', function(err, oldCid) {
if (!err && result === 1) { RDB.handle(err);
RDB.set('tid:' + tid + ':cid', cid);
categories.get_category([cid], function(data) { RDB.smove('categories:' + oldCid + ':tid', 'categories:' + cid + ':tid', tid, function(err, result) {
RDB.set('tid:' + tid + ':category_name', data.categories[0].name); if (!err && result === 1) {
RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); RDB.set('tid:' + tid + ':cid', cid);
}); categories.get_category([cid], function(data) {
socket.emit('api:topic.move', { status: 'ok' }); RDB.set('tid:' + tid + ':category_name', data.categories[0].name);
io.sockets.in('topic_' + tid).emit('event:topic_moved', { tid: tid }); RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug);
} else { });
socket.emit('api:topic.move', { status: 'error' }); socket.emit('api:topic.move', { status: 'ok' });
} io.sockets.in('topic_' + tid).emit('event:topic_moved', { tid: tid });
}); } else {
}); socket.emit('api:topic.move', { status: 'error' });
} }
});
});
}
}(exports)); }(exports));

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save