Merge branch 'staging' into remove-angular

Conflicts:
	server/boot/user.js
	server/views/account/show.jade
This commit is contained in:
Quincy Larson
2015-10-06 16:00:59 -07:00
82 changed files with 3453 additions and 1621 deletions

View File

@@ -1,12 +1,19 @@
import _ from 'lodash';
import async from 'async';
import dedent from 'dedent';
import moment from 'moment';
import { Observable } from 'rx';
import debugFactory from 'debug';
import { ifNoUser401 } from '../utils/middleware';
import {
frontEndChallangeId,
fullStackChallangeId
} from '../utils/constantStrings.json';
import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
import { observeQuery } from '../utils/rx';
const debug = debugFactory('freecc:boot:user');
const daysBetween = 1.5;
const sendNonUserToMap = ifNoUserRedirectTo('/map');
function calcCurrentStreak(cals) {
const revCals = cals.concat([Date.now()]).slice().reverse();
@@ -52,7 +59,16 @@ function dayDiff([head, tail]) {
module.exports = function(app) {
var router = app.loopback.Router();
var User = app.models.User;
var Story = app.models.Story;
function findUserByUsername$(username, fields) {
return observeQuery(
User,
'findOne',
{
where: { username },
fields
}
);
}
router.get('/login', function(req, res) {
res.redirect(301, '/signin');
@@ -68,15 +84,35 @@ module.exports = function(app) {
router.post('/reset-password', postReset);
router.get('/email-signup', getEmailSignup);
router.get('/email-signin', getEmailSignin);
router.get('/account/api', getAccountAngular);
router.get(
'/toggle-lockdown-mode',
sendNonUserToMap,
toggleLockdownMode
);
router.post(
'/account/delete',
ifNoUser401,
postDeleteAccount
);
router.get('/account/unlink/:provider', getOauthUnlink);
router.get('/account', getAccount);
// Ensure this is the last route!
router.get(
'/account',
sendNonUserToMap,
getAccount
);
router.get('/vote1', vote1);
router.get('/vote2', vote2);
// Ensure these are the last routes!
router.get(
'/:username/front-end-certification',
showCert
);
router.get(
'/:username/full-stack-certification',
showCert
);
router.get('/:username', returnUser);
app.use(router);
@@ -114,18 +150,8 @@ module.exports = function(app) {
}
function getAccount(req, res) {
if (!req.user) {
return res.redirect('/');
}
res.render('account/account', {
title: 'Manage your Free Code Camp Account'
});
}
function getAccountAngular(req, res) {
res.json({
user: req.user || {}
});
const { username } = req.user;
return res.redirect('/' + username);
}
function returnUser(req, res, next) {
@@ -133,26 +159,18 @@ module.exports = function(app) {
const { path } = req;
User.findOne(
{ where: { username } },
function(err, user) {
function(err, profileUser) {
if (err) {
return next(err);
}
if (!user) {
if (!profileUser) {
req.flash('errors', {
msg: `404: We couldn't find path ${ path }`
});
return res.redirect('/');
}
if (!user.isGithubCool && !user.isMigrationGrandfathered) {
req.flash('errors', {
msg: `
user ${ username } has not completed account signup
`
});
return res.redirect('/');
}
var cals = user
var cals = profileUser
.progressTimestamps
.map(objOrNum => {
return typeof objOrNum === 'number' ?
@@ -161,10 +179,10 @@ module.exports = function(app) {
})
.sort();
user.currentStreak = calcCurrentStreak(cals);
user.longestStreak = calcLongestStreak(cals);
profileUser.currentStreak = calcCurrentStreak(cals);
profileUser.longestStreak = calcLongestStreak(cals);
const data = user
const data = profileUser
.progressTimestamps
.map((objOrNum) => {
return typeof objOrNum === 'number' ?
@@ -179,34 +197,53 @@ module.exports = function(app) {
return data;
}, {});
const challenges = user.completedChallenges.filter(function(obj) {
const baseAndZip = profileUser.completedChallenges.filter(
function(obj) {
return obj.challengeType === 3 || obj.challengeType === 4;
});
}
);
const bonfires = user.completedChallenges.filter(function(obj) {
const bonfires = profileUser.completedChallenges.filter(function(obj) {
return obj.challengeType === 5 && (obj.name || '').match(/Bonfire/g);
});
const waypoints = profileUser.completedChallenges.filter(function(obj) {
return (obj.name || '').match(/^Waypoint/i);
});
debug('user is fec', profileUser.isFrontEndCert);
res.render('account/show', {
title: 'Camper ' + user.username + '\'s portfolio',
username: user.username,
name: user.name,
isMigrationGrandfathered: user.isMigrationGrandfathered,
isGithubCool: user.isGithubCool,
location: user.location,
github: user.githubURL,
linkedin: user.linkedin,
google: user.google,
facebook: user.facebook,
twitter: user.twitter,
picture: user.picture,
progressTimestamps: user.progressTimestamps,
title: 'Camper ' + profileUser.username + '\'s portfolio',
username: profileUser.username,
name: profileUser.name,
isMigrationGrandfathered: profileUser.isMigrationGrandfathered,
isGithubCool: profileUser.isGithubCool,
isLocked: !!profileUser.isLocked,
isFrontEndCert: profileUser.isFrontEndCert,
isFullStackCert: profileUser.isFullStackCert,
isHonest: profileUser.isHonest,
location: profileUser.location,
calender: data,
challenges: challenges,
bonfires: bonfires,
moment: moment,
longestStreak: user.longestStreak,
currentStreak: user.currentStreak
github: profileUser.githubURL,
linkedin: profileUser.linkedin,
google: profileUser.google,
facebook: profileUser.facebook,
twitter: profileUser.twitter,
picture: profileUser.picture,
progressTimestamps: profileUser.progressTimestamps,
baseAndZip,
bonfires,
waypoints,
moment,
longestStreak: profileUser.longestStreak,
currentStreak: profileUser.currentStreak
});
}
);
@@ -237,6 +274,134 @@ module.exports = function(app) {
});
}
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('/');
});
}
function showCert(req, res, next) {
const username = req.params.username.toLowerCase();
const { user } = req;
const showFront = req.path.split('/').pop() === 'front-end-certification';
Observable.just(user)
.flatMap(user => {
if (user && user.username === username) {
return Observable.just(user);
}
return findUserByUsername$(username, {
isFrontEndCert: true,
isFullStackCert: true,
completedChallenges: true,
username: true,
name: true
});
})
.subscribe(
(user) => {
if (!user) {
req.flash('errors', {
msg: `404: We couldn't find the user ${username}`
});
return res.redirect('/');
}
if (!user.isGithubCool) {
req.flash('errors', {
msg: dedent`
This user needs to link GitHub with their account
in order to display this certificate to the public.
`
});
return res.redirect('back');
}
if (user.isLocked) {
req.flash('errors', {
msg: dedent`
${username} has chosen to hide their work from the public.
They need to unhide their work in order for this certificate to
be verifiable.
`
});
return res.redirect('back');
}
if (!user.isHonest) {
req.flash('errors', {
msg: dedent`
${username} has not agreed to our Academic Honesty Pledge yet.
`
});
return res.redirect('back');
}
if (
showFront && user.isFrontEndCert ||
!showFront && user.isFullStackCert
) {
var { completedDate } = _.find(user.completedChallenges, {
id: showFront ?
frontEndChallangeId :
fullStackChallangeId
});
return res.render(
showFront ?
'certificate/front-end.jade' :
'certificate/full-stack.jade',
{
username: user.username,
date: moment(new Date(completedDate))
.format('MMMM, Do YYYY'),
name: user.name
}
);
}
req.flash('errors', {
msg: showFront ?
`Looks like user ${username} is not Front End certified` :
`Looks like user ${username} is not Full Stack certified`
});
res.redirect('/map');
},
next
);
}
function toggleLockdownMode(req, res, next) {
if (req.user.isLocked === true) {
req.user.isLocked = false;
return req.user.save(function(err) {
if (err) { return next(err); }
req.flash('success', {
msg: dedent`
Other people can now view all your challenge solutions.
You can change this back at any time in the "Manage My Account"
section at the bottom of this page.
`
});
res.redirect('/' + req.user.username);
});
}
req.user.isLocked = true;
return req.user.save(function(err) {
if (err) { return next(err); }
req.flash('success', {
msg: dedent`
All your challenge solutions are now hidden from other people.
You can change this back at any time in the "Manage My Account"
section at the bottom of this page.
`
});
res.redirect('/' + req.user.username);
});
}
function postDeleteAccount(req, res, next) {
User.destroyById(req.user.id, function(err) {
if (err) { return next(err); }
@@ -246,25 +411,6 @@ module.exports = function(app) {
});
}
function getOauthUnlink(req, res, next) {
var provider = req.params.provider;
User.findById(req.user.id, function(err, user) {
if (err) { return next(err); }
user[provider] = null;
user.tokens =
_.reject(user.tokens, function(token) {
return token.kind === provider;
});
user.save(function(err) {
if (err) { return next(err); }
req.flash('info', { msg: provider + ' account has been unlinked.' });
res.redirect('/account');
});
});
}
function getReset(req, res) {
if (!req.accessToken) {
req.flash('errors', { msg: 'access token invalid' });
@@ -306,11 +452,6 @@ module.exports = function(app) {
});
}
/**
* POST /forgot
* Create a random token, then the send user an email with a reset link.
*/
function postForgot(req, res) {
const errors = req.validationErrors();
const email = req.body.email.toLowerCase();
@@ -337,6 +478,7 @@ module.exports = function(app) {
});
}
/*
function updateUserStoryPictures(userId, picture, username, cb) {
Story.find({ 'author.userId': userId }, function(err, stories) {
if (err) { return cb(err); }
@@ -357,4 +499,35 @@ module.exports = function(app) {
});
});
}
*/
function vote1(req, res, next) {
if (req.user) {
req.user.tshirtVote = 1;
req.user.save(function(err) {
if (err) { return next(err); }
req.flash('success', { msg: 'Thanks for voting!' });
res.redirect('/map');
});
} else {
req.flash('error', { msg: 'You must be signed in to vote.' });
res.redirect('/map');
}
}
function vote2(req, res, next) {
if (req.user) {
req.user.tshirtVote = 2;
req.user.save(function(err) {
if (err) { return next(err); }
req.flash('success', { msg: 'Thanks for voting!' });
res.redirect('/map');
});
} else {
req.flash('error', {msg: 'You must be signed in to vote.'});
res.redirect('/map');
}
}
};