Module: UserNotificationsHelper

Includes:
GlobalPath
Included in:
UserNotificationRenderer, UserNotifications
Defined in:
app/helpers/user_notifications_helper.rb

Instance Method Summary collapse

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Instance Method Details

#correct_top_margin(html, desired) ⇒ Object



13
14
15
16
17
18
19
# File 'app/helpers/user_notifications_helper.rb', line 13

def correct_top_margin(html, desired)
  fragment = Nokogiri::HTML5.fragment(html)
  if para = fragment.css("p:first").first
    para["style"] = "margin-top: #{desired};"
  end
  fragment.to_html.html_safe
end

#digest_custom(i18n_key) ⇒ Object



92
93
94
# File 'app/helpers/user_notifications_helper.rb', line 92

def digest_custom(i18n_key)
  PrettyText.format_for_email(I18n.t(i18n_key)).html_safe
end

#digest_custom_html(position_key) ⇒ Object



84
85
86
# File 'app/helpers/user_notifications_helper.rb', line 84

def digest_custom_html(position_key)
  digest_custom "user_notifications.digest.custom.html.#{position_key}"
end

#digest_custom_text(position_key) ⇒ Object



88
89
90
# File 'app/helpers/user_notifications_helper.rb', line 88

def digest_custom_text(position_key)
  digest_custom "user_notifications.digest.custom.text.#{position_key}"
end

#email_excerpt(html_arg, post = nil) ⇒ Object



57
58
59
60
# File 'app/helpers/user_notifications_helper.rb', line 57

def email_excerpt(html_arg, post = nil)
  html = (first_paragraphs_from(html_arg) || html_arg).to_s
  PrettyText.format_for_email(html, post).html_safe
end

#email_image_url(basename) ⇒ Object



100
101
102
# File 'app/helpers/user_notifications_helper.rb', line 100

def email_image_url(basename)
  UrlHelper.absolute("#{Discourse.base_path}/images/emails/#{basename}")
end

#first_paragraphs_from(html) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/user_notifications_helper.rb', line 32

def first_paragraphs_from(html)
  doc = Nokogiri.HTML5(html)

  result = +""
  length = 0

  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
    end

  return result unless result.blank?

  # If there is no first paragraph with text, return the first paragraph with
  # something else (an image) or div (a onebox).
  doc.css(
    "body > p:not(:empty), body > div:not(:empty), body > p > div.lightbox-wrapper img",
  ).first
end

#format_for_email(post, use_excerpt) ⇒ Object



79
80
81
82
# File 'app/helpers/user_notifications_helper.rb', line 79

def format_for_email(post, use_excerpt)
  html = use_excerpt ? post.excerpt : post.cooked
  PrettyText.format_for_email(html, post).html_safe
end


28
29
30
# File 'app/helpers/user_notifications_helper.rb', line 28

def html_site_link
  "<a href='#{Discourse.base_url}'>#{@site_name}</a>"
end

#indent(text, by = 2) ⇒ Object



6
7
8
9
10
11
# File 'app/helpers/user_notifications_helper.rb', line 6

def indent(text, by = 2)
  spacer = " " * by
  result = +""
  text.each_line { |line| result << spacer << line }
  result
end

#logo_urlObject



21
22
23
24
25
26
# File 'app/helpers/user_notifications_helper.rb', line 21

def logo_url
  logo_url = SiteSetting.site_digest_logo_url
  logo_url = SiteSetting.site_logo_url if logo_url.blank? || logo_url =~ /\.svg\z/i
  return nil if logo_url.blank? || logo_url =~ /\.svg\z/i
  logo_url
end

#normalize_name(name) ⇒ Object



62
63
64
# File 'app/helpers/user_notifications_helper.rb', line 62

def normalize_name(name)
  name.downcase.gsub(/[\s_-]/, "")
end

#show_image_with_url(url) ⇒ Object



96
97
98
# File 'app/helpers/user_notifications_helper.rb', line 96

def show_image_with_url(url)
  !(url.nil? || url.downcase.end_with?("svg"))
end

#show_name_on_post(post) ⇒ Object



74
75
76
77
# File 'app/helpers/user_notifications_helper.rb', line 74

def show_name_on_post(post)
  SiteSetting.enable_names? && SiteSetting.display_name_on_posts? && post.user.name.present? &&
    normalize_name(post.user.name) != normalize_name(post.user.username)
end

#show_username_on_post(post) ⇒ Object



66
67
68
69
70
71
72
# File 'app/helpers/user_notifications_helper.rb', line 66

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

#url_for_email(href) ⇒ Object



104
105
106
107
108
# File 'app/helpers/user_notifications_helper.rb', line 104

def url_for_email(href)
  URI(href).host.present? ? href : UrlHelper.absolute("#{Discourse.base_path}#{href}")
rescue URI::Error
  href
end