2013-06-04 04:12:24 +08:00
|
|
|
module UserNotificationsHelper
|
2018-12-06 09:37:35 +08:00
|
|
|
include GlobalPath
|
2014-07-09 01:03:11 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def indent(text, by = 2)
|
2013-06-04 04:12:24 +08:00
|
|
|
spacer = " " * by
|
|
|
|
result = ""
|
|
|
|
text.each_line do |line|
|
|
|
|
result << spacer << line
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-24 15:13:15 +08:00
|
|
|
def correct_top_margin(html, desired)
|
|
|
|
fragment = Nokogiri::HTML.fragment(html)
|
|
|
|
if para = fragment.css("p:first").first
|
|
|
|
para["style"] = "margin-top: #{desired};"
|
|
|
|
end
|
|
|
|
fragment.to_html.html_safe
|
|
|
|
end
|
|
|
|
|
2013-11-29 06:20:56 +08:00
|
|
|
def logo_url
|
2018-11-14 15:03:02 +08: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 19:21:28 +08:00
|
|
|
return nil if logo_url.blank? || logo_url =~ /\.svg$/i
|
2019-01-02 15:29:17 +08:00
|
|
|
logo_url
|
2013-11-29 06:20:56 +08:00
|
|
|
end
|
|
|
|
|
2016-02-13 01:09:29 +08:00
|
|
|
def html_site_link(color)
|
|
|
|
"<a href='#{Discourse.base_url}' style='color: ##{color}'>#{@site_name}</a>"
|
2013-11-29 06:20:56 +08:00
|
|
|
end
|
|
|
|
|
2018-03-13 06:12:09 +08:00
|
|
|
def first_paragraphs_from(html)
|
2014-01-23 04:30:30 +08:00
|
|
|
doc = Nokogiri::HTML(html)
|
2014-02-14 05:11:01 +08:00
|
|
|
|
|
|
|
result = ""
|
2018-03-13 06:12:09 +08:00
|
|
|
length = 0
|
2018-05-14 00:23:17 +08:00
|
|
|
|
|
|
|
doc.css('body > p, aside.onebox, body > ul, body > blockquote').each do |node|
|
2016-12-20 06:05:49 +08:00
|
|
|
if node.text.present?
|
|
|
|
result << node.to_s
|
2018-03-13 06:12:09 +08:00
|
|
|
length += node.inner_text.length
|
|
|
|
return result if length >= SiteSetting.digest_min_excerpt_length
|
2014-02-14 05:11:01 +08:00
|
|
|
end
|
2014-01-23 04:30:30 +08:00
|
|
|
end
|
2018-05-14 00:23:17 +08:00
|
|
|
|
2014-02-14 05:11:01 +08:00
|
|
|
return result unless result.blank?
|
2014-01-23 04:30:30 +08:00
|
|
|
|
|
|
|
# If there is no first paragaph, return the first div (onebox)
|
|
|
|
doc.css('div').first
|
|
|
|
end
|
|
|
|
|
2018-03-09 06:59:33 +08:00
|
|
|
def email_excerpt(html_arg)
|
2018-03-13 06:12:09 +08:00
|
|
|
html = (first_paragraphs_from(html_arg) || html_arg).to_s
|
2016-04-14 01:24:39 +08:00
|
|
|
PrettyText.format_for_email(html).html_safe
|
2013-11-29 06:20:56 +08:00
|
|
|
end
|
2014-04-18 00:32:51 +08:00
|
|
|
|
2015-04-24 22:35:03 +08:00
|
|
|
def normalize_name(name)
|
2015-04-23 04:26:57 +08:00
|
|
|
name.downcase.gsub(/[\s_-]/, '')
|
|
|
|
end
|
|
|
|
|
2016-11-14 08:09:24 +08:00
|
|
|
def show_username_on_post(post)
|
|
|
|
return true if SiteSetting.prioritize_username_in_ux
|
|
|
|
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-23 04:15:23 +08:00
|
|
|
def show_name_on_post(post)
|
2016-11-14 08:09:24 +08:00
|
|
|
return true unless SiteSetting.prioritize_username_in_ux
|
|
|
|
|
2015-04-23 04:15:23 +08:00
|
|
|
SiteSetting.enable_names? &&
|
|
|
|
SiteSetting.display_name_on_posts? &&
|
|
|
|
post.user.name.present? &&
|
2015-04-24 22:35:03 +08:00
|
|
|
normalize_name(post.user.name) != normalize_name(post.user.username)
|
2015-04-23 04:15:23 +08:00
|
|
|
end
|
|
|
|
|
2016-06-21 23:12:30 +08:00
|
|
|
def format_for_email(post, use_excerpt)
|
2016-04-14 01:24:39 +08:00
|
|
|
html = use_excerpt ? post.excerpt : post.cooked
|
2016-06-21 23:12:30 +08:00
|
|
|
PrettyText.format_for_email(html, post).html_safe
|
2014-04-18 00:32:51 +08:00
|
|
|
end
|
2014-04-18 02:40:30 +08:00
|
|
|
|
2016-08-24 04:06:02 +08: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-29 03:35:59 +08:00
|
|
|
def show_image_with_url(url)
|
|
|
|
!(url.nil? || url.downcase.end_with?('svg'))
|
|
|
|
end
|
|
|
|
|
2016-11-11 07:16:24 +08:00
|
|
|
def email_image_url(basename)
|
|
|
|
UrlHelper.absolute("#{Discourse.base_uri}/images/emails/#{basename}")
|
|
|
|
end
|
|
|
|
|
2016-11-22 01:33:40 +08:00
|
|
|
def url_for_email(href)
|
|
|
|
URI(href).host.present? ? href : UrlHelper.absolute("#{Discourse.base_uri}#{href}")
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2016-11-22 01:33:40 +08:00
|
|
|
href
|
|
|
|
end
|
|
|
|
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|