Update to add biggerphoto on parsing user profile

This commit is contained in:
Wu Cheng-Han 2017-02-03 21:48:36 +08:00
parent ef0ac7768d
commit 8cfbfa4352

View file

@ -79,39 +79,54 @@ module.exports = function (sequelize, DataTypes) {
if (profile) { if (profile) {
profile = { profile = {
name: profile.displayName || profile.username, name: profile.displayName || profile.username,
photo: User.parsePhotoByProfile(profile) photo: User.parsePhotoByProfile(profile),
biggerphoto: User.parsePhotoByProfile(profile, true)
} }
} }
return profile; return profile;
}, },
parsePhotoByProfile: function (profile) { parsePhotoByProfile: function (profile, bigger) {
var photo = null; var photo = null;
switch (profile.provider) { switch (profile.provider) {
case "facebook": case "facebook":
photo = 'https://graph.facebook.com/' + profile.id + '/picture?width=96'; photo = 'https://graph.facebook.com/' + profile.id + '/picture';
if (bigger) photo += '?width=400';
else photo += '?width=96';
break; break;
case "twitter": case "twitter":
photo = 'https://twitter.com/' + profile.username + '/profile_image?size=bigger'; photo = 'https://twitter.com/' + profile.username + '/profile_image';
if (bigger) photo += '?size=original';
else photo += '?size=bigger';
break; break;
case "github": case "github":
photo = 'https://avatars.githubusercontent.com/u/' + profile.id + '?s=96'; photo = 'https://avatars.githubusercontent.com/u/' + profile.id;
if (bigger) photo += '?s=400';
else photo += '?s=96';
break; break;
case "gitlab": case "gitlab":
photo = profile.avatarUrl.replace(/(\?s=)\d*$/i, '$196'); photo = profile.avatarUrl;
if (bigger) photo.replace(/(\?s=)\d*$/i, '$1400');
else photo.replace(/(\?s=)\d*$/i, '$196');
break; break;
case "dropbox": case "dropbox":
//no image api provided, use gravatar //no image api provided, use gravatar
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value) + '?s=96'; photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value);
if (bigger) photo += '?s=400';
else photo += '?s=96';
break; break;
case "google": case "google":
photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196'); photo = profile.photos[0].value;
if (bigger) photo.replace(/(\?sz=)\d*$/i, '$1400');
else photo.replace(/(\?sz=)\d*$/i, '$196');
break; break;
case "ldap": case "ldap":
//no image api provided, //no image api provided,
//use gravatar if email exists, //use gravatar if email exists,
//otherwise generate a letter avatar //otherwise generate a letter avatar
if (profile.emails[0]) { if (profile.emails[0]) {
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]) + '?s=96'; photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]);
if (bigger) photo += '?s=400';
else photo += '?s=96';
} else { } else {
photo = letterAvatars(profile.username); photo = letterAvatars(profile.username);
} }
@ -123,7 +138,8 @@ module.exports = function (sequelize, DataTypes) {
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email); var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email);
return { return {
name: email.substring(0, email.lastIndexOf("@")), name: email.substring(0, email.lastIndexOf("@")),
photo: photoUrl += '?s=96' photo: photoUrl += '?s=96',
biggerphoto: photoUrl += '?s=400'
}; };
} }
} }