Rafael dos Santos Silva bf5611f7eb
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.
2021-02-01 20:49:32 -03:00

75 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module DiscourseNarrativeBot
class CertificateGenerator
def initialize(user, date, avatar_url)
@user = user
@avatar_url = avatar_url
date =
begin
Date.parse(date)
rescue ArgumentError => e
if e.message == 'invalid date'
Date.parse(Date.today.to_s)
else
raise e
end
end
@date = I18n.l(date, format: :date_only)
@discobot_user = ::DiscourseNarrativeBot::Base.new.discobot_user
end
def new_user_track
svg_default_width = 538.583
logo_container = logo_group(55, svg_default_width, 280)
ApplicationController.render(inline: read_template('new_user'), assigns: assign_options(svg_default_width, logo_container))
end
def advanced_user_track
svg_default_width = 722.8
logo_container = logo_group(40, svg_default_width, 350)
ApplicationController.render(inline: read_template('advanced_user'), assigns: assign_options(svg_default_width, logo_container))
end
private
def read_template(filename)
File.read(File.expand_path("../templates/#{filename}.svg.erb", __FILE__))
end
def assign_options(width, logo_group)
{
width: width,
discobot_user: @discobot_user,
date: @date,
avatar_url: @avatar_url,
logo_group: logo_group,
name: name
}
end
def name
@user.username.titleize
end
def logo_group(size, width, height)
return unless SiteSetting.site_logo_small_url.present?
uri = URI(SiteSetting.site_logo_small_url)
logo_uri =
if uri.host.blank? || uri.scheme.blank?
URI("#{Discourse.base_url}/#{uri.path}")
else
uri
end
{ size: size, width: width, height: height, logo_uri: logo_uri }
end
end
end