3 var StatusError = require('statuserror');
4 var identityService = require('gitter-web-identity');
5 var avatars = require('gitter-web-avatars');
8 * Hide the resolved email address from the caller
10 function maskEmail(email) {
13 .map(function(item, index) {
15 var gmailMagic = item.split('+')[0];
16 return gmailMagic.slice(0, -8) + '****';
23 function validateIsString(value) {
24 if (value && typeof value !== 'string') {
25 throw new StatusError(400);
31 function parseAndValidateInput(input) {
32 // Check for new cleaner method first...
34 if (!input.externalId) throw new StatusError(400);
40 externalId: input.emailAddress,
41 emailAddress: input.emailAddress
45 case identityService.GITHUB_IDENTITY_PROVIDER:
46 case identityService.GITLAB_IDENTITY_PROVIDER:
47 case identityService.TWITTER_IDENTITY_PROVIDER:
50 externalId: input.externalId,
51 emailAddress: input.emailAddress
54 throw new StatusError(400);
58 // Older (crappier) method
61 function addUserIdentifer(identifier, key) {
62 var value = input[key];
65 if (typeof value !== 'string') {
66 throw new StatusError(400);
69 types[identifier] = value;
72 addUserIdentifer('gitter', 'username');
73 addUserIdentifer(identityService.GITHUB_IDENTITY_PROVIDER, 'githubUsername');
74 addUserIdentifer(identityService.GITLAB_IDENTITY_PROVIDER, 'gitlabUsername');
75 // TODO: this doesn't actually work in the rest if the invites code
76 addUserIdentifer(identityService.TWITTER_IDENTITY_PROVIDER, 'twitterUsername');
78 var emailAddress = validateIsString(input.email);
80 var keys = Object.keys(types);
82 // You can't specify more than one username
83 if (keys.length > 1) throw new StatusError(400);
88 // Provided the username from an external service, and
89 // optionally an email address
91 externalId = types[type];
93 // No external username provided. Use the email address if it exists.
94 if (!emailAddress) throw new StatusError(400);
96 externalId = emailAddress;
101 externalId: externalId,
102 emailAddress: emailAddress
106 function getAvatar(type, externalId, resolvedEmailAddress) {
107 // TODO: what about an arbitrary gitter user?
109 if (type === identityService.GITHUB_IDENTITY_PROVIDER) {
110 return avatars.getForGitHubUsername(externalId);
113 // TODO: what about a twitter user? At least one that has signed up.
114 // TODO: what about a gitlab user? At least one that has signed up.
116 if (resolvedEmailAddress) {
117 return avatars.getForGravatarEmail(resolvedEmailAddress);
120 return avatars.getDefault();
124 parseAndValidateInput: parseAndValidateInput,
125 // or should these be elsewhere?
126 maskEmail: maskEmail,