FEATURE: Implement Onebox for posts including polls. (#7539)

This commit is contained in:
Bianca Nenciu 2019-05-29 18:05:52 +03:00 committed by Régis Hanol
parent c77bc525cb
commit 227c45107d
6 changed files with 29 additions and 3 deletions

View File

@ -453,11 +453,11 @@ class Post < ActiveRecord::Base
# Strip out most of the markup
def excerpt(maxlength = nil, options = {})
Post.excerpt(cooked, maxlength, options)
Post.excerpt(cooked, maxlength, options.merge(post: self))
end
def excerpt_for_topic
Post.excerpt(cooked, 220, strip_links: true, strip_images: true)
Post.excerpt(cooked, 220, strip_links: true, strip_images: true, post: self)
end
def is_first_post?

View File

@ -24,6 +24,7 @@ class UserProfile < ActiveRecord::Base
BAKED_VERSION = 1
def bio_excerpt(length = 350, opts = {})
return nil if bio_cooked.blank?
excerpt = PrettyText.excerpt(bio_cooked, length, opts).sub(/<br>$/, '')
return excerpt if excerpt.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
PrettyText.strip_links(excerpt)
@ -35,7 +36,6 @@ class UserProfile < ActiveRecord::Base
end
def bio_summary
return nil unless bio_cooked.present?
bio_excerpt(500, strip_links: true, text_entities: true)
end

View File

@ -347,6 +347,7 @@ module PrettyText
def self.excerpt(html, max_length, options = {})
# TODO: properly fix this HACK in ExcerptParser without introducing XSS
doc = Nokogiri::HTML.fragment(html)
DiscourseEvent.trigger(:reduce_excerpt, doc, options)
strip_image_wrapping(doc)
html = doc.to_html

View File

@ -22,6 +22,7 @@ en:
poll_minimum_trust_level_to_create: "Define the minimum trust level needed to create polls."
poll:
poll: "poll"
invalid_argument: "Invalid value '%{value}' for argument '%{argument}'."
multiple_polls_without_name: "There are multiple polls without a name. Use the '<code>name</code>' attribute to uniquely identify your polls."

View File

@ -435,6 +435,13 @@ after_initialize do
end
end
on(:reduce_excerpt) do |doc, options|
post = options[:post]
doc.css("div.poll").each do |poll|
poll.replace "<a href='#{UrlHelper.escape_uri(post.url)}'>#{I18n.t("poll.poll")}</a>"
end
end
on(:post_created) do |post|
DiscoursePoll::Poll.schedule_jobs(post)

View File

@ -119,4 +119,21 @@ describe PrettyText do
expect(n cooked).to eq(n expected)
end
it 'can onebox posts' do
post = Fabricate(:post, raw: <<~EOF)
A post with a poll
[poll type=regular]
* Hello
* World
[/poll]
EOF
onebox = Oneboxer.onebox_raw(post.full_url, user_id: Fabricate(:user).id)
doc = Nokogiri::HTML(onebox[:preview])
expect(onebox[:preview]).to include("A post with a poll")
expect(onebox[:preview]).to include("<a href=\"#{post.url}\">poll</a>")
end
end