Merge branch 'staging' into nonprofit-improvements
Conflicts: controllers/resources.js seed_data/bonfires.json views/partials/navbar.jade
This commit is contained in:
@@ -1,32 +1,32 @@
|
||||
var async = require('async'),
|
||||
path = require('path'),
|
||||
moment = require('moment'),
|
||||
Twit = require('twit'),
|
||||
debug = require('debug')('freecc:cntr:resources'),
|
||||
cheerio = require('cheerio'),
|
||||
request = require('request'),
|
||||
R = require('ramda'),
|
||||
_ = require('lodash'),
|
||||
fs = require('fs'),
|
||||
|
||||
constantStrings = require('./constantStrings.json'),
|
||||
User = require('../models/User'),
|
||||
Challenge = require('./../models/Challenge'),
|
||||
Courseware = require('./../models/Courseware'),
|
||||
Bonfire = require('./../models/Bonfire'),
|
||||
Story = require('./../models/Story'),
|
||||
FieldGuide = require('./../models/FieldGuide'),
|
||||
Nonprofit = require('./../models/Nonprofit'),
|
||||
Comment = require('./../models/Comment'),
|
||||
resources = require('./resources.json'),
|
||||
secrets = require('./../config/secrets'),
|
||||
bonfires = require('../seed_data/bonfires.json'),
|
||||
nonprofits = require('../seed_data/nonprofits.json'),
|
||||
coursewares = require('../seed_data/coursewares.json'),
|
||||
fieldGuides = require('../seed_data/field-guides.json'),
|
||||
moment = require('moment'),
|
||||
Twit = require('twit'),
|
||||
https = require('https'),
|
||||
debug = require('debug')('freecc:cntr:resources'),
|
||||
cheerio = require('cheerio'),
|
||||
request = require('request'),
|
||||
R = require('ramda');
|
||||
fieldGuides = require('../seed_data/field-guides.json');
|
||||
|
||||
/**
|
||||
* Cached values
|
||||
*/
|
||||
var allBonfireIds, allBonfireNames, allCoursewareIds, allCoursewareNames,
|
||||
allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
|
||||
allBonfireIndexesAndNames;
|
||||
var allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
|
||||
challengeMap, challengeMapWithIds,
|
||||
challengeMapWithNames, allChallengeIds, allChallenges;
|
||||
|
||||
/**
|
||||
* GET /
|
||||
@@ -44,7 +44,81 @@ Array.zip = function(left, right, combinerFunction) {
|
||||
return results;
|
||||
};
|
||||
|
||||
(function() {
|
||||
if (!challengeMap) {
|
||||
var localChallengeMap = {};
|
||||
var files = fs.readdirSync(
|
||||
path.join(__dirname, '/../seed_data/challenges')
|
||||
);
|
||||
var keyCounter = 0;
|
||||
files = files.map(function (file) {
|
||||
return require(
|
||||
path.join(__dirname, '/../seed_data/challenges/' + file)
|
||||
);
|
||||
});
|
||||
files = files.sort(function (a, b) {
|
||||
return a.order - b.order;
|
||||
});
|
||||
files.forEach(function (file) {
|
||||
localChallengeMap[keyCounter++] = file;
|
||||
});
|
||||
challengeMap = _.cloneDeep(localChallengeMap);
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
||||
getChallengeMapWithIds: function() {
|
||||
if (!challengeMapWithIds) {
|
||||
challengeMapWithIds = {};
|
||||
Object.keys(challengeMap).forEach(function (key) {
|
||||
var onlyIds = challengeMap[key].challenges.map(function (elem) {
|
||||
return elem._id;
|
||||
});
|
||||
challengeMapWithIds[key] = onlyIds;
|
||||
});
|
||||
}
|
||||
return challengeMapWithIds;
|
||||
},
|
||||
|
||||
allChallengeIds: function() {
|
||||
|
||||
if (!allChallengeIds) {
|
||||
allChallengeIds = [];
|
||||
Object.keys(this.getChallengeMapWithIds()).forEach(function(key) {
|
||||
allChallengeIds.push(challengeMapWithIds[key]);
|
||||
});
|
||||
allChallengeIds = R.flatten(allChallengeIds);
|
||||
}
|
||||
return allChallengeIds;
|
||||
},
|
||||
|
||||
allChallenges: function() {
|
||||
if (!allChallenges) {
|
||||
allChallenges = [];
|
||||
Object.keys(this.getChallengeMapWithNames()).forEach(function(key) {
|
||||
allChallenges.push(challengeMap[key].challenges);
|
||||
});
|
||||
allChallenges = R.flatten(allChallenges);
|
||||
}
|
||||
return allChallenges;
|
||||
},
|
||||
|
||||
getChallengeMapWithNames: function() {
|
||||
if (!challengeMapWithNames) {
|
||||
challengeMapWithNames = {};
|
||||
Object.keys(challengeMap).
|
||||
forEach(function (key) {
|
||||
var onlyNames = challengeMap[key].challenges.map(function (elem) {
|
||||
return elem.name;
|
||||
});
|
||||
challengeMapWithNames[key] = onlyNames;
|
||||
});
|
||||
}
|
||||
return challengeMapWithNames;
|
||||
},
|
||||
|
||||
sitemap: function sitemap(req, res, next) {
|
||||
var appUrl = 'http://www.freecodecamp.com';
|
||||
var now = moment(new Date()).format('YYYY-MM-DD');
|
||||
@@ -66,29 +140,17 @@ module.exports = {
|
||||
},
|
||||
|
||||
challenges: function (callback) {
|
||||
Courseware.aggregate()
|
||||
Challenge.aggregate()
|
||||
.group({_id: 1, names: { $addToSet: '$name'}})
|
||||
.exec(function (err, challenges) {
|
||||
if (err) {
|
||||
debug('Courseware err: ', err);
|
||||
debug('Challenge err: ', err);
|
||||
callback(err);
|
||||
} else {
|
||||
callback(null, challenges[0].names);
|
||||
}
|
||||
});
|
||||
},
|
||||
bonfires: function (callback) {
|
||||
Bonfire.aggregate()
|
||||
.group({_id: 1, names: { $addToSet: '$name'}})
|
||||
.exec(function (err, bonfires) {
|
||||
if (err) {
|
||||
debug('Bonfire err: ', err);
|
||||
callback(err);
|
||||
} else {
|
||||
callback(null, bonfires[0].names);
|
||||
}
|
||||
});
|
||||
},
|
||||
stories: function (callback) {
|
||||
Story.aggregate()
|
||||
.group({_id: 1, links: {$addToSet: '$link'}})
|
||||
@@ -136,7 +198,6 @@ module.exports = {
|
||||
now: now,
|
||||
users: results.users,
|
||||
challenges: results.challenges,
|
||||
bonfires: results.bonfires,
|
||||
stories: results.stories,
|
||||
nonprofits: results.nonprofits,
|
||||
fieldGuides: results.fieldGuides
|
||||
@@ -152,7 +213,7 @@ module.exports = {
|
||||
res.redirect('http://freecode.slack.com');
|
||||
} else {
|
||||
res.render('resources/chat', {
|
||||
title: "Watch us code live on Twitch.tv"
|
||||
title: 'Watch us code live on Twitch.tv'
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -169,6 +230,13 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
catPhotoSubmit: function catPhotoSubmit(req, res) {
|
||||
res.send(
|
||||
'Success! You have submitted your cat photo. Return to your website ' +
|
||||
'by typing any letter into your code editor.'
|
||||
);
|
||||
},
|
||||
|
||||
nonprofits: function nonprofits(req, res) {
|
||||
res.render('resources/nonprofits', {
|
||||
title: 'A guide to our Nonprofit Projects'
|
||||
@@ -199,8 +267,8 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
unsubscribe: function unsubscribe(req, res) {
|
||||
User.findOne({email: req.params.email}, function(err, user) {
|
||||
unsubscribe: function unsubscribe(req, res, next) {
|
||||
User.findOne({ email: req.params.email }, function(err, user) {
|
||||
if (user) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
@@ -220,52 +288,106 @@ module.exports = {
|
||||
|
||||
unsubscribed: function unsubscribed(req, res) {
|
||||
res.render('resources/unsubscribed', {
|
||||
title: "You have been unsubscribed"
|
||||
title: 'You have been unsubscribed'
|
||||
});
|
||||
},
|
||||
|
||||
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});
|
||||
});
|
||||
});
|
||||
githubCalls: function(req, res, next) {
|
||||
var githubHeaders = {
|
||||
headers: {
|
||||
'User-Agent': constantStrings.gitHubUserAgent
|
||||
},
|
||||
port: 80
|
||||
};
|
||||
request(
|
||||
[
|
||||
'https://api.github.com/repos/freecodecamp/',
|
||||
'freecodecamp/pulls?client_id=',
|
||||
secrets.github.clientID,
|
||||
'&client_secret=',
|
||||
secrets.github.clientSecret
|
||||
].join(''),
|
||||
githubHeaders,
|
||||
function(err, status1, pulls) {
|
||||
if (err) { return next(err); }
|
||||
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
|
||||
].join(''),
|
||||
githubHeaders,
|
||||
function (err, status2, issues) {
|
||||
if (err) { return next(err); }
|
||||
issues = ((pulls === parseInt(pulls, 10)) && issues) ?
|
||||
Object.keys(JSON.parse(issues)).length - pulls :
|
||||
"Can't connect to GitHub";
|
||||
res.send({
|
||||
issues: issues,
|
||||
pulls: pulls
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
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));
|
||||
});
|
||||
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));
|
||||
});
|
||||
},
|
||||
|
||||
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));
|
||||
});
|
||||
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));
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
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";
|
||||
if (
|
||||
!req.user.profile.picture ||
|
||||
req.user.profile.picture.indexOf('apple-touch-icon-180x180.png') !== -1
|
||||
) {
|
||||
req.user.profile.picture =
|
||||
'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png';
|
||||
// TODO(berks): unhandled callback
|
||||
req.user.save();
|
||||
}
|
||||
}
|
||||
var date1 = new Date("10/15/2014");
|
||||
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));
|
||||
var announcements = resources.announcements;
|
||||
function numberWithCommas(x) {
|
||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
}
|
||||
User.count({}, function (err, c3) {
|
||||
if (err) {
|
||||
@@ -283,113 +405,41 @@ module.exports = {
|
||||
},
|
||||
|
||||
randomPhrase: function() {
|
||||
return resources.phrases[Math.floor(
|
||||
Math.random() * resources.phrases.length)];
|
||||
return resources.phrases[
|
||||
Math.floor(Math.random() * resources.phrases.length)
|
||||
];
|
||||
},
|
||||
|
||||
randomVerb: function() {
|
||||
return resources.verbs[Math.floor(
|
||||
Math.random() * resources.verbs.length)];
|
||||
return resources.verbs[
|
||||
Math.floor(Math.random() * resources.verbs.length)
|
||||
];
|
||||
},
|
||||
|
||||
randomCompliment: function() {
|
||||
return resources.compliments[Math.floor(
|
||||
Math.random() * resources.compliments.length)];
|
||||
},
|
||||
|
||||
allBonfireIds: function() {
|
||||
if (allBonfireIds) {
|
||||
return allBonfireIds;
|
||||
} else {
|
||||
allBonfireIds = 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;
|
||||
});
|
||||
return allBonfireIds;
|
||||
}
|
||||
},
|
||||
|
||||
bonfiresIndexesAndNames: function() {
|
||||
if (allBonfireIndexesAndNames) {
|
||||
return allBonfireIndexesAndNames
|
||||
} else {
|
||||
var obj = {};
|
||||
bonfires.forEach(function(elem) {
|
||||
obj[elem._id] = elem.name;
|
||||
});
|
||||
allBonfireIndexesAndNames = obj;
|
||||
return allBonfireIndexesAndNames;
|
||||
}
|
||||
},
|
||||
|
||||
ensureBonfireNames: function(completedBonfires) {
|
||||
return completedBonfires.map(function(elem) {
|
||||
return ({
|
||||
name: this.bonfiresIndexesAndNames()[elem._id],
|
||||
_id: elem.id,
|
||||
completedDate: elem.completedDate,
|
||||
completedWith: elem.completedWith,
|
||||
solution: elem.solution
|
||||
});
|
||||
}.bind(this));
|
||||
return resources.compliments[
|
||||
Math.floor(Math.random() * resources.compliments.length)
|
||||
];
|
||||
},
|
||||
|
||||
allFieldGuideIds: function() {
|
||||
if (allFieldGuideIds) {
|
||||
return allFieldGuideIds;
|
||||
} else {
|
||||
allFieldGuideIds = fieldGuides.
|
||||
map(function (elem) {
|
||||
return elem._id;
|
||||
});
|
||||
allFieldGuideIds = fieldGuides.map(function (elem) {
|
||||
return elem._id;
|
||||
});
|
||||
return allFieldGuideIds;
|
||||
}
|
||||
},
|
||||
|
||||
allBonfireNames: function() {
|
||||
if (allBonfireNames) {
|
||||
return allBonfireNames;
|
||||
} else {
|
||||
allBonfireNames = 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
|
||||
};
|
||||
});
|
||||
return allBonfireNames;
|
||||
}
|
||||
},
|
||||
|
||||
allFieldGuideNames: function() {
|
||||
if (allFieldGuideNames) {
|
||||
return allFieldGuideNames;
|
||||
} else {
|
||||
allFieldGuideNames = fieldGuides.
|
||||
map(function (elem) {
|
||||
return {
|
||||
name: elem.name
|
||||
};
|
||||
});
|
||||
allFieldGuideNames = fieldGuides.map(function (elem) {
|
||||
return { name: elem.name };
|
||||
});
|
||||
return allFieldGuideNames;
|
||||
}
|
||||
},
|
||||
@@ -398,61 +448,10 @@ module.exports = {
|
||||
if (allNonprofitNames) {
|
||||
return allNonprofitNames;
|
||||
} else {
|
||||
allNonprofitNames = nonprofits.
|
||||
map(function (elem) {
|
||||
return {
|
||||
name: elem.name
|
||||
};
|
||||
});
|
||||
return allNonprofitNames;
|
||||
}
|
||||
},
|
||||
|
||||
allCoursewareIds: function() {
|
||||
if (allCoursewareIds) {
|
||||
return allCoursewareIds;
|
||||
} else {
|
||||
allCoursewareIds = 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;
|
||||
});
|
||||
return allCoursewareIds;
|
||||
}
|
||||
},
|
||||
|
||||
allCoursewareNames: function() {
|
||||
if (allCoursewareNames) {
|
||||
return allCoursewareNames;
|
||||
} else {
|
||||
allCoursewareNames = coursewares.
|
||||
map(function (elem) {
|
||||
return {
|
||||
name: elem.name,
|
||||
difficulty: elem.difficulty,
|
||||
challengeType: elem.challengeType,
|
||||
_id: elem._id
|
||||
};
|
||||
}).
|
||||
sort(function (a, b) {
|
||||
return a.difficulty - b.difficulty;
|
||||
}).
|
||||
map(function (elem) {
|
||||
return {
|
||||
name: elem.name,
|
||||
challengeType: elem.challengeType,
|
||||
_id: elem._id
|
||||
};
|
||||
allNonprofitNames = nonprofits.map(function (elem) {
|
||||
return { name: elem.name };
|
||||
});
|
||||
return allCoursewareNames;
|
||||
return allNonprofitNames;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -467,16 +466,25 @@ module.exports = {
|
||||
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 metaImage = $("meta[property='og:image']");
|
||||
var urlImage = metaImage.attr('content') ?
|
||||
metaImage.attr('content') :
|
||||
'';
|
||||
|
||||
var metaTitle = $('title');
|
||||
var description = metaDescription.attr('content') ? metaDescription.attr('content') : '';
|
||||
result.title = metaTitle.text().length < 90 ? metaTitle.text() : metaTitle.text().slice(0, 87) + "...";
|
||||
var description = metaDescription.attr('content') ?
|
||||
metaDescription.attr('content') :
|
||||
'';
|
||||
|
||||
result.title = metaTitle.text().length < 90 ?
|
||||
metaTitle.text() :
|
||||
metaTitle.text().slice(0, 87) + '...';
|
||||
|
||||
result.image = urlImage;
|
||||
result.description = description;
|
||||
callback(null, result);
|
||||
} else {
|
||||
callback('failed');
|
||||
callback(new Error('failed'));
|
||||
}
|
||||
});
|
||||
})();
|
||||
@@ -536,24 +544,33 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
codepenResources: {
|
||||
twitter: function(req, res) {
|
||||
twitter: function(req, res, next) {
|
||||
// sends out random tweets about javascript
|
||||
var T = new Twit({
|
||||
consumer_key: secrets.twitter.consumerKey,
|
||||
consumer_secret: secrets.twitter.consumerSecret,
|
||||
access_token: secrets.twitter.token,
|
||||
access_token_secret: secrets.twitter.tokenSecret
|
||||
'consumer_key': secrets.twitter.consumerKey,
|
||||
'consumer_secret': secrets.twitter.consumerSecret,
|
||||
'access_token': secrets.twitter.token,
|
||||
'access_token_secret': secrets.twitter.tokenSecret
|
||||
});
|
||||
|
||||
var screenName;
|
||||
if (req.params.screenName) {
|
||||
screenName = req.params.screenName;
|
||||
} else {
|
||||
screenName = 'freecodecamp';
|
||||
}
|
||||
|
||||
T.get('statuses/user_timeline', {screen_name: screenName, count:10}, function(err, data, response) {
|
||||
return res.json(data);
|
||||
});
|
||||
T.get(
|
||||
'statuses/user_timeline',
|
||||
{
|
||||
'screen_name': screenName,
|
||||
count: 10
|
||||
},
|
||||
function(err, data) {
|
||||
if (err) { return next(err); }
|
||||
return res.json(data);
|
||||
}
|
||||
);
|
||||
},
|
||||
twitterFCCStream: function() {
|
||||
// sends out a tweet stream from FCC's account
|
||||
|
Reference in New Issue
Block a user