From 234cd5c3e7f8d1be7c98af7eeb572e5b79f0a16a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 20 May 2020 10:16:28 +0800 Subject: [PATCH] FIX: Switch discobot to pull avatar from gravatar. --- app/models/user_avatar.rb | 12 ++++++++++-- .../db/fixtures/001_discobot.rb | 14 +++++++------- ...st_gravatar_download_attempt_on_user_avatars.rb | 12 ++++++++++++ plugins/discourse-narrative-bot/plugin.rb | 6 ++++++ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 plugins/discourse-narrative-bot/db/post_migrate/20200520015508_clear_last_gravatar_download_attempt_on_user_avatars.rb diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index f6dbf133126..bd2751d6b45 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -5,6 +5,14 @@ class UserAvatar < ActiveRecord::Base belongs_to :gravatar_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) gravatar_upload_id == id || custom_upload_id == id end @@ -12,14 +20,14 @@ class UserAvatar < ActiveRecord::Base def update_gravatar! DistributedMutex.synchronize("update_gravatar_#{user_id}") do begin - self.update!(last_gravatar_download_attempt: Time.now) + self.update!(last_gravatar_download_attempt: Time.zone.now) max = Discourse.avatar_sizes.max # The user could be deleted before this executes 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)}" # follow redirects in case gravatar change rules on us diff --git a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb index 3972a350fbf..bc204e4021b 100644 --- a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb +++ b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb @@ -4,20 +4,20 @@ discobot_username = 'discobot' def seed_primary_email UserEmail.seed do |ue| - ue.id = -2 + ue.id = DiscourseNarrativeBot::BOT_USER_ID ue.email = "discobot_email" ue.primary = true - ue.user_id = -2 + ue.user_id = DiscourseNarrativeBot::BOT_USER_ID 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) seed_primary_email User.seed do |u| - u.id = -2 + u.id = DiscourseNarrativeBot::BOT_USER_ID u.name = discobot_username u.username = suggested_username u.username_lower = suggested_username.downcase @@ -32,7 +32,7 @@ unless user = User.find_by(id: -2) begin UserAvatar.import_url_for_user( "https://cdn.discourse.org/dev/uploads/default/original/2X/e/edb63d57a720838a7ce6a68f02ba4618787f2299.png", - User.find(-2), + User.find(DiscourseNarrativeBot::BOT_USER_ID), override_gravatar: true ) rescue @@ -41,7 +41,7 @@ unless user = User.find_by(id: -2) end end -bot = User.find(-2) +bot = User.find(DiscourseNarrativeBot::BOT_USER_ID) # ensure discobot has a primary email unless bot.primary_email @@ -62,4 +62,4 @@ if !bot.user_profile.bio_raw ) end -Group.user_trust_level_change!(-2, TrustLevel[4]) +Group.user_trust_level_change!(DiscourseNarrativeBot::BOT_USER_ID, TrustLevel[4]) diff --git a/plugins/discourse-narrative-bot/db/post_migrate/20200520015508_clear_last_gravatar_download_attempt_on_user_avatars.rb b/plugins/discourse-narrative-bot/db/post_migrate/20200520015508_clear_last_gravatar_download_attempt_on_user_avatars.rb new file mode 100644 index 00000000000..5f80851d7ec --- /dev/null +++ b/plugins/discourse-narrative-bot/db/post_migrate/20200520015508_clear_last_gravatar_download_attempt_on_user_avatars.rb @@ -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 diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb index a6be5dd056f..61047c0e8b2 100644 --- a/plugins/discourse-narrative-bot/plugin.rb +++ b/plugins/discourse-narrative-bot/plugin.rb @@ -55,6 +55,7 @@ after_initialize do module ::DiscourseNarrativeBot PLUGIN_NAME = "discourse-narrative-bot".freeze + BOT_USER_ID = -2 class Engine < ::Rails::Engine engine_name PLUGIN_NAME @@ -271,4 +272,9 @@ after_initialize do end end end + + UserAvatar.register_custom_user_gravatar_email_hash( + DiscourseNarrativeBot::BOT_USER_ID, + "discobot@discourse.org" + ) end