From 1b8793e7a41e8053e53c332da3c05bb40f9d73dc Mon Sep 17 00:00:00 2001 From: Stasiek Michalski Date: Thu, 12 Mar 2020 16:23:55 +0100 Subject: [PATCH] FEATURE: Add support for custom gravatar-like services (#9137) Adds 3 config values that allow to set a custom provider of Gravatar-like API accessible from gravatar_base_url. The gravatar_name is purely cosmetic, but helps with associating name with the service that actually provides the avatars. gravatar_login_url is a link relative to gravatar_base_url, which provides the user with the login to the Gravatar service --- .../discourse/controllers/avatar-selector.js.es6 | 5 +++++ .../discourse/templates/modal/avatar-selector.hbs | 6 +++--- app/models/user.rb | 2 +- app/models/user_avatar.rb | 2 +- config/locales/client.en.yml | 8 ++++---- config/locales/server.en.yml | 4 ++++ config/site_settings.yml | 9 +++++++++ spec/models/user_spec.rb | 10 ++++++++++ 8 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/avatar-selector.js.es6 b/app/assets/javascripts/discourse/controllers/avatar-selector.js.es6 index 959b296cecb..5fdbe79e3e4 100644 --- a/app/assets/javascripts/discourse/controllers/avatar-selector.js.es6 +++ b/app/assets/javascripts/discourse/controllers/avatar-selector.js.es6 @@ -4,8 +4,13 @@ import ModalFunctionality from "discourse/mixins/modal-functionality"; import { ajax } from "discourse/lib/ajax"; import { allowsImages } from "discourse/lib/uploads"; import { popupAjaxError } from "discourse/lib/ajax-error"; +import { setting } from "discourse/lib/computed"; export default Controller.extend(ModalFunctionality, { + gravatarName: setting("gravatar_name"), + gravatarBaseUrl: setting("gravatar_base_url"), + gravatarLoginUrl: setting("gravatar_login_url"), + @discourseComputed( "selected", "user.system_avatar_upload_id", diff --git a/app/assets/javascripts/discourse/templates/modal/avatar-selector.hbs b/app/assets/javascripts/discourse/templates/modal/avatar-selector.hbs index 10e5c4bf26f..98504b09818 100644 --- a/app/assets/javascripts/discourse/templates/modal/avatar-selector.hbs +++ b/app/assets/javascripts/discourse/templates/modal/avatar-selector.hbs @@ -14,16 +14,16 @@
{{radio-button id="gravatar" name="avatar" value="gravatar" selection=selected}} - + {{d-button action=(action "refreshGravatar") - title="user.change_avatar.refresh_gravatar_title" + translatedTitle=(i18n "user.change_avatar.refresh_gravatar_title" gravatarName=gravatarName) disabled=gravatarRefreshDisabled icon="sync" class="btn-default avatar-selector-refresh-gravatar"}} {{#if gravatarFailed}} -

{{i18n 'user.change_avatar.gravatar_failed'}}

+

{{I18n 'user.change_avatar.gravatar_failed' gravatarName=gravatarName}}

{{/if}}
{{#if allowAvatarUpload}} diff --git a/app/models/user.rb b/app/models/user.rb index fd81a979d0a..4b0437261cd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -741,7 +741,7 @@ class User < ActiveRecord::Base end def self.gravatar_template(email) - "//www.gravatar.com/avatar/#{self.email_hash(email)}.png?s={size}&r=pg&d=identicon" + "//#{SiteSetting.gravatar_base_url}/avatar/#{self.email_hash(email)}.png?s={size}&r=pg&d=identicon" end # Don't pass this up to the client - it's meant for server side use diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index 215524a743c..c3df573c324 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -20,7 +20,7 @@ class UserAvatar < ActiveRecord::Base return if user.blank? || user.primary_email.blank? email_hash = user_id == Discourse::SYSTEM_USER_ID ? User.email_hash("info@discourse.org") : user.email_hash - gravatar_url = "https://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404" + gravatar_url = "https://#{SiteSetting.gravatar_base_url}/avatar/#{email_hash}.png?s=#{max}&d=404" # follow redirects in case gravatar change rules on us tempfile = FileHelper.download( diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 4d61f5a28a7..a276cc86143 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1057,10 +1057,10 @@ en: change_avatar: title: "Change your profile picture" - gravatar: "Gravatar, based on" - gravatar_title: "Change your avatar on Gravatar's website" - gravatar_failed: "We could not find a Gravatar with that email address." - refresh_gravatar_title: "Refresh your Gravatar" + gravatar: "{{gravatarName}}, based on" + gravatar_title: "Change your avatar on {{gravatarName}}'s website" + gravatar_failed: "We could not find a {{gravatarName}} with that email address." + refresh_gravatar_title: "Refresh your {{gravatarName}}" letter_based: "System assigned profile picture" uploaded_avatar: "Custom picture" uploaded_avatar_empty: "Add a custom picture" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index f2608550873..4b7298af425 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2185,6 +2185,10 @@ en: dashboard_general_tab_activity_metrics: "Choose reports to be displayed as activity metrics on the general tab." + gravatar_name: "Name of the Gravatar provider" + gravatar_base_url: "Url of the Gravatar provider's API base" + gravatar_login_url: "Url relative to gravatar_base_url, which provides the user with the login to the Gravatar service" + errors: invalid_email: "Invalid email address." invalid_username: "There's no user with that username." diff --git a/config/site_settings.yml b/config/site_settings.yml index 9e9b80aaac7..56d742e5656 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -598,6 +598,15 @@ users: client: true max_notifications_per_user: default: 10000 + gravatar_name: + default: Gravatar + client: true + gravatar_base_url: + default: www.gravatar.com + client: true + gravatar_login_url: + default: /emails + client: true groups: enable_group_directory: diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d94d8d47c5f..3e62fd05888 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1248,6 +1248,16 @@ describe User do end + describe "#custom_gravatar" do + before do + SiteSetting.gravatar_base_url = "seccdn.libravatar.org" + end + + it "returns a gravatar url as set in the settings" do + expect(User.gravatar_template("em@il.com")).to eq("//seccdn.libravatar.org/avatar/6dc2fde946483a1d8a84b89345a1b638.png?s={size}&r=pg&d=identicon") + end + end + describe "#letter_avatar_color" do before do SiteSetting.restrict_letter_avatar_colors = "2F70AC|ED207B|AAAAAA|77FF33"