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

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