FIX: Make discobot certificate faster/non blocking (#11344)

This moves the way we add the user avatar and site logo
to the discobot certificates from embeded base64 png to
just using the files urls in the href to the image tag.

This will make generation faster and the certificate
smaller overall, but it can't be used in a  `img` tag
anymore, since SVGs in `img` tags don't load the external images

In order to work around that we will move the certificate
in posts to an iframe, which works fine without any user
visible changes. For this to be possible the plugin automatically
adds the site current domain to the list of allowed iframe origins.
This commit is contained in:
Rafael dos Santos Silva
2021-02-01 20:49:32 -03:00
committed by GitHub
parent ca4a962766
commit bf5611f7eb
6 changed files with 33 additions and 64 deletions

View File

@ -50,6 +50,11 @@ after_initialize do
# Disable welcome message because that is what the bot is supposed to replace.
SiteSetting.send_welcome_message = false if SiteSetting.send_welcome_message
certificate_path = "#{Discourse.base_url}/discobot/certificate.svg"
if SiteSetting.discourse_narrative_bot_enabled && !SiteSetting.allowed_iframes.include?(certificate_path)
SiteSetting.allowed_iframes = SiteSetting.allowed_iframes.split('|').append("#{Discourse.base_url}/discobot/certificate.svg").join('|')
end
require_dependency 'plugin_store'
module ::DiscourseNarrativeBot
@ -94,8 +99,7 @@ after_initialize do
raise Discourse::NotFound if user.blank?
hijack do
avatar_data = fetch_avatar(user)
generator = CertificateGenerator.new(user, params[:date], avatar_data)
generator = CertificateGenerator.new(user, params[:date], avatar_url(user))
svg = params[:type] == 'advanced' ? generator.advanced_user_track : generator.new_user_track
@ -107,16 +111,8 @@ after_initialize do
private
def fetch_avatar(user)
avatar_url = UrlHelper.absolute(Discourse.base_path + user.avatar_template.gsub('{size}', '250'))
FileHelper.download(
avatar_url.to_s,
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: 'narrative-bot-avatar',
follow_redirect: true
)&.read
rescue OpenURI::HTTPError
# Ignore if fetching image returns a non 200 response
def avatar_url(user)
UrlHelper.absolute(Discourse.base_path + user.avatar_template.gsub('{size}', '250'))
end
end
end