2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-03 15:12:24 -05:00
|
|
|
module UserNotificationsHelper
|
2018-12-05 19:37:35 -06:00
|
|
|
include GlobalPath
|
2014-07-08 12:03:11 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def indent(text, by = 2)
|
2013-06-03 15:12:24 -05:00
|
|
|
spacer = " " * by
|
2019-05-02 17:17:27 -05:00
|
|
|
result = +""
|
2023-01-09 06:20:10 -06:00
|
|
|
text.each_line { |line| result << spacer << line }
|
2013-06-03 15:12:24 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-24 02:13:15 -05:00
|
|
|
def correct_top_margin(html, desired)
|
2020-05-04 22:46:57 -05:00
|
|
|
fragment = Nokogiri::HTML5.fragment(html)
|
2013-07-24 02:13:15 -05:00
|
|
|
if para = fragment.css("p:first").first
|
|
|
|
para["style"] = "margin-top: #{desired};"
|
|
|
|
end
|
|
|
|
fragment.to_html.html_safe
|
|
|
|
end
|
|
|
|
|
2013-11-28 16:20:56 -06:00
|
|
|
def logo_url
|
2018-11-14 01:03:02 -06:00
|
|
|
logo_url = SiteSetting.site_digest_logo_url
|
|
|
|
logo_url = SiteSetting.site_logo_url if logo_url.blank? || logo_url =~ /\.svg$/i
|
2016-04-02 06:21:28 -05:00
|
|
|
return nil if logo_url.blank? || logo_url =~ /\.svg$/i
|
2019-01-02 01:29:17 -06:00
|
|
|
logo_url
|
2013-11-28 16:20:56 -06:00
|
|
|
end
|
|
|
|
|
2020-02-11 14:52:38 -06:00
|
|
|
def html_site_link
|
|
|
|
"<a href='#{Discourse.base_url}'>#{@site_name}</a>"
|
2013-11-28 16:20:56 -06:00
|
|
|
end
|
|
|
|
|
2018-03-12 17:12:09 -05:00
|
|
|
def first_paragraphs_from(html)
|
2023-01-09 06:20:10 -06:00
|
|
|
doc = Nokogiri.HTML5(html)
|
2014-02-13 15:11:01 -06:00
|
|
|
|
2019-05-02 17:17:27 -05:00
|
|
|
result = +""
|
2018-03-12 17:12:09 -05:00
|
|
|
length = 0
|
2018-05-13 11:23:17 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
doc
|
|
|
|
.css("body > p, aside.onebox, body > ul, body > blockquote")
|
|
|
|
.each do |node|
|
|
|
|
if node.text.present?
|
|
|
|
result << node.to_s
|
|
|
|
length += node.inner_text.length
|
|
|
|
return result if length >= SiteSetting.digest_min_excerpt_length
|
|
|
|
end
|
2014-02-13 15:11:01 -06:00
|
|
|
end
|
2018-05-13 11:23:17 -05:00
|
|
|
|
2014-02-13 15:11:01 -06:00
|
|
|
return result unless result.blank?
|
2014-01-22 14:30:30 -06:00
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
# If there is no first paragraph with text, return the first paragraph with
|
2019-11-19 11:31:00 -06:00
|
|
|
# something else (an image) or div (a onebox).
|
2023-01-09 06:20:10 -06:00
|
|
|
doc.css(
|
|
|
|
"body > p:not(:empty), body > div:not(:empty), body > p > div.lightbox-wrapper img",
|
|
|
|
).first
|
2014-01-22 14:30:30 -06:00
|
|
|
end
|
|
|
|
|
2019-05-20 03:04:23 -05:00
|
|
|
def email_excerpt(html_arg, post = nil)
|
2018-03-12 17:12:09 -05:00
|
|
|
html = (first_paragraphs_from(html_arg) || html_arg).to_s
|
2019-05-20 03:04:23 -05:00
|
|
|
PrettyText.format_for_email(html, post).html_safe
|
2013-11-28 16:20:56 -06:00
|
|
|
end
|
2014-04-17 11:32:51 -05:00
|
|
|
|
2015-04-24 09:35:03 -05:00
|
|
|
def normalize_name(name)
|
2023-01-09 06:20:10 -06:00
|
|
|
name.downcase.gsub(/[\s_-]/, "")
|
2015-04-22 15:26:57 -05:00
|
|
|
end
|
|
|
|
|
2016-11-13 18:09:24 -06:00
|
|
|
def show_username_on_post(post)
|
|
|
|
return true unless SiteSetting.enable_names?
|
|
|
|
return true unless SiteSetting.display_name_on_posts?
|
|
|
|
return true unless post.user.name.present?
|
|
|
|
|
|
|
|
normalize_name(post.user.name) != normalize_name(post.user.username)
|
|
|
|
end
|
|
|
|
|
2015-04-22 15:15:23 -05:00
|
|
|
def show_name_on_post(post)
|
2023-01-09 06:20:10 -06:00
|
|
|
SiteSetting.enable_names? && SiteSetting.display_name_on_posts? && post.user.name.present? &&
|
2015-04-24 09:35:03 -05:00
|
|
|
normalize_name(post.user.name) != normalize_name(post.user.username)
|
2015-04-22 15:15:23 -05:00
|
|
|
end
|
|
|
|
|
2016-06-21 10:12:30 -05:00
|
|
|
def format_for_email(post, use_excerpt)
|
2016-04-13 12:24:39 -05:00
|
|
|
html = use_excerpt ? post.excerpt : post.cooked
|
2016-06-21 10:12:30 -05:00
|
|
|
PrettyText.format_for_email(html, post).html_safe
|
2014-04-17 11:32:51 -05:00
|
|
|
end
|
2014-04-17 13:40:30 -05:00
|
|
|
|
2016-08-23 15:06:02 -05:00
|
|
|
def digest_custom_html(position_key)
|
|
|
|
digest_custom "user_notifications.digest.custom.html.#{position_key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def digest_custom_text(position_key)
|
|
|
|
digest_custom "user_notifications.digest.custom.text.#{position_key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def digest_custom(i18n_key)
|
|
|
|
PrettyText.format_for_email(I18n.t(i18n_key)).html_safe
|
|
|
|
end
|
|
|
|
|
2016-11-28 13:35:59 -06:00
|
|
|
def show_image_with_url(url)
|
2023-01-09 06:20:10 -06:00
|
|
|
!(url.nil? || url.downcase.end_with?("svg"))
|
2016-11-28 13:35:59 -06:00
|
|
|
end
|
|
|
|
|
2016-11-10 17:16:24 -06:00
|
|
|
def email_image_url(basename)
|
2020-10-09 06:51:24 -05:00
|
|
|
UrlHelper.absolute("#{Discourse.base_path}/images/emails/#{basename}")
|
2016-11-10 17:16:24 -06:00
|
|
|
end
|
|
|
|
|
2016-11-21 11:33:40 -06:00
|
|
|
def url_for_email(href)
|
2020-10-09 06:51:24 -05:00
|
|
|
URI(href).host.present? ? href : UrlHelper.absolute("#{Discourse.base_path}#{href}")
|
2018-08-14 05:23:32 -05:00
|
|
|
rescue URI::Error
|
2016-11-21 11:33:40 -06:00
|
|
|
href
|
|
|
|
end
|
2013-06-03 15:12:24 -05:00
|
|
|
end
|