3 var env
= require('gitter-web-env');
4 var config
= env
.config
;
5 const debug
= require('debug')('gitter:app:groups:updateGroupAvatar');
6 const assert
= require('assert');
7 var GitHubUserService
= require('gitter-web-github').GitHubUserService
;
12 } = require('gitter-web-gitlab');
13 var extractGravatarVersion
= require('gitter-web-avatars/server/extract-gravatar-version');
14 var Group
= require('gitter-web-persistence').Group
;
15 const mongoUtils
= require('gitter-web-persistence-utils/lib/mongo-utils');
16 const getGithubUsernameFromGroup
= require('./get-github-username-from-group');
17 const isGitlabSecurityDescriptorType
= require('gitter-web-shared/is-gitlab-security-descriptor-type');
19 var LOCK_TIMEOUT_SECONDS
= 10;
21 var redisClient
= env
.ioredis
.createClient(config
.get('redis_nopersist'), {
22 keyPrefix
: 'avatar-check:'
26 * Returns true if the avatar was updated
28 async
function updateGitterGroupAvatarForGithub(group
, githubUsername
) {
29 const groupId
= group
._id
;
30 const sanitizedGroupId
= mongoUtils
.asObjectID(groupId
);
32 const result
= await redisClient
.set(
33 'group:' + sanitizedGroupId
,
40 let githubUser
= false;
41 if (result
=== 'OK') {
42 const githubUserService
= new GitHubUserService();
43 githubUser
= await githubUserService
.getUser(githubUsername
);
47 `Attempting GitHub avatar update for group=${sanitizedGroupId} githubUsername=${githubUsername} githubUser=${JSON.stringify(
56 const avatarVersion
= extractGravatarVersion(githubUser
.avatar_url
);
63 _id
: sanitizedGroupId
,
66 avatarVersion
: { $lt
: avatarVersion
}
75 avatarCheckedDate
: new Date(),
76 avatarVersion
: avatarVersion
82 return result
.nModified
>= 1;
87 * Returns true if the avatar was updated
89 async
function updateGitterGroupAvatarForGitlab(group
) {
90 const groupId
= group
._id
;
91 const type
= group
.sd
&& group
.sd
.type
;
92 const externalId
= group
.sd
&& group
.sd
.externalId
;
93 const sanitizedGroupId
= mongoUtils
.asObjectID(groupId
);
95 assert(isGitlabSecurityDescriptorType(type
));
97 const result
= await redisClient
.set(
98 'group:' + sanitizedGroupId
,
101 LOCK_TIMEOUT_SECONDS
,
106 if (result
=== 'OK') {
107 if (type
=== 'GL_GROUP') {
108 // TODO: How do we handle private groups since we aren't passing in a user with GitLab access tokens who has access?
109 const gitlabGroupService
= new GitLabGroupService(/* user */);
110 gitlabObject
= await gitlabGroupService
.getGroup(externalId
);
111 } else if (type
=== 'GL_PROJECT') {
112 const gitlabProjectService
= new GitLabProjectService(/* user */);
113 gitlabObject
= await gitlabProjectService
.getProject(externalId
);
114 } else if (type
=== 'GL_USER') {
115 const gitlabUserService
= new GitLabUserService(/* user */);
116 gitlabObject
= await gitlabUserService
.getUserById(externalId
);
123 `Attempting GitLab avatar update for group=${sanitizedGroupId} ${type}=${JSON.stringify(
134 _id
: sanitizedGroupId
138 // As a note `gitlabGroup.avatar_url` can be `null` if an avatar has not been set yet
139 avatarUrl
: gitlabObject
.avatar_url
,
140 // `avatarVersion` needs to be `1` in order for the
141 // `!group.avatarVersion` logic to work in `group-avatars.js`.
142 // If it's `0`, it always updates avatar.
144 avatarCheckedDate
: new Date()
150 return result
.nModified
>= 1;
154 async
function updateGroupAvatar(group
) {
155 const githubUsername
= getGithubUsernameFromGroup(group
);
156 if (githubUsername
) {
157 return updateGitterGroupAvatarForGithub(group
, githubUsername
);
158 } else if (isGitlabSecurityDescriptorType(group
.sd
&& group
.sd
.type
)) {
159 return updateGitterGroupAvatarForGitlab(group
);
163 module
.exports
= updateGroupAvatar
;