forked from nishant8BITS/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetAvatarSuggestion.js
More file actions
122 lines (104 loc) · 3.14 KB
/
getAvatarSuggestion.js
File metadata and controls
122 lines (104 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* global Gravatar */
function getAvatarSuggestionForUser(user) {
check(user, Object);
const avatars = [];
if (user.services.facebook && user.services.facebook.id && RocketChat.settings.get('Accounts_OAuth_Facebook')) {
avatars.push({
service: 'facebook',
url: `https://graph.facebook.com/${ user.services.facebook.id }/picture?type=large`
});
}
if (user.services.google && user.services.google.picture && user.services.google.picture !== 'https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg' && RocketChat.settings.get('Accounts_OAuth_Google')) {
avatars.push({
service: 'google',
url: user.services.google.picture
});
}
if (user.services.github && user.services.github.username && RocketChat.settings.get('Accounts_OAuth_Github')) {
avatars.push({
service: 'github',
url: `https://avatars.githubusercontent.com/${ user.services.github.username }?s=200`
});
}
if (user.services.linkedin && user.services.linkedin.pictureUrl && RocketChat.settings.get('Accounts_OAuth_Linkedin')) {
avatars.push({
service: 'linkedin',
url: user.services.linkedin.pictureUrl
});
}
if (user.services.twitter && user.services.twitter.profile_image_url_https && RocketChat.settings.get('Accounts_OAuth_Twitter')) {
avatars.push({
service: 'twitter',
url: user.services.twitter.profile_image_url_https.replace(/_normal|_bigger/, '')
});
}
if (user.services.gitlab && user.services.gitlab.avatar_url && RocketChat.settings.get('Accounts_OAuth_Gitlab')) {
avatars.push({
service: 'gitlab',
url: user.services.gitlab.avatar_url
});
}
if (user.services.sandstorm && user.services.sandstorm.picture && Meteor.settings['public'].sandstorm) {
avatars.push({
service: 'sandstorm',
url: user.services.sandstorm.picture
});
}
if (user.emails && user.emails.length > 0) {
for (const email of user.emails) {
if (email.verified === true) {
avatars.push({
service: 'gravatar',
url: Gravatar.imageUrl(email.address, {
'default': '404',
size: 200,
secure: true
})
});
}
if (email.verified !== true) {
avatars.push({
service: 'gravatar',
url: Gravatar.imageUrl(email.address, {
'default': '404',
size: 200,
secure: true
})
});
}
}
}
const validAvatars = {};
for (const avatar of avatars) {
try {
const result = HTTP.get(avatar.url, {
npmRequestOptions: {
encoding: 'binary'
}
});
if (result.statusCode === 200) {
let blob = `data:${ result.headers['content-type'] };base64,`;
blob += Buffer.from(result.content, 'binary').toString('base64');
avatar.blob = blob;
avatar.contentType = result.headers['content-type'];
validAvatars[avatar.service] = avatar;
}
} catch (error) {
// error;
}
}
return validAvatars;
}
this.getAvatarSuggestionForUser = getAvatarSuggestionForUser;
Meteor.methods({
getAvatarSuggestion() {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'getAvatarSuggestion'
});
}
this.unblock();
const user = Meteor.user();
return getAvatarSuggestionForUser(user);
}
});