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