FIX: Respect invalidate_oneboxes option for inline oneboxes

This commit is contained in:
Vinoth Kannan 2018-09-03 22:33:43 +05:30
parent ecf60c0c33
commit 24a14af15a
7 changed files with 40 additions and 8 deletions

View File

@ -100,7 +100,7 @@ function applyOnebox(state, silent) {
let options = state.md.options.discourse; let options = state.md.options.discourse;
if (options.lookupInlineOnebox) { if (options.lookupInlineOnebox) {
onebox = options.lookupInlineOnebox(href); onebox = options.lookupInlineOnebox(href, options.invalidateOneboxes);
} }
if (onebox && onebox.title) { if (onebox && onebox.title) {

View File

@ -31,7 +31,8 @@ export function buildOptions(state) {
previewing, previewing,
linkify, linkify,
censoredWords, censoredWords,
mentionLookup mentionLookup,
invalidateOneboxes
} = state; } = state;
let features = { let features = {
@ -80,7 +81,8 @@ export function buildOptions(state) {
markdownIt: true, markdownIt: true,
injectLineNumbersToPreview: injectLineNumbersToPreview:
siteSettings.enable_advanced_editor_preview_sync, siteSettings.enable_advanced_editor_preview_sync,
previewing previewing,
invalidateOneboxes
}; };
// note, this will mutate options due to the way the API is designed // note, this will mutate options due to the way the API is designed

View File

@ -23,8 +23,9 @@ class InlineOneboxer
def self.lookup(url, opts = nil) def self.lookup(url, opts = nil)
opts ||= {} opts ||= {}
opts = opts.with_indifferent_access
unless opts[:skip_cache] unless opts[:skip_cache] || opts[:invalidate]
cached = cache_lookup(url) cached = cache_lookup(url)
return cached if cached.present? return cached if cached.present?
end end

View File

@ -172,6 +172,10 @@ module PrettyText
buffer << "__optInput.userId = #{opts[:user_id].to_i};\n" buffer << "__optInput.userId = #{opts[:user_id].to_i};\n"
end end
if opts[:invalidate_oneboxes]
buffer << "__optInput.invalidateOneboxes = true;\n"
end
buffer << "__textOptions = __buildOptions(__optInput);\n" buffer << "__textOptions = __buildOptions(__optInput);\n"
buffer << ("__pt = new __PrettyText(__textOptions);") buffer << ("__pt = new __PrettyText(__textOptions);")

View File

@ -77,8 +77,8 @@ module PrettyText
result result
end end
def lookup_inline_onebox(url) def lookup_inline_onebox(url, opts = {})
InlineOneboxer.lookup(url) InlineOneboxer.lookup(url, opts)
end end
def get_topic_info(topic_id) def get_topic_info(topic_id)

View File

@ -49,8 +49,14 @@ function __getURL(url) {
return url; return url;
} }
function __lookupInlineOnebox(url) { function __lookupInlineOnebox(url, invalidate = false) {
return __helpers.lookup_inline_onebox(url); const opts = {};
if (invalidate) {
opts["invalidate"] = true;
}
return __helpers.lookup_inline_onebox(url, opts);
} }
function __lookupImageUrls(urls) { function __lookupImageUrls(urls) {

View File

@ -1195,6 +1195,25 @@ HTML
expect(PrettyText.cook(raw)).to eq(cooked.strip) expect(PrettyText.cook(raw)).to eq(cooked.strip)
end end
it "invalidates the onebox url" do
topic = Fabricate(:topic)
url = topic.url
raw = "Hello #{url}"
PrettyText.cook(raw)
topic.title = "Updated: #{topic.title}"
topic.save
cooked = <<~HTML
<p>Hello <a href="#{url}">#{topic.title}</a></p>
HTML
expect(PrettyText.cook(raw)).not_to eq(cooked.strip)
expect(PrettyText.cook(raw, invalidate_oneboxes: true)).to eq(cooked.strip)
expect(PrettyText.cook(raw)).to eq(cooked.strip)
end
end end
describe "image decoding" do describe "image decoding" do