mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 19:53:44 +08:00
5aaf7e3316
Our instance used for template rendering needs a lock to ensure there is no race condition where rendering happens on 2 threads at the same time. This can lead to local poisoning which can cause unexpected results in emails
36 lines
816 B
Ruby
36 lines
816 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Email
|
|
class Renderer
|
|
|
|
def initialize(message, opts = nil)
|
|
@message = message
|
|
@opts = opts || {}
|
|
end
|
|
|
|
def text
|
|
return @text if @text
|
|
@text = (+(@message.text_part ? @message.text_part : @message).body.to_s).force_encoding('UTF-8')
|
|
@text = CGI.unescapeHTML(@text)
|
|
end
|
|
|
|
def html
|
|
style = if @message.html_part
|
|
Email::Styles.new(@message.html_part.body.to_s, @opts)
|
|
else
|
|
unstyled = UserNotificationRenderer.render(
|
|
template: 'layouts/email_template',
|
|
format: :html,
|
|
locals: { html_body: PrettyText.cook(text).html_safe }
|
|
)
|
|
Email::Styles.new(unstyled, @opts)
|
|
end
|
|
|
|
style.format_basic
|
|
style.format_html
|
|
style.to_html
|
|
end
|
|
|
|
end
|
|
end
|