Skip to content

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

PERF: Use the embed URL index to find imported topics in RSS polling

Régis Hanol3b29c7b176c1
diff --git a/plugins/discourse-rss-polling/app/services/discourse_rss_polling/rss_feed/action/imported_topics.rb b/plugins/discourse-rss-polling/app/services/discourse_rss_polling/rss_feed/action/imported_topics.rb
index 3992b836b22..812819c8bf0 100644
--- a/plugins/discourse-rss-polling/app/services/discourse_rss_polling/rss_feed/action/imported_topics.rb
+++ b/plugins/discourse-rss-polling/app/services/discourse_rss_polling/rss_feed/action/imported_topics.rb
@@ -7,24 +7,20 @@ module DiscourseRssPolling
option :feed_items
def call
- keys = feed_items.index_with { |feed_item| embed_key(feed_item.url) }
- topic_urls = topic_urls_by_key(keys.values)
-
- keys.each_with_object({}) do |(feed_item, key), imported|
- imported[feed_item] = topic_urls[key] if key && topic_urls.key?(key)
- end
+ topic_urls = topic_urls_by_key(feed_items.map(&:url))
+ feed_items.index_with { |feed_item| topic_urls[embed_key(feed_item.url)] }.compact
end
private
- def topic_urls_by_key(keys)
- keys = keys.compact.uniq
- return {} if keys.empty?
-
- patterns = keys.map { |key| "^https?://#{Regexp.escape(key)}$" }
+ def topic_urls_by_key(urls)
+ urls = urls.compact_blank.uniq
+ return {} if urls.empty?
TopicEmbed
- .where("embed_url ~* ANY(ARRAY[?])", patterns)
+ .with_embed_urls(urls)
+ .select(:id, :embed_url, :topic_id)
+ .order(:id)
.includes(:topic)
.each_with_object({}) do |embed, topic_urls|
next if embed.topic.nil?
@@ -36,7 +32,7 @@ module DiscourseRssPolling
def embed_key(url)
return if url.blank?
- TopicEmbed.normalize_url(url).sub(%r{\Ahttps?://}, "")
+ TopicEmbed.embed_url_key(url)
end
end
end
diff --git a/plugins/discourse-rss-polling/spec/services/rss_feed/action/imported_topics_spec.rb b/plugins/discourse-rss-polling/spec/services/rss_feed/action/imported_topics_spec.rb
index 4eadb097ab4..d126c2af283 100644
--- a/plugins/discourse-rss-polling/spec/services/rss_feed/action/imported_topics_spec.rb
+++ b/plugins/discourse-rss-polling/spec/services/rss_feed/action/imported_topics_spec.rb
@@ -20,6 +20,19 @@ RSpec.describe DiscourseRssPolling::RssFeed::Action::ImportedTopics do
expect(imported.keys).to contain_exactly(imported_item)
expect(imported[imported_item]).to be_present
end
+
+ context "when the item url differs by scheme, case or trailing slash" do
+ let(:imported_item) do
+ instance_double(
+ DiscourseRssPolling::FeedItem,
+ url: "http://Blog.Discourse.org/2017/09/poll-feed-spec-fixture",
+ )
+ end
+
+ it "returns the topic url for the imported item" do
+ expect(imported.keys).to contain_exactly(imported_item)
+ end
+ end
end
context "when no item has been imported" do