@ -60,104 +60,180 @@ var RDB = require('./redis.js'),
Categories . get = function ( callback , category _id , current _user ) {
var range _var = ( category _id ) ? 'categories:' + category _id + ':tid' : 'topics:tid' ;
RDB . smembers ( range _var , function ( err , tids ) {
var title = [ ] ,
uid = [ ] ,
timestamp = [ ] ,
slug = [ ] ,
postcount = [ ] ,
locked = [ ] ,
deleted = [ ] ,
pinned = [ ] ;
for ( var i = 0 , ii = tids . length ; i < ii ; i ++ ) {
title . push ( 'tid:' + tids [ i ] + ':title' ) ;
uid . push ( 'tid:' + tids [ i ] + ':uid' ) ;
timestamp . push ( 'tid:' + tids [ i ] + ':timestamp' ) ;
slug . push ( 'tid:' + tids [ i ] + ':slug' ) ;
postcount . push ( 'tid:' + tids [ i ] + ':postcount' ) ;
locked . push ( 'tid:' + tids [ i ] + ':locked' ) ;
deleted . push ( 'tid:' + tids [ i ] + ':deleted' ) ;
pinned . push ( 'tid:' + tids [ i ] + ':pinned' ) ;
}
var multi = RDB . multi ( )
RDB . smembers ( 'categories:' + category _id + ':tid' , function ( err , tids ) {
RDB . multi ( )
. get ( 'cid:' + category _id + ':name' )
. smembers ( 'cid:' + category _id + ':active_users' ) ;
if ( tids . length > 0 ) {
multi
. mget ( title )
. mget ( uid )
. mget ( timestamp )
. mget ( slug )
. mget ( postcount )
. mget ( locked )
. mget ( deleted )
. mget ( pinned )
. smembers ( 'cid:' + category _id + ':active_users' )
. exec ( function ( err , replies ) {
category _name = replies [ 0 ] ;
active _usernames = replies [ 1 ] ;
if ( category _name === null ) {
callback ( false ) ;
}
var active _users = [ ] ;
for ( var username in active _usernames ) {
active _users . push ( { 'username' : active _usernames [ username ] } ) ;
}
var categoryData = {
'category_name' : category _name ,
'show_topic_button' : 'show' ,
'category_id' : category _id ,
'active_users' : active _users ,
'topics' : [ ]
} ;
function getTopics ( next ) {
Categories . getTopicsByTids ( tids , current _user , function ( topics ) {
next ( null , topics ) ;
} , category _id ) ;
}
function getModerators ( next ) {
Categories . getModerators ( category _id , function ( moderators ) {
next ( null , moderators ) ;
} ) ;
}
if ( tids . length === 0 ) {
getModerators ( function ( err , moderators ) {
categoryData . moderator _block _class = moderators . length > 0 ? '' : 'none' ;
categoryData . moderators = moderators ;
callback ( categoryData ) ;
} ) ;
} else {
async . parallel ( [ getTopics , getModerators ] , function ( err , results ) {
categoryData . topics = results [ 0 ] ;
categoryData . moderator _block _class = results [ 1 ] . length > 0 ? '' : 'none' ;
categoryData . moderators = results [ 1 ] ;
callback ( categoryData ) ;
} ) ;
}
} ) ;
} ) ;
}
// not the permanent location for this function
Categories . getLatestTopics = function ( current _user , start , end , callback ) {
RDB . zrange ( 'topics:recent' , 0 , - 1 , function ( err , tids ) {
var latestTopics = {
'category_name' : 'Recent' ,
'show_topic_button' : 'hidden' ,
'category_id' : false ,
'topics' : [ ]
} ;
if ( ! tids . length ) {
callback ( latestTopics ) ;
return ;
}
Categories . getTopicsByTids ( tids , current _user , function ( topicData ) {
latestTopics . topics = topicData ;
callback ( latestTopics ) ;
} ) ;
} ) ;
}
Categories . getTopicsByTids = function ( tids , current _user , callback , category _id /*temporary*/ ) {
var title = [ ] ,
uid = [ ] ,
timestamp = [ ] ,
slug = [ ] ,
postcount = [ ] ,
locked = [ ] ,
deleted = [ ] ,
pinned = [ ] ;
for ( var i = 0 , ii = tids . length ; i < ii ; i ++ ) {
title . push ( 'tid:' + tids [ i ] + ':title' ) ;
uid . push ( 'tid:' + tids [ i ] + ':uid' ) ;
timestamp . push ( 'tid:' + tids [ i ] + ':timestamp' ) ;
slug . push ( 'tid:' + tids [ i ] + ':slug' ) ;
postcount . push ( 'tid:' + tids [ i ] + ':postcount' ) ;
locked . push ( 'tid:' + tids [ i ] + ':locked' ) ;
deleted . push ( 'tid:' + tids [ i ] + ':deleted' ) ;
pinned . push ( 'tid:' + tids [ i ] + ':pinned' ) ;
}
RDB . multi ( )
. mget ( title )
. mget ( uid )
. mget ( timestamp )
. mget ( slug )
. mget ( postcount )
. mget ( locked )
. mget ( deleted )
. mget ( pinned )
. exec ( function ( err , replies ) {
var retrieved _topics = [ ] ;
title = replies [ 0 ] ;
uid = replies [ 1 ] ;
timestamp = replies [ 2 ] ;
slug = replies [ 3 ] ;
postcount = replies [ 4 ] ;
locked = replies [ 5 ] ;
deleted = replies [ 6 ] ;
pinned = replies [ 7 ] ;
multi . exec ( function ( err , replies ) {
category _name = replies [ 0 ] ;
function getUserNames ( next ) {
user . get _usernames _by _uids ( uid , function ( userNames ) {
next ( null , userNames ) ;
} ) ;
}
if ( category _id && category _name === null ) {
callback ( false ) ;
return ;
function hasReadTopics ( next ) {
topics . hasReadTopics ( tids , current _user , function ( hasRead ) {
next ( null , hasRead ) ;
} ) ;
}
active _usernames = replies [ 1 ] ;
var retrieved _topics = [ ] ;
if ( tids . length == 0 ) {
callback ( {
'category_name' : category _id ? category _name : 'Recent' ,
'show_topic_button' : category _id ? 'show' : 'hidden' ,
'category_id' : category _id || 0 ,
'topics' : [ ]
function getTeaserInfo ( next ) {
topics . get _teasers ( tids , function ( teasers ) {
next ( null , teasers ) ;
} ) ;
}
title = replies [ 2 ] ;
uid = replies [ 3 ] ;
timestamp = replies [ 4 ] ;
slug = replies [ 5 ] ;
postcount = replies [ 6 ] ;
locked = replies [ 7 ] ;
deleted = replies [ 8 ] ;
pinned = replies [ 9 ] ;
var usernames ,
has _read ,
moderators ,
teaser _info ,
privileges ;
function generate _topic ( ) {
if ( ! usernames || ! has _read || ! moderators || ! teaser _info || ! privileges ) return ;
if ( tids . length > 0 ) {
for ( var i = 0 , ii = title . length ; i < ii ; i ++ ) {
if ( ! deleted [ i ] || ( deleted [ i ] && privileges . view _deleted ) || uid [ i ] === current _user ) {
retrieved _topics . push ( {
'title' : title [ i ] ,
'uid' : uid [ i ] ,
'username' : usernames [ i ] ,
'timestamp' : timestamp [ i ] ,
'relativeTime' : utils . relativeTime ( timestamp [ i ] ) ,
'slug' : slug [ i ] ,
'post_count' : postcount [ i ] ,
'lock-icon' : locked [ i ] === '1' ? 'icon-lock' : 'none' ,
'deleted' : deleted [ i ] ,
'deleted-class' : deleted [ i ] ? 'deleted' : '' ,
'pinned' : parseInt ( pinned [ i ] || 0 ) , // For sorting purposes
'pin-icon' : pinned [ i ] === '1' ? 'icon-pushpin' : 'none' ,
'badgeclass' : ( has _read [ i ] && current _user != 0 ) ? '' : 'badge-important' ,
'teaser_text' : teaser _info [ i ] . text ,
'teaser_username' : teaser _info [ i ] . username
} ) ;
}
// temporary. I don't think this call should belong here
function getPrivileges ( next ) {
Categories . privileges ( category _id , current _user , function ( user _privs ) {
next ( null , user _privs ) ;
} ) ;
}
async . parallel ( [ getUserNames , hasReadTopics , getTeaserInfo , getPrivileges ] , function ( err , results ) {
var usernames = results [ 0 ] ,
hasReadTopics = results [ 1 ] ,
teaserInfo = results [ 2 ] ,
privileges = results [ 3 ] ;
for ( var i = 0 , ii = tids . length ; i < ii ; i ++ ) {
if ( ! deleted [ i ] || ( deleted [ i ] && privileges . view _deleted ) || uid [ i ] === current _user ) {
retrieved _topics . push ( {
'title' : title [ i ] ,
'uid' : uid [ i ] ,
'username' : usernames [ i ] ,
'timestamp' : timestamp [ i ] ,
'relativeTime' : utils . relativeTime ( timestamp [ i ] ) ,
'slug' : slug [ i ] ,
'post_count' : postcount [ i ] ,
'lock-icon' : locked [ i ] === '1' ? 'icon-lock' : 'none' ,
'deleted' : deleted [ i ] ,
'deleted-class' : deleted [ i ] ? 'deleted' : '' ,
'pinned' : parseInt ( pinned [ i ] || 0 ) , // For sorting purposes
'pin-icon' : pinned [ i ] === '1' ? 'icon-pushpin' : 'none' ,
'badgeclass' : ( hasReadTopics [ i ] && current _user != 0 ) ? '' : 'badge-important' ,
'teaser_text' : teaserInfo [ i ] . text ,
'teaser_username' : teaserInfo [ i ] . username
} ) ;
}
}
@ -170,53 +246,16 @@ var RDB = require('./redis.js'),
}
} ) ;
var active _users = [ ] ;
for ( var username in active _usernames ) {
active _users . push ( { 'username' : active _usernames [ username ] } ) ;
}
callback ( {
'category_name' : category _id ? category _name : 'Recent' ,
'show_topic_button' : category _id ? 'show' : 'hidden' ,
'category_id' : category _id || 0 ,
'topics' : retrieved _topics ,
'active_users' : active _users ,
'moderator_block_class' : moderators . length > 0 ? '' : 'none' ,
'moderators' : moderators
} ) ;
}
user . get _usernames _by _uids ( uid , function ( userNames ) {
usernames = userNames ;
generate _topic ( ) ;
} ) ;
topics . hasReadTopics ( tids , current _user , function ( hasRead ) {
has _read = hasRead ;
generate _topic ( ) ;
} ) ;
Categories . getModerators ( category _id , function ( mods ) {
moderators = mods ;
generate _topic ( ) ;
} ) ;
topics . get _teasers ( tids , function ( teasers ) {
teaser _info = teasers ;
generate _topic ( ) ;
} ) ;
Categories . privileges ( category _id , current _user , function ( user _privs ) {
privileges = user _privs ;
callback ( retrieved _topics ) ;
} ) ;
} ) ;
} ) ;
}
Categories . getAllCategories = function ( callback , current _user ) {
RDB . lrange ( 'categories:cid' , 0 , - 1 , function ( err , cids ) {
RDB . handle ( err ) ;
Categories . get _category ( cids , callback , current _user ) ;
Categories . getCategories ( cids , callback , current _user ) ;
} ) ;
}
@ -251,7 +290,7 @@ var RDB = require('./redis.js'),
Categories . get _category = function ( cids , callback , current _user ) {
Categories . get Categories = function ( cids , callback , current _user ) {
var name = [ ] ,
description = [ ] ,
icon = [ ] ,