2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
module DiscourseNarrativeBot
|
|
|
|
class CertificateGenerator
|
|
|
|
def initialize(user, date)
|
|
|
|
@user = user
|
2017-12-21 20:29:35 -05:00
|
|
|
|
|
|
|
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)
|
2017-05-26 01:00:17 -04:00
|
|
|
@discobot_user = User.find(-2)
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_user_track
|
|
|
|
width = 538.583 # Default width for the SVG
|
2019-10-09 02:45:01 -04:00
|
|
|
ApplicationController.render(inline: File.read(File.expand_path('../templates/new_user.svg.erb', __FILE__)),
|
|
|
|
assigns: { width: width,
|
|
|
|
discobot_user: @discobot_user,
|
|
|
|
date: @date,
|
|
|
|
avatar_url: base64_image_link(avatar_url),
|
|
|
|
logo_group: logo_group(55, width, 350),
|
|
|
|
name: name })
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def advanced_user_track
|
|
|
|
width = 722.8 # Default width for the SVG
|
2019-10-09 02:45:01 -04:00
|
|
|
ApplicationController.render(inline: File.read(File.expand_path('../templates/advanced_user.svg.erb', __FILE__)),
|
|
|
|
assigns: { width: width,
|
|
|
|
discobot_user: @discobot_user,
|
|
|
|
date: @date,
|
|
|
|
avatar_url: base64_image_link(avatar_url),
|
|
|
|
logo_group: logo_group(40, width, 280),
|
|
|
|
name: name })
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def name
|
|
|
|
@user.username.titleize
|
|
|
|
end
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def logo_group(size, width, height)
|
2018-11-14 02:03:02 -05:00
|
|
|
return unless SiteSetting.site_logo_small_url.present?
|
2017-12-21 20:30:24 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
begin
|
2018-11-14 02:03:02 -05:00
|
|
|
uri = URI(SiteSetting.site_logo_small_url)
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
logo_uri =
|
|
|
|
if uri.host.blank? || uri.scheme.blank?
|
|
|
|
URI("#{Discourse.base_url}/#{uri.path}")
|
|
|
|
else
|
|
|
|
uri
|
|
|
|
end
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
<<~URL
|
2017-05-24 01:50:20 -04:00
|
|
|
<g transform="translate(#{width / 2 - (size / 2)} #{height})">
|
2017-07-14 02:16:29 -04:00
|
|
|
<image height="#{size}px" width="#{size}px" #{base64_image_link(logo_uri)}/>
|
2017-05-24 01:50:20 -04:00
|
|
|
</g>
|
|
|
|
URL
|
2018-06-07 01:28:18 -04:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
''
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def base64_image_link(url)
|
|
|
|
if image = fetch_image(url)
|
|
|
|
"xlink:href=\"data:image/png;base64,#{Base64.strict_encode64(image)}\""
|
|
|
|
else
|
|
|
|
""
|
2017-07-14 02:16:29 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2017-07-14 02:16:29 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def fetch_image(url)
|
|
|
|
URI(url).open('rb', redirect: true, allow_redirections: :all).read
|
|
|
|
rescue OpenURI::HTTPError
|
|
|
|
# Ignore if fetching image returns a non 200 response
|
|
|
|
end
|
2017-07-14 02:16:29 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def avatar_url
|
|
|
|
UrlHelper.absolute(Discourse.base_uri + @user.avatar_template.gsub('{size}', '250'))
|
|
|
|
end
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
end
|