refactor all boot to export func and use loopback

This commit is contained in:
Berkeley Martinez
2015-06-03 16:19:23 -07:00
parent 65cde59f0d
commit 8165105b29
9 changed files with 2028 additions and 1951 deletions

View File

@ -79,6 +79,7 @@
"passport-twitter": "~1.0.2",
"ramda": "~0.10.0",
"request": "~2.53.0",
"rx": "^2.5.3",
"sanitize-html": "~1.6.1",
"sitemap": "~0.7.4",
"twit": "~1.1.20",

View File

@ -1,16 +1,18 @@
var R = require('ramda'),
express = require('express'),
// Rx = require('rx'),
// debug = require('debug')('freecc:fieldguides'),
resources = require('../resources/resources');
var router = express.Router();
module.exports = function(app) {
var router = app.Router();
var FieldGuide = app.models.FieldGuide;
router.get('/field-guide/all-articles', showAllFieldGuides);
router.get('/field-guide/:fieldGuideName', returnIndividualFieldGuide);
router.get('/field-guide/', returnNextFieldGuide);
router.post('/completed-field-guide/', completedFieldGuide);
router.get('/field-guide/all-articles', showAllFieldGuides);
router.get('/field-guide/:fieldGuideName', returnIndividualFieldGuide);
router.get('/field-guide/', returnNextFieldGuide);
router.post('/completed-field-guide/', completedFieldGuide);
function returnIndividualFieldGuide(req, res, next) {
function returnIndividualFieldGuide(req, res, next) {
var dashedName = req.params.fieldGuideName;
if (req.user) {
var completed = req.user.completedFieldGuides;
@ -23,9 +25,12 @@ function returnIndividualFieldGuide(req, res, next) {
});
req.user.uncompletedFieldGuides = uncompletedFieldGuides;
// TODO(berks): handle callback properly
req.user.save();
req.user.save(function(err) {
if (err) { return next(err); }
});
}
// NOTE(berks): loopback might have issue with regex here.
FieldGuide.find(
{ dashedName: new RegExp(dashedName, 'i') },
function(err, fieldGuideFromMongo) {
@ -55,9 +60,9 @@ function returnIndividualFieldGuide(req, res, next) {
});
}
);
}
}
function showAllFieldGuides(req, res) {
function showAllFieldGuides(req, res) {
var allFieldGuideNamesAndIds = resources.allFieldGuideNamesAndIds();
var completedFieldGuides = [];
@ -68,9 +73,9 @@ function showAllFieldGuides(req, res) {
allFieldGuideNamesAndIds: allFieldGuideNamesAndIds,
completedFieldGuides: completedFieldGuides
});
}
}
function returnNextFieldGuide(req, res, next) {
function returnNextFieldGuide(req, res, next) {
if (!req.user) {
return res.redirect('/field-guide/how-do-i-use-this-guide');
}
@ -98,9 +103,9 @@ function returnNextFieldGuide(req, res, next) {
var nameString = fieldGuide.name.toLowerCase().replace(/\s/g, '-');
return res.redirect('../field-guide/' + nameString);
});
}
}
function completedFieldGuide(req, res, next) {
function completedFieldGuide(req, res, next) {
var fieldGuideId = req.body.fieldGuideInfo.fieldGuideId;
req.user.completedFieldGuides.push(fieldGuideId);
@ -117,6 +122,5 @@ function completedFieldGuide(req, res, next) {
}
res.send(true);
});
}
module.exports = router;
}
};

View File

@ -1,11 +1,11 @@
var express = require('express');
var router = express.Router();
var message =
'Learn to Code JavaScript and get a Coding Job by Helping Nonprofits';
router.get('/', index);
module.exports = function(app) {
var router = app.Router();
router.get('/', index);
function index(req, res, next) {
function index(req, res, next) {
if (req.user && !req.user.profile.picture) {
req.user.profile.picture =
'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png';
@ -17,6 +17,5 @@ function index(req, res, next) {
} else {
res.render('home', { title: message });
}
}
module.exports = router;
}
};

View File

@ -1,28 +1,31 @@
var express = require('express'),
Nonprofit = require('../../common/models/Nonprofit');
var router = express.Router();
module.exports = function(app) {
var router = app.Router();
var Nonprofit = app.models.Nonprofit;
router.get('/nonprofits/directory', nonprofitsDirectory);
router.get('/nonprofits/:nonprofitName', returnIndividualNonprofit);
router.get('/nonprofits/directory', nonprofitsDirectory);
router.get('/nonprofits/:nonprofitName', returnIndividualNonprofit);
function nonprofitsDirectory(req, res, next) {
Nonprofit.find({ estimatedHours: { $gt: 0 } }, function(err, nonprofits) {
function nonprofitsDirectory(req, res, next) {
Nonprofit.find(
{ where: { estimatedHours: { $gt: 0 } } },
function(err, nonprofits) {
if (err) { return next(err); }
res.render('nonprofits/directory', {
title: 'Nonprofits we help',
nonprofits: nonprofits
});
});
}
}
);
}
function returnIndividualNonprofit(req, res, next) {
function returnIndividualNonprofit(req, res, next) {
var dashedName = req.params.nonprofitName;
var nonprofitName = dashedName.replace(/\-/g, ' ');
Nonprofit.find(
{ name: new RegExp(nonprofitName, 'i') },
{ where: { name: new RegExp(nonprofitName, 'i') } },
function(err, nonprofit) {
if (err) {
return next(err);
@ -95,10 +98,10 @@ function returnIndividualNonprofit(req, res, next) {
});
}
);
}
}
/*
function interestedInNonprofit(req, res, next) {
/*
function interestedInNonprofit(req, res, next) {
if (req.user) {
Nonprofit.findOne(
{ name: new RegExp(req.params.nonprofitName.replace(/-/, ' '), 'i') },
@ -120,7 +123,6 @@ function interestedInNonprofit(req, res, next) {
}
);
}
}
*/
module.exports = router;
}
*/
};

View File

@ -1,68 +1,67 @@
var express = require('express'),
passport = require('passport'),
var passport = require('passport'),
passportConf = require('../../config/passport');
var router = express.Router();
var passportOptions = {
module.exports = function(app) {
var router = app.Router();
var passportOptions = {
successRedirect: '/',
failureRedirect: '/login'
};
};
router.all('/account', passportConf.isAuthenticated);
router.all('/account', passportConf.isAuthenticated);
router.get('/auth/twitter', passport.authenticate('twitter'));
router.get('/auth/twitter', passport.authenticate('twitter'));
router.get(
router.get(
'/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/login'
})
);
);
router.get(
router.get(
'/auth/linkedin',
passport.authenticate('linkedin', {
state: 'SOME STATE'
})
);
);
router.get(
router.get(
'/auth/linkedin/callback',
passport.authenticate('linkedin', passportOptions)
);
);
router.get(
router.get(
'/auth/facebook',
passport.authenticate('facebook', {scope: ['email', 'user_location']})
);
);
router.get(
router.get(
'/auth/facebook/callback',
passport.authenticate('facebook', passportOptions), function (req, res) {
res.redirect(req.session.returnTo || '/');
}
);
);
router.get('/auth/github', passport.authenticate('github'));
router.get('/auth/github', passport.authenticate('github'));
router.get(
router.get(
'/auth/github/callback',
passport.authenticate('github', passportOptions), function (req, res) {
res.redirect(req.session.returnTo || '/');
}
);
);
router.get(
router.get(
'/auth/google',
passport.authenticate('google', {scope: 'profile email'})
);
);
router.get(
router.get(
'/auth/google/callback',
passport.authenticate('google', passportOptions), function (req, res) {
res.redirect(req.session.returnTo || '/');
}
);
module.exports = router;
);
};

View File

@ -1,47 +1,48 @@
var express = require('express');
var router = express.Router();
module.exports = function(app) {
var router = app.Router();
router.get('/nonprofit-project-instructions', function(req, res) {
router.get('/nonprofit-project-instructions', function(req, res) {
res.redirect(
301,
'/field-guide/how-do-free-code-camp\'s-nonprofit-projects-work'
);
});
});
router.get('/agile', function(req, res) {
router.get('/agile', function(req, res) {
res.redirect(301, '/pmi-acp-agile-project-managers');
});
});
router.get('/live-pair-programming', function(req, res) {
router.get('/live-pair-programming', function(req, res) {
res.redirect(301, '/field-guide/live-stream-pair-programming-on-twitch.tv');
});
});
router.get('/install-screenhero', function(req, res) {
router.get('/install-screenhero', function(req, res) {
res.redirect(301, '/field-guide/install-screenhero');
});
});
router.get('/guide-to-our-nonprofit-projects', function(req, res) {
router.get('/guide-to-our-nonprofit-projects', function(req, res) {
res.redirect(301, '/field-guide/a-guide-to-our-nonprofit-projects');
});
});
router.get('/chromebook', function(req, res) {
router.get('/chromebook', function(req, res) {
res.redirect(301, '/field-guide/chromebook');
});
});
router.get('/deploy-a-website', function(req, res) {
router.get('/deploy-a-website', function(req, res) {
res.redirect(301, '/field-guide/deploy-a-website');
});
});
router.get('/gmail-shortcuts', function(req, res) {
router.get('/gmail-shortcuts', function(req, res) {
res.redirect(301, '/field-guide/gmail-shortcuts');
});
});
router.get('/nodeschool-challenges', function(req, res) {
router.get('/nodeschool-challenges', function(req, res) {
res.redirect(301, '/field-guide/nodeschool-challenges');
});
});
router.get('/privacy', function(req, res) {
res.redirect(301, '/field-guide/what-is-the-free-code-camp-privacy-policy?');
});
module.exports = router;
router.get('/privacy', function(req, res) {
res.redirect(
301, '/field-guide/what-is-the-free-code-camp-privacy-policy?'
);
});
};

View File

@ -1,33 +1,33 @@
var nodemailer = require('nodemailer'),
sanitizeHtml = require('sanitize-html'),
express = require('express'),
moment = require('moment'),
mongodb = require('mongodb'),
// debug = require('debug')('freecc:cntr:story'),
Story = require('../../common/models/Story'),
Comment = require('../../common/models/Comment'),
User = require('../../common/models/User'),
resources = require('../resources/resources'),
MongoClient = mongodb.MongoClient,
secrets = require('../../config/secrets'),
router = express.Router();
secrets = require('../../config/secrets');
router.get('/stories/hotStories', hotJSON);
router.get('/stories/recentStories', recentJSON);
router.get('/stories/comments/:id', comments);
router.post('/stories/comment/', commentSubmit);
router.post('/stories/comment/:id/comment', commentOnCommentSubmit);
router.put('/stories/comment/:id/edit', commentEdit);
router.get('/stories/submit', submitNew);
router.get('/stories/submit/new-story', preSubmit);
router.post('/stories/preliminary', newStory);
router.post('/stories/', storySubmission);
router.get('/news/', hot);
router.post('/stories/search', getStories);
router.get('/news/:storyName', returnIndividualStory);
router.post('/stories/upvote/', upvote);
module.exports = function(app) {
var router = app.Router();
var User = app.models.User;
var Story = app.models.Story;
function hotRank(timeValue, rank) {
router.get('/stories/hotStories', hotJSON);
router.get('/stories/recentStories', recentJSON);
router.get('/stories/comments/:id', comments);
router.post('/stories/comment/', commentSubmit);
router.post('/stories/comment/:id/comment', commentOnCommentSubmit);
router.put('/stories/comment/:id/edit', commentEdit);
router.get('/stories/submit', submitNew);
router.get('/stories/submit/new-story', preSubmit);
router.post('/stories/preliminary', newStory);
router.post('/stories/', storySubmission);
router.get('/news/', hot);
router.post('/stories/search', getStories);
router.get('/news/:storyName', returnIndividualStory);
router.post('/stories/upvote/', upvote);
function hotRank(timeValue, rank) {
/*
* Hotness ranking algorithm: http://amix.dk/blog/post/19588
* tMS = postedOnDate - foundationTime;
@ -39,10 +39,9 @@ function hotRank(timeValue, rank) {
var z = Math.log(rank) / Math.log(10);
hotness = z + (timeValue / time48Hours);
return hotness;
}
}
function hotJSON(req, res, next) {
function hotJSON(req, res, next) {
var story = Story.find({}).sort({'timePosted': -1}).limit(1000);
story.exec(function(err, stories) {
if (err) {
@ -60,9 +59,9 @@ function hotJSON(req, res, next) {
}).slice(0, sliceVal));
});
}
}
function recentJSON(req, res, next) {
function recentJSON(req, res, next) {
var story = Story.find({}).sort({'timePosted': -1}).limit(100);
story.exec(function(err, stories) {
if (err) {
@ -70,40 +69,40 @@ function recentJSON(req, res, next) {
}
return res.json(stories);
});
}
}
function hot(req, res) {
function hot(req, res) {
return res.render('stories/index', {
title: 'Hot stories currently trending on Camper News',
page: 'hot'
});
}
}
function submitNew(req, res) {
function submitNew(req, res) {
return res.render('stories/index', {
title: 'Submit a new story to Camper News',
page: 'submit'
});
}
}
/*
/*
* no used anywhere
function search(req, res) {
function search(req, res) {
return res.render('stories/index', {
title: 'Search the archives of Camper News',
page: 'search'
});
}
}
function recent(req, res) {
function recent(req, res) {
return res.render('stories/index', {
title: 'Recently submitted stories on Camper News',
page: 'recent'
});
}
*/
}
*/
function preSubmit(req, res) {
function preSubmit(req, res) {
var data = req.query;
var cleanData = sanitizeHtml(data.url, {
@ -131,10 +130,10 @@ function preSubmit(req, res) {
storyImage: image,
storyMetaDescription: description
});
}
}
function returnIndividualStory(req, res, next) {
function returnIndividualStory(req, res, next) {
var dashedName = req.params.storyName;
var storyName = dashedName.replace(/\-/g, ' ').trim();
@ -191,9 +190,9 @@ function returnIndividualStory(req, res, next) {
hasUserVoted: userVoted
});
});
}
}
function getStories(req, res, next) {
function getStories(req, res, next) {
MongoClient.connect(secrets.db, function(err, database) {
if (err) {
return next(err);
@ -233,9 +232,9 @@ function getStories(req, res, next) {
return res.sendStatus(404);
});
});
}
}
function upvote(req, res, next) {
function upvote(req, res, next) {
var data = req.body.data;
Story.find({'_id': data.id}, function(err, story) {
if (err) {
@ -252,9 +251,12 @@ function upvote(req, res, next) {
story.markModified('rank');
story.save();
// NOTE(Berks): This logic is full of wholes and race conditions
// this could be the source of many 'can't set headers after they are sent'
// this could be the source of many 'can't set headers after
// they are sent'
// errors. This needs cleaning
User.findOne({'_id': story.author.userId}, function(err, user) {
User.findOne(
{ where: { id: story.author.userId } },
function(err, user) {
if (err) { return next(err); }
user.progressTimestamps.push(Date.now() || 0);
@ -267,23 +269,26 @@ function upvote(req, res, next) {
return next(err);
}
});
});
}
);
return res.send(story);
});
}
}
function comments(req, res, next) {
function comments(req, res, next) {
var data = req.params.id;
Comment.find({'_id': data}, function(err, comment) {
Comment.find(
{ where: {'_id': data } },
function(err, comment) {
if (err) {
return next(err);
}
comment = comment.pop();
return res.send(comment);
});
}
}
function newStory(req, res, next) {
function newStory(req, res, next) {
if (!req.user) {
return next(new Error('Must be logged in'));
}
@ -305,7 +310,9 @@ function newStory(req, res, next) {
if (url.search(/^https?:\/\//g) === -1) {
url = 'http://' + url;
}
Story.find({'link': url}, function(err, story) {
Story.find(
{ where: {'link': url} },
function(err, story) {
if (err) {
return next(err);
}
@ -319,7 +326,8 @@ function newStory(req, res, next) {
});
}
resources.getURLTitle(url, processResponse);
});
}
);
function processResponse(err, story) {
if (err) {
@ -340,9 +348,9 @@ function newStory(req, res, next) {
});
}
}
}
}
function storySubmission(req, res, next) {
function storySubmission(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized'));
@ -360,7 +368,7 @@ function storySubmission(req, res, next) {
}
Story.count({
storyLink: new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i')
storyLink: { like: new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i') }
}, function (err, storyCount) {
if (err) {
return next(err);
@ -416,9 +424,9 @@ function storySubmission(req, res, next) {
});
});
});
}
}
function commentSubmit(req, res, next) {
function commentSubmit(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized'));
@ -453,25 +461,29 @@ function commentSubmit(req, res, next) {
});
commentSave(comment, Story, res, next);
}
}
function commentOnCommentSubmit(req, res, next) {
function commentOnCommentSubmit(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized'));
}
var sanitizedBody = sanitizeHtml(data.body,
var sanitizedBody = sanitizeHtml(
data.body,
{
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
}
).replace(/"/g, '"');
if (data.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
var comment = new Comment({
associatedPost: data.associatedPost,
body: sanitizedBody,
@ -490,11 +502,11 @@ function commentOnCommentSubmit(req, res, next) {
commentOn: Date.now()
});
commentSave(comment, Comment, res, next);
}
}
function commentEdit(req, res, next) {
function commentEdit(req, res, next) {
Comment.find({'_id': req.params.id}, function(err, cmt) {
Comment.find({ id: req.params.id }, function(err, cmt) {
if (err) {
return next(err);
}
@ -504,7 +516,6 @@ function commentEdit(req, res, next) {
return next(new Error('Not authorized'));
}
var sanitizedBody = sanitizeHtml(req.body.body, {
allowedTags: [],
allowedAttributes: []
@ -518,7 +529,7 @@ function commentEdit(req, res, next) {
cmt.body = sanitizedBody;
cmt.commentOn = Date.now();
cmt.save(function (err) {
cmt.save(function(err) {
if (err) {
return next(err);
}
@ -527,9 +538,9 @@ function commentEdit(req, res, next) {
});
}
}
function commentSave(comment, Context, res, next) {
function commentSave(comment, Context, res, next) {
comment.save(function(err, data) {
if (err) {
return next(err);
@ -538,7 +549,7 @@ function commentSave(comment, Context, res, next) {
// Based on the context retrieve the parent
// object of the comment (Story/Comment)
Context.find({
'_id': data.associatedPost
id: data.associatedPost
}, function (err, associatedContext) {
if (err) {
return next(err);
@ -604,6 +615,5 @@ function commentSave(comment, Context, res, next) {
return next(err);
}
});
}
module.exports = router;
}
};

View File

@ -5,63 +5,64 @@ var _ = require('lodash'),
nodemailer = require('nodemailer'),
passport = require('passport'),
moment = require('moment'),
express = require('express'),
debug = require('debug')('freecc:cntr:userController'),
User = require('../../common/models/User'),
secrets = require('../../config/secrets'),
resources = require('./../resources/resources');
var router = express.Router();
router.get('/login', function(req, res) {
res.redirect(301, '/signin');
});
router.get('/logout', function(req, res) {
res.redirect(301, '/signout');
});
router.get('/signin', getSignin);
router.post('/signin', postSignin);
router.get('/signout', signout);
router.get('/forgot', getForgot);
router.post('/forgot', postForgot);
router.get('/reset/:token', getReset);
router.post('/reset/:token', postReset);
router.get('/email-signup', getEmailSignup);
router.get('/email-signin', getEmailSignin);
router.post('/email-signup', postEmailSignup);
router.post('/email-signin', postSignin);
router.get('/account/api', getAccountAngular);
router.get('/api/checkUniqueUsername/:username', checkUniqueUsername);
router.get('/api/checkExistingUsername/:username', checkExistingUsername);
router.get('/api/checkUniqueEmail/:email', checkUniqueEmail);
router.post('/account/profile', postUpdateProfile);
router.post('/account/password', postUpdatePassword);
router.post('/account/delete', postDeleteAccount);
router.get('/account/unlink/:provider', getOauthUnlink);
router.get('/account', getAccount);
// Ensure this is the last route!
router.get('/:username', returnUser);
module.exports = function(app) {
var router = app.Router();
var User = app.models.User;
/**
router.get('/login', function(req, res) {
res.redirect(301, '/signin');
});
router.get('/logout', function(req, res) {
res.redirect(301, '/signout');
});
router.get('/signin', getSignin);
router.post('/signin', postSignin);
router.get('/signout', signout);
router.get('/forgot', getForgot);
router.post('/forgot', postForgot);
router.get('/reset/:token', getReset);
router.post('/reset/:token', postReset);
router.get('/email-signup', getEmailSignup);
router.get('/email-signin', getEmailSignin);
router.post('/email-signup', postEmailSignup);
router.post('/email-signin', postSignin);
router.get('/account/api', getAccountAngular);
router.get('/api/checkUniqueUsername/:username', checkUniqueUsername);
router.get('/api/checkExistingUsername/:username', checkExistingUsername);
router.get('/api/checkUniqueEmail/:email', checkUniqueEmail);
router.post('/account/profile', postUpdateProfile);
router.post('/account/password', postUpdatePassword);
router.post('/account/delete', postDeleteAccount);
router.get('/account/unlink/:provider', getOauthUnlink);
router.get('/account', getAccount);
// Ensure this is the last route!
router.get('/:username', returnUser);
/**
* GET /signin
* Siginin page.
*/
function getSignin (req, res) {
function getSignin (req, res) {
if (req.user) {
return res.redirect('/');
}
res.render('account/signin', {
title: 'Free Code Camp Login'
});
}
}
/**
/**
* POST /signin
* Sign in using email and password.
*/
function postSignin (req, res, next) {
function postSignin (req, res, next) {
req.assert('email', 'Email is not valid').isEmail();
req.assert('password', 'Password cannot be blank').notEmpty();
@ -94,52 +95,52 @@ function postSignin (req, res, next) {
return res.redirect(req.session.returnTo || '/');
});
})(req, res, next);
}
}
/**
/**
* GET /signout
* Log out.
*/
function signout (req, res) {
function signout (req, res) {
req.logout();
res.redirect('/');
}
}
/**
/**
* GET /email-signup
* Signup page.
*/
function getEmailSignin (req, res) {
function getEmailSignin (req, res) {
if (req.user) {
return res.redirect('/');
}
res.render('account/email-signin', {
title: 'Sign in to your Free Code Camp Account'
});
}
}
/**
/**
* GET /signin
* Signup page.
*/
function getEmailSignup (req, res) {
function getEmailSignup (req, res) {
if (req.user) {
return res.redirect('/');
}
res.render('account/email-signup', {
title: 'Create Your Free Code Camp Account'
});
}
}
/**
/**
* POST /email-signup
* Create a new local account.
*/
function postEmailSignup (req, res, next) {
function postEmailSignup (req, res, next) {
req.assert('email', 'valid email required').isEmail();
var errors = req.validationErrors();
@ -233,34 +234,34 @@ function postEmailSignup (req, res, next) {
});
});
});
}
}
/**
/**
* GET /account
* Profile page.
*/
function getAccount (req, res) {
function getAccount (req, res) {
res.render('account/account', {
title: 'Manage your Free Code Camp Account'
});
}
}
/**
/**
* Angular API Call
*/
function getAccountAngular (req, res) {
function getAccountAngular (req, res) {
res.json({
user: req.user
});
}
}
/**
/**
* Unique username check API Call
*/
function checkUniqueUsername (req, res, next) {
function checkUniqueUsername (req, res, next) {
User.count(
{ 'profile.username': req.params.username.toLowerCase() },
function (err, data) {
@ -271,13 +272,13 @@ function checkUniqueUsername (req, res, next) {
return res.send(false);
}
});
}
}
/**
/**
* Existing username check
*/
function checkExistingUsername (req, res, next) {
function checkExistingUsername (req, res, next) {
User.count(
{ 'profile.username': req.params.username.toLowerCase() },
function (err, data) {
@ -289,13 +290,13 @@ function checkExistingUsername (req, res, next) {
}
}
);
}
}
/**
/**
* Unique email check API Call
*/
function checkUniqueEmail (req, res, next) {
function checkUniqueEmail (req, res, next) {
User.count(
{ email: decodeURIComponent(req.params.email).toLowerCase() },
function (err, data) {
@ -307,15 +308,15 @@ function checkUniqueEmail (req, res, next) {
}
}
);
}
}
/**
/**
* GET /campers/:username
* Public Profile page.
*/
function returnUser (req, res, next) {
function returnUser (req, res, next) {
User.find(
{ 'profile.username': req.params.username.toLowerCase() },
function(err, user) {
@ -325,8 +326,8 @@ function returnUser (req, res, next) {
}
if (user[0]) {
user = user[0];
user.progressTimestamps = user.progressTimestamps.sort(function(a, b) {
user.progressTimestamps =
user.progressTimestamps.sort(function(a, b) {
return a - b;
});
@ -442,14 +443,14 @@ function returnUser (req, res, next) {
}
}
);
}
}
/**
/**
* POST /account/profile
* Update profile information.
*/
function postUpdateProfile (req, res, next) {
function postUpdateProfile (req, res, next) {
User.findById(req.user.id, function(err) {
if (err) { return next(err); }
@ -486,29 +487,34 @@ function postUpdateProfile (req, res, next) {
});
return res.redirect('/account');
}
user.email = req.body.email.trim() || '';
user.profile.name = req.body.name.trim() || '';
user.profile.username = req.body.username.trim() || '';
user.profile.location = req.body.location.trim() || '';
user.profile.githubProfile = req.body.githubProfile.trim() || '';
user.profile.facebookProfile = req.body.facebookProfile.trim() || '';
user.profile.linkedinProfile = req.body.linkedinProfile.trim() || '';
user.profile.codepenProfile = req.body.codepenProfile.trim() || '';
user.profile.twitterHandle = req.body.twitterHandle.trim() || '';
user.profile.bio = req.body.bio.trim() || '';
var body = req.body || {};
user.email = body.email.trim() || '';
user.profile.name = body.name.trim() || '';
user.profile.username = body.username.trim() || '';
user.profile.location = body.location.trim() || '';
user.profile.picture = req.body.picture.trim() ||
user.profile.githubProfile = body.githubProfile.trim() || '';
user.profile.facebookProfile = body.facebookProfile.trim() || '';
user.profile.linkedinProfile = body.linkedinProfile.trim() || '';
user.profile.codepenProfile = body.codepenProfile.trim() || '';
user.profile.twitterHandle = body.twitterHandle.trim() || '';
user.profile.bio = body.bio.trim() || '';
user.profile.picture = body.picture.trim() ||
'https://s3.amazonaws.com/freecodecamp/' +
'camper-image-placeholder.png';
user.portfolio.website1Title = req.body.website1Title.trim() || '';
user.portfolio.website1Link = req.body.website1Link.trim() || '';
user.portfolio.website1Image = req.body.website1Image.trim() || '';
user.portfolio.website2Title = req.body.website2Title.trim() || '';
user.portfolio.website2Link = req.body.website2Link.trim() || '';
user.portfolio.website2Image = req.body.website2Image.trim() || '';
user.portfolio.website3Title = req.body.website3Title.trim() || '';
user.portfolio.website3Link = req.body.website3Link.trim() || '';
user.portfolio.website3Image = req.body.website3Image.trim() || '';
user.portfolio.website1Title = body.website1Title.trim() || '';
user.portfolio.website1Link = body.website1Link.trim() || '';
user.portfolio.website1Image = body.website1Image.trim() || '';
user.portfolio.website2Title = body.website2Title.trim() || '';
user.portfolio.website2Link = body.website2Link.trim() || '';
user.portfolio.website2Image = body.website2Image.trim() || '';
user.portfolio.website3Title = body.website3Title.trim() || '';
user.portfolio.website3Link = body.website3Link.trim() || '';
user.portfolio.website3Image = body.website3Image.trim() || '';
user.save(function (err) {
@ -532,15 +538,17 @@ function postUpdateProfile (req, res, next) {
);
});
});
}
}
/**
/**
* POST /account/password
* Update current password.
*/
function postUpdatePassword (req, res, next) {
req.assert('password', 'Password must be at least 4 characters long').len(4);
function postUpdatePassword (req, res, next) {
req.assert('password', 'Password must be at least 4 characters long')
.len(4);
req.assert('confirmPassword', 'Passwords do not match')
.equals(req.body.password);
@ -563,28 +571,28 @@ function postUpdatePassword (req, res, next) {
res.redirect('/account');
});
});
}
}
/**
/**
* POST /account/delete
* Delete user account.
*/
function postDeleteAccount (req, res, next) {
User.remove({ _id: req.user.id }, function(err) {
function postDeleteAccount (req, res, next) {
User.destroyById(req.user.id, function(err) {
if (err) { return next(err); }
req.logout();
req.flash('info', { msg: 'Your account has been deleted.' });
res.redirect('/');
});
}
}
/**
/**
* GET /account/unlink/:provider
* Unlink OAuth provider.
*/
function getOauthUnlink (req, res, next) {
function getOauthUnlink (req, res, next) {
var provider = req.params.provider;
User.findById(req.user.id, function(err, user) {
if (err) { return next(err); }
@ -601,21 +609,25 @@ function getOauthUnlink (req, res, next) {
res.redirect('/account');
});
});
}
}
/**
/**
* GET /reset/:token
* Reset Password page.
*/
function getReset (req, res, next) {
function getReset (req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/');
}
User
.findOne({ resetPasswordToken: req.params.token })
.where('resetPasswordExpires').gt(Date.now())
.exec(function(err, user) {
User.findOne(
{
where: {
resetPasswordToken: req.params.token,
resetPasswordExpires: Date.now()
}
},
function(err, user) {
if (err) { return next(err); }
if (!user) {
req.flash('errors', {
@ -628,14 +640,14 @@ function getReset (req, res, next) {
token: req.params.token
});
});
}
}
/**
/**
* POST /reset/:token
* Process the reset password request.
*/
function postReset (req, res, next) {
function postReset (req, res, next) {
var errors = req.validationErrors();
if (errors) {
@ -645,10 +657,14 @@ function postReset (req, res, next) {
async.waterfall([
function(done) {
User
.findOne({ resetPasswordToken: req.params.token })
.where('resetPasswordExpires').gt(Date.now())
.exec(function(err, user) {
User.findOne(
{
where: {
resetPasswordToken: req.params.token,
resetPasswordExpires: Date.now()
}
},
function(err, user) {
if (err) { return next(err); }
if (!user) {
req.flash('errors', {
@ -702,28 +718,28 @@ function postReset (req, res, next) {
if (err) { return next(err); }
res.redirect('/');
});
}
}
/**
/**
* GET /forgot
* Forgot Password page.
*/
function getForgot (req, res) {
function getForgot (req, res) {
if (req.isAuthenticated()) {
return res.redirect('/');
}
res.render('account/forgot', {
title: 'Forgot Password'
});
}
}
/**
/**
* POST /forgot
* Create a random token, then the send user an email with a reset link.
*/
function postForgot (req, res, next) {
function postForgot (req, res, next) {
var errors = req.validationErrors();
if (errors) {
@ -801,6 +817,5 @@ function postForgot (req, res, next) {
if (err) { return next(err); }
res.redirect('/forgot');
});
}
module.exports = router;
}
};

View File

@ -1,44 +1,45 @@
var express = require('express'),
var Rx = require('rx'),
Twit = require('twit'),
async = require('async'),
moment = require('moment'),
Twit = require('twit'),
Slack = require('node-slack'),
request = require('request'),
debug = require('debug')('freecc:cntr:resources'),
constantStrings = require('../resources/constantStrings.json'),
User = require('../../common/models/User'),
Challenge = require('../../common/models/Challenge'),
Story = require('../../common/models/Story'),
FieldGuide = require('../../common/models/FieldGuide'),
Nonprofit = require('../../common/models/Nonprofit'),
constantStrings = require('../resources/constantStrings.json'),
secrets = require('../../config/secrets');
var slack = new Slack(secrets.slackHook);
var router = express.Router();
module.exports = function(app) {
var router = app.Router();
var User = app.models.User;
var Challenge = app.models.Challenge;
var Story = app.models.Store;
var FieldGuide = app.models.FieldGuide;
var Nonprofit = app.models.Nonprofit;
router.get('/api/github', githubCalls);
router.get('/api/blogger', bloggerCalls);
router.get('/api/trello', trelloCalls);
router.get('/api/codepen/twitter/:screenName', twitter);
router.get('/sitemap.xml', sitemap);
router.post('/get-help', getHelp);
router.post('/get-pair', getPair);
router.get('/chat', chat);
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('/jobs-form', jobsForm);
router.get('/submit-cat-photo', catPhotoSubmit);
router.get('/unsubscribe/:email', unsubscribe);
router.get('/unsubscribed', unsubscribed);
router.get('/cats.json', getCats);
router.get('/api/github', githubCalls);
router.get('/api/blogger', bloggerCalls);
router.get('/api/trello', trelloCalls);
router.get('/api/codepen/twitter/:screenName', twitter);
router.get('/sitemap.xml', sitemap);
router.post('/get-help', getHelp);
router.post('/get-pair', getPair);
router.get('/chat', chat);
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('/jobs-form', jobsForm);
router.get('/submit-cat-photo', catPhotoSubmit);
router.get('/unsubscribe/:email', unsubscribe);
router.get('/unsubscribed', unsubscribed);
router.get('/cats.json', getCats);
router.get('/api/slack', slackInvite);
router.get('/api/slack', slackInvite);
function slackInvite(req, res, next) {
function slackInvite(req, res, next) {
if (req.user) {
if (req.user.email) {
var invite = {
@ -63,7 +64,8 @@ function slackInvite(req, res, next) {
if (!error && response.statusCode === 200) {
req.flash('success', {
msg: 'We\'ve successfully requested an invite for you.' +
' Please check your email and follow the instructions from Slack.'
' Please check your email and follow the ' +
'instructions from Slack.'
});
req.user.sentSlackInvite = true;
req.user.save(function(err) {
@ -97,9 +99,9 @@ function slackInvite(req, res, next) {
});
return res.redirect('/account');
}
}
}
function twitter(req, res, next) {
function twitter(req, res, next) {
// sends out random tweets about javascript
var T = new Twit({
'consumer_key': secrets.twitter.consumerKey,
@ -126,10 +128,10 @@ function twitter(req, res, next) {
return res.json(data);
}
);
}
}
function getHelp(req, res) {
function getHelp(req, res) {
var userName = req.user.profile.username;
var code = req.body.payload.code ? '\n```\n' +
req.body.payload.code + '\n```\n'
@ -147,9 +149,9 @@ function getHelp(req, res) {
'3609875545/569237541c920fa78d78902069615caf.jpeg'
});
return res.sendStatus(200);
}
}
function getPair(req, res) {
function getPair(req, res) {
var userName = req.user.profile.username;
var challenge = req.body.payload.challenge;
slack.send({
@ -171,84 +173,130 @@ function getPair(req, res) {
].join(''),
channel: '#letspair',
username: 'Companion Cube',
'icon_url': 'https://lh3.googleusercontent.com/-f6xDPDV2rPE/AAAAAAAAAAI/' +
'icon_url':
'https://lh3.googleusercontent.com/-f6xDPDV2rPE/AAAAAAAAAAI/' +
'AAAAAAAAAAA/mdlESXQu11Q/photo.jpg'
});
return res.sendStatus(200);
}
}
function sitemap(req, res, next) {
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.aggregate()
.group({_id: 1, usernames: { $addToSet: '$profile.username'}})
.match({'profile.username': { $ne: ''}})
.exec(function(err, users) {
User.find(
{
where: { 'profile.username': { nlike: '' } },
fields: { 'profile.username': true }
},
function(err, users) {
if (err) {
debug('User err: ', err);
callback(err);
} else {
callback(null, users[0].usernames);
Rx.Observable.from(users)
.map(function(user) {
return user.profile.username;
})
.toArray()
.subscribe(
function(usernames) {
callback(null, usernames);
},
callback
);
}
});
},
challenges: function (callback) {
Challenge.aggregate()
.group({_id: 1, names: { $addToSet: '$name'}})
.exec(function (err, challenges) {
Challenge.find(
{ fields: { name: true } },
function (err, challenges) {
if (err) {
debug('Challenge err: ', err);
callback(err);
} else {
callback(null, challenges[0].names);
Rx.Observable.from(challenges)
.map(function(challenge) {
return challenge.name;
})
.toArray()
.subscribe(
callback.bind(callback, null),
callback
);
}
});
},
stories: function (callback) {
Story.aggregate()
.group({_id: 1, links: {$addToSet: '$link'}})
.exec(function (err, stories) {
Story.find(
{ field: { link: true } },
function (err, stories) {
if (err) {
debug('Story err: ', err);
callback(err);
} else {
callback(null, stories[0].links);
Rx.Observable.from(stories)
.map(function(story) {
return story.link;
})
.toArray()
.subscribe(
callback.bind(callback, null),
callback
);
}
});
}
);
},
nonprofits: function (callback) {
Nonprofit.aggregate()
.group({_id: 1, names: { $addToSet: '$name'}})
.exec(function (err, nonprofits) {
Nonprofit.find(
{ field: { name: true } },
function(err, nonprofits) {
if (err) {
debug('User err: ', err);
callback(err);
} else {
callback(null, nonprofits[0].names);
Rx.Observable.from(nonprofits)
.map(function(nonprofit) {
return nonprofit.name;
})
.toArray()
.subscribe(
callback.bind(callback, null),
callback
);
}
});
},
fieldGuides: function (callback) {
FieldGuide.aggregate()
.group({_id: 1, names: { $addToSet: '$name'}})
.exec(function (err, fieldGuides) {
fieldGuides: function(callback) {
FieldGuide.find(
{ field: { name: true } },
function(err, fieldGuides) {
if (err) {
debug('User err: ', err);
callback(err);
} else {
callback(null, fieldGuides[0].names);
Rx.Observable.from(fieldGuides)
.map(function(fieldGuide) {
return fieldGuide.name;
})
.toArray()
.subscribe(
callback.bind(callback, null),
callback
);
}
});
}
}, function (err, results) {
}, function(err, results) {
if (err) {
return next(err);
} else {
}
setTimeout(function() {
res.header('Content-Type', 'application/xml');
res.render('resources/sitemap', {
@ -262,11 +310,10 @@ function sitemap(req, res, next) {
});
}, 0);
}
}
);
}
}
function chat(req, res) {
function chat(req, res) {
if (req.user && req.user.progressTimestamps.length > 5) {
res.redirect('http://freecodecamp.slack.com');
} else {
@ -274,53 +321,53 @@ function chat(req, res) {
title: 'Watch us code live on Twitch.tv'
});
}
}
}
function jobsForm(req, res) {
function jobsForm(req, res) {
res.render('resources/jobs-form', {
title: 'Employer Partnership Form for Job Postings,' +
' Recruitment and Corporate Sponsorships'
});
}
}
function catPhotoSubmit(req, res) {
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.'
);
}
}
function nonprofits(req, res) {
function nonprofits(req, res) {
res.render('resources/nonprofits', {
title: 'A guide to our Nonprofit Projects'
});
}
}
function nonprofitsForm(req, res) {
function nonprofitsForm(req, res) {
res.render('resources/nonprofits-form', {
title: 'Nonprofit Projects Proposal Form'
});
}
}
function agileProjectManagers(req, res) {
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) {
function agileProjectManagersForm(req, res) {
res.render('resources/pmi-acp-agile-project-managers-form', {
title: 'Agile Project Management Program Application Form'
});
}
}
function twitch(req, res) {
function twitch(req, res) {
res.render('resources/twitch', {
title: 'Enter Free Code Camp\'s Chat Rooms'
});
}
}
function unsubscribe(req, res, next) {
function unsubscribe(req, res, next) {
User.findOne({ email: req.params.email }, function(err, user) {
if (user) {
if (err) {
@ -337,15 +384,15 @@ function unsubscribe(req, res, next) {
res.redirect('/unsubscribed');
}
});
}
}
function unsubscribed(req, res) {
function unsubscribed(req, res) {
res.render('resources/unsubscribed', {
title: 'You have been unsubscribed'
});
}
}
function githubCalls(req, res, next) {
function githubCalls(req, res, next) {
var githubHeaders = {
headers: {
'User-Agent': constantStrings.gitHubUserAgent
@ -389,9 +436,9 @@ function githubCalls(req, res, next) {
);
}
);
}
}
function trelloCalls(req, res, next) {
function trelloCalls(req, res, next) {
request(
'https://trello.com/1/boards/BA3xVpz9/cards?key=' +
secrets.trello.key,
@ -403,9 +450,9 @@ function trelloCalls(req, res, next) {
res.end(JSON.stringify(trello));
});
}
}
function bloggerCalls(req, res, next) {
function bloggerCalls(req, res, next) {
request(
'https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/' +
'posts?key=' +
@ -419,9 +466,9 @@ function bloggerCalls(req, res, next) {
res.end(JSON.stringify(blog));
}
);
}
}
function getCats(req, res) {
function getCats(req, res) {
res.send(
[
{
@ -441,6 +488,5 @@ function getCats(req, res) {
}
]
);
}
module.exports = router;
}
};