discourse/app/helpers/user_notifications_helper.rb
James Kiesel feffe23cc5 FEATURE: More granular mailing list mode (#4068)
* Rearrange frontend to account for mailing list mode

* Allow update of user preference for mailing list frequency

* Add mailing list frequency estimate

* Simplify frequency estimate; disable activity summary for mailing list mode

* Remove combined updates

* Add specs for enqueue mailing list mode job

* Write mailing list method for mailer

* Fix linting error

* Account for stale topics

* Add translations for default mailing list setting

* One query for mailing list topics

* Fix failing spec

* WIP

* Flesh out html template

* First pass at text-based mailing list summary

* Add user avatar

* Properly format posts for mailing list

* Move make_all_links_absolute into Email::Styles

* Apply first_seen_at to user

* Send mailing list email summary hourly based on first_seen_at

* Branch and test cleanup

* Use existing mailing list mode estimate

* Fix failing specs
2016-05-21 15:17:54 +02:00

73 lines
1.8 KiB
Ruby

module UserNotificationsHelper
def indent(text, by=2)
spacer = " " * by
result = ""
text.each_line do |line|
result << spacer << line
end
result
end
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
def logo_url
logo_url = SiteSetting.digest_logo_url
logo_url = SiteSetting.logo_url if logo_url.blank? || logo_url =~ /\.svg$/i
return nil if logo_url.blank? || logo_url =~ /\.svg$/i
if logo_url !~ /http(s)?\:\/\//
logo_url = "#{Discourse.base_url}#{logo_url}"
end
logo_url
end
def html_site_link(color)
"<a href='#{Discourse.base_url}' style='color: ##{color}'>#{@site_name}</a>"
end
def first_paragraph_from(html)
doc = Nokogiri::HTML(html)
result = ""
doc.css('p').each do |p|
if p.text.present?
result << p.to_s
return result if result.size >= 100
end
end
return result unless result.blank?
# If there is no first paragaph, return the first div (onebox)
doc.css('div').first
end
def email_excerpt(html, posts_count)
# only include 1st paragraph when more than 1 posts
html = first_paragraph_from(html).to_s if posts_count > 1
PrettyText.format_for_email(html).html_safe
end
def normalize_name(name)
name.downcase.gsub(/[\s_-]/, '')
end
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
def format_for_email(post, use_excerpt, style: nil)
html = use_excerpt ? post.excerpt : post.cooked
PrettyText.format_for_email(html, post, style: style).html_safe
end
end