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

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

This reverts commit 1062dbc3e9cbf9370803c0acea55eb3c054060ca.

Looks like it’s causing some errors on migration.

/var/www/discourse/lib/image_sizer.rb:6:in `resize'
/var/www/discourse/lib/upload_creator.rb:120:in `block in create_for'
/var/www/discourse/lib/distributed_mutex.rb:33:in `block in synchronize'
/var/www/discourse/lib/distributed_mutex.rb:29:in `synchronize'
/var/www/discourse/lib/distributed_mutex.rb:29:in `synchronize'
/var/www/discourse/lib/distributed_mutex.rb:14:in `synchronize'
/var/www/discourse/lib/upload_creator.rb:37:in `create_for'

diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb
index e2b7f44..f6dbf13 100644
--- a/app/models/user_avatar.rb
+++ b/app/models/user_avatar.rb
@@ -96,31 +96,16 @@ class UserAvatar < ActiveRecord::Base
 
     return unless tempfile
 
-    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
+    ext = FastImage.type(tempfile).to_s
+    tempfile.rewind
 
-  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)
+    upload = UploadCreator.new(tempfile, "external-avatar." + ext, origin: avatar_url, 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) ||
@@ -129,6 +114,11 @@ 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
deleted file mode 100644
index b6c7113..0000000
Binary files a/plugins/discourse-narrative-bot/assets/images/discobot.png and /dev/null differ
diff --git a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
index f30861e..3972a35 100644
--- a/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
+++ b/plugins/discourse-narrative-bot/db/fixtures/001_discobot.rb
@@ -1,24 +1,23 @@
 # frozen_string_literal: true
 
 discobot_username = 'discobot'
-discobot_user_id = -2
 
-def seed_primary_email(user_id)
+def seed_primary_email
   UserEmail.seed do |ue|
-    ue.id = user_id
+    ue.id = -2
     ue.email = "discobot_email"
     ue.primary = true
-    ue.user_id = user_id
+    ue.user_id = -2
   end
 end
 
-unless user = User.find_by(id: discobot_user_id)
+unless user = User.find_by(id: -2)
   suggested_username = UserNameSuggester.suggest(discobot_username)
 
-  seed_primary_email(discobot_user_id)
+  seed_primary_email
 
   User.seed do |u|
-    u.id = discobot_user_id
+    u.id = -2
     u.name = discobot_username
     u.username = suggested_username
     u.username_lower = suggested_username.downcase
@@ -27,13 +26,26 @@ unless user = User.find_by(id: discobot_user_id)
     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(discobot_user_id)
+bot = User.find(-2)
 
 # ensure discobot has a primary email
 unless bot.primary_email
-  seed_primary_email(discobot_user_id)
+  seed_primary_email
   bot.reload
 end
 
@@ -50,10 +62,4 @@ if !bot.user_profile.bio_raw
   )
 end
 
-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])
+Group.user_trust_level_change!(-2, TrustLevel[4])

GitHub sha: f5bdbd34