freeCodeCamp/server/boot/randomAPIs.js

287 lines
8.6 KiB
JavaScript
Raw Normal View History

import request from 'request';
import constantStrings from '../utils/constantStrings.json';
import testimonials from '../resources/testimonials.json';
import secrets from '../../config/secrets';
module.exports = function(app) {
const router = app.loopback.Router();
const User = app.models.User;
router.get('/api/github', githubCalls);
router.get('/chat', chat);
router.get('/coding-bootcamp-cost-calculator', bootcampCalculator);
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);
2016-03-15 06:57:04 +00:00
router.get('/unsubscribe/:email', unsubscribeMonthly);
router.get('/unsubscribe-notifications/:email', unsubscribeNotifications);
2016-03-15 07:00:53 +00:00
router.get('/unsubscribe-quincy/:email', unsubscribeQuincy);
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);
router.get('/stories', showTestimonials);
2016-02-06 05:06:30 +00:00
router.get('/shop', showShop);
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);
router.get('/terms', terms);
router.get('/privacy', privacy);
router.get('/how-nonprofit-projects-work', howNonprofitProjectsWork);
router.get('/code-of-conduct', codeOfConduct);
router.get('/academic-honesty', academicHonesty);
2015-12-07 06:42:52 +00:00
router.get(
'/the-fastest-web-page-on-the-internet',
theFastestWebPageOnTheInternet
);
app.use(router);
function chat(req, res) {
res.redirect('https://gitter.im/FreeCodeCamp/FreeCodeCamp');
2015-06-16 04:44:19 +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'
});
}
function howNonprofitProjectsWork(req, res) {
res.render('resources/how-nonprofit-projects-work', {
title: 'How our nonprofit projects work'
});
}
function codeOfConduct(req, res) {
res.render('resources/code-of-conduct', {
title: 'Code of Conduct'
});
}
function academicHonesty(req, res) {
res.render('resources/academic-honesty', {
title: 'Academic Honesty policy'
});
}
function theFastestWebPageOnTheInternet(req, res) {
res.render('resources/the-fastest-web-page-on-the-internet', {
title: 'This is the fastest web page on the internet'
});
}
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'
});
}
function confirmStickers(req, res) {
2016-03-03 04:54:14 +00:00
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) {
2016-03-03 04:54:14 +00:00
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!');
}
function bootcampCalculator(req, res) {
res.render('resources/calculator', {
title: 'Coding Bootcamp Cost Calculator'
});
}
function nonprofits(req, res) {
res.render('resources/nonprofits', {
2015-10-31 09:59:09 +00:00
title: 'Your Nonprofit Can Get Pro Bono Code'
});
}
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) {
2016-03-05 23:20:34 +00:00
res.redirect('https://twitch.tv/freecodecamp');
}
2016-03-15 06:57:04 +00:00
function unsubscribeMonthly(req, res, next) {
2016-03-15 18:22:54 +00:00
req.checkParams('email', 'Must send a valid email').isEmail();
return User.findOne({ where: { email: req.params.email } }, (err, user) => {
if (err) { return next(err); }
2016-03-21 22:14:48 +00:00
if (!user) {
req.flash('info', {
msg: 'Email address not found. ' +
'Please update your Email preferences from your profile.'
});
return res.redirect('/map');
}
return user.updateAttributes({
2016-04-15 15:06:03 +00:00
sendMonthlyEmail: false,
sendQuincyEmail: false,
sendNotificationEmail: false
}, (err) => {
2016-03-03 04:54:14 +00:00
if (err) { return next(err); }
2016-03-15 18:22:54 +00:00
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/unsubscribed');
2016-03-15 06:57:04 +00:00
});
});
}
function unsubscribeNotifications(req, res, next) {
2016-03-15 18:22:54 +00:00
req.checkParams('email', 'Must send a valid email').isEmail();
return User.findOne({ where: { email: req.params.email } }, (err, user) => {
if (err) { return next(err); }
2016-03-21 22:14:48 +00:00
if (!user) {
req.flash('info', {
msg: 'Email address not found. ' +
'Please update your Email preferences from your profile.'
});
return res.redirect('/map');
}
2016-03-15 18:22:54 +00:00
return user.updateAttribute('sendNotificationEmail', false, (err) => {
2016-03-15 06:57:04 +00:00
if (err) { return next(err); }
2016-03-15 18:22:54 +00:00
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/unsubscribed');
2016-03-15 06:57:04 +00:00
});
});
}
function unsubscribeQuincy(req, res, next) {
2016-03-15 18:22:54 +00:00
req.checkParams('email', 'Must send a valid email').isEmail();
return User.findOne({ where: { email: req.params.email } }, (err, user) => {
if (err) { return next(err); }
2016-03-21 22:14:48 +00:00
if (!user) {
req.flash('info', {
msg: 'Email address not found. ' +
'Please update your Email preferences from your profile.'
});
return res.redirect('/map');
}
return user.updateAttributes({
2016-04-15 15:06:03 +00:00
sendQuincyEmail: false,
sendMonthlyEmail: false,
sendNotificationEmail: false
}, (err) => {
2016-03-15 06:57:04 +00:00
if (err) { return next(err); }
2016-03-15 18:22:54 +00:00
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/unsubscribed');
2016-03-15 06:57:04 +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'
});
}
function githubCalls(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';
2016-03-03 04:54:14 +00:00
return 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) {
if (err) { return next(err); }
issues = ((pulls === parseInt(pulls, 10)) && issues) ?
Object.keys(JSON.parse(issues)).length - pulls :
"Can't connect to GitHub";
2016-03-03 04:54:14 +00:00
return res.send({
issues: issues,
pulls: pulls
});
}
);
}
);
}
};