Merge branch 'staging' of http://github.com/FreeCodeCamp/freecodecamp into staging

This commit is contained in:
terakilobyte
2015-06-12 15:30:25 -04:00
4 changed files with 40 additions and 25 deletions

View File

@ -3,6 +3,11 @@ var debug = require('debug')('freecc:models:userIdent');
var defaultProfileImage =
require('../utils/constantStrings.json').defaultProfileImage;
function getFirstImageFromProfile(profile) {
return profile && profile.photos && profile.photos[0] ?
profile.photos[0].value :
null;
}
module.exports = function(UserIdent) {
UserIdent.observe('before save', function(ctx, next) {
var userIdent = ctx.currentInstance || ctx.instance;
@ -17,23 +22,27 @@ module.exports = function(UserIdent) {
return next();
}
var picture = userIdent.profile && userIdent.profile[0] ?
userIdent.profile[0].value :
null;
var picture = getFirstImageFromProfile(userIdent.profile);
// check if user has picture
// set user.picture from twitter
if (picture && !user.picture || user.picture === defaultProfileImage) {
debug('use has no pic');
debug('picture', picture, user.picture);
// check if picture was found
// check if user has no picture
// check if user has default picture
// set user.picture from oauth provider
if (
picture &&
(!user.picture || user.picture === defaultProfileImage)
) {
debug('setting user picture');
user.picture = userIdent.profile.photos[0].value;
user.save(function(err) {
return user.save(function(err) {
if (err) { return next(err); }
next();
});
} else {
debug('exiting after user ident');
next();
}
debug('exiting after user ident');
next();
});
});
};

View File

@ -8,6 +8,8 @@ module.exports = function(User) {
// see:
// https://github.com/strongloop/loopback/issues/1137#issuecomment-109200135
delete User.validations.email;
// set salt factor for passwords
User.settings.saltWorkFactor = 5;
debug('setting up user hooks');
// send verification email to new camper
User.afterRemote('create', function(ctx, user, next) {

View File

@ -242,5 +242,5 @@
"property": "doesExist"
}
],
"methods": ["login"]
"methods": []
}

View File

@ -1,32 +1,36 @@
var Rx = require('rx');
var debug = require('debug')('freecc:user:remote');
function destroyById(id, Model) {
return Rx.Observable.create(function(observer) {
Model.destroyById(id, function(err) {
if (err) { return observer.onError(err); }
observer.onCompleted();
});
return Rx.Disposable(Rx.helpers.noop);
});
function destroyAllRelated(id, Model) {
return Rx.Observable.fromNodeCallback(
Model.destroyAll,
Model
)({ userId: id });
}
module.exports = function(app) {
var User = app.models.User;
var UserIdentity = app.models.UserIdentity;
var UserCredential = app.models.UserCredential;
User.observe('after delete', function(ctx, next) {
User.observe('before delete', function(ctx, next) {
debug('removing user', ctx.where);
var id = ctx.where && ctx.where.id ? ctx.where.id : null;
if (!id) {
return next();
}
Rx.Observable.combineLatest(
destroyById(id, UserIdentity),
destroyById(id, UserCredential),
Rx.helpers.noop
destroyAllRelated(id, UserIdentity),
destroyAllRelated(id, UserCredential),
function(identData, credData) {
return {
identData: identData,
credData: credData
};
}
).subscribe(
Rx.helpers.noop,
function(data) {
debug('deleted', data);
},
function(err) {
debug('error deleting user %s stuff', id, err);
next(err);