fix grab user photo from profile
This commit is contained in:
@ -3,6 +3,11 @@ var debug = require('debug')('freecc:models:userIdent');
|
|||||||
var defaultProfileImage =
|
var defaultProfileImage =
|
||||||
require('../utils/constantStrings.json').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) {
|
module.exports = function(UserIdent) {
|
||||||
UserIdent.observe('before save', function(ctx, next) {
|
UserIdent.observe('before save', function(ctx, next) {
|
||||||
var userIdent = ctx.currentInstance || ctx.instance;
|
var userIdent = ctx.currentInstance || ctx.instance;
|
||||||
@ -17,23 +22,27 @@ module.exports = function(UserIdent) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
var picture = userIdent.profile && userIdent.profile[0] ?
|
var picture = getFirstImageFromProfile(userIdent.profile);
|
||||||
userIdent.profile[0].value :
|
|
||||||
null;
|
|
||||||
|
|
||||||
// check if user has picture
|
debug('picture', picture, user.picture);
|
||||||
// set user.picture from twitter
|
// check if picture was found
|
||||||
if (picture && !user.picture || user.picture === defaultProfileImage) {
|
// check if user has no picture
|
||||||
debug('use has no pic');
|
// 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.picture = userIdent.profile.photos[0].value;
|
||||||
user.save(function(err) {
|
return user.save(function(err) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
debug('exiting after user ident');
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug('exiting after user ident');
|
||||||
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user