Skip to content

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

FIX: use FinalDestination#get for RSS polling to fix auth and eliminate redundant requests (#37840)

Régis Hanold2d1b4f53d2a
diff --git a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
index b7fba3507d6..1a0eed654b3 100644
--- a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
+++ b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
@@ -8,7 +8,8 @@ module Jobs
def execute(args)
return unless SiteSetting.rss_polling_enabled
- return unless @author = User.find_by_username(args[:author_username])
+ @author = User.find_by_username(args[:author_username])
+ return if @author.nil?
@discourse_category_id = args[:discourse_category_id]
return if @discourse_category_id.present? && !Category.exists?(@discourse_category_id)
@@ -39,13 +40,10 @@ module Jobs
def poll_feed
topics_polled_from_feed.each do |feed_item|
- next if !feed_item.content.present?
- next if !feed_item.title.present?
-
- if (
- feed_category_filter.present? &&
- feed_item.categories.none? { |c| c.include?(feed_category_filter) }
- )
+ next if feed_item.content.blank?
+ next if feed_item.title.blank?
+ if feed_category_filter.present? &&
+ feed_item.categories.none? { |c| c.include?(feed_category_filter) }
next
end
@@ -54,9 +52,7 @@ module Jobs
updated_tags = discourse_tags
if !SiteSetting.rss_polling_update_tags
url = TopicEmbed.normalize_url(feed_item.url)
- embed = TopicEmbed.topic_embed_by_url(url)
- topic_exists = embed.present?
- updated_tags = nil if topic_exists
+ updated_tags = nil if TopicEmbed.topic_embed_by_url(url).present?
end
post =
@@ -90,36 +86,38 @@ module Jobs
end
def set_image_as_thumbnail(post, image_link)
- begin
- final_destination = FinalDestination.new(image_link)
- image_final_url = final_destination.resolve
- image_data = Excon.new(image_final_url.to_s).request(method: :get, expects: 200).body
- tmp = Tempfile.new("downloaded_image")
- tmp.binmode
- tmp.write(image_data)
- tmp.rewind
- source_filename = File.basename(URI.parse(image_final_url).path)
- upload = UploadCreator.new(tmp, source_filename).create_for(post.user.id)
- UploadReference.ensure_exist!(upload_ids: [upload.id], target: post)
- post.raw = "<img src=\"#{upload.url}\"><br/><br/>#{post.raw}"
- post.save!
- post.rebake!
- rescue => e
- Rails.logger.error(
- "RSS Polling: Unable to download and save #{image_link} for post ##{post.id} #{e.message}",
- )
- ensure
- begin
- tmp.close
- rescue StandardError
- nil
- end
- begin
- tmp.unlink
- rescue StandardError
- nil
+ tmp = nil
+ image_uri = nil
+
+ fd = FinalDestination.new(image_link)
+ fd.get do |response, chunk, uri|
+ throw :done if uri.blank? || !response.is_a?(Net::HTTPSuccess)
+
+ if tmp.nil?
+ image_uri = uri
+ tmp = Tempfile.new("downloaded_image")
+ tmp.binmode
end
+
+ tmp.write(chunk)
end
+
+ return if tmp.nil?
+
+ tmp.rewind
+ source_filename = File.basename(image_uri.path)
+ upload = UploadCreator.new(tmp, source_filename).create_for(post.user.id)
+ UploadReference.ensure_exist!(upload_ids: [upload.id], target: post)
+ post.raw = "<img src=\"#{upload.url}\"><br/><br/>#{post.raw}"
+ post.save!
+ post.rebake!
+ rescue => e
+ Rails.logger.error(
+ "RSS Polling: Unable to download and save #{image_link} for post ##{post.id} #{e.message}",
+ )
+ ensure
+ tmp&.close
+ tmp&.unlink
end
def topics_polled_from_feed
@@ -135,13 +133,16 @@ module Jobs
end
def fetch_raw_feed
- final_destination = FinalDestination.new(@feed_url)
- feed_final_url = final_destination.resolve
- return nil unless final_destination.status == :resolved
+ body = +""
+
+ FinalDestination
+ .new(@feed_url)
+ .get do |response, chunk, uri|
+ throw :done if uri.blank? || !response.is_a?(Net::HTTPSuccess)
+ body << chunk
+ end
- Excon.new(feed_final_url.to_s).request(method: :get, expects: 200).body
- rescue Excon::Error::HTTPStatus
- nil
+ body.presence
end
end
end
diff --git a/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb b/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
index 1b02efe6a69..0693e527c24 100644
--- a/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
+++ b/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
@@ -12,7 +12,6 @@ RSpec.describe Jobs::DiscourseRssPolling::PollFeed do
describe "#execute" do
before do
Discourse.redis.del("rss-polling-feed-polled:#{Digest::SHA1.hexdigest(feed_url)}")
- stub_request(:head, feed_url).to_return(status: 200, body: "")
stub_request(:get, feed_url).to_return(status: 200, body: raw_feed)
end
@@ -108,7 +107,7 @@ RSpec.describe Jobs::DiscourseRssPolling::PollFeed do
end
it "is not raising error if http request failed" do
- stub_request(:get, feed_url).to_raise(Excon::Error::HTTPStatus)
+ stub_request(:get, feed_url).to_return(status: 500)
job.execute(feed_url: feed_url, author_username: author.username)
end
@@ -144,5 +143,20 @@ RSpec.describe Jobs::DiscourseRssPolling::PollFeed do
job.execute(feed_url: feed_url, author_username: author.username)
}.not_to raise_error
end
+
+ it "works with authentication in query parameters" do
+ authenticated_url = "#{feed_url}?api_key=test123&api_username=testuser"
+
+ Discourse.redis.del("rss-polling-feed-polled:#{Digest::SHA1.hexdigest(authenticated_url)}")
+
+ stub_request(:get, authenticated_url).to_return(
+ status: 200,
+ body: file_from_fixtures("feed.rss", "feed"),
+ )
+
+ expect {
+ job.execute(feed_url: authenticated_url, author_username: author.username)
+ }.to change { author.topics.count }.by(1)
+ end
end
end