2015-06-03 23:19:23 +00:00
|
|
|
var Rx = require('rx'),
|
2015-06-01 23:48:16 +00:00
|
|
|
async = require('async'),
|
|
|
|
moment = require('moment'),
|
|
|
|
request = require('request'),
|
|
|
|
debug = require('debug')('freecc:cntr:resources'),
|
2015-06-04 00:14:45 +00:00
|
|
|
constantStrings = require('../utils/constantStrings.json'),
|
2015-10-07 07:26:17 +00:00
|
|
|
labs = require('../resources/labs.json'),
|
|
|
|
testimonials = require('../resources/testimonials.json'),
|
2015-06-02 22:05:26 +00:00
|
|
|
secrets = require('../../config/secrets');
|
2015-06-01 23:48:16 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
module.exports = function(app) {
|
2015-06-03 23:31:42 +00:00
|
|
|
var router = app.loopback.Router();
|
2015-06-03 23:19:23 +00:00
|
|
|
var User = app.models.User;
|
|
|
|
var Challenge = app.models.Challenge;
|
2015-06-21 03:54:39 +00:00
|
|
|
var Story = app.models.Story;
|
2015-06-03 23:19:23 +00:00
|
|
|
var Nonprofit = app.models.Nonprofit;
|
|
|
|
|
|
|
|
router.get('/api/github', githubCalls);
|
|
|
|
router.get('/api/blogger', bloggerCalls);
|
|
|
|
router.get('/api/trello', trelloCalls);
|
|
|
|
router.get('/sitemap.xml', sitemap);
|
|
|
|
router.get('/chat', chat);
|
2015-08-01 00:30:27 +00:00
|
|
|
router.get('/coding-bootcamp-cost-calculator', bootcampCalculator);
|
2015-06-03 23:19:23 +00:00
|
|
|
router.get('/twitch', twitch);
|
|
|
|
router.get('/pmi-acp-agile-project-managers', agileProjectManagers);
|
|
|
|
router.get('/pmi-acp-agile-project-managers-form', agileProjectManagersForm);
|
|
|
|
router.get('/nonprofits', nonprofits);
|
|
|
|
router.get('/nonprofits-form', nonprofitsForm);
|
|
|
|
router.get('/unsubscribe/:email', unsubscribe);
|
|
|
|
router.get('/unsubscribed', unsubscribed);
|
2015-08-08 23:13:23 +00:00
|
|
|
router.get('/get-started', getStarted);
|
2015-08-14 00:20:52 +00:00
|
|
|
router.get('/submit-cat-photo', submitCatPhoto);
|
2015-10-07 01:19:43 +00:00
|
|
|
router.get('/labs', showLabs);
|
2015-10-07 07:26:17 +00:00
|
|
|
router.get('/stories', showTestimonials);
|
2016-02-06 05:06:30 +00:00
|
|
|
router.get('/shop', showShop);
|
2016-02-17 23:07:12 +00:00
|
|
|
router.get('/shop/cancel-stickers', cancelStickers);
|
|
|
|
router.get('/shop/confirm-stickers', confirmStickers);
|
2015-11-04 01:39:02 +00:00
|
|
|
router.get('/all-stories', showAllTestimonials);
|
2016-01-13 21:37:00 +00:00
|
|
|
router.get('/terms', terms);
|
|
|
|
router.get('/privacy', privacy);
|
|
|
|
router.get('/code-of-conduct', codeOfConduct);
|
2016-02-14 00:32:14 +00:00
|
|
|
router.get('/academic-honesty', academicHonesty);
|
|
|
|
|
2015-12-07 06:42:52 +00:00
|
|
|
router.get(
|
|
|
|
'/the-fastest-web-page-on-the-internet',
|
|
|
|
theFastestWebPageOnTheInternet
|
|
|
|
);
|
2015-06-03 23:19:23 +00:00
|
|
|
|
2015-06-03 23:31:42 +00:00
|
|
|
app.use(router);
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
function sitemap(req, res, next) {
|
|
|
|
var appUrl = 'http://www.freecodecamp.com';
|
|
|
|
var now = moment(new Date()).format('YYYY-MM-DD');
|
|
|
|
|
|
|
|
// TODO(berks): refactor async to rx
|
|
|
|
async.parallel({
|
|
|
|
users: function(callback) {
|
|
|
|
User.find(
|
|
|
|
{
|
2015-06-21 04:15:41 +00:00
|
|
|
where: { username: { nlike: '' } },
|
|
|
|
fields: { username: true }
|
2015-06-03 23:19:23 +00:00
|
|
|
},
|
|
|
|
function(err, users) {
|
|
|
|
if (err) {
|
|
|
|
debug('User err: ', err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2015-06-21 04:15:41 +00:00
|
|
|
Rx.Observable.from(users, null, null, Rx.Scheduler.default)
|
2015-06-03 23:19:23 +00:00
|
|
|
.map(function(user) {
|
2015-06-04 19:54:41 +00:00
|
|
|
return user.username;
|
2015-06-03 23:19:23 +00:00
|
|
|
})
|
|
|
|
.toArray()
|
|
|
|
.subscribe(
|
|
|
|
function(usernames) {
|
|
|
|
callback(null, usernames);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-07 07:28:42 +00:00
|
|
|
challenges: function(callback) {
|
2015-06-03 23:19:23 +00:00
|
|
|
Challenge.find(
|
|
|
|
{ fields: { name: true } },
|
2015-10-07 07:28:42 +00:00
|
|
|
function(err, challenges) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) {
|
|
|
|
debug('Challenge err: ', err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2015-06-21 04:15:41 +00:00
|
|
|
Rx.Observable.from(challenges, null, null, Rx.Scheduler.default)
|
2015-06-03 23:19:23 +00:00
|
|
|
.map(function(challenge) {
|
|
|
|
return challenge.name;
|
|
|
|
})
|
|
|
|
.toArray()
|
|
|
|
.subscribe(
|
|
|
|
callback.bind(callback, null),
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2015-10-07 07:28:42 +00:00
|
|
|
stories: function(callback) {
|
2015-06-03 23:19:23 +00:00
|
|
|
Story.find(
|
|
|
|
{ field: { link: true } },
|
2015-10-07 07:28:42 +00:00
|
|
|
function(err, stories) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) {
|
|
|
|
debug('Story err: ', err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2015-06-21 04:15:41 +00:00
|
|
|
Rx.Observable.from(stories, null, null, Rx.Scheduler.default)
|
2015-06-03 23:19:23 +00:00
|
|
|
.map(function(story) {
|
|
|
|
return story.link;
|
|
|
|
})
|
|
|
|
.toArray()
|
|
|
|
.subscribe(
|
|
|
|
callback.bind(callback, null),
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2015-10-07 07:28:42 +00:00
|
|
|
nonprofits: function(callback) {
|
2015-06-03 23:19:23 +00:00
|
|
|
Nonprofit.find(
|
|
|
|
{ field: { name: true } },
|
|
|
|
function(err, nonprofits) {
|
|
|
|
if (err) {
|
|
|
|
debug('User err: ', err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2015-06-21 04:15:41 +00:00
|
|
|
Rx.Observable.from(nonprofits, null, null, Rx.Scheduler.default)
|
2015-06-03 23:19:23 +00:00
|
|
|
.map(function(nonprofit) {
|
|
|
|
return nonprofit.name;
|
|
|
|
})
|
|
|
|
.toArray()
|
|
|
|
.subscribe(
|
|
|
|
callback.bind(callback, null),
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function(err, results) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-06-21 04:15:41 +00:00
|
|
|
process.nextTick(function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
res.header('Content-Type', 'application/xml');
|
|
|
|
res.render('resources/sitemap', {
|
|
|
|
appUrl: appUrl,
|
|
|
|
now: now,
|
|
|
|
users: results.users,
|
|
|
|
challenges: results.challenges,
|
|
|
|
stories: results.stories,
|
2015-08-01 04:55:49 +00:00
|
|
|
nonprofits: results.nonprofits
|
2015-06-01 22:30:43 +00:00
|
|
|
});
|
2015-06-21 04:15:41 +00:00
|
|
|
});
|
2015-06-01 22:30:43 +00:00
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function chat(req, res) {
|
2015-08-01 00:30:27 +00:00
|
|
|
res.redirect('https://gitter.im/FreeCodeCamp/FreeCodeCamp');
|
2015-06-16 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 01:19:43 +00:00
|
|
|
function showLabs(req, res) {
|
|
|
|
res.render('resources/labs', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Projects Built by Free Code Camp Software Engineers',
|
2015-10-07 01:19:43 +00:00
|
|
|
projects: labs
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:37:00 +00:00
|
|
|
function terms(req, res) {
|
|
|
|
res.render('resources/terms-of-service', {
|
|
|
|
title: 'Terms of Service'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function privacy(req, res) {
|
|
|
|
res.render('resources/privacy', {
|
2016-02-14 02:26:59 +00:00
|
|
|
title: 'Privacy policy'
|
2016-01-13 21:37:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function codeOfConduct(req, res) {
|
|
|
|
res.render('resources/code-of-conduct', {
|
|
|
|
title: 'Code of Conduct'
|
|
|
|
});
|
|
|
|
}
|
2016-01-09 08:30:12 +00:00
|
|
|
|
2016-02-14 00:32:14 +00:00
|
|
|
function academicHonesty(req, res) {
|
|
|
|
res.render('resources/academic-honesty', {
|
|
|
|
title: 'Academic Honesty policy'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-25 16:23:35 +00:00
|
|
|
function theFastestWebPageOnTheInternet(req, res) {
|
|
|
|
res.render('resources/the-fastest-web-page-on-the-internet', {
|
|
|
|
title: 'This is the fastest web page on the internet'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-07 07:26:17 +00:00
|
|
|
function showTestimonials(req, res) {
|
2015-10-07 04:28:00 +00:00
|
|
|
res.render('resources/stories', {
|
2015-11-04 05:51:16 +00:00
|
|
|
title: 'Testimonials from Happy Free Code Camp Students ' +
|
|
|
|
'who got Software Engineer Jobs',
|
2015-11-04 01:58:47 +00:00
|
|
|
stories: testimonials.slice(0, 72),
|
2015-11-04 01:39:02 +00:00
|
|
|
moreStories: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showAllTestimonials(req, res) {
|
|
|
|
res.render('resources/stories', {
|
2015-11-04 05:51:16 +00:00
|
|
|
title: 'Testimonials from Happy Free Code Camp Students ' +
|
|
|
|
'who got Software Engineer Jobs',
|
2015-11-04 01:39:02 +00:00
|
|
|
stories: testimonials,
|
|
|
|
moreStories: false
|
2015-10-07 01:19:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-06 05:06:30 +00:00
|
|
|
function showShop(req, res) {
|
|
|
|
res.render('resources/shop', {
|
|
|
|
title: 'Support Free Code Camp by Buying t-shirts, ' +
|
|
|
|
'stickers, and other goodies'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:07:12 +00:00
|
|
|
function confirmStickers(req, res) {
|
|
|
|
req.flash('success', { msg: 'Thank you for supporting our community! You should receive your stickers in the ' +
|
|
|
|
'mail soon!'});
|
|
|
|
res.redirect('/shop');
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelStickers(req, res) {
|
|
|
|
req.flash('info', { msg: 'You\'ve cancelled your purchase of our stickers. You can '
|
|
|
|
+ 'support our community any time by buying some.'});
|
|
|
|
res.redirect('/shop');
|
|
|
|
}
|
2015-08-14 00:20:52 +00:00
|
|
|
function submitCatPhoto(req, res) {
|
|
|
|
res.send('Submitted!');
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:30:27 +00:00
|
|
|
function bootcampCalculator(req, res) {
|
|
|
|
res.render('resources/calculator', {
|
|
|
|
title: 'Coding Bootcamp Cost Calculator'
|
|
|
|
});
|
2015-06-21 01:40:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
function nonprofits(req, res) {
|
|
|
|
res.render('resources/nonprofits', {
|
2015-10-31 09:59:09 +00:00
|
|
|
title: 'Your Nonprofit Can Get Pro Bono Code'
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function nonprofitsForm(req, res) {
|
|
|
|
res.render('resources/nonprofits-form', {
|
|
|
|
title: 'Nonprofit Projects Proposal Form'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function agileProjectManagers(req, res) {
|
|
|
|
res.render('resources/pmi-acp-agile-project-managers', {
|
|
|
|
title: 'Get Agile Project Management Experience for the PMI-ACP'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function agileProjectManagersForm(req, res) {
|
|
|
|
res.render('resources/pmi-acp-agile-project-managers-form', {
|
|
|
|
title: 'Agile Project Management Program Application Form'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function twitch(req, res) {
|
|
|
|
res.render('resources/twitch', {
|
2015-08-13 08:20:45 +00:00
|
|
|
title: 'Watch us code on Twitch.tv and LiveCoding.tv'
|
2015-06-03 23:19:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsubscribe(req, res, next) {
|
2015-06-21 04:15:41 +00:00
|
|
|
User.findOne({ where: { email: req.params.email } }, function(err, user) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (user) {
|
2015-06-01 22:30:43 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
user.sendMonthlyEmail = false;
|
2015-10-07 07:28:42 +00:00
|
|
|
user.save(function() {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.redirect('/unsubscribed');
|
|
|
|
});
|
|
|
|
} else {
|
2015-06-01 22:30:43 +00:00
|
|
|
res.redirect('/unsubscribed');
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2015-06-01 22:30:43 +00:00
|
|
|
});
|
2015-06-03 23:19:23 +00:00
|
|
|
}
|
2015-06-01 22:30:43 +00:00
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
function unsubscribed(req, res) {
|
|
|
|
res.render('resources/unsubscribed', {
|
|
|
|
title: 'You have been unsubscribed'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:13:23 +00:00
|
|
|
function getStarted(req, res) {
|
|
|
|
res.render('resources/get-started', {
|
|
|
|
title: 'How to get started with Free Code Camp'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-03 23:19:23 +00:00
|
|
|
function githubCalls(req, res, next) {
|
|
|
|
var githubHeaders = {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': constantStrings.gitHubUserAgent
|
2015-06-01 22:30:43 +00:00
|
|
|
},
|
2015-06-03 23:19:23 +00:00
|
|
|
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,
|
2015-10-07 07:28:42 +00:00
|
|
|
function(err, status2, issues) {
|
2015-06-03 23:19:23 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function trelloCalls(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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bloggerCalls(req, res, next) {
|
|
|
|
request(
|
|
|
|
'https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/' +
|
|
|
|
'posts?key=' +
|
|
|
|
secrets.blogger.key,
|
2015-10-07 07:28:42 +00:00
|
|
|
function(err, status, blog) {
|
2015-06-03 23:19:23 +00:00
|
|
|
if (err) { return next(err); }
|
|
|
|
|
|
|
|
blog = (status && status.statusCode === 200) ?
|
|
|
|
JSON.parse(blog) :
|
|
|
|
'Can\'t connect to Blogger';
|
|
|
|
res.end(JSON.stringify(blog));
|
2015-06-01 22:30:43 +00:00
|
|
|
}
|
2015-06-03 23:19:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|