summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph (Sheogorath) Kern2018-06-24 01:59:06 +0200
committerGitHub2018-06-24 01:59:06 +0200
commitd87505d5836ea1bad60c0e2c2a8aa25b9b4e1590 (patch)
tree1678fdde33d523d2c63d8540541bb11a8cb35ac3 /lib
parentb8726bbe8da69a8562df2fe82d70f8238aed542f (diff)
parent318b2d378f54805e99b7022db15136df7c920083 (diff)
Merge pull request #854 from hackmdio/feature/disableGravatar
Allow to disable gravatar
Diffstat (limited to 'lib')
-rw-r--r--lib/config/default.js1
-rw-r--r--lib/config/environment.js1
-rw-r--r--lib/letter-avatars.js16
-rw-r--r--lib/models/user.js30
4 files changed, 21 insertions, 27 deletions
diff --git a/lib/config/default.js b/lib/config/default.js
index 2a91c852..8e71029e 100644
--- a/lib/config/default.js
+++ b/lib/config/default.js
@@ -147,5 +147,6 @@ module.exports = {
},
email: true,
allowEmailRegister: true,
+ allowGravatar: true,
allowPDFExport: true
}
diff --git a/lib/config/environment.js b/lib/config/environment.js
index e1c11569..0ca3d920 100644
--- a/lib/config/environment.js
+++ b/lib/config/environment.js
@@ -120,5 +120,6 @@ module.exports = {
},
email: toBooleanConfig(process.env.HMD_EMAIL),
allowEmailRegister: toBooleanConfig(process.env.HMD_ALLOW_EMAIL_REGISTER),
+ allowGravatar: toBooleanConfig(process.env.HMD_ALLOW_GRAVATAR),
allowPDFExport: toBooleanConfig(process.env.HMD_ALLOW_PDF_EXPORT)
}
diff --git a/lib/letter-avatars.js b/lib/letter-avatars.js
index b5b1d9e7..53fa011a 100644
--- a/lib/letter-avatars.js
+++ b/lib/letter-avatars.js
@@ -1,5 +1,6 @@
'use strict'
// external modules
+const md5 = require('blueimp-md5')
const randomcolor = require('randomcolor')
const config = require('./config')
@@ -24,6 +25,17 @@ exports.generateAvatar = function (name) {
return svg
}
-exports.generateAvatarURL = function (name) {
- return config.serverURL + '/user/' + name + '/avatar.svg'
+exports.generateAvatarURL = function (name, email = '', big = true) {
+ let photo
+ if (email !== '' && config.allowGravatar) {
+ photo = 'https://www.gravatar.com/avatar/' + md5(email.toLowerCase())
+ if (big) {
+ photo += '?s=400'
+ } else {
+ photo += '?s=96'
+ }
+ } else {
+ photo = config.serverURL + '/user/' + (name || email.substring(0, email.lastIndexOf('@')) || md5(email.toLowerCase())) + '/avatar.svg'
+ }
+ return photo
}
diff --git a/lib/models/user.js b/lib/models/user.js
index 5dd13869..1bd8c745 100644
--- a/lib/models/user.js
+++ b/lib/models/user.js
@@ -1,6 +1,5 @@
'use strict'
// external modules
-var md5 = require('blueimp-md5')
var Sequelize = require('sequelize')
var scrypt = require('scrypt')
@@ -128,10 +127,7 @@ module.exports = function (sequelize, DataTypes) {
}
break
case 'dropbox':
- // no image api provided, use gravatar
- photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value)
- if (bigger) photo += '?s=400'
- else photo += '?s=96'
+ photo = generateAvatarURL('', profile.emails[0].value, bigger)
break
case 'google':
photo = profile.photos[0].value
@@ -139,35 +135,19 @@ module.exports = function (sequelize, DataTypes) {
else photo = photo.replace(/(\?sz=)\d*$/i, '$196')
break
case 'ldap':
- // no image api provided,
- // use gravatar if email exists,
- // otherwise generate a letter avatar
- if (profile.emails[0]) {
- photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
- if (bigger) photo += '?s=400'
- else photo += '?s=96'
- } else {
- photo = generateAvatarURL(profile.username)
- }
+ photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
break
case 'saml':
- if (profile.emails[0]) {
- photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
- if (bigger) photo += '?s=400'
- else photo += '?s=96'
- } else {
- photo = generateAvatarURL(profile.username)
- }
+ photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
break
}
return photo
},
parseProfileByEmail: function (email) {
- var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email)
return {
name: email.substring(0, email.lastIndexOf('@')),
- photo: photoUrl + '?s=96',
- biggerphoto: photoUrl + '?s=400'
+ photo: generateAvatarURL('', email, false),
+ biggerphoto: generateAvatarURL('', email, true)
}
}
}