Skip to content

Commit d87505d

Browse files
authored
Merge pull request #854 from hackmdio/feature/disableGravatar
Allow to disable gravatar
2 parents b8726bb + 318b2d3 commit d87505d

5 files changed

Lines changed: 23 additions & 27 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ There are some config settings you need to change in the files below.
209209
| `HMD_EMAIL` | `true` or `false` | set to allow email signin |
210210
| `HMD_ALLOW_PDF_EXPORT` | `true` or `false` | Enable or disable PDF exports |
211211
| `HMD_ALLOW_EMAIL_REGISTER` | `true` or `false` | set to allow email register (only applied when email is set, default is `true`. Note `bin/manage_users` might help you if registration is `false`.) |
212+
| `HMD_ALLOW_GRAVATAR` | `true` or `false` | set to `false` to disable gravatar as profile picture source on your instance |
212213
| `HMD_IMAGE_UPLOAD_TYPE` | `imgur`, `s3`, `minio` or `filesystem` | Where to upload images. For S3, see our Image Upload Guides for [S3](docs/guides/s3-image-upload.md) or [Minio](docs/guides/minio-image-upload.md) |
213214
| `HMD_S3_ACCESS_KEY_ID` | no example | AWS access key id |
214215
| `HMD_S3_SECRET_ACCESS_KEY` | no example | AWS secret key |
@@ -271,6 +272,7 @@ There are some config settings you need to change in the files below.
271272
| `documentMaxLength` | `100000` | note max length |
272273
| `email` | `true` or `false` | set to allow email signin |
273274
| `allowEmailRegister` | `true` or `false` | set to allow email register (only applied when email is set, default is `true`. Note `bin/manage_users` might help you if registration is `false`.) |
275+
| `allowGravatar` | `true` or `false` | set to `false` to disable gravatar as profile picture source on your instance |
274276
| `imageUploadType` | `imgur`, `s3`, `minio`, `azure` or `filesystem`(default) | Where to upload images. For S3, see our Image Upload Guides for [S3](docs/guides/s3-image-upload.md) or [Minio](docs/guides/minio-image-upload.md)|
275277
| `minio` | `{ "accessKey": "YOUR_MINIO_ACCESS_KEY", "secretKey": "YOUR_MINIO_SECRET_KEY", "endpoint": "YOUR_MINIO_HOST", port: 9000, secure: true }` | When `imageUploadType` is set to `minio`, you need to set this key. Also checkout our [Minio Image Upload Guide](docs/guides/minio-image-upload.md) |
276278
| `s3` | `{ "accessKeyId": "YOUR_S3_ACCESS_KEY_ID", "secretAccessKey": "YOUR_S3_ACCESS_KEY", "region": "YOUR_S3_REGION" }` | When `imageuploadtype` be set to `s3`, you would also need to setup this key, check our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) |

lib/config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,6 @@ module.exports = {
147147
},
148148
email: true,
149149
allowEmailRegister: true,
150+
allowGravatar: true,
150151
allowPDFExport: true
151152
}

lib/config/environment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,6 @@ module.exports = {
120120
},
121121
email: toBooleanConfig(process.env.HMD_EMAIL),
122122
allowEmailRegister: toBooleanConfig(process.env.HMD_ALLOW_EMAIL_REGISTER),
123+
allowGravatar: toBooleanConfig(process.env.HMD_ALLOW_GRAVATAR),
123124
allowPDFExport: toBooleanConfig(process.env.HMD_ALLOW_PDF_EXPORT)
124125
}

lib/letter-avatars.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22
// external modules
3+
const md5 = require('blueimp-md5')
34
const randomcolor = require('randomcolor')
45
const config = require('./config')
56

@@ -24,6 +25,17 @@ exports.generateAvatar = function (name) {
2425
return svg
2526
}
2627

27-
exports.generateAvatarURL = function (name) {
28-
return config.serverURL + '/user/' + name + '/avatar.svg'
28+
exports.generateAvatarURL = function (name, email = '', big = true) {
29+
let photo
30+
if (email !== '' && config.allowGravatar) {
31+
photo = 'https://www.gravatar.com/avatar/' + md5(email.toLowerCase())
32+
if (big) {
33+
photo += '?s=400'
34+
} else {
35+
photo += '?s=96'
36+
}
37+
} else {
38+
photo = config.serverURL + '/user/' + (name || email.substring(0, email.lastIndexOf('@')) || md5(email.toLowerCase())) + '/avatar.svg'
39+
}
40+
return photo
2941
}

lib/models/user.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22
// external modules
3-
var md5 = require('blueimp-md5')
43
var Sequelize = require('sequelize')
54
var scrypt = require('scrypt')
65

@@ -128,46 +127,27 @@ module.exports = function (sequelize, DataTypes) {
128127
}
129128
break
130129
case 'dropbox':
131-
// no image api provided, use gravatar
132-
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value)
133-
if (bigger) photo += '?s=400'
134-
else photo += '?s=96'
130+
photo = generateAvatarURL('', profile.emails[0].value, bigger)
135131
break
136132
case 'google':
137133
photo = profile.photos[0].value
138134
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
139135
else photo = photo.replace(/(\?sz=)\d*$/i, '$196')
140136
break
141137
case 'ldap':
142-
// no image api provided,
143-
// use gravatar if email exists,
144-
// otherwise generate a letter avatar
145-
if (profile.emails[0]) {
146-
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
147-
if (bigger) photo += '?s=400'
148-
else photo += '?s=96'
149-
} else {
150-
photo = generateAvatarURL(profile.username)
151-
}
138+
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
152139
break
153140
case 'saml':
154-
if (profile.emails[0]) {
155-
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
156-
if (bigger) photo += '?s=400'
157-
else photo += '?s=96'
158-
} else {
159-
photo = generateAvatarURL(profile.username)
160-
}
141+
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
161142
break
162143
}
163144
return photo
164145
},
165146
parseProfileByEmail: function (email) {
166-
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email)
167147
return {
168148
name: email.substring(0, email.lastIndexOf('@')),
169-
photo: photoUrl + '?s=96',
170-
biggerphoto: photoUrl + '?s=400'
149+
photo: generateAvatarURL('', email, false),
150+
biggerphoto: generateAvatarURL('', email, true)
171151
}
172152
}
173153
}

0 commit comments

Comments
 (0)