From 03143d944973b39730576928dcf2f9007996acd8 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Tue, 21 Jan 2020 17:42:41 +0200 Subject: [PATCH] FIX: Do not extract dates from quotes and Oneboxes (#8754) Post.local_dates used to contain dates from quotes and Oneboxes. --- plugins/discourse-local-dates/plugin.rb | 22 ++++++++++-------- .../spec/models/post_spec.rb | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/plugins/discourse-local-dates/plugin.rb b/plugins/discourse-local-dates/plugin.rb index 5ee9fefad73..616d337270f 100644 --- a/plugins/discourse-local-dates/plugin.rb +++ b/plugins/discourse-local-dates/plugin.rb @@ -26,19 +26,21 @@ after_initialize do register_post_custom_field_type(DiscourseLocalDates::POST_CUSTOM_FIELD, :json) on(:before_post_process_cooked) do |doc, post| - dates = - doc.css('span.discourse-local-date').map do |cooked_date| - date = {} - cooked_date.attributes.values.each do |attribute| - data_name = attribute.name&.gsub('data-', '') - if data_name && %w[date time timezone recurring].include?(data_name) - unless attribute.value == 'undefined' - date[data_name] = CGI.escapeHTML(attribute.value || '') - end + dates = [] + + doc.css('span.discourse-local-date').map do |cooked_date| + next if cooked_date.ancestors("aside").length > 0 + date = {} + cooked_date.attributes.values.each do |attribute| + data_name = attribute.name&.gsub('data-', '') + if data_name && %w[date time timezone recurring].include?(data_name) + unless attribute.value == 'undefined' + date[data_name] = CGI.escapeHTML(attribute.value || '') end end - date end + dates << date + end if dates.present? post.custom_fields[DiscourseLocalDates::POST_CUSTOM_FIELD] = dates diff --git a/plugins/discourse-local-dates/spec/models/post_spec.rb b/plugins/discourse-local-dates/spec/models/post_spec.rb index 904b2f6c134..ef003b09d57 100644 --- a/plugins/discourse-local-dates/spec/models/post_spec.rb +++ b/plugins/discourse-local-dates/spec/models/post_spec.rb @@ -26,6 +26,29 @@ describe Post do expect(post.local_dates).to eq([]) end + + it "should not contain dates from quotes" do + post = Fabricate(:post, raw: <<~SQL) + [quote] + [date=2018-09-17 time=01:39:00 format="LLL" timezone="Europe/Paris" timezones="Europe/Paris|America/Los_Angeles"] + [/quote] + SQL + CookedPostProcessor.new(post).post_process + + expect(post.local_dates.count).to eq(0) + end + + it "should not contain dates from examples" do + Oneboxer.stubs(:cached_onebox).with('https://example.com').returns(<<-HTML) + + HTML + post = Fabricate(:post, raw: "https://example.com") + CookedPostProcessor.new(post).post_process + + expect(post.local_dates.count).to eq(0) + end end end