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.
This commit is contained in:
Guo Xiang Tan 2020-05-05 16:42:48 +08:00 committed by Alan Guo Xiang Tan
parent b299f5f491
commit 1062dbc3e9
3 changed files with 35 additions and 31 deletions

View File

@ -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!

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -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])