A platform for community discussion. Free, open, simple.
FEATURE: Implement Onebox for posts including polls. (#7539)
Bianca Nenciu227c45107df6
diff --git a/app/models/post.rb b/app/models/post.rbindex efb75dd8827..09691d10b86 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -453,11 +453,11 @@ class Post < ActiveRecord::Base# Strip out most of the markupdef excerpt(maxlength = nil, options = {})- Post.excerpt(cooked, maxlength, options)+ Post.excerpt(cooked, maxlength, options.merge(post: self))enddef 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)enddef is_first_post?diff --git a/app/models/user_profile.rb b/app/models/user_profile.rbindex a6453dec4ed..3e51fd91733 100644--- a/app/models/user_profile.rb+++ b/app/models/user_profile.rb@@ -24,6 +24,7 @@ class UserProfile < ActiveRecord::BaseBAKED_VERSION = 1def 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::Baseenddef bio_summary- return nil unless bio_cooked.present?bio_excerpt(500, strip_links: true, text_entities: true)enddiff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex 79b6e66a4d7..123bb916474 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -347,6 +347,7 @@ module PrettyTextdef self.excerpt(html, max_length, options = {})# TODO: properly fix this HACK in ExcerptParser without introducing XSSdoc = Nokogiri::HTML.fragment(html)+ DiscourseEvent.trigger(:reduce_excerpt, doc, options)strip_image_wrapping(doc)html = doc.to_htmldiff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.ymlindex 1fdcbf47ca2..c667a9dd95c 100644--- a/plugins/poll/config/locales/server.en.yml+++ b/plugins/poll/config/locales/server.en.yml@@ -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."diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rbindex 7b64ddf5790..4a494cf4c5b 100644--- a/plugins/poll/plugin.rb+++ b/plugins/poll/plugin.rb@@ -435,6 +435,13 @@ after_initialize doendend+ 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)diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rbindex a7e1f3d36c7..a1b834ac7ca 100644--- a/plugins/poll/spec/lib/pretty_text_spec.rb+++ b/plugins/poll/spec/lib/pretty_text_spec.rb@@ -119,4 +119,21 @@ describe PrettyText doexpect(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>")+ endend