Skip to content

A platform for community discussion. Free, open, simple.

FIX: include title in local onebox when linking to a different topic

Régis Hanol60ec483caae5
diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6
index b0623158e4c..86a9b02e501 100644
--- a/app/assets/javascripts/discourse/components/composer-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6
@@ -372,7 +372,13 @@ export default Ember.Component.extend({
post.set('refreshedPost', true);
}
- $oneboxes.each((_, o) => load({ elem: o, refresh, ajax, categoryId: this.get('composer.category.id') }));
+ $oneboxes.each((_, o) => load({
+ elem: o,
+ refresh,
+ ajax,
+ categoryId: this.get('composer.category.id'),
+ topicId: this.get('composer.topic.id')
+ }));
},
_warnMentionedGroups($preview) {
diff --git a/app/assets/javascripts/discourse/components/composer-title.js.es6 b/app/assets/javascripts/discourse/components/composer-title.js.es6
index 86acf45c773..23d5443b0cc 100644
--- a/app/assets/javascripts/discourse/components/composer-title.js.es6
+++ b/app/assets/javascripts/discourse/components/composer-title.js.es6
@@ -86,6 +86,7 @@ export default Ember.Component.extend({
ajax,
synchronous: true,
categoryId: this.get('composer.category.id'),
+ topicId: this.get('composer.topic.id')
});
if (loadOnebox && loadOnebox.then) {
diff --git a/app/assets/javascripts/pretty-text/oneboxer.js.es6 b/app/assets/javascripts/pretty-text/oneboxer.js.es6
index 90538994b7f..aef317865dd 100644
--- a/app/assets/javascripts/pretty-text/oneboxer.js.es6
+++ b/app/assets/javascripts/pretty-text/oneboxer.js.es6
@@ -43,12 +43,17 @@ function loadNext(ajax) {
let timeoutMs = 150;
let removeLoading = true;
- const { url, refresh, $elem, categoryId } = loadingQueue.shift();
+ const { url, refresh, $elem, categoryId, topicId } = loadingQueue.shift();
// Retrieve the onebox
return ajax("/onebox", {
dataType: 'html',
- data: { url, refresh, category_id: categoryId },
+ data: {
+ url,
+ refresh,
+ category_id: categoryId,
+ topic_id: topicId
+ },
cache: true
}).then(html => {
let $html = $(html);
@@ -59,7 +64,7 @@ function loadNext(ajax) {
if (result && result.jqXHR && result.jqXHR.status === 429) {
timeoutMs = 2000;
removeLoading = false;
- loadingQueue.unshift({ url, refresh, $elem, categoryId });
+ loadingQueue.unshift({ url, refresh, $elem, categoryId, topicId });
} else {
failedCache[normalize(url)] = true;
}
@@ -74,7 +79,7 @@ function loadNext(ajax) {
// Perform a lookup of a onebox based an anchor $element.
// It will insert a loading indicator and remove it when the loading is complete or fails.
-export function load({ elem , refresh = true, ajax, synchronous = false, categoryId }) {
+export function load({ elem , refresh = true, ajax, synchronous = false, categoryId, topicId }) {
const $elem = $(elem);
// If the onebox has loaded or is loading, return
@@ -98,7 +103,7 @@ export function load({ elem , refresh = true, ajax, synchronous = false, categor
$elem.addClass('loading-onebox');
// Add to the loading queue
- loadingQueue.push({ url, refresh, $elem, categoryId });
+ loadingQueue.push({ url, refresh, $elem, categoryId, topicId });
// Load next url in queue
if (synchronous) {
diff --git a/app/controllers/onebox_controller.rb b/app/controllers/onebox_controller.rb
index dea29ce5368..f093342c772 100644
--- a/app/controllers/onebox_controller.rb
+++ b/app/controllers/onebox_controller.rb
@@ -15,6 +15,7 @@ class OneboxController < ApplicationController
user_id = current_user.id
category_id = params[:category_id].to_i
+ topic_id = params[:topic_id].to_i
invalidate = params[:refresh] == 'true'
url = params[:url]
@@ -24,7 +25,8 @@ class OneboxController < ApplicationController
preview = Oneboxer.preview(url,
invalidate_oneboxes: invalidate,
user_id: user_id,
- category_id: category_id
+ category_id: category_id,
+ topic_id: topic_id
)
preview.strip! if preview.present?
diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb
index 338ec2b2a80..ecf9d14195e 100644
--- a/lib/oneboxer.rb
+++ b/lib/oneboxer.rb
@@ -170,6 +170,10 @@ module Oneboxer
return unless Guardian.new(current_user).can_see_category?(current_category)
end
+ if current_topic = Topic.find_by(id: opts[:topic_id])
+ return unless Guardian.new(current_user).can_see_topic?(current_topic)
+ end
+
topic = Topic.find_by(id: route[:topic_id])
return unless topic
@@ -187,7 +191,7 @@ module Oneboxer
return if !post || post.hidden || post.post_type != Post.types[:regular]
- if post_number > 1
+ if post_number > 1 && current_topic&.id == topic.id
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength)
excerpt.gsub!(/[\r\n]+/, " ")
excerpt.gsub!("[/quote]", "[quote]") # don't break my quote
diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb
index 5fb6e2ccfc4..ee1e4600bb7 100644
--- a/spec/components/oneboxer_spec.rb
+++ b/spec/components/oneboxer_spec.rb
@@ -18,8 +18,11 @@ describe Oneboxer do
%{<a href="#{url}">#{url}</a>}
end
- def preview(url, user, category = Category.first)
- Oneboxer.preview("#{Discourse.base_url}#{url}", user_id: user.id, category_id: category.id).to_s
+ def preview(url, user = nil, category = nil, topic = nil)
+ Oneboxer.preview("#{Discourse.base_url}#{url}",
+ user_id: user&.id,
+ category_id: category&.id,
+ topic_id: topic&.id).to_s
end
it "links to a topic/post" do
@@ -44,7 +47,8 @@ describe Oneboxer do
expect(preview(public_topic.relative_url, user, public_category)).to include(public_topic.title)
expect(preview(public_post.url, user, public_category)).to include(public_topic.title)
- expect(preview(public_reply.url, user, public_category)).to include(public_reply.cooked)
+ expect(preview(public_reply.url, user, public_category)).to include(public_reply.excerpt)
+ expect(preview(public_reply.url, user, public_category, public_topic)).not_to include(public_topic.title)
expect(preview(public_hidden.url, user, public_category)).to match_html(link(public_hidden.url))
expect(preview(secured_topic.relative_url, user, public_category)).to match_html(link(secured_topic.relative_url))
expect(preview(secured_post.url, user, public_category)).to match_html(link(secured_post.url))
@@ -57,27 +61,27 @@ describe Oneboxer do
expect(preview(public_topic.relative_url, staff, secured_category)).to include(public_topic.title)
expect(preview(public_post.url, staff, secured_category)).to include(public_topic.title)
- expect(preview(public_reply.url, staff, secured_category)).to include(public_reply.cooked)
+ expect(preview(public_reply.url, staff, secured_category)).to include(public_reply.excerpt)
expect(preview(public_hidden.url, staff, secured_category)).to match_html(link(public_hidden.url))
expect(preview(secured_topic.relative_url, staff, secured_category)).to include(secured_topic.title)
expect(preview(secured_post.url, staff, secured_category)).to include(secured_topic.title)
- expect(preview(secured_reply.url, staff, secured_category)).to include(secured_reply.cooked)
+ expect(preview(secured_reply.url, staff, secured_category)).to include(secured_reply.excerpt)
+ expect(preview(secured_reply.url, staff, secured_category, secured_topic)).not_to include(secured_topic.title)
end
it "links to an user profile" do
user = Fabricate(:user)
- expect(preview("/u/does-not-exist", user)).to match_html(link("/u/does-not-exist"))
- expect(preview("/u/#{user.username}", user)).to include(user.name)
+ expect(preview("/u/does-not-exist")).to match_html(link("/u/does-not-exist"))
+ expect(preview("/u/#{user.username}")).to include(user.name)
end
it "links to an upload" do
- user = Fabricate(:user)
path = "/uploads/default/original/3X/e/8/e8fcfa624e4fb6623eea57f54941a58ba797f14d"
- expect(preview("#{path}.pdf", user)).to match_html(link("#{path}.pdf"))
- expect(preview("#{path}.MP3", user)).to include("<audio ")
- expect(preview("#{path}.mov", user)).to include("<video ")
+ expect(preview("#{path}.pdf")).to match_html(link("#{path}.pdf"))
+ expect(preview("#{path}.MP3")).to include("<audio ")
+ expect(preview("#{path}.mov")).to include("<video ")
end
end