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

This reverts commit 1062dbc3e9.

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'
```
This commit is contained in:
Robin Ward 2020-05-06 11:28:02 -04:00
parent 2d31a14789
commit f5bdbd347e
3 changed files with 31 additions and 35 deletions

View File

@ -96,31 +96,16 @@ class UserAvatar < ActiveRecord::Base
return unless tempfile return unless tempfile
create_custom_avatar( ext = FastImage.type(tempfile).to_s
user, tempfile.rewind
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
def self.create_custom_avatar(user, file, override_gravatar: false, origin: nil) upload = UploadCreator.new(tempfile, "external-avatar." + ext, origin: avatar_url, type: "avatar").create_for(user.id)
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 user.create_user_avatar! unless user.user_avatar
if !user.user_avatar.contains_upload?(upload.id) if !user.user_avatar.contains_upload?(upload.id)
user.user_avatar.update!(custom_upload_id: upload.id) user.user_avatar.update!(custom_upload_id: upload.id)
override_gravatar = !options || options[:override_gravatar]
if user.uploaded_avatar_id.nil? || if user.uploaded_avatar_id.nil? ||
!user.user_avatar.contains_upload?(user.uploaded_avatar_id) || !user.user_avatar.contains_upload?(user.uploaded_avatar_id) ||
@ -129,6 +114,11 @@ class UserAvatar < ActiveRecord::Base
user.update!(uploaded_avatar_id: upload.id) user.update!(uploaded_avatar_id: upload.id)
end end
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 end
def self.ensure_consistency! def self.ensure_consistency!

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

View File

@ -1,24 +1,23 @@
# frozen_string_literal: true # frozen_string_literal: true
discobot_username = 'discobot' discobot_username = 'discobot'
discobot_user_id = -2
def seed_primary_email(user_id) def seed_primary_email
UserEmail.seed do |ue| UserEmail.seed do |ue|
ue.id = user_id ue.id = -2
ue.email = "discobot_email" ue.email = "discobot_email"
ue.primary = true ue.primary = true
ue.user_id = user_id ue.user_id = -2
end end
end end
unless user = User.find_by(id: discobot_user_id) unless user = User.find_by(id: -2)
suggested_username = UserNameSuggester.suggest(discobot_username) suggested_username = UserNameSuggester.suggest(discobot_username)
seed_primary_email(discobot_user_id) seed_primary_email
User.seed do |u| User.seed do |u|
u.id = discobot_user_id u.id = -2
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
@ -27,13 +26,26 @@ unless user = User.find_by(id: discobot_user_id)
u.approved = true u.approved = true
u.trust_level = TrustLevel[4] u.trust_level = TrustLevel[4]
end 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 end
bot = User.find(discobot_user_id) bot = User.find(-2)
# ensure discobot has a primary email # ensure discobot has a primary email
unless bot.primary_email unless bot.primary_email
seed_primary_email(discobot_user_id) seed_primary_email
bot.reload bot.reload
end end
@ -50,10 +62,4 @@ if !bot.user_profile.bio_raw
) )
end end
if !Rails.env.test? && (bot.user_avatar&.custom_upload_id.blank?) Group.user_trust_level_change!(-2, TrustLevel[4])
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])