FIX: Switch discobot to pull avatar from gravatar.

This commit is contained in:
Guo Xiang Tan 2020-05-20 10:16:28 +08:00
parent 6b92c78033
commit 234cd5c3e7
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
4 changed files with 35 additions and 9 deletions

View File

@ -5,6 +5,14 @@ class UserAvatar < ActiveRecord::Base
belongs_to :gravatar_upload, class_name: 'Upload' belongs_to :gravatar_upload, class_name: 'Upload'
belongs_to :custom_upload, class_name: 'Upload' belongs_to :custom_upload, class_name: 'Upload'
@@custom_user_gravatar_email_hash = {
Discourse::SYSTEM_USER_ID => User.email_hash("info@discourse.org")
}
def self.register_custom_user_gravatar_email_hash(user_id, email)
@@custom_user_gravatar_email_hash[user_id] = User.email_hash(email)
end
def contains_upload?(id) def contains_upload?(id)
gravatar_upload_id == id || custom_upload_id == id gravatar_upload_id == id || custom_upload_id == id
end end
@ -12,14 +20,14 @@ class UserAvatar < ActiveRecord::Base
def update_gravatar! def update_gravatar!
DistributedMutex.synchronize("update_gravatar_#{user_id}") do DistributedMutex.synchronize("update_gravatar_#{user_id}") do
begin begin
self.update!(last_gravatar_download_attempt: Time.now) self.update!(last_gravatar_download_attempt: Time.zone.now)
max = Discourse.avatar_sizes.max max = Discourse.avatar_sizes.max
# The user could be deleted before this executes # The user could be deleted before this executes
return if user.blank? || user.primary_email.blank? 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 email_hash = @@custom_user_gravatar_email_hash[user_id] || user.email_hash
gravatar_url = "https://#{SiteSetting.gravatar_base_url}/avatar/#{email_hash}.png?s=#{max}&d=404&reset_cache=#{SecureRandom.urlsafe_base64(5)}" gravatar_url = "https://#{SiteSetting.gravatar_base_url}/avatar/#{email_hash}.png?s=#{max}&d=404&reset_cache=#{SecureRandom.urlsafe_base64(5)}"
# follow redirects in case gravatar change rules on us # follow redirects in case gravatar change rules on us

View File

@ -4,20 +4,20 @@ discobot_username = 'discobot'
def seed_primary_email def seed_primary_email
UserEmail.seed do |ue| UserEmail.seed do |ue|
ue.id = -2 ue.id = DiscourseNarrativeBot::BOT_USER_ID
ue.email = "discobot_email" ue.email = "discobot_email"
ue.primary = true ue.primary = true
ue.user_id = -2 ue.user_id = DiscourseNarrativeBot::BOT_USER_ID
end end
end end
unless user = User.find_by(id: -2) unless user = User.find_by(id: DiscourseNarrativeBot::BOT_USER_ID)
suggested_username = UserNameSuggester.suggest(discobot_username) suggested_username = UserNameSuggester.suggest(discobot_username)
seed_primary_email seed_primary_email
User.seed do |u| User.seed do |u|
u.id = -2 u.id = DiscourseNarrativeBot::BOT_USER_ID
u.name = discobot_username u.name = discobot_username
u.username = suggested_username u.username = suggested_username
u.username_lower = suggested_username.downcase u.username_lower = suggested_username.downcase
@ -32,7 +32,7 @@ unless user = User.find_by(id: -2)
begin begin
UserAvatar.import_url_for_user( UserAvatar.import_url_for_user(
"https://cdn.discourse.org/dev/uploads/default/original/2X/e/edb63d57a720838a7ce6a68f02ba4618787f2299.png", "https://cdn.discourse.org/dev/uploads/default/original/2X/e/edb63d57a720838a7ce6a68f02ba4618787f2299.png",
User.find(-2), User.find(DiscourseNarrativeBot::BOT_USER_ID),
override_gravatar: true override_gravatar: true
) )
rescue rescue
@ -41,7 +41,7 @@ unless user = User.find_by(id: -2)
end end
end end
bot = User.find(-2) bot = User.find(DiscourseNarrativeBot::BOT_USER_ID)
# ensure discobot has a primary email # ensure discobot has a primary email
unless bot.primary_email unless bot.primary_email
@ -62,4 +62,4 @@ if !bot.user_profile.bio_raw
) )
end end
Group.user_trust_level_change!(-2, TrustLevel[4]) Group.user_trust_level_change!(DiscourseNarrativeBot::BOT_USER_ID, TrustLevel[4])

View File

@ -0,0 +1,12 @@
class ClearLastGravatarDownloadAttemptOnUserAvatars < ActiveRecord::Migration[6.0]
def up
execute <<~SQL
UPDATE user_avatars
SET last_gravatar_download_attempt = null
WHERE user_id = -2 AND custom_upload_id IS NULL AND gravatar_upload_id IS NULL
SQL
end
def down
end
end

View File

@ -55,6 +55,7 @@ after_initialize do
module ::DiscourseNarrativeBot module ::DiscourseNarrativeBot
PLUGIN_NAME = "discourse-narrative-bot".freeze PLUGIN_NAME = "discourse-narrative-bot".freeze
BOT_USER_ID = -2
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
engine_name PLUGIN_NAME engine_name PLUGIN_NAME
@ -271,4 +272,9 @@ after_initialize do
end end
end end
end end
UserAvatar.register_custom_user_gravatar_email_hash(
DiscourseNarrativeBot::BOT_USER_ID,
"discobot@discourse.org"
)
end end