Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / groups / lib / update-group-avatar.js
blob165929483d475a9ab6e701bc581fbe7ff3b32a3a
1 'use strict';
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;
8 const {
9 GitLabGroupService,
10 GitLabProjectService,
11 GitLabUserService
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:'
23 });
25 /**
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,
34 '1',
35 'EX',
36 LOCK_TIMEOUT_SECONDS,
37 'NX'
40 let githubUser = false;
41 if (result === 'OK') {
42 const githubUserService = new GitHubUserService();
43 githubUser = await githubUserService.getUser(githubUsername);
46 debug(
47 `Attempting GitHub avatar update for group=${sanitizedGroupId} githubUsername=${githubUsername} githubUser=${JSON.stringify(
48 githubUser
49 )}`
52 if (!githubUser) {
53 return false;
56 const avatarVersion = extractGravatarVersion(githubUser.avatar_url);
57 if (!avatarVersion) {
58 return false;
61 return Group.update(
63 _id: sanitizedGroupId,
64 $or: [
66 avatarVersion: { $lt: avatarVersion }
69 avatarVersion: null
74 $set: {
75 avatarCheckedDate: new Date(),
76 avatarVersion: avatarVersion
80 .exec()
81 .then(result => {
82 return result.nModified >= 1;
83 });
86 /**
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,
99 '1',
100 'EX',
101 LOCK_TIMEOUT_SECONDS,
102 'NX'
105 let gitlabObject;
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);
117 } else {
118 return false;
122 debug(
123 `Attempting GitLab avatar update for group=${sanitizedGroupId} ${type}=${JSON.stringify(
124 gitlabObject
128 if (!gitlabObject) {
129 return false;
132 return Group.update(
134 _id: sanitizedGroupId
137 $set: {
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.
143 avatarVersion: 1,
144 avatarCheckedDate: new Date()
148 .exec()
149 .then(result => {
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;