2015-06-11 18:38:08 +00:00
|
|
|
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) {
|
2015-06-11 19:13:22 +00:00
|
|
|
var userIdent = ctx.currentInstance || ctx.instance;
|
2015-06-11 20:19:44 +00:00
|
|
|
if (!userIdent) {
|
2015-06-11 19:13:22 +00:00
|
|
|
debug('no user identity instance found');
|
|
|
|
return next();
|
|
|
|
}
|
2015-06-11 18:38:08 +00:00
|
|
|
userIdent.user(function(err, user) {
|
|
|
|
if (err) { return next(err); }
|
2015-06-11 19:13:22 +00:00
|
|
|
if (!user) {
|
|
|
|
debug('no user attached to identity!');
|
|
|
|
return next();
|
|
|
|
}
|
2015-06-11 18:38:08 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|