From fc2d61136d67432051417272348039599def96a2 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 25 Sep 2015 14:52:43 +1000 Subject: [PATCH] FEATURE: add context for cross topic links --- lib/cooked_post_processor.rb | 16 +++++++++++++++- lib/onebox/engine/discourse_local_onebox.rb | 11 +++++++---- .../onebox/engine/discourse_local_onebox_spec.rb | 7 ++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 13d30c2c972..a74e4fa7414 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -245,7 +245,21 @@ class CookedPostProcessor } # apply oneboxes - Oneboxer.apply(@doc) { |url| Oneboxer.onebox(url, args) } + Oneboxer.apply(@doc) { |url| + + # hack urls to create proper expansions + if url =~ Regexp.new("^#{Discourse.base_url.gsub(".","\\.")}.*$", true) + uri = URI.parse(url) rescue nil + if uri && uri.path + route = Rails.application.routes.recognize_path(uri.path) rescue nil + if route && route[:controller] == 'topics' + url += (url =~ /\?/ ? "&" : "?") + "&source_topic_id=#{@post.topic_id}" + end + end + end + + Oneboxer.onebox(url, args) + } # make sure we grab dimensions for oneboxed images oneboxed_images.each { |img| limit_size!(img) } diff --git a/lib/onebox/engine/discourse_local_onebox.rb b/lib/onebox/engine/discourse_local_onebox.rb index db4075b43e2..06d54494687 100644 --- a/lib/onebox/engine/discourse_local_onebox.rb +++ b/lib/onebox/engine/discourse_local_onebox.rb @@ -33,13 +33,14 @@ module Onebox def to_html uri = URI::parse(@url) route = Rails.application.routes.recognize_path(uri.path) - + url = @url.sub(/[&?]source_topic_id=(\d+)/, "") + source_topic_id = $1.to_i # Figure out what kind of onebox to show based on the URL case route[:controller] when 'topics' - linked = "#{@url}" + linked = "#{url}" if route[:post_number].present? && route[:post_number].to_i > 1 # Post Link post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number].to_i) @@ -56,7 +57,9 @@ module Onebox excerpt.gsub!("[/quote]", "[quote]") quote = "[quote=\"#{post.user.username}, topic:#{topic.id}, slug:#{slug}, post:#{post.post_number}\"]#{excerpt}[/quote]" - cooked = PrettyText.cook(quote) + args = {} + args[:topic_id] = source_topic_id if source_topic_id > 0 + cooked = PrettyText.cook(quote, args) return cooked else @@ -77,7 +80,7 @@ module Onebox end quote = post.excerpt(SiteSetting.post_onebox_maxlength) - args = { original_url: @url, + args = { original_url: url, title: topic.title, avatar: PrettyText.avatar_img(topic.user.avatar_template, 'tiny'), posts_count: topic.posts_count, diff --git a/spec/components/onebox/engine/discourse_local_onebox_spec.rb b/spec/components/onebox/engine/discourse_local_onebox_spec.rb index 4ca2d8eaf3c..2b5beb1fb16 100644 --- a/spec/components/onebox/engine/discourse_local_onebox_spec.rb +++ b/spec/components/onebox/engine/discourse_local_onebox_spec.rb @@ -35,9 +35,14 @@ describe Onebox::Engine::DiscourseLocalOnebox do end it "returns some onebox goodness if post exists and can be seen" do - url = "#{Discourse.base_url}#{post2.url}" + url = "#{Discourse.base_url}#{post2.url}?source_topic_id=#{post2.topic_id+1}" Guardian.any_instance.stubs(:can_see?).returns(true) html = Onebox.preview(url).to_s + expect(html).to include(post2.excerpt) + expect(html).to include(post2.topic.title) + + url = "#{Discourse.base_url}#{post2.url}" + html = Onebox.preview(url).to_s expect(html).to include(post2.user.username) expect(html).to include(post2.excerpt) end