@ -20,7 +20,7 @@ marked.setOptions({
( function ( Topics ) {
( function ( Topics ) {
Topics . getTopicData = function ( tid , callback ) {
Topics . getTopicData = function ( tid , callback ) {
RDB . hgetall ( 'topic:' + tid , function ( err , data ) {
RDB . hgetall ( 'topic:' + tid , function ( err , data ) {
@ -45,15 +45,15 @@ marked.setOptions({
posts . getPostsByTid ( tid , start , end , function ( postData ) {
posts . getPostsByTid ( tid , start , end , function ( postData ) {
if ( Array . isArray ( postData ) && ! postData . length )
if ( Array . isArray ( postData ) && ! postData . length )
return callback ( [ ] ) ;
return callback ( [ ] ) ;
function getFavouritesData ( next ) {
function getFavouritesData ( next ) {
var pids = [ ] ;
var pids = [ ] ;
for ( var i = 0 ; i < postData . length ; ++ i )
for ( var i = 0 ; i < postData . length ; ++ i )
pids . push ( postData [ i ] . pid ) ;
pids . push ( postData [ i ] . pid ) ;
favourites . getFavouritesByPostIDs ( pids , current _user , function ( fav _data ) {
favourites . getFavouritesByPostIDs ( pids , current _user , function ( fav _data ) {
next ( null , fav _data ) ;
next ( null , fav _data ) ;
} ) ;
} ) ;
}
}
function addUserInfoToPosts ( next ) {
function addUserInfoToPosts ( next ) {
@ -63,16 +63,16 @@ marked.setOptions({
} ) ;
} ) ;
}
}
async . each ( postData , iterator , function ( err ) {
async . each ( postData , iterator , function ( err ) {
next ( err , null ) ;
next ( err , null ) ;
} ) ;
} ) ;
}
}
function getPrivileges ( next ) {
function getPrivileges ( next ) {
threadTools . privileges ( tid , current _user , function ( privData ) {
threadTools . privileges ( tid , current _user , function ( privData ) {
next ( null , privData ) ;
next ( null , privData ) ;
} ) ;
} ) ;
}
}
async . parallel ( [ getFavouritesData , addUserInfoToPosts , getPrivileges ] , function ( err , results ) {
async . parallel ( [ getFavouritesData , addUserInfoToPosts , getPrivileges ] , function ( err , results ) {
var fav _data = results [ 0 ] ,
var fav _data = results [ 0 ] ,
@ -84,7 +84,7 @@ marked.setOptions({
postData [ i ] [ 'display_moderator_tools' ] = ( postData [ i ] . uid == current _user || privileges . editable ) ? 'show' : 'none' ;
postData [ i ] [ 'display_moderator_tools' ] = ( postData [ i ] . uid == current _user || privileges . editable ) ? 'show' : 'none' ;
postData [ i ] . show _banned = postData [ i ] . user _banned === '1' ? 'show' : 'hide' ;
postData [ i ] . show _banned = postData [ i ] . user _banned === '1' ? 'show' : 'hide' ;
}
}
callback ( postData ) ;
callback ( postData ) ;
} ) ;
} ) ;
} ) ;
} ) ;
@ -101,9 +101,9 @@ marked.setOptions({
var timestamp = Date . now ( ) ;
var timestamp = Date . now ( ) ;
var args = [ 'topics:recent' , '+inf' , timestamp - 86400000 , 'WITHSCORES' , 'LIMIT' , start , end - start + 1 ] ;
var args = [ 'topics:recent' , '+inf' , timestamp - 86400000 , 'WITHSCORES' , 'LIMIT' , start , end - start + 1 ] ;
RDB . zrevrangebyscore ( args , function ( err , tids ) {
RDB . zrevrangebyscore ( args , function ( err , tids ) {
var latestTopics = {
var latestTopics = {
'category_name' : 'Recent' ,
'category_name' : 'Recent' ,
'show_sidebar' : 'hidden' ,
'show_sidebar' : 'hidden' ,
@ -126,18 +126,18 @@ marked.setOptions({
} ) ;
} ) ;
} ) ;
} ) ;
}
}
Topics . getTotalUnread = function ( uid , callback ) {
Topics . getTotalUnread = function ( uid , callback ) {
RDB . zrevrange ( 'topics:recent' , 0 , 21 , function ( err , tids ) {
RDB . zrevrange ( 'topics:recent' , 0 , 21 , function ( err , tids ) {
Topics . hasReadTopics ( tids , uid , function ( read ) {
Topics . hasReadTopics ( tids , uid , function ( read ) {
var unreadTids = tids . filter ( function ( tid , index , self ) {
var unreadTids = tids . filter ( function ( tid , index , self ) {
return read [ index ] === 0 ;
return read [ index ] === 0 ;
} ) ;
} ) ;
callback ( {
callback ( {
count : unreadTids . length
count : unreadTids . length
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ;
} ;
@ -154,13 +154,13 @@ marked.setOptions({
} ;
} ;
RDB . zrevrange ( 'topics:recent' , start , stop , function ( err , tids ) {
RDB . zrevrange ( 'topics:recent' , start , stop , function ( err , tids ) {
function noUnreadTopics ( ) {
function noUnreadTopics ( ) {
unreadTopics . no _topics _message = 'show' ;
unreadTopics . no _topics _message = 'show' ;
unreadTopics . show _markallread _button = 'hidden' ;
unreadTopics . show _markallread _button = 'hidden' ;
callback ( unreadTopics ) ;
callback ( unreadTopics ) ;
}
}
function sendUnreadTopics ( topicIds ) {
function sendUnreadTopics ( topicIds ) {
Topics . getTopicsByTids ( topicIds , uid , function ( topicData ) {
Topics . getTopicsByTids ( topicIds , uid , function ( topicData ) {
unreadTopics . topics = topicData ;
unreadTopics . topics = topicData ;
@ -172,29 +172,29 @@ marked.setOptions({
callback ( unreadTopics ) ;
callback ( unreadTopics ) ;
} ) ;
} ) ;
}
}
if ( ! tids || ! tids . length ) {
if ( ! tids || ! tids . length ) {
noUnreadTopics ( ) ;
noUnreadTopics ( ) ;
return ;
return ;
}
}
if ( uid === 0 ) {
if ( uid === 0 ) {
sendUnreadTopics ( tids ) ;
sendUnreadTopics ( tids ) ;
} else {
} else {
Topics . hasReadTopics ( tids , uid , function ( read ) {
Topics . hasReadTopics ( tids , uid , function ( read ) {
var unreadTids = tids . filter ( function ( tid , index , self ) {
var unreadTids = tids . filter ( function ( tid , index , self ) {
return read [ index ] === 0 ;
return read [ index ] === 0 ;
} ) ;
} ) ;
if ( ! unreadTids || ! unreadTids . length ) {
if ( ! unreadTids || ! unreadTids . length ) {
noUnreadTopics ( ) ;
noUnreadTopics ( ) ;
return ;
return ;
}
}
sendUnreadTopics ( unreadTids ) ;
sendUnreadTopics ( unreadTids ) ;
} ) ;
} ) ;
}
}
} ) ;
} ) ;
}
}
@ -202,18 +202,16 @@ marked.setOptions({
Topics . getTopicsByTids = function ( tids , current _user , callback , category _id ) {
Topics . getTopicsByTids = function ( tids , current _user , callback , category _id ) {
var retrieved _topics = [ ] ;
var retrieved _topics = [ ] ;
if ( ! Array . isArray ( tids ) || tids . length === 0 ) {
if ( ! Array . isArray ( tids ) || tids . length === 0 ) {
callback ( retrieved _topics ) ;
callback ( retrieved _topics ) ;
return ;
return ;
}
}
function getTopicInfo ( topicData , callback ) {
function getTopicInfo ( topicData , callback ) {
function getUserInfo ( next ) {
function getUserInfo ( next ) {
user . getUserFields ( topicData . uid , [ 'username' ] , function ( userData ) {
user . getUserFields ( topicData . uid , [ 'username' ] , next ) ;
next ( null , userData ) ;
} ) ;
}
}
function hasReadTopic ( next ) {
function hasReadTopic ( next ) {
@ -256,7 +254,7 @@ marked.setOptions({
if ( ! topicData ) {
if ( ! topicData ) {
return callback ( null ) ;
return callback ( null ) ;
}
}
getTopicInfo ( topicData , function ( topicInfo ) {
getTopicInfo ( topicData , function ( topicInfo ) {
topicData [ 'pin-icon' ] = topicData . pinned === '1' ? 'icon-pushpin' : 'none' ;
topicData [ 'pin-icon' ] = topicData . pinned === '1' ? 'icon-pushpin' : 'none' ;
@ -274,12 +272,12 @@ marked.setOptions({
if ( isTopicVisible ( topicData , topicInfo ) )
if ( isTopicVisible ( topicData , topicInfo ) )
retrieved _topics . push ( topicData ) ;
retrieved _topics . push ( topicData ) ;
callback ( null ) ;
callback ( null ) ;
} ) ;
} ) ;
} ) ;
} ) ;
}
}
async . eachSeries ( tids , loadTopic , function ( err ) {
async . eachSeries ( tids , loadTopic , function ( err ) {
if ( ! err ) {
if ( ! err ) {
callback ( retrieved _topics ) ;
callback ( retrieved _topics ) ;
@ -290,7 +288,7 @@ marked.setOptions({
Topics . getTopicWithPosts = function ( tid , current _user , callback ) {
Topics . getTopicWithPosts = function ( tid , current _user , callback ) {
threadTools . exists ( tid , function ( exists ) {
threadTools . exists ( tid , function ( exists ) {
if ( ! exists )
if ( ! exists )
return callback ( new Error ( 'Topic tid \'' + tid + '\' not found' ) ) ;
return callback ( new Error ( 'Topic tid \'' + tid + '\' not found' ) ) ;
Topics . markAsRead ( tid , current _user ) ;
Topics . markAsRead ( tid , current _user ) ;
@ -311,8 +309,8 @@ marked.setOptions({
threadTools . privileges ( tid , current _user , function ( privData ) {
threadTools . privileges ( tid , current _user , function ( privData ) {
next ( null , privData ) ;
next ( null , privData ) ;
} ) ;
} ) ;
}
}
function getCategoryData ( next ) {
function getCategoryData ( next ) {
Topics . getCategoryData ( tid , next ) ;
Topics . getCategoryData ( tid , next ) ;
}
}
@ -323,14 +321,14 @@ marked.setOptions({
callback ( err , null ) ;
callback ( err , null ) ;
return ;
return ;
}
}
var topicData = results [ 0 ] ,
var topicData = results [ 0 ] ,
topicPosts = results [ 1 ] ,
topicPosts = results [ 1 ] ,
privileges = results [ 2 ] ,
privileges = results [ 2 ] ,
categoryData = results [ 3 ] ;
categoryData = results [ 3 ] ;
var main _posts = topicPosts . splice ( 0 , 1 ) ;
var main _posts = topicPosts . splice ( 0 , 1 ) ;
callback ( null , {
callback ( null , {
'topic_name' : topicData . title ,
'topic_name' : topicData . title ,
'category_name' : categoryData . name ,
'category_name' : categoryData . name ,
@ -378,7 +376,7 @@ marked.setOptions({
if ( err ) {
if ( err ) {
throw new Error ( err ) ;
throw new Error ( err ) ;
}
}
var topicData = results [ 0 ] ,
var topicData = results [ 0 ] ,
hasRead = results [ 1 ] ,
hasRead = results [ 1 ] ,
teaser = results [ 2 ] ;
teaser = results [ 2 ] ;
@ -389,7 +387,7 @@ marked.setOptions({
topicData . teaser _username = teaser . username || '' ;
topicData . teaser _username = teaser . username || '' ;
topicData . teaser _timestamp = teaser . timestamp ? utils . relativeTime ( teaser . timestamp ) : '' ;
topicData . teaser _timestamp = teaser . timestamp ? utils . relativeTime ( teaser . timestamp ) : '' ;
topicData . teaser _userpicture = teaser . picture ;
topicData . teaser _userpicture = teaser . picture ;
callback ( topicData ) ;
callback ( topicData ) ;
} ) ;
} ) ;
}
}
@ -431,7 +429,7 @@ marked.setOptions({
} ) ;
} ) ;
} ) ;
} ) ;
}
}
Topics . markAllRead = function ( uid , callback ) {
Topics . markAllRead = function ( uid , callback ) {
RDB . smembers ( 'topics:tid' , function ( err , tids ) {
RDB . smembers ( 'topics:tid' , function ( err , tids ) {
if ( err ) {
if ( err ) {
@ -439,13 +437,13 @@ marked.setOptions({
callback ( err , null ) ;
callback ( err , null ) ;
return ;
return ;
}
}
if ( tids && tids . length ) {
if ( tids && tids . length ) {
for ( var i = 0 ; i < tids . length ; ++ i ) {
for ( var i = 0 ; i < tids . length ; ++ i ) {
Topics . markAsRead ( tids [ i ] , uid ) ;
Topics . markAsRead ( tids [ i ] , uid ) ;
}
}
}
}
callback ( null , true ) ;
callback ( null , true ) ;
} ) ;
} ) ;
}
}
@ -459,15 +457,15 @@ marked.setOptions({
}
}
Topics . markUnRead = function ( tid ) {
Topics . markUnRead = function ( tid ) {
RDB . del ( 'tid:' + tid + ':read_by_uid' ) ;
RDB . del ( 'tid:' + tid + ':read_by_uid' ) ;
}
}
Topics . markAsRead = function ( tid , uid ) {
Topics . markAsRead = function ( tid , uid ) {
RDB . sadd ( schema . topics ( tid ) . read _by _uid , uid ) ;
RDB . sadd ( schema . topics ( tid ) . read _by _uid , uid ) ;
Topics . getTopicField ( tid , 'cid' , function ( err , cid ) {
Topics . getTopicField ( tid , 'cid' , function ( err , cid ) {
categories . isTopicsRead ( cid , uid , function ( read ) {
categories . isTopicsRead ( cid , uid , function ( read ) {
if ( read ) {
if ( read ) {
categories . markAsRead ( cid , uid ) ;
categories . markAsRead ( cid , uid ) ;
@ -480,9 +478,9 @@ marked.setOptions({
var batch = RDB . multi ( ) ;
var batch = RDB . multi ( ) ;
for ( var i = 0 , ii = tids . length ; i < ii ; i ++ ) {
for ( var i = 0 , ii = tids . length ; i < ii ; i ++ ) {
batch . sismember ( schema . topics ( tids [ i ] ) . read _by _uid , uid ) ;
batch . sismember ( schema . topics ( tids [ i ] ) . read _by _uid , uid ) ;
}
}
batch . exec ( function ( err , hasRead ) {
batch . exec ( function ( err , hasRead ) {
callback ( hasRead ) ;
callback ( hasRead ) ;
} ) ;
} ) ;
@ -497,9 +495,9 @@ marked.setOptions({
console . log ( err ) ;
console . log ( err ) ;
callback ( false ) ;
callback ( false ) ;
}
}
} ) ;
} ) ;
}
}
Topics . getTeasers = function ( tids , callback ) {
Topics . getTeasers = function ( tids , callback ) {
var teasers = [ ] ;
var teasers = [ ] ;
if ( Array . isArray ( tids ) ) {
if ( Array . isArray ( tids ) ) {
@ -520,7 +518,10 @@ marked.setOptions({
if ( ! err ) {
if ( ! err ) {
posts . getPostFields ( pid , [ 'content' , 'uid' , 'timestamp' ] , function ( postData ) {
posts . getPostFields ( pid , [ 'content' , 'uid' , 'timestamp' ] , function ( postData ) {
user . getUserFields ( postData . uid , [ 'username' , 'picture' ] , function ( userData ) {
user . getUserFields ( postData . uid , [ 'username' , 'picture' ] , function ( err , userData ) {
if ( err )
return callback ( err , null ) ;
var stripped = postData . content ,
var stripped = postData . content ,
timestamp = postData . timestamp ;
timestamp = postData . timestamp ;
@ -546,34 +547,34 @@ marked.setOptions({
type : 'error' ,
type : 'error' ,
timeout : 2000 ,
timeout : 2000 ,
title : 'Title too short' ,
title : 'Title too short' ,
message : "Please enter a longer title. At least " + config. minimumTitleLength + " characters." ,
message : "Please enter a longer title. At least " + meta. config. minimumTitleLength + " characters." ,
alert _id : 'post_error'
alert _id : 'post_error'
} ) ;
} ) ;
}
}
Topics . post = function ( uid , title , content , category _id , images , callback ) {
Topics . post = function ( uid , title , content , category _id , images , callback ) {
if ( ! category _id )
if ( ! category _id )
throw new Error ( 'Attempted to post without a category_id' ) ;
throw new Error ( 'Attempted to post without a category_id' ) ;
if ( content )
if ( content )
content = content . trim ( ) ;
content = content . trim ( ) ;
if ( title )
if ( title )
title = title . trim ( ) ;
title = title . trim ( ) ;
if ( uid === 0 ) {
if ( uid === 0 ) {
callback ( new Error ( 'not-logged-in' ) , null ) ;
callback ( new Error ( 'not-logged-in' ) , null ) ;
return ;
return ;
} else if ( ! title || title . length < config. minimumTitleLength ) {
} else if ( ! title || title . length < meta. config. minimumTitleLength ) {
callback ( new Error ( 'title-too-short' ) , null ) ;
callback ( new Error ( 'title-too-short' ) , null ) ;
return ;
return ;
} else if ( ! content || content . length < config. miminumPostLength ) {
} else if ( ! content || content . length < meta. config. miminumPostLength ) {
callback ( new Error ( 'content-too-short' ) , null ) ;
callback ( new Error ( 'content-too-short' ) , null ) ;
return ;
return ;
}
}
user . getUserField ( uid , 'lastposttime' , function ( lastposttime ) {
user . getUserField ( uid , 'lastposttime' , function ( lastposttime ) {
if ( Date . now ( ) - lastposttime < config. postDelay ) {
if ( Date . now ( ) - lastposttime < meta. config. postDelay ) {
callback ( new Error ( 'too-many-posts' ) , null ) ;
callback ( new Error ( 'too-many-posts' ) , null ) ;
return ;
return ;
}
}
@ -604,9 +605,9 @@ marked.setOptions({
'postcount' : 0 ,
'postcount' : 0 ,
'locked' : 0 ,
'locked' : 0 ,
'deleted' : 0 ,
'deleted' : 0 ,
'pinned' : 0
'pinned' : 0
} ) ;
} ) ;
topicSearch . index ( title , tid ) ;
topicSearch . index ( title , tid ) ;
RDB . set ( 'topicslug:' + slug + ':tid' , tid ) ;
RDB . set ( 'topicslug:' + slug + ':tid' , tid ) ;
@ -647,7 +648,7 @@ marked.setOptions({
Topics . getTopicField = function ( tid , field , callback ) {
Topics . getTopicField = function ( tid , field , callback ) {
RDB . hget ( 'topic:' + tid , field , callback ) ;
RDB . hget ( 'topic:' + tid , field , callback ) ;
}
}
Topics . getTopicFields = function ( tid , fields , callback ) {
Topics . getTopicFields = function ( tid , fields , callback ) {
RDB . hmgetObject ( 'topic:' + tid , fields , function ( err , data ) {
RDB . hmgetObject ( 'topic:' + tid , fields , function ( err , data ) {
if ( err === null ) {
if ( err === null ) {
@ -656,7 +657,7 @@ marked.setOptions({
else {
else {
console . log ( err ) ;
console . log ( err ) ;
}
}
} ) ;
} ) ;
}
}
Topics . setTopicField = function ( tid , field , value ) {
Topics . setTopicField = function ( tid , field , value ) {
@ -677,7 +678,7 @@ marked.setOptions({
RDB . zadd ( schema . topics ( ) . recent , timestamp , tid ) ;
RDB . zadd ( schema . topics ( ) . recent , timestamp , tid ) ;
Topics . setTopicField ( tid , 'lastposttime' , timestamp ) ;
Topics . setTopicField ( tid , 'lastposttime' , timestamp ) ;
}
}
Topics . addPostToTopic = function ( tid , pid ) {
Topics . addPostToTopic = function ( tid , pid ) {
RDB . rpush ( 'tid:' + tid + ':posts' , pid ) ;
RDB . rpush ( 'tid:' + tid + ':posts' , pid ) ;
}
}