3 var isGitHubUser = require('../shared/is-github-user');
4 var avatarCdnResolver = require('../shared/avatar-cdn-resolver');
5 var extractTwitterAvatarInfo = require('../shared/extract-twitter-avatar-info');
6 var gravatar = require('./gravatar');
8 var clientEnv = require('gitter-client-env');
9 var avatarUrl = clientEnv.avatarsUrl;
11 var DEFAULT = avatarUrl + '/default';
13 function getForGitHubUsername(githubUsername) {
14 return avatarCdnResolver('/gh/u/' + githubUsername);
17 function getForGravatarEmail(emailAddress) {
18 var hash = gravatar.hashEmail(emailAddress);
19 return avatarCdnResolver('/gravatar/m/' + hash);
22 function getForTwitterUrl(twitterUrl) {
23 var info = extractTwitterAvatarInfo(twitterUrl);
24 if (!info) return DEFAULT;
25 return avatarCdnResolver('/tw/i/' + info.id + '/' + info.filename);
28 function getForGroupId(groupId) {
29 if (!groupId) return DEFAULT;
30 return avatarCdnResolver('/group/i/' + groupId);
33 function getForGroup(group) {
34 if (!group) return DEFAULT;
35 var groupId = group.id || group._id;
36 if (!groupId) return DEFAULT;
38 if (group.avatarVersion) {
39 return avatarCdnResolver('/group/iv/' + group.avatarVersion + '/' + groupId);
41 return getForGroupId(groupId);
46 * This will change in future
48 function getForRoomUri(uri) {
49 if (!uri) return DEFAULT;
50 var orgOrUser = uri.split('/')[0];
51 return avatarCdnResolver('/gh/u/' + orgOrUser);
55 * This will change in future
57 function getForUser(user) {
58 if (!user) return DEFAULT;
59 var username = user.username;
60 if (!username) return DEFAULT;
62 if (!isGitHubUser(user)) {
63 // In future, all users will be routed here
64 // Get our services to resolve the user
65 return avatarCdnResolver('/g/u/' + username);
68 var gv = user.gravatarVersion || user.gv;
71 // Use the versioned interface
72 return avatarCdnResolver('/gh/uv/' + gv + '/' + username);
74 // Use the unversioned interface, with a shorter cache time
75 return avatarCdnResolver('/gh/u/' + username);
79 function getDefault() {
84 getForGitHubUsername: getForGitHubUsername,
85 getForGravatarEmail: getForGravatarEmail,
86 getForTwitterUrl: getForTwitterUrl,
87 getForGroupId: getForGroupId,
88 getForGroup: getForGroup,
89 getForRoomUri: getForRoomUri,
90 getForUser: getForUser,
91 getDefault: getDefault