2015-03-18 04:09:54 +00:00
var async = require ( 'async' ) ,
2015-03-28 08:38:11 +00:00
User = require ( '../models/User' ) ,
Challenge = require ( './../models/Challenge' ) ,
Bonfire = require ( './../models/Bonfire' ) ,
Story = require ( './../models/Story' ) ,
2015-03-30 22:55:00 +00:00
Wiki = require ( './../models/Wiki' ) ,
2015-03-31 03:08:29 +00:00
Nonprofit = require ( './../models/Nonprofit' ) ,
2015-03-28 08:38:11 +00:00
Comment = require ( './../models/Comment' ) ,
resources = require ( './resources.json' ) ,
steps = resources . steps ,
secrets = require ( './../config/secrets' ) ,
bonfires = require ( '../seed_data/bonfires.json' ) ,
2015-03-31 03:08:29 +00:00
nonprofits = require ( '../seed_data/nonprofits.json' ) ,
2015-03-28 08:38:11 +00:00
coursewares = require ( '../seed_data/coursewares.json' ) ,
2015-03-30 22:55:00 +00:00
wikis = require ( '../seed_data/wikis.json' ) ,
2015-03-28 08:38:11 +00:00
moment = require ( 'moment' ) ,
https = require ( 'https' ) ,
debug = require ( 'debug' ) ( 'freecc:cntr:resources' ) ,
cheerio = require ( 'cheerio' ) ,
request = require ( 'request' ) ,
R = require ( 'ramda' ) ;
2014-12-11 04:44:33 +00:00
2014-11-10 18:29:16 +00:00
/ * *
* GET /
* Resources .
* /
2014-12-26 08:05:00 +00:00
2015-03-21 07:34:06 +00:00
Array . zip = function ( left , right , combinerFunction ) {
var counter ,
results = [ ] ;
for ( counter = 0 ; counter < Math . min ( left . length , right . length ) ; counter ++ ) {
2015-03-28 08:38:11 +00:00
results . push ( combinerFunction ( left [ counter ] , right [ counter ] ) ) ;
2015-03-21 07:34:06 +00:00
}
return results ;
} ;
2014-12-23 16:48:28 +00:00
module . exports = {
2015-03-28 08:38:11 +00:00
privacy : function privacy ( req , res ) {
res . render ( 'resources/privacy' , {
title : 'Privacy'
} ) ;
} ,
sitemap : function sitemap ( req , res , next ) {
var appUrl = 'http://www.freecodecamp.com' ;
var now = moment ( new Date ( ) ) . format ( 'YYYY-MM-DD' ) ;
User . find ( { 'profile.username' : { '$ne' : '' } } , function ( err , users ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
return next ( err ) ;
}
Challenge . find ( { } , function ( err , challenges ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
return next ( err ) ;
}
Bonfire . find ( { } , function ( err , bonfires ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
return next ( err ) ;
}
Story . find ( { } , function ( err , stories ) {
2015-03-06 08:13:58 +00:00
if ( err ) {
2015-03-28 08:38:11 +00:00
debug ( 'User err: ' , err ) ;
return next ( err ) ;
2015-03-06 08:13:58 +00:00
}
2015-03-28 08:38:11 +00:00
res . header ( 'Content-Type' , 'application/xml' ) ;
res . render ( 'resources/sitemap' , {
appUrl : appUrl ,
now : now ,
users : users ,
challenges : challenges ,
bonfires : bonfires ,
stories : stories
2015-03-06 08:13:58 +00:00
} ) ;
2015-03-28 08:38:11 +00:00
} ) ;
2015-03-06 08:13:58 +00:00
} ) ;
2015-03-28 08:38:11 +00:00
} ) ;
} ) ;
} ,
chat : function chat ( req , res ) {
2015-04-05 07:16:23 +00:00
if ( req . user && req . user . progressTimestamps . length > 5 ) {
res . redirect ( 'http://gitter.im/freecodecamp/freecodecamp' ) ;
} else {
res . render ( 'resources/chat' , {
title : "Enter Free Code Camp's Chat Rooms"
} ) ;
}
2015-03-28 08:38:11 +00:00
} ,
jqueryExercises : function jqueryExercises ( req , res ) {
res . render ( 'resources/jquery-exercises' , {
title : 'jQuery Exercises'
} ) ;
} ,
githubCalls : function ( req , res ) {
var githubHeaders = { headers : { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1521.3 Safari/537.36' } , port : 80 } ;
request ( 'https://api.github.com/repos/freecodecamp/freecodecamp/pulls?client_id=' + secrets . github . clientID + '&client_secret=' + secrets . github . clientSecret , githubHeaders , function ( err , status1 , pulls ) {
pulls = pulls ? Object . keys ( JSON . parse ( pulls ) ) . length : "Can't connect to github" ;
request ( 'https://api.github.com/repos/freecodecamp/freecodecamp/issues?client_id=' + secrets . github . clientID + '&client_secret=' + secrets . github . clientSecret , githubHeaders , function ( err , status2 , issues ) {
issues = ( ( pulls === parseInt ( pulls ) ) && issues ) ? Object . keys ( JSON . parse ( issues ) ) . length - pulls : "Can't connect to GitHub" ;
res . send ( { "issues" : issues , "pulls" : pulls } ) ;
} ) ;
} ) ;
} ,
2015-03-29 10:19:19 +00:00
trelloCalls : function ( req , res , next ) {
request ( 'https://trello.com/1/boards/BA3xVpz9/cards?key=' + secrets . trello . key , function ( err , status , trello ) {
if ( err ) { return next ( err ) ; }
trello = ( status && status . statusCode === 200 ) ? ( JSON . parse ( trello ) ) : "Can't connect to to Trello" ;
res . end ( JSON . stringify ( trello ) ) ;
} ) ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-29 10:19:19 +00:00
bloggerCalls : function ( req , res , next ) {
request ( 'https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/posts?key=' + secrets . blogger . key , function ( err , status , blog ) {
if ( err ) { return next ( err ) ; }
blog = ( status && status . statusCode === 200 ) ? JSON . parse ( blog ) : "Can't connect to Blogger" ;
res . end ( JSON . stringify ( blog ) ) ;
} ) ;
} ,
2015-03-28 08:38:11 +00:00
about : function ( req , res , next ) {
if ( req . user ) {
if ( ! req . user . profile . picture || req . user . profile . picture === "https://s3.amazonaws.com/freecodecamp/favicons/apple-touch-icon-180x180.png" ) {
req . user . profile . picture = "https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png" ;
req . user . save ( ) ;
}
}
2015-03-29 10:19:19 +00:00
var date1 = new Date ( "10/15/2014" ) ;
2015-03-28 08:38:11 +00:00
var date2 = new Date ( ) ;
2015-03-29 21:44:49 +00:00
2015-03-28 08:38:11 +00:00
var progressTimestamps = req . user . progressTimestamps ;
var now = Date . now ( ) || 0 ;
2015-04-04 03:05:53 +00:00
//if (req.user.pointsNeedMigration) {
// var challengesHash = req.user.challengesHash;
// for (var key in challengesHash) {
// if (challengesHash[key] > 0) {
// req.user.progressTimestamps.push(challengesHash[key]);
// }
// }
//
// var oldChallengeKeys = R.keys(req.user.challengesHash);
//
// var updatedTimesFromOldChallenges = oldChallengeKeys.map(function(timeStamp) {
// if (timeStamp.toString().length !== 13) {
// timeStamp *= 1000;
// }
// return timeStamp;
// });
//
// var newTimeStamps = R.map(function(timeStamp) {
// if (timeStamp.toString().length !== 13) {
// timeStamp *= 1000;
// }
// return timeStamp;
// }, req.user.progressTimestamps);
//
// req.user.progressTimestamps = newTimeStamps;
//
// req.user.completedCoursewares = Array.zip(updatedTimesFromOldChallenges, coursewares,
// function(left, right) {
// return ({
// completedDate: left.timeStamp,
// _id: right._id,
// name: right.name
// });
// }).filter(function(elem) {
// return elem.completedDate !== 0;
// });
// req.user.pointsNeedMigration = false;
// req.user.save();
//}
2015-03-28 08:38:11 +00:00
if ( progressTimestamps [ progressTimestamps . length - 1 ] <= ( now - 43200 ) ) {
req . user . progressTimestamps . push ( now ) ;
}
2015-03-29 21:44:49 +00:00
2015-03-28 08:38:11 +00:00
var timeDiff = Math . abs ( date2 . getTime ( ) - date1 . getTime ( ) ) ;
var daysRunning = Math . ceil ( timeDiff / ( 1000 * 3600 * 24 ) ) ;
var announcements = resources . announcements ;
function numberWithCommas ( x ) {
return x . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , "," ) ;
}
User . count ( { } , function ( err , c3 ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
return next ( err ) ;
}
User . count ( { 'points' : { '$gt' : 53 } } , function ( err , all ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
return next ( err ) ;
2015-03-08 22:16:44 +00:00
}
2015-03-21 07:34:06 +00:00
2015-03-28 08:38:11 +00:00
res . render ( 'resources/learn-to-code' , {
2015-04-04 03:05:53 +00:00
title : 'About Free Code Camp' ,
2015-03-28 08:38:11 +00:00
daysRunning : daysRunning ,
c3 : numberWithCommas ( c3 ) ,
all : all ,
announcements : announcements
2015-02-01 22:22:34 +00:00
} ) ;
2015-03-28 08:38:11 +00:00
} ) ;
} ) ;
} ,
randomPhrase : function ( ) {
var phrases = resources . phrases ;
return phrases [ Math . floor ( Math . random ( ) * phrases . length ) ] ;
} ,
randomVerb : function ( ) {
var verbs = resources . verbs ;
return verbs [ Math . floor ( Math . random ( ) * verbs . length ) ] ;
} ,
randomCompliment : function ( ) {
var compliments = resources . compliments ;
return compliments [ Math . floor ( Math . random ( ) * compliments . length ) ] ;
} ,
allBonfireIds : function ( ) {
return bonfires . map ( function ( elem ) {
return {
_id : elem . _id ,
difficulty : elem . difficulty
}
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return elem . _id ;
} ) ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-28 08:38:11 +00:00
allBonfireNames : function ( ) {
return bonfires . map ( function ( elem ) {
return {
name : elem . name ,
difficulty : elem . difficulty ,
_id : elem . _id
}
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return {
name : elem . name ,
_id : elem . _id
}
} ) ;
} ,
2015-03-30 22:55:00 +00:00
allWikiNames : function ( ) {
return wikis . map ( function ( elem ) {
2015-03-30 22:15:07 +00:00
return {
2015-03-30 22:55:00 +00:00
name : elem . name
2015-03-31 03:08:29 +00:00
}
} )
} ,
allNonprofitNames : function ( ) {
return nonprofits . map ( function ( elem ) {
return {
name : elem . name
2015-03-30 22:15:07 +00:00
}
} )
} ,
2015-03-28 08:38:11 +00:00
getAllCourses : function ( ) {
"use strict" ;
return coursewares ;
} ,
allCoursewareIds : function ( ) {
return coursewares . map ( function ( elem ) {
return {
_id : elem . _id ,
difficulty : elem . difficulty
} ;
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return elem . _id ;
} ) ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-28 08:38:11 +00:00
allCoursewareNames : function ( ) {
return coursewares . map ( function ( elem ) {
return {
name : elem . name ,
difficulty : elem . difficulty ,
2015-04-04 03:05:53 +00:00
challengeType : elem . challengeType ,
2015-03-28 08:38:11 +00:00
_id : elem . _id
} ;
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return {
name : elem . name ,
2015-04-04 03:05:53 +00:00
challengeType : elem . challengeType ,
2015-03-28 08:38:11 +00:00
_id : elem . _id
} ;
} ) ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-28 08:38:11 +00:00
whichEnvironment : function ( ) {
return process . env . NODE _ENV ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-28 08:38:11 +00:00
getURLTitle : function ( url , callback ) {
( function ( ) {
var result = { title : '' , image : '' , url : '' , description : '' } ;
request ( url , function ( error , response , body ) {
if ( ! error && response . statusCode === 200 ) {
var $ = cheerio . load ( body ) ;
var metaDescription = $ ( "meta[name='description']" ) ;
var metaImage = $ ( "meta[property='og:image']" ) ;
var urlImage = metaImage . attr ( 'content' ) ? metaImage . attr ( 'content' ) : '' ;
var description = metaDescription . attr ( 'content' ) ? metaDescription . attr ( 'content' ) : '' ;
result . title = $ ( 'title' ) . text ( ) . length < 141 ? $ ( 'title' ) . text ( ) : $ ( 'title' ) . text ( ) . slice ( 0 , 137 ) + " ..." ;
result . image = urlImage ;
result . description = description ;
callback ( null , result ) ;
} else {
callback ( 'failed' ) ;
}
} ) ;
} ) ( ) ;
} ,
2015-03-30 22:15:07 +00:00
2015-03-28 08:38:11 +00:00
updateUserStoryPictures : function ( userId , picture , username , cb ) {
var counter = 0 ,
foundStories ,
foundComments ;
Story . find ( { 'author.userId' : userId } , function ( err , stories ) {
if ( err ) {
return cb ( err ) ;
}
foundStories = stories ;
counter ++ ;
saveStoriesAndComments ( ) ;
} ) ;
Comment . find ( { 'author.userId' : userId } , function ( err , comments ) {
if ( err ) {
return cb ( err ) ;
}
foundComments = comments ;
counter ++ ;
saveStoriesAndComments ( ) ;
} ) ;
function saveStoriesAndComments ( ) {
if ( counter !== 2 ) {
return ;
}
var tasks = [ ] ;
R . forEach ( function ( comment ) {
comment . author . picture = picture ;
comment . author . username = username ;
comment . markModified ( 'author' ) ;
tasks . push ( function ( cb ) {
comment . save ( cb ) ;
2015-02-22 09:23:50 +00:00
} ) ;
2015-03-28 08:38:11 +00:00
} , foundComments ) ;
R . forEach ( function ( story ) {
story . author . picture = picture ;
story . author . username = username ;
story . markModified ( 'author' ) ;
tasks . push ( function ( cb ) {
story . save ( cb ) ;
2015-03-08 07:50:51 +00:00
} ) ;
2015-03-28 08:38:11 +00:00
} , foundStories ) ;
async . parallel ( tasks , function ( err ) {
if ( err ) { return cb ( err ) ; }
cb ( ) ;
} ) ;
2015-03-08 07:50:51 +00:00
}
2015-03-28 08:38:11 +00:00
}
2015-03-08 07:50:51 +00:00
} ;