FIX: Discobot has not been created with our custom avatar.

FIX: Discobot has not been created with our custom avatar.

Previously the image was imported from a Discourse hosted CDN but the URL has since become invalid. However, it was not caught since all errors are rescued. This commit fixes the issue by shipping the user avatar with the plugin.

diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb
index f6dbf13..e2b7f44 100644
--- a/app/models/user_avatar.rb
+++ b/app/models/user_avatar.rb
@@ -96,16 +96,31 @@ class UserAvatar < ActiveRecord::Base
 
     return unless tempfile
 
-    ext = FastImage.type(tempfile).to_s
-    tempfile.rewind
+    create_custom_avatar(
+      user,
+      tempfile,
+      override_gravatar: options&.dig(:override_gravatar),
+      origin: avatar_url
+    )
+  rescue Net::ReadTimeout, OpenURI::HTTPError
+    # skip saving, we are not connected to the net
+  ensure
+    tempfile.close! if tempfile && tempfile.respond_to?(:close!)
+  end
 
-    upload = UploadCreator.new(tempfile, "external-avatar." + ext, origin: avatar_url, type: "avatar").create_for(user.id)
+  def self.create_custom_avatar(user, file, override_gravatar: false, origin: nil)
+    ext = FastImage.type(file).to_s
+    file.rewind
+
+    upload = UploadCreator.new(file, "external-avatar.#{ext}",
+      origin: origin,
+      type: "avatar"
+    ).create_for(user.id)
 
     user.create_user_avatar! unless user.user_avatar
 
     if !user.user_avatar.contains_upload?(upload.id)
       user.user_avatar.update!(custom_upload_id: upload.id)
-      override_gravatar = !options || options[:override_gravatar]
 
       if user.uploaded_avatar_id.nil? ||
           !user.user_avatar.contains_upload?(user.uploaded_avatar_id) ||
@@ -114,11 +129,6 @@ class UserAvatar < ActiveRecord::Base
         user.update!(uploaded_avatar_id: upload.id)
       end
     end
-
-  rescue Net::ReadTimeout, OpenURI::HTTPError
-    # skip saving, we are not connected to the net
-  ensure
-    tempfile.close! if tempfile && tempfile.respond_to?(:close!)
   end
 
   def self.ensure_consistency!
diff --git a/plugins/discourse-narrative-bot/assets/images/discobot.png b/plugins/discourse-narrative-bot/assets/images/discobot.png
new file mode 100644
index 0000000..b6c7113
Binary files /dev/null and b/plugins/discourse-narrative-bot/assets/images/discobot.png differ
diff --git a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
index 3972a35..f30861e 100644
--- a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
+++ b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
@@ -1,23 +1,24 @@
 # frozen_string_literal: true
 
 discobot_username = 'discobot'
+discobot_user_id = -2
 
-def seed_primary_email
+def seed_primary_email(user_id)
   UserEmail.seed do |ue|
-    ue.id = -2
+    ue.id = user_id
     ue.email = "discobot_email"
     ue.primary = true
-    ue.user_id = -2
+    ue.user_id = user_id
   end
 end
 
-unless user = User.find_by(id: -2)
+unless user = User.find_by(id: discobot_user_id)
   suggested_username = UserNameSuggester.suggest(discobot_username)
 
-  seed_primary_email
+  seed_primary_email(discobot_user_id)
 
   User.seed do |u|
-    u.id = -2
+    u.id = discobot_user_id
     u.name = discobot_username
     u.username = suggested_username
     u.username_lower = suggested_username.downcase
@@ -26,26 +27,13 @@ unless user = User.find_by(id: -2)
     u.approved = true
     u.trust_level = TrustLevel[4]
   end
-
-  # TODO Pull the user avatar from that thread for now. In the future, pull it from a local file or from some central discobot repo.
-  if !Rails.env.test?
-    begin
-      UserAvatar.import_url_for_user(
-        "https://cdn.discourse.org/dev/uploads/default/original/2X/e/edb63d57a720838a7ce6a68f02ba4618787f2299.png",
-        User.find(-2),
-        override_gravatar: true
-      )
-    rescue
-      # In case the avatar can't be downloaded, don't fail seed
-    end
-  end
 end
 
-bot = User.find(-2)
+bot = User.find(discobot_user_id)
 
 # ensure discobot has a primary email
 unless bot.primary_email
-  seed_primary_email
+  seed_primary_email(discobot_user_id)
   bot.reload
 end
 
@@ -62,4 +50,10 @@ if !bot.user_profile.bio_raw
   )
 end
 
-Group.user_trust_level_change!(-2, TrustLevel[4])
+if !Rails.env.test? && (bot.user_avatar&.custom_upload_id.blank?)
+  File.open(Rails.root.join("plugins", "discourse-narrative-bot", "assets", "images", "discobot.png"), 'r') do |file|
+    UserAvatar.create_custom_avatar(bot, file, override_gravatar: true)
+  end
+end
+
+Group.user_trust_level_change!(discobot_user_id, TrustLevel[4])

GitHub sha: 1062dbc3

This commit appears in #9638 which was approved by eviltrout. It was merged by tgxworld.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/multisite-migrate-error/150579/1