Skip to content

Commit

Permalink
FIX: Provide the ability to reduce cooked content
Browse files Browse the repository at this point in the history
This allows us to strip polls from the group posts page.
  • Loading branch information
eviltrout committed Apr 13, 2016
1 parent 8fcd359 commit e913799
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
7 changes: 4 additions & 3 deletions app/helpers/user_notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def first_paragraph_from(html)
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
raw format_for_email(html)
PrettyText.format_for_email(html).html_safe
end

def normalize_name(name)
Expand All @@ -65,8 +65,9 @@ def show_name_on_post(post)
normalize_name(post.user.name) != normalize_name(post.user.username)
end

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

end
6 changes: 6 additions & 0 deletions app/serializers/group_post_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def topic
object.topic
end

def cooked
fragment = Nokogiri::HTML.fragment(object.cooked)
DiscourseEvent.trigger(:reduce_cooked, fragment, object)
fragment.to_html
end

def category
object.topic.category
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/email/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</td>
</tr>
<tr>
<td class='body'><%= format_for_email(use_excerpt ? post.excerpt : post.cooked) %></td>
<td class='body'><%= format_for_email(post, use_excerpt) %></td>
</tr>
</tbody>
</table>
3 changes: 2 additions & 1 deletion lib/pretty_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ def self.strip_image_wrapping(doc)
doc.css(".lightbox-wrapper .meta").remove
end

def self.format_for_email(html)
def self.format_for_email(html, post = nil)
doc = Nokogiri::HTML.fragment(html)
DiscourseEvent.trigger(:reduce_cooked, doc, post)
make_all_links_absolute(doc)
strip_image_wrapping(doc)
doc.to_html
Expand Down
26 changes: 12 additions & 14 deletions plugins/poll/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@

after_initialize do

# turn polls into a link in emails
Email::Styles.register_plugin_style do |fragment, opts|
post = Post.find_by(id: opts[:post_id]) rescue nil
if post.nil? || post.trashed?
fragment.css(".poll").each(&:remove)
else
post_url = "#{Discourse.base_url}#{post.url}"
fragment.css(".poll").each do |poll|
poll.replace "<p><a href='#{post_url}'>#{I18n.t("poll.email.link_to_poll")}</a></p>"
end
end
end

module ::DiscoursePoll
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
Expand Down Expand Up @@ -281,7 +268,7 @@ def toggle_status
self.errors.add(:base, I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")) :
self.errors.add(:base, I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: poll["name"]))
return
end
end
end

# store the valid poll
Expand Down Expand Up @@ -367,6 +354,17 @@ def toggle_status
user ? [POLLS_CUSTOM_FIELD, VOTES_CUSTOM_FIELD] : [POLLS_CUSTOM_FIELD]
end

on(:reduce_cooked) do |fragment, post|
if post.nil? || post.trashed?
fragment.css(".poll, [data-poll-name]").each(&:remove)
else
post_url = "#{Discourse.base_url}#{post.url}"
fragment.css(".poll, [data-poll-name]").each do |poll|
poll.replace "<p><a href='#{post_url}'>#{I18n.t("poll.email.link_to_poll")}</a></p>"
end
end
end

# tells the front-end we have a poll for that post
on(:post_created) do |post|
next if post.is_first_post? || post.custom_fields[POLLS_CUSTOM_FIELD].blank?
Expand Down

0 comments on commit e913799

Please sign in to comment.