@ -42,6 +42,7 @@ module.exports = function(Topics) {
} ;
} ;
Topics . addPostData = function ( postData , uid , callback ) {
Topics . addPostData = function ( postData , uid , callback ) {
var st = process . hrtime ( ) ;
var pids = postData . map ( function ( post ) {
var pids = postData . map ( function ( post ) {
return post . pid ;
return post . pid ;
} ) ;
} ) ;
@ -54,27 +55,43 @@ module.exports = function(Topics) {
favourites . getVoteStatusByPostIDs ( pids , uid , next ) ;
favourites . getVoteStatusByPostIDs ( pids , uid , next ) ;
} ,
} ,
userData : function ( next ) {
userData : function ( next ) {
async . each ( postData , function ( post , next ) {
var uids = [ ] ;
async . parallel ( {
for ( var i = 0 ; i < postData . length ; ++ i ) {
editor : function ( next ) {
if ( uids . indexOf ( postData [ i ] . uid ) === - 1 ) {
if ( ! post . editor ) {
uids . push ( postData [ i ] . uid ) ;
return next ( ) ;
}
}
user . getUserFields ( post . editor , [ 'username' , 'userslug' ] , next ) ;
} ,
user : function ( next ) {
posts . getUserInfoForPost ( post , next ) ;
}
}
} , function ( err , results ) {
posts . getUserInfoForPosts ( uids , function ( err , users ) {
if ( err ) {
if ( err ) {
return next ( err ) ;
return next ( err ) ;
}
}
post . user = results . user ;
var userData = { } ;
post . editor = results . editor ;
users . forEach ( function ( user ) {
next ( ) ;
userData [ user . uid ] = user ;
} ) ;
next ( null , userData ) ;
} ) ;
} ,
editors : function ( next ) {
var editors = [ ] ;
for ( var i = 0 ; i < postData . length ; ++ i ) {
if ( postData [ i ] . editor && editors . indexOf ( postData [ i ] . editor ) === - 1 ) {
editors . push ( postData [ i ] . editor ) ;
}
}
user . getMultipleUserFields ( editors , [ 'uid' , 'username' , 'userslug' ] , function ( err , editors ) {
if ( err ) {
return next ( err ) ;
}
var editorData = { } ;
editors . forEach ( function ( editor ) {
editorData [ editor . uid ] = editor ;
} )
next ( null , editorData ) ;
} ) ;
} ) ;
} , next ) ;
} ,
} ,
privileges : function ( next ) {
privileges : function ( next ) {
async . map ( pids , function ( pid , next ) {
async . map ( pids , function ( pid , next ) {
@ -88,9 +105,11 @@ module.exports = function(Topics) {
for ( var i = 0 ; i < postData . length ; ++ i ) {
for ( var i = 0 ; i < postData . length ; ++ i ) {
postData [ i ] . deleted = parseInt ( postData [ i ] . deleted , 10 ) === 1 ;
postData [ i ] . deleted = parseInt ( postData [ i ] . deleted , 10 ) === 1 ;
postData [ i ] . user = results . userData [ postData [ i ] . uid ] ;
postData [ i ] . editor = postData [ i ] . editor ? results . editors [ postData [ i ] . editor ] : null ;
postData [ i ] . favourited = results . favourites [ i ] ;
postData [ i ] . favourited = results . favourites [ i ] ;
postData [ i ] . upvoted = results . voteData [ i ] . upvoted ;
postData [ i ] . upvoted = results . voteData .upvotes [ i ] ;
postData [ i ] . downvoted = results . voteData [i ] . downvoted ;
postData [ i ] . downvoted = results . voteData .downvotes [ i ] ;
postData [ i ] . votes = postData [ i ] . votes || 0 ;
postData [ i ] . votes = postData [ i ] . votes || 0 ;
postData [ i ] . display _moderator _tools = results . privileges [ i ] . editable ;
postData [ i ] . display _moderator _tools = results . privileges [ i ] . editable ;
postData [ i ] . display _move _tools = results . privileges [ i ] . move && postData [ i ] . index !== 0 ;
postData [ i ] . display _move _tools = results . privileges [ i ] . move && postData [ i ] . index !== 0 ;
@ -100,7 +119,7 @@ module.exports = function(Topics) {
postData [ i ] . content = '[[topic:post_is_deleted]]' ;
postData [ i ] . content = '[[topic:post_is_deleted]]' ;
}
}
}
}
process . profile ( 'derp' , st ) ;
callback ( null , postData ) ;
callback ( null , postData ) ;
} ) ;
} ) ;
} ;
} ;