2019-02-18 19:32:49 +00:00
|
|
|
const githubRegex = /github/i;
|
2015-08-21 23:08:49 +00:00
|
|
|
const providerHash = {
|
|
|
|
facebook: ({ id }) => id,
|
2017-07-13 18:39:07 +00:00
|
|
|
github: ({ username }) => username,
|
2015-08-21 23:08:49 +00:00
|
|
|
twitter: ({ username }) => username,
|
|
|
|
linkedin({ _json }) {
|
2019-02-18 19:32:49 +00:00
|
|
|
return (_json && _json.publicProfileUrl) || null;
|
2015-08-21 23:08:49 +00:00
|
|
|
},
|
|
|
|
google: ({ id }) => id
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getUsernameFromProvider(provider, profile) {
|
2019-02-18 19:32:49 +00:00
|
|
|
return typeof providerHash[provider] === 'function'
|
|
|
|
? providerHash[provider](profile)
|
|
|
|
: null;
|
2015-08-21 23:08:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:39:07 +00:00
|
|
|
// createProfileAttributes(provider: String, profile: {}) => Object
|
|
|
|
export function createUserUpdatesFromProfile(provider, profile) {
|
|
|
|
if (githubRegex.test(provider)) {
|
|
|
|
return createProfileAttributesFromGithub(profile);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
[getSocialProvider(provider)]: getUsernameFromProvider(
|
|
|
|
getSocialProvider(provider),
|
|
|
|
profile
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// createProfileAttributes(profile) => profileUpdate
|
|
|
|
function createProfileAttributesFromGithub(profile) {
|
|
|
|
const {
|
2018-05-15 13:56:26 +00:00
|
|
|
profileUrl: githubProfile,
|
2017-07-13 18:39:07 +00:00
|
|
|
username,
|
2019-02-18 19:32:49 +00:00
|
|
|
_json: { avatar_url: picture, blog: website, location, bio, name } = {}
|
2017-07-13 18:39:07 +00:00
|
|
|
} = profile;
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
location,
|
|
|
|
bio,
|
|
|
|
website,
|
|
|
|
picture,
|
2018-05-15 13:56:26 +00:00
|
|
|
githubProfile
|
2017-07-13 18:39:07 +00:00
|
|
|
};
|
2015-08-12 19:15:19 +00:00
|
|
|
}
|
2015-08-13 02:08:05 +00:00
|
|
|
|
|
|
|
export function getSocialProvider(provider) {
|
|
|
|
return provider.split('-')[0];
|
|
|
|
}
|