diff --git a/common/models/User-Identity.js b/common/models/User-Identity.js index b6e03d8a24645f..ff4508d0add40b 100644 --- a/common/models/User-Identity.js +++ b/common/models/User-Identity.js @@ -1,27 +1,34 @@ -// var debug = require('debug')('freecc:models:userIdent'); -// -// module.exports = function(UserIdent) { -// -// UserIdent.observe('before save', function(ctx, next) { -// -// var userIdent = ctx.instance; -// userIdent.user(function(err, user) { -// if (err) { return next(err); } -// debug('got user', user.username); -// -// // check if user has picture -// // set user.picture from twitter -// if (!user.picture) { -// debug('use has no pic'); -// user.picture = userIdent.profile.photos[0].value; -// user.save(function(err) { -// if (err) { return next(err); } -// next(); -// }); -// } else { -// debug('exiting after user ident'); -// next(); -// } -// }); -// }); -// }; +var debug = require('debug')('freecc:models:userIdent'); + +var defaultProfileImage = + require('../utils/constantStrings.json').defaultProfileImage; + +module.exports = function(UserIdent) { + + UserIdent.observe('before save', function(ctx, next) { + var userIdent = ctx.currentInstance; + // treat userIdent as immutable + userIdent.user(function(err, user) { + if (err) { return next(err); } + debug('got user', user.username); + + var picture = userIdent.profile && userIdent.profile[0] ? + userIdent.profile[0].value : + null; + + // check if user has picture + // set user.picture from twitter + if (picture && !user.picture || user.picture === defaultProfileImage) { + debug('use has no pic'); + user.picture = userIdent.profile.photos[0].value; + user.save(function(err) { + if (err) { return next(err); } + next(); + }); + } else { + debug('exiting after user ident'); + next(); + } + }); + }); +}; diff --git a/common/utils/constantStrings.json b/common/utils/constantStrings.json new file mode 100644 index 00000000000000..e144beb2b62f47 --- /dev/null +++ b/common/utils/constantStrings.json @@ -0,0 +1,3 @@ +{ + "defaultProfileImage": "https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png" +} diff --git a/server/boot/home.js b/server/boot/home.js index affbcef8b78aa5..2a75265e1ac7b7 100644 --- a/server/boot/home.js +++ b/server/boot/home.js @@ -1,3 +1,5 @@ +var defaultProfileImage = + require('../../common/utils/constantStrings.json').defaultProfileImage; var message = 'Learn to Code JavaScript and get a Coding Job by Helping Nonprofits'; @@ -9,8 +11,7 @@ module.exports = function(app) { function index(req, res, next) { if (req.user && !req.user.picture) { - req.user.picture = - 'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png'; + req.user.picture = defaultProfileImage; req.user.save(function(err) { if (err) { return next(err); }