2014-12-23 16:48:28 +00:00
var User = require ( '../models/User' ) ,
2015-03-06 08:13:58 +00:00
Challenge = require ( './../models/Challenge' ) ,
Bonfire = require ( './../models/Bonfire' ) ,
2015-03-08 07:50:51 +00:00
Story = require ( './../models/Story' ) ,
Comment = require ( './../models/Comment' ) ,
2014-12-23 16:48:28 +00:00
resources = require ( './resources.json' ) ,
2014-12-26 08:05:00 +00:00
steps = resources . steps ,
2014-12-26 20:15:24 +00:00
secrets = require ( './../config/secrets' ) ,
2015-03-06 08:13:58 +00:00
bonfires = require ( '../seed_data/bonfires.json' ) ,
coursewares = require ( '../seed_data/coursewares.json' ) ,
moment = require ( 'moment' ) ,
2015-03-07 23:57:30 +00:00
https = require ( 'https' ) ,
2015-03-07 07:08:15 +00:00
debug = require ( 'debug' ) ( 'freecc:cntr:resources' ) ,
cheerio = require ( 'cheerio' ) ,
2015-03-08 07:50:51 +00:00
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
2014-12-23 16:48:28 +00:00
module . exports = {
2015-01-27 07:51:59 +00:00
privacy : function privacy ( req , res ) {
res . render ( 'resources/privacy' , {
title : 'Privacy'
} ) ;
} ,
2015-03-06 08:13:58 +00:00
sitemap : function sitemap ( req , res , next ) {
var appUrl = 'http://www.freecodecamp.com' ;
var now = moment ( new Date ) . format ( 'YYYY-MM-DD' ) ;
errors = { } ;
User . find ( { 'profile.username' : { '$ne' : '' } } , function ( err , users ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
Challenge . find ( { } , function ( err , challenges ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
Bonfire . find ( { } , function ( err , bonfires ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
2015-03-09 14:18:31 +00:00
Story . find ( { } , function ( err , stories ) {
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
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-01-27 07:51:59 +00:00
deployAWebsite : function deployAWebsite ( req , res ) {
res . render ( 'resources/deploy-a-website' , {
title : 'Deploy a Dynamic Website in 7 Minutes'
} ) ;
} ,
2015-02-20 00:33:08 +00:00
chat : function chat ( req , res ) {
res . render ( 'resources/chat' , {
title : "Enter Free Code Camp's Chat Rooms"
} ) ;
} ,
2015-01-27 07:51:59 +00:00
nonprofitProjectInstructions : function nonprofitProjectInstructions ( req , res ) {
res . render ( 'resources/nonprofit-project-instructions' , {
title : 'Nonprofit Project Instructions'
} ) ;
} ,
gmailShortcuts : function gmailShortcuts ( req , res ) {
res . render ( 'resources/gmail-shortcuts' , {
title : 'These Gmail Shortcuts will save you Hours'
} ) ;
} ,
2015-03-08 09:36:09 +00:00
guideToOurNonprofitProjects : function guideToOurNonprofitProjects ( req , res ) {
res . render ( 'resources/guide-to-our-nonprofit-projects' , {
title : 'A guide to our Nonprofit Projects'
} ) ;
} ,
2015-01-27 07:51:59 +00:00
controlShortcuts : function controlShortcuts ( req , res ) {
res . render ( 'resources/control-shortcuts' , {
title : 'These Control Shortcuts will save you Hours'
} ) ;
} ,
chromebook : function chromebook ( req , res ) {
res . render ( 'resources/chromebook' , {
title : 'Win a Chromebook'
} ) ;
} ,
jqueryExercises : function jqueryExercises ( req , res ) {
res . render ( 'resources/jquery-exercises' , {
title : 'jQuery Exercises'
} ) ;
} ,
livePairProgramming : function ( req , res ) {
res . render ( 'resources/live-pair-programming' , {
title : 'Live Pair Programming'
} ) ;
2015-02-17 22:10:15 +00:00
} ,
installScreenHero : function ( req , res ) {
res . render ( 'resources/install-screenhero' , {
title : 'Install ScreenHero'
} ) ;
2015-01-27 07:51:59 +00:00
} ,
javaScriptInYourInbox : function ( req , res ) {
res . render ( 'resources/javascript-in-your-inbox' , {
title : 'JavaScript in your Inbox'
} ) ;
} ,
2015-02-17 07:35:02 +00:00
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 } ;
2015-03-07 23:57:30 +00:00
request ( 'https://api.github.com/repos/freecodecamp/freecodecamp/pulls?client_id=' + secrets . github . clientID + '&client_secret=' + secrets . github . clientSecret , githubHeaders , function ( err , status1 , pulls ) {
2015-02-17 21:31:59 +00:00
pulls = pulls ? Object . keys ( JSON . parse ( pulls ) ) . length : "Can't connect to github" ;
2015-03-07 23:57:30 +00:00
request ( 'https://api.github.com/repos/freecodecamp/freecodecamp/issues?client_id=' + secrets . github . clientID + '&client_secret=' + secrets . github . clientSecret , githubHeaders , function ( err , status2 , issues ) {
2015-02-17 07:35:02 +00:00
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-07 23:57:30 +00:00
2015-03-15 00:25:56 +00:00
trelloCalls : function ( req , res , next ) {
2015-03-07 23:57:30 +00:00
request ( 'https://trello.com/1/boards/BA3xVpz9/cards?key=' + secrets . trello . key , function ( err , status , trello ) {
2015-03-15 00:25:56 +00:00
if ( err ) { return next ( err ) ; }
2015-03-14 23:13:08 +00:00
trello = ( status && status . statusCode == 200 ) ? ( JSON . parse ( trello ) ) . length : "Can't connect to to Trello" ;
2015-02-17 07:35:02 +00:00
res . send ( { "trello" : trello } ) ;
} ) ;
} ,
2015-03-15 00:25:56 +00:00
bloggerCalls : function ( req , res , next ) {
2015-03-07 23:57:30 +00:00
request ( 'https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/posts?key=' + secrets . blogger . key , function ( err , status , blog ) {
2015-03-15 00:25:56 +00:00
if ( err ) { return next ( err ) ; }
2015-03-14 23:13:08 +00:00
blog = ( status && status . statusCode == 200 ) ? JSON . parse ( blog ) : '' ;
2015-02-17 23:35:16 +00:00
res . send ( {
blog1Title : blog ? blog [ "items" ] [ 0 ] [ "title" ] : "Can't connect to Blogger" ,
blog1Link : blog ? blog [ "items" ] [ 0 ] [ "url" ] : "http://blog.freecodecamp.com" ,
blog2Title : blog ? blog [ "items" ] [ 1 ] [ "title" ] : "Can't connect to Blogger" ,
blog2Link : blog ? blog [ "items" ] [ 1 ] [ "url" ] : "http://blog.freecodecamp.com" ,
blog3Title : blog ? blog [ "items" ] [ 2 ] [ "title" ] : "Can't connect to Blogger" ,
blog3Link : blog ? blog [ "items" ] [ 2 ] [ "url" ] : "http://blog.freecodecamp.com" ,
blog4Title : blog ? blog [ "items" ] [ 3 ] [ "title" ] : "Can't connect to Blogger" ,
blog4Link : blog ? blog [ "items" ] [ 3 ] [ "url" ] : "http://blog.freecodecamp.com" ,
blog5Title : blog ? blog [ "items" ] [ 4 ] [ "title" ] : "Can't connect to Blogger" ,
blog5Link : blog ? blog [ "items" ] [ 4 ] [ "url" ] : "http://blog.freecodecamp.com"
} ) ;
2015-02-17 07:35:02 +00:00
} ) ;
} ,
2015-01-27 07:51:59 +00:00
about : function ( req , res ) {
2015-03-08 22:16:44 +00:00
if ( req . user ) {
2015-03-12 06:58:16 +00:00
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" ;
2015-03-08 22:59:18 +00:00
req . user . save ( ) ;
2015-03-08 22:16:44 +00:00
}
}
2015-01-27 07:51:59 +00:00
var date1 = new Date ( "10/15/2014" ) ;
var date2 = new Date ( ) ;
var timeDiff = Math . abs ( date2 . getTime ( ) - date1 . getTime ( ) ) ;
var daysRunning = Math . ceil ( timeDiff / ( 1000 * 3600 * 24 ) ) ;
2015-02-17 07:35:02 +00:00
var announcements = resources . announcements ;
2015-02-26 22:38:22 +00:00
function numberWithCommas ( x ) {
return x . toString ( ) . replace ( /\B(?=(\d{3})+(?!\d))/g , "," ) ;
}
2015-02-19 06:58:10 +00:00
User . count ( { } , function ( err , c3 ) {
2015-02-17 07:35:02 +00:00
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
2015-02-19 06:58:10 +00:00
User . count ( { 'points' : { '$gt' : 53 } } , function ( err , all ) {
2015-02-17 07:35:02 +00:00
if ( err ) {
debug ( 'User err: ' , err ) ;
next ( err ) ;
}
2015-02-26 22:38:22 +00:00
2015-02-19 06:58:10 +00:00
res . render ( 'resources/learn-to-code' , {
title : 'About Free Code Camp and Our Team of Volunteers' ,
daysRunning : daysRunning ,
2015-02-26 22:38:22 +00:00
c3 : numberWithCommas ( c3 ) ,
2015-02-19 06:58:10 +00:00
all : all ,
announcements : announcements
2015-01-13 05:28:58 +00:00
} ) ;
2015-01-07 07:08:54 +00:00
} ) ;
} ) ;
2015-01-27 07:51:59 +00:00
} ,
2015-01-25 15:02:41 +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 ) ] ;
2015-01-26 23:28:14 +00:00
} ,
2015-01-27 06:22:02 +00:00
allBonfireIds : function ( ) {
return bonfires . map ( function ( elem ) {
2015-01-27 08:29:17 +00:00
return {
_id : elem . _id ,
difficulty : elem . difficulty
}
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
2015-01-27 06:22:02 +00:00
return elem . _id ;
2015-02-01 22:22:34 +00:00
} ) ;
} ,
allBonfireNames : function ( ) {
return bonfires . map ( function ( elem ) {
return {
name : elem . name ,
difficulty : elem . difficulty
}
2015-01-27 06:22:02 +00:00
} )
2015-02-01 22:22:34 +00:00
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return elem . name ;
} ) ;
2015-02-02 07:35:27 +00:00
} ,
allCoursewareIds : function ( ) {
return coursewares . map ( function ( elem ) {
return {
_id : elem . _id ,
difficulty : elem . difficulty
2015-03-07 08:42:22 +00:00
} ;
2015-02-02 07:35:27 +00:00
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return elem . _id ;
} ) ;
} ,
allCoursewareNames : function ( ) {
return coursewares . map ( function ( elem ) {
return {
name : elem . name ,
difficulty : elem . difficulty
2015-03-07 08:42:22 +00:00
} ;
2015-02-02 07:35:27 +00:00
} )
. sort ( function ( a , b ) {
return a . difficulty - b . difficulty ;
} )
. map ( function ( elem ) {
return elem . name ;
} ) ;
2015-02-07 00:55:48 +00:00
} ,
whichEnvironment : function ( ) {
return process . env . NODE _ENV ;
2015-03-06 08:25:28 +00:00
} ,
2015-03-07 08:42:22 +00:00
getURLTitle : function ( url , callback ) {
2015-03-09 04:08:39 +00:00
debug ( 'got url in meta scraping function' , url ) ;
2015-03-07 08:42:22 +00:00
( function ( ) {
2015-03-09 09:21:41 +00:00
var result = { title : '' , image : '' , url : '' , description : '' } ;
2015-03-07 08:42:22 +00:00
request ( url , function ( error , response , body ) {
if ( ! error && response . statusCode === 200 ) {
var $ = cheerio . load ( body ) ;
2015-03-09 09:21:41 +00:00
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' ) : '' ;
2015-03-10 17:05:29 +00:00
result . title = $ ( 'title' ) . text ( ) . length < 141 ? $ ( 'title' ) . text ( ) : $ ( 'title' ) . text ( ) . slice ( 0 , 137 ) + " ..." ;
2015-03-09 04:08:39 +00:00
result . image = urlImage ;
2015-03-09 09:21:41 +00:00
result . description = description ;
2015-03-07 08:42:22 +00:00
callback ( null , result ) ;
} else {
callback ( 'failed' ) ;
}
} ) ;
} ) ( ) ;
2015-03-08 07:50:51 +00:00
} ,
2015-03-09 02:59:50 +00:00
updateUserStoryPictures : function ( userId , picture , username ) {
2015-03-08 07:50:51 +00:00
var counter = 0 ,
foundStories ,
foundComments ;
2014-12-23 16:48:28 +00:00
2015-03-08 07:50:51 +00:00
Story . find ( { 'author.userId' : userId } , function ( err , stories ) {
if ( err ) {
throw err ;
}
foundStories = stories ;
counter ++ ;
saveStoriesAndComments ( ) ;
} ) ;
Comment . find ( { 'author.userId' : userId } , function ( err , comments ) {
if ( err ) {
throw err ;
}
foundComments = comments ;
counter ++ ;
saveStoriesAndComments ( ) ;
} ) ;
2014-12-23 16:48:28 +00:00
2015-03-08 07:50:51 +00:00
function saveStoriesAndComments ( ) {
if ( counter !== 2 ) {
return ;
}
R . forEach ( function ( comment ) {
comment . author . picture = picture ;
2015-03-09 02:59:50 +00:00
comment . author . username = username ;
2015-03-08 07:50:51 +00:00
comment . markModified ( 'author' ) ;
comment . save ( ) ;
} , foundComments ) ;
2014-11-12 01:52:03 +00:00
2015-03-08 07:50:51 +00:00
R . forEach ( function ( story ) {
story . author . picture = picture ;
2015-03-09 02:59:50 +00:00
story . author . username = username ;
2015-03-08 07:50:51 +00:00
story . markModified ( 'author' ) ;
story . save ( ) ;
} , foundStories ) ;
}
}
} ;