FIX: postprocessing ignored cook method

This commit is contained in:
Gerhard Schlager 2017-10-17 20:37:51 +02:00
parent 1481462cbf
commit 4205c1ad2b
4 changed files with 49 additions and 27 deletions

View File

@ -5,7 +5,6 @@ require_dependency 'enum'
require_dependency 'post_analyzer' require_dependency 'post_analyzer'
require_dependency 'validators/post_validator' require_dependency 'validators/post_validator'
require_dependency 'plugin/filter' require_dependency 'plugin/filter'
require_dependency 'email_cook'
require 'archetype' require 'archetype'
require 'digest/sha1' require 'digest/sha1'
@ -231,37 +230,32 @@ class Post < ActiveRecord::Base
!add_nofollow? !add_nofollow?
end end
def cook(*args) def cook(raw, opts = {})
# For some posts, for example those imported via RSS, we support raw HTML. In that # For some posts, for example those imported via RSS, we support raw HTML. In that
# case we can skip the rendering pipeline. # case we can skip the rendering pipeline.
return raw if cook_method == Post.cook_methods[:raw_html] return raw if cook_method == Post.cook_methods[:raw_html]
cooked = options = opts.dup
if cook_method == Post.cook_methods[:email] options[:cook_method] = cook_method
EmailCook.new(raw).cook
else
cloned = args.dup
cloned[1] ||= {}
post_user = self.user post_user = self.user
cloned[1][:user_id] = post_user.id if post_user options[:user_id] = post_user.id if post_user
if add_nofollow? if add_nofollow?
post_analyzer.cook(*args) cooked = post_analyzer.cook(raw, options)
else else
# At trust level 3, we don't apply nofollow to links # At trust level 3, we don't apply nofollow to links
cloned[1][:omit_nofollow] = true options[:omit_nofollow] = true
post_analyzer.cook(*cloned) cooked = post_analyzer.cook(raw, options)
end
end end
new_cooked = Plugin::Filter.apply(:after_post_cook, self, cooked) new_cooked = Plugin::Filter.apply(:after_post_cook, self, cooked)
if post_type == Post.types[:regular] if post_type == Post.types[:regular]
if new_cooked != cooked && new_cooked.blank? if new_cooked != cooked && new_cooked.blank?
Rails.logger.debug("Plugin is blanking out post: #{self.url}\nraw: #{self.raw}") Rails.logger.debug("Plugin is blanking out post: #{self.url}\nraw: #{raw}")
elsif new_cooked.blank? elsif new_cooked.blank?
Rails.logger.debug("Blank post detected post: #{self.url}\nraw: #{self.raw}") Rails.logger.debug("Blank post detected post: #{self.url}\nraw: #{raw}")
end end
end end

View File

@ -1,4 +1,5 @@
require_dependency 'oneboxer' require_dependency 'oneboxer'
require_dependency 'email_cook'
class PostAnalyzer class PostAnalyzer
@ -13,12 +14,19 @@ class PostAnalyzer
end end
# What we use to cook posts # What we use to cook posts
def cook(*args) def cook(raw, opts = {})
cooked = PrettyText.cook(*args) cook_method = opts[:cook_method]
return raw if cook_method == Post.cook_methods[:raw_html]
if cook_method == Post.cook_methods[:email]
cooked = EmailCook.new(raw).cook
else
cooked = PrettyText.cook(raw, opts)
end
result = Oneboxer.apply(cooked, topic_id: @topic_id) do |url, _| result = Oneboxer.apply(cooked, topic_id: @topic_id) do |url, _|
@found_oneboxes = true @found_oneboxes = true
Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes] Oneboxer.invalidate(url) if opts[:invalidate_oneboxes]
Oneboxer.cached_onebox(url) Oneboxer.cached_onebox(url)
end end

View File

@ -20,6 +20,7 @@ class CookedPostProcessor
@cooking_options[:topic_id] = post.topic_id @cooking_options[:topic_id] = post.topic_id
@cooking_options = @cooking_options.symbolize_keys @cooking_options = @cooking_options.symbolize_keys
@cooking_options[:omit_nofollow] = true if post.omit_nofollow? @cooking_options[:omit_nofollow] = true if post.omit_nofollow?
@cooking_options[:cook_method] = post.cook_method
analyzer = post.post_analyzer analyzer = post.post_analyzer
@doc = Nokogiri::HTML::fragment(analyzer.cook(post.raw, @cooking_options)) @doc = Nokogiri::HTML::fragment(analyzer.cook(post.raw, @cooking_options))

View File

@ -10,19 +10,18 @@ describe PostAnalyzer do
let(:raw) { "Here's a tweet:\n#{url}" } let(:raw) { "Here's a tweet:\n#{url}" }
let(:options) { {} } let(:options) { {} }
let(:args) { [raw, options] }
before { Oneboxer.stubs(:onebox) } before { Oneboxer.stubs(:onebox) }
it 'fetches the cached onebox for any urls in the post' do it 'fetches the cached onebox for any urls in the post' do
Oneboxer.expects(:cached_onebox).with url Oneboxer.expects(:cached_onebox).with url
post_analyzer.cook(*args) post_analyzer.cook(raw, options)
expect(post_analyzer.found_oneboxes?).to be(true) expect(post_analyzer.found_oneboxes?).to be(true)
end end
it 'does not invalidate the onebox cache' do it 'does not invalidate the onebox cache' do
Oneboxer.expects(:invalidate).with(url).never Oneboxer.expects(:invalidate).with(url).never
post_analyzer.cook(*args) post_analyzer.cook(raw, options)
end end
context 'when invalidating oneboxes' do context 'when invalidating oneboxes' do
@ -30,9 +29,29 @@ describe PostAnalyzer do
it 'invalidates the oneboxes for urls in the post' do it 'invalidates the oneboxes for urls in the post' do
Oneboxer.expects(:invalidate).with url Oneboxer.expects(:invalidate).with url
post_analyzer.cook(*args) post_analyzer.cook(raw, options)
end end
end end
it "does nothing when the cook_method is 'raw_html'" do
cooked = post_analyzer.cook('Hello <div/> world', cook_method: Post.cook_methods[:raw_html])
expect(cooked).to eq('Hello <div/> world')
end
it "does not interpret Markdown when cook_method is 'email'" do
cooked = post_analyzer.cook('*this is not italic* and here is a link: https://www.example.com', cook_method: Post.cook_methods[:email])
expect(cooked).to eq('*this is not italic* and here is a link: <a href="https://www.example.com">https://www.example.com</a>')
end
it "does interpret Markdown when cook_method is 'regular'" do
cooked = post_analyzer.cook('*this is italic*', cook_method: Post.cook_methods[:regular])
expect(cooked).to eq('<p><em>this is italic</em></p>')
end
it "does interpret Markdown when not cook_method is set" do
cooked = post_analyzer.cook('*this is italic*')
expect(cooked).to eq('<p><em>this is italic</em></p>')
end
end end
context "links" do context "links" do