Skip to content

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

FIX: RSS polling breaks silently after a username rename (#39460)

Régis Hanol25bd06dadd9a
diff --git a/plugins/discourse-rss-polling/app/controllers/discourse_rss_polling/feed_settings_controller.rb b/plugins/discourse-rss-polling/app/controllers/discourse_rss_polling/feed_settings_controller.rb
index d556c44e85f..2191b71c4bf 100644
--- a/plugins/discourse-rss-polling/app/controllers/discourse_rss_polling/feed_settings_controller.rb
+++ b/plugins/discourse-rss-polling/app/controllers/discourse_rss_polling/feed_settings_controller.rb
@@ -5,34 +5,45 @@ module DiscourseRssPolling
requires_plugin "discourse-rss-polling"
def show
- render json: FeedSettingFinder.all
+ feeds = RssFeed.includes(:user)
+ render json:
+ ActiveModel::ArraySerializer.new(
+ feeds,
+ each_serializer: FeedSettingSerializer,
+ root: :feed_settings,
+ ).to_json
end
def update
- feed = params[:feed_setting]
-
- if feed
- rss_feed = RssFeed.find_by_id(feed["id"]) || RssFeed.new
-
- tags = feed["discourse_tags"]&.map { |t| t["name"] }&.join(",")
- rss_feed.assign_attributes(
- url: feed["feed_url"],
- author: feed["author_username"],
- category_id: feed["discourse_category_id"],
- tags:,
- category_filter: feed["feed_category_filter"],
- )
- if rss_feed.save
- render json: { success: true }
- else
+ FeedSetting::Update.call(params: params[:feed_setting]) do
+ on_success { render json: { success: true } }
+ on_failed_contract do |contract|
+ render json: {
+ success: false,
+ errors: contract.errors.full_messages,
+ },
+ status: :bad_request
+ end
+ on_model_not_found(:user) do |_, params:|
+ render json: {
+ success: false,
+ errors: [
+ I18n.t(
+ "rss_polling.errors.unknown_author_username",
+ username: params.author_username,
+ ),
+ ],
+ },
+ status: :unprocessable_entity
+ end
+ on_model_errors(:rss_feed) do |rss_feed|
render json: {
success: false,
errors: rss_feed.errors.full_messages,
},
status: :unprocessable_entity
end
- else
- render json: { success: false, error: "Invalid feed data" }, status: :bad_request
+ on_failure { render json: { success: false }, status: :unprocessable_entity }
end
end
@@ -47,11 +58,5 @@ module DiscourseRssPolling
render json: { success: false, error: "Feed not found" }, status: :not_found
end
end
-
- private
-
- def feed_setting_params
- params.require(:feed_settings)
- end
end
end
diff --git a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_all_feeds.rb b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_all_feeds.rb
index 646bd59f74b..418dd8de28c 100644
--- a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_all_feeds.rb
+++ b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_all_feeds.rb
@@ -5,20 +5,17 @@ module Jobs
class PollAllFeeds < ::Jobs::Scheduled
every 5.minutes
+ REDIS_KEY = "rss-polling-feeds-polled"
+
def execute(args)
return unless SiteSetting.rss_polling_enabled
+ return unless not_polled_recently?
- poll_all_feeds if not_polled_recently?
+ ::DiscourseRssPolling::RssFeed.includes(:user).find_each(&:poll)
end
private
- def poll_all_feeds
- ::DiscourseRssPolling::FeedSettingFinder.all.each(&:poll)
- end
-
- REDIS_KEY = "rss-polling-feeds-polled"
-
def not_polled_recently?
Discourse.redis.set(
REDIS_KEY,
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 9e84e4325a5..e1bce2af1fa 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,8 +8,7 @@ module Jobs
def execute(args)
return unless SiteSetting.rss_polling_enabled
- @author = User.find_by_username(args[:author_username])
- return if @author.nil?
+ @author = resolve_author(args)
@discourse_category_id = args[:discourse_category_id]
return if @discourse_category_id.present? && !Category.exists?(@discourse_category_id)
@@ -25,6 +24,26 @@ module Jobs
attr_reader :feed_url, :author, :discourse_category_id, :discourse_tags, :feed_category_filter
+ def resolve_author(args)
+ if args[:user_id]
+ user = User.find_by(id: args[:user_id])
+ return user if user
+
+ Rails.logger.warn(
+ "RSS Polling: user_id #{args[:user_id]} not found for feed #{args[:feed_url]}, falling back to system user",
+ )
+ elsif args[:author_username].present?
+ user = User.find_by_username(args[:author_username])
+ return user if user
+
+ Rails.logger.warn(
+ "RSS Polling: username '#{args[:author_username]}' not found for feed #{args[:feed_url]}, falling back to system user",
+ )
+ end
+
+ Discourse.system_user
+ end
+
def feed_key
"rss-polling-feed-polled:#{Digest::SHA1.hexdigest(feed_url)}"
end
diff --git a/plugins/discourse-rss-polling/app/models/discourse_rss_polling/feed_setting.rb b/plugins/discourse-rss-polling/app/models/discourse_rss_polling/feed_setting.rb
deleted file mode 100644
index cc462a54eb1..00000000000
--- a/plugins/discourse-rss-polling/app/models/discourse_rss_polling/feed_setting.rb
+++ /dev/null
@@ -1,53 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseRssPolling
- class FeedSetting
- include ActiveModel::Serialization
-
- attr_accessor(
- :id,
- :feed_url,
- :author_username,
- :discourse_category_id,
- :discourse_tags,
- :feed_category_filter,
- )
-
- def initialize(
- id: nil,
- feed_url:,
- author_username:,
- discourse_category_id:,
- discourse_tags:,
- feed_category_filter:
- )
- @id = id
- @feed_url = feed_url
- @author_username = author_username
- @discourse_category_id = discourse_category_id
- @discourse_tags = discourse_tags
- @feed_category_filter = feed_category_filter
- end
-
- def poll(inline: false)
- if inline
- Jobs::DiscourseRssPolling::PollFeed.new.execute(
- feed_url: feed_url,
- author_username: author_username,
- discourse_category_id: discourse_category_id,
- discourse_tags: discourse_tags,
- feed_category_filter: feed_category_filter,
- )
- else
- Jobs.enqueue(
- "DiscourseRssPolling::PollFeed",
- feed_url: feed_url,
- author_username: author_username,
- discourse_category_id: discourse_category_id,
- discourse_tags: discourse_tags,
- feed_category_filter: feed_category_filter,
- )
- end
- end
- end
-end
diff --git a/plugins/discourse-rss-polling/app/models/discourse_rss_polling/rss_feed.rb b/plugins/discourse-rss-polling/app/models/discourse_rss_polling/rss_feed.rb
index ff5b6e96f92..77b4b0e3782 100644
--- a/plugins/discourse-rss-polling/app/models/discourse_rss_polling/rss_feed.rb
+++ b/plugins/discourse-rss-polling/app/models/discourse_rss_polling/rss_feed.rb
@@ -2,7 +2,29 @@
module DiscourseRssPolling
class RssFeed < ActiveRecord::Base
+ self.ignored_columns += %i[author]
+
+ belongs_to :user, optional: true
+
validates :url, presence: true
+
+ delegate :username, to: :user, prefix: :author, allow_nil: true
+
+ def poll(inline: false)
+ args = {
+ feed_url: url,
+ user_id: user_id,
+ discourse_category_id: category_id,
+ discourse_tags: tags&.split(","),
+ feed_category_filter: category_filter,
+ }
+
+ if inline
+ Jobs::DiscourseRssPolling::PollFeed.new.execute(args)
+ else
+ Jobs.enqueue("DiscourseRssPolling::PollFeed", args)
+ end
+ end
end
end
@@ -11,11 +33,15 @@ end
# Table name: discourse_rss_polling_rss_feeds
#
# id :bigint not null, primary key
-# url :string not null
# category_filter :string
-# author :string
-# category_id :integer
# tags :string
+# url :string not null
# created_at :datetime not null
# updated_at :datetime not null
+# category_id :integer
+# user_id :bigint
+#
+# Indexes
+#
+# index_discourse_rss_polling_rss_feeds_on_user_id (user_id)
#
diff --git a/plugins/discourse-rss-polling/app/serializers/discourse_rss_polling/feed_setting_serializer.rb b/plugins/discourse-rss-polling/app/serializers/discourse_rss_polling/feed_setting_serializer.rb
new file mode 100644
index 00000000000..6923388ff2b
--- /dev/null
+++ b/plugins/discourse-rss-polling/app/serializers/discourse_rss_polling/feed_setting_serializer.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module DiscourseRssPolling
+ class FeedSettingSerializer < ApplicationSerializer
+ attributes :id,
+ :feed_url,
+ :user_id,
+ :author_username,
+ :discourse_category_id,
+ :discourse_tags,
+ :feed_category_filter
+
+ def feed_url
+ object.url
+ end
+
+ def discourse_category_id
+ object.category_id
+ end
+
+ def discourse_tags
+ object.tags&.split(",")
+ end
+
+ def feed_category_filter
+ object.category_filter
+ end
+ end
+end
diff --git a/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting/update.rb b/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting/update.rb
new file mode 100644
index 00000000000..63c0dc5060e
--- /dev/null
+++ b/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting/update.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module DiscourseRssPolling
+ module FeedSetting
+ class Update
+ include Service::Base
+
+ params do
+ attribute :id, :integer
+ attribute :feed_url, :string
+ attribute :author_username, :string
+ attribute :discourse_category_id, :integer
+ attribute :discourse_tags, :array
+ attribute :feed_category_filter, :string
+
+ validates :feed_url, presence: true
+ validates :author_username, presence: true
+
+ before_validation :normalize_tags
+
+ def normalize_tags
+ return if discourse_tags.blank?
+
+ self.discourse_tags =
+ discourse_tags.map { |tag| tag.is_a?(String) ? tag : tag["name"] || tag[:name] }.compact
+ end
+
+ def tag_names
+ discourse_tags&.join(",").presence
+ end
+ end
+
+ model :user
+ model :rss_feed, :upsert_rss_feed
+
+ private
+
+ def fetch_user(params:)
+ ::User.find_by_username(params.author_username)
+ end
+
+ def upsert_rss_feed(params:, user:)
+ feed = RssFeed.find_or_initialize_by(id: params.id)
+ feed.assign_attributes(
+ url: params.feed_url,
+ user: user,
+ category_id: params.discourse_category_id,
+ tags: params.tag_names,
+ category_filter: params.feed_category_filter,
+ )
+ feed.save!
+ feed
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting_finder.rb b/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting_finder.rb
deleted file mode 100644
index 682a3c7b3d7..00000000000
--- a/plugins/discourse-rss-polling/app/services/discourse_rss_polling/feed_setting_finder.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseRssPolling
- class FeedSettingFinder
- def self.by_embed_url(embed_url)
- host = URI.parse(embed_url).host.sub(/^www\./, "")
- feed = RssFeed.where("url LIKE ?", "%#{host}%").first
- return nil if !feed
- FeedSetting.new(
- id: feed.id,
- feed_url: feed.url,
- author_username: feed.author,
- discourse_category_id: feed.category_id,
- discourse_tags: feed.tags.nil? ? nil : feed.tags.split(","),
- feed_category_filter: feed.category_filter,
- )
- end
-
- def self.all
- new.all
- end
-
- def initialize
- @condition = Proc.new { |*| true }
- end
-
- def where(&block)
- @condition = block
- self
- end
-
- def all
- RssFeed.all.map do |feed|
- FeedSetting.new(
- id: feed.id,
- feed_url: feed.url,
- author_username: feed.author,
- discourse_category_id: feed.category_id,
- discourse_tags: feed.tags.nil? ? nil : feed.tags.split(","),
- feed_category_filter: feed.category_filter,
- )
- end
- end
-
- def take
- all&.first
- end
- end
-end
diff --git a/plugins/discourse-rss-polling/config/locales/server.en.yml b/plugins/discourse-rss-polling/config/locales/server.en.yml
index ae33efcd5ad..2ce2728fb05 100644
--- a/plugins/discourse-rss-polling/config/locales/server.en.yml
+++ b/plugins/discourse-rss-polling/config/locales/server.en.yml
@@ -4,3 +4,6 @@ en:
rss_polling_frequency: "[RSS Polling] Feed polling frequency, in minutes"
rss_polling_use_pubdate: "[RSS Polling] Set topic creation date to the pubDate found in the RSS feed"
rss_polling_update_tags: "[RSS Polling] Update topic tags when a topic is updated. Disabling this pulls initial tags but disables further tag changes."
+ rss_polling:
+ errors:
+ unknown_author_username: "No user found with username '%{username}'."
diff --git a/plugins/discourse-rss-polling/db/migrate/20260422144944_add_user_id_to_discourse_rss_polling_rss_feeds.rb b/plugins/discourse-rss-polling/db/migrate/20260422144944_add_user_id_to_discourse_rss_polling_rss_feeds.rb
new file mode 100644
index 00000000000..0d439f2ae95
--- /dev/null
+++ b/plugins/discourse-rss-polling/db/migrate/20260422144944_add_user_id_to_discourse_rss_polling_rss_feeds.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+class AddUserIdToDiscourseRssPollingRssFeeds < ActiveRecord::Migration[8.0]
+ SYSTEM_USER_ID = -1
+
+ def up
+ add_column :discourse_rss_polling_rss_feeds, :user_id, :bigint
+ add_index :discourse_rss_polling_rss_feeds, :user_id
+
+ execute <<~SQL
+ UPDATE discourse_rss_polling_rss_feeds feeds
+ SET user_id = users.id
+ FROM users
+ WHERE feeds.user_id IS NULL
+ AND LOWER(users.username) = LOWER(feeds.author)
+ SQL
+
+ # Unresolvable rows fall back to the system user so polling keeps
+ # working; admins can reassign from the UI afterwards.
+ execute <<~SQL
+ UPDATE discourse_rss_polling_rss_feeds
+ SET user_id = #{SYSTEM_USER_ID}
+ WHERE user_id IS NULL
+ SQL
+
+ change_column_default :discourse_rss_polling_rss_feeds, :author, nil
+ Migration::ColumnDropper.mark_readonly(:discourse_rss_polling_rss_feeds, :author)
+ end
+
+ def down
+ Migration::ColumnDropper.drop_readonly(:discourse_rss_polling_rss_feeds, :author)
+ remove_index :discourse_rss_polling_rss_feeds, :user_id
+ remove_column :discourse_rss_polling_rss_feeds, :user_id
+ end
+end
diff --git a/plugins/discourse-rss-polling/spec/fabricators/rss_feed_fabricator.rb b/plugins/discourse-rss-polling/spec/fabricators/rss_feed_fabricator.rb
new file mode 100644
index 00000000000..0cd02bb6236
--- /dev/null
+++ b/plugins/discourse-rss-polling/spec/fabricators/rss_feed_fabricator.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+Fabricator(:rss_feed, from: "DiscourseRssPolling::RssFeed") do
+ url { sequence(:url) { |i| "https://blog.example.com/feed-#{i}" } }
+ user
+end
diff --git a/plugins/discourse-rss-polling/spec/jobs/poll_all_feeds_spec.rb b/plugins/discourse-rss-polling/spec/jobs/poll_all_feeds_spec.rb
index 0f4248a07cf..119990f400a 100644
--- a/plugins/discourse-rss-polling/spec/jobs/poll_all_feeds_spec.rb
+++ b/plugins/discourse-rss-polling/spec/jobs/poll_all_feeds_spec.rb
@@ -3,39 +3,37 @@
RSpec.describe Jobs::DiscourseRssPolling::PollAllFeeds do
subject(:job) { described_class.new }
+ fab!(:user_a) { Fabricate(:user, username: "feed_user_a") }
+ fab!(:user_b) { Fabricate(:user, username: "feed_user_b") }
+
before { SiteSetting.rss_polling_enabled = true }
describe "#execute" do
before do
- DiscourseRssPolling::RssFeed.create!(url: "https://www.example.com/feed", author: "system")
- DiscourseRssPolling::RssFeed.create!(
- url: "https://blog.discourse.org/feed/",
- author: "discourse",
- )
+ Fabricate(:rss_feed, url: "https://www.example.com/feed", user: user_a)
+ Fabricate(:rss_feed, url: "https://blog.discourse.org/feed/", user: user_b)
Jobs.run_later!
Discourse.redis.del("rss-polling-feeds-polled")
end
- it "queues correct PollFeed jobs" do
+ it "queues a PollFeed job per feed with the right user_id" do
Sidekiq::Testing.fake! do
expect { job.execute({}) }.to change { Jobs::DiscourseRssPolling::PollFeed.jobs.size }.by(2)
- enqueued_jobs_args =
- Jobs::DiscourseRssPolling::PollFeed.jobs.last(2).map { |job| job["args"][0] }
-
- expect(enqueued_jobs_args[0]["feed_url"]).to eq("https://www.example.com/feed")
- expect(enqueued_jobs_args[0]["author_username"]).to eq("system")
+ enqueued = Jobs::DiscourseRssPolling::PollFeed.jobs.last(2).map { |j| j["args"][0] }
- expect(enqueued_jobs_args[1]["feed_url"]).to eq("https://blog.discourse.org/feed/")
- expect(enqueued_jobs_args[1]["author_username"]).to eq("discourse")
+ expect(enqueued).to contain_exactly(
+ hash_including("feed_url" => "https://www.example.com/feed", "user_id" => user_a.id),
+ hash_including("feed_url" => "https://blog.discourse.org/feed/", "user_id" => user_b.id),
+ )
end
end
it "is rate limited" do
Sidekiq::Testing.fake! do
expect { job.execute({}) }.to change { Jobs::DiscourseRssPolling::PollFeed.jobs.size }.by(2)
- expect { job.execute({}) }.to_not change { Jobs::DiscourseRssPolling::PollFeed.jobs.size }
+ expect { job.execute({}) }.not_to change { Jobs::DiscourseRssPolling::PollFeed.jobs.size }
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 89fb79b3a7b..d604a5eb605 100644
--- a/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
+++ b/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
@@ -160,5 +160,42 @@ RSpec.describe Jobs::DiscourseRssPolling::PollFeed do
job.execute(feed_url: authenticated_url, author_username: author.username)
}.to change { author.topics.count }.by(1)
end
+
+ context "with user_id" do
+ it "creates a topic when given user_id" do
+ expect { job.execute(feed_url: feed_url, user_id: author.id) }.to change {
+ author.topics.count
+ }.by(1)
+ end
+
+ it "keeps working after the user is renamed" do
+ UsernameChanger.change(author, "renamed_account")
+
+ expect { job.execute(feed_url: feed_url, user_id: author.id) }.to change {
+ author.reload.topics.count
+ }.by(1)
+ end
+
+ it "falls back to the system user and logs when the referenced user no longer exists" do
+ deleted_id = author.id
+ author.destroy!
+
+ Rails.logger.expects(:warn).with(includes("not found")).at_least_once
+
+ expect { job.execute(feed_url: feed_url, user_id: deleted_id) }.to change {
+ Discourse.system_user.topics.count
+ }.by(1)
+ end
+ end
+
+ context "with an unknown author_username (legacy fallback)" do
+ it "falls back to the system user and logs a warning" do
+ Rails.logger.expects(:warn).with(includes("not found")).at_least_once
+
+ expect { job.execute(feed_url: feed_url, author_username: "ghost_user") }.to change {
+ Discourse.system_user.topics.count
+ }.by(1)
+ end
+ end
end
end
diff --git a/plugins/discourse-rss-polling/spec/models/feed_setting_spec.rb b/plugins/discourse-rss-polling/spec/models/feed_setting_spec.rb
deleted file mode 100644
index b3e82c23fa1..00000000000
--- a/plugins/discourse-rss-polling/spec/models/feed_setting_spec.rb
+++ /dev/null
@@ -1,105 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe DiscourseRssPolling::FeedSetting do
- SiteSetting.rss_polling_enabled = true
- let(:feed_url) { "https://blog.discourse.org/feed/" }
- let(:author) { Fabricate(:user, refresh_auto_groups: true) }
- let(:category) { Fabricate(:category) }
- let(:tag) { Fabricate(:tag) }
- let(:feed_category_filter) { "spec" }
- let(:feed_setting) do
- DiscourseRssPolling::FeedSetting.new(
- feed_url: feed_url,
- author_username: author.username,
- discourse_category_id: category.id,
- discourse_tags: [tag.name],
- feed_category_filter: feed_category_filter,
- )
- end
- let(:wrong_feed_setting) do
- DiscourseRssPolling::FeedSetting.new(
- feed_url: feed_url,
- author_username: author.username,
- discourse_category_id: category.id,
- discourse_tags: [tag.name],
- feed_category_filter: "non existing category",
- )
- end
- let(:missing_username_feed_setting) do
- DiscourseRssPolling::FeedSetting.new(
- feed_url: feed_url,
- author_username: nil,
- discourse_category_id: category.id,
- discourse_tags: [tag.name],
- feed_category_filter: feed_category_filter,
- )
- end
- let(:poll_feed_job) { Jobs::DiscourseRssPolling::PollFeed }
-
- describe "#poll" do
- context "with inline: false" do
- before { Jobs.run_later! }
-
- it "enqueues a Jobs::DiscourseRssPolling::PollFeed job with the correct arguments" do
- Sidekiq::Testing.fake! do
- expect { feed_setting.poll }.to change(poll_feed_job.jobs, :size).by(1)
-
- enqueued_job = poll_feed_job.jobs.last
-
- expect(enqueued_job["args"][0]["feed_url"]).to eq(feed_url)
- expect(enqueued_job["args"][0]["author_username"]).to eq(author.username)
- expect(enqueued_job["args"][0]["discourse_category_id"]).to eq(category.id)
- expect(enqueued_job["args"][0]["discourse_tags"]).to eq([tag.name])
- expect(enqueued_job["args"][0]["feed_category_filter"]).to eq(feed_category_filter)
- end
- end
- end
-
- context "with inline: true" do
- before { SiteSetting.tagging_enabled = true }
-
- it "polls and the feed and creates the new topics" 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: file_from_fixtures("feed.rss", "feed"),
- )
-
- expect { feed_setting.poll(inline: true) }.to change { author.topics.count }
-
- topic = author.topics.last
-
- expect(topic.title).to eq("Poll Feed Spec Fixture")
- expect(topic.first_post.raw).to include("<p>This is the body &amp; content. </p>")
- expect(topic.topic_embed.embed_url).to eq(
- "https://blog.discourse.org/2017/09/poll-feed-spec-fixture",
- )
- expect(topic.category).to eq(category)
- expect(topic.tags.first.name).to eq(tag.name)
- end
-
- it "polls and the feed and does not create the new topics because of the category filter" 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: file_from_fixtures("feed.rss", "feed"),
- )
-
- expect { wrong_feed_setting.poll(inline: true) }.not_to change { author.topics.count }
- end
-
- it "does not create the new topics because of the missing username" 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: file_from_fixtures("feed.rss", "feed"),
- )
-
- expect { missing_username_feed_setting.poll(inline: true) }.not_to change { Topic.count }
- end
- end
- end
-end
diff --git a/plugins/discourse-rss-polling/spec/models/rss_feed_spec.rb b/plugins/discourse-rss-polling/spec/models/rss_feed_spec.rb
new file mode 100644
index 00000000000..e6926629572
--- /dev/null
+++ b/plugins/discourse-rss-polling/spec/models/rss_feed_spec.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseRssPolling::RssFeed do
+ fab!(:user)
+
+ describe "#user" do
+ it "belongs_to a user" do
+ feed = Fabricate(:rss_feed, user: user)
+ expect(feed.user).to eq(user)
+ end
+ end
+
+ describe "#author_username" do
+ it "returns the associated user's current username" do
+ feed = Fabricate(:rss_feed, user: user)
+ expect(feed.author_username).to eq(user.username)
+ end
+
+ it "reflects username changes without requiring a feed update" do
+ feed = Fabricate(:rss_feed, user: user)
+ UsernameChanger.change(user, "shiny_new_name")
+ expect(feed.reload.author_username).to eq("shiny_new_name")
+ end
+
+ it "returns nil when no user is associated" do
+ feed = Fabricate(:rss_feed, user: nil)
+ expect(feed.author_username).to be_nil
+ end
+ end
+
+ describe "legacy author column" do
+ it "is hidden by ignored_columns so the model cannot drift again" do
+ expect { described_class.new(author: "anything") }.to raise_error(
+ ActiveModel::UnknownAttributeError,
+ )
+ end
+ end
+
+ describe "#poll" do
+ fab!(:feed) do
+ Fabricate(
+ :rss_feed,
+ url: "https://example.com/feed",
+ user: user,
+ category_id: 1,
+ tags: "foo,bar",
+ category_filter: "updates",
+ )
+ end
+
+ it "enqueues a PollFeed job with the feed's attributes" do
+ Sidekiq::Testing.fake! do
+ expect { feed.poll }.to change { Jobs::DiscourseRssPolling::PollFeed.jobs.size }.by(1)
+
+ args = Jobs::DiscourseRssPolling::PollFeed.jobs.last["args"][0]
+ expect(args).to include(
+ "feed_url" => "https://example.com/feed",
+ "user_id" => user.id,
+ "discourse_category_id" => 1,
+ "discourse_tags" => %w[foo bar],
+ "feed_category_filter" => "updates",
+ )
+ end
+ end
+
+ it "executes the PollFeed job inline when inline: true is passed" do
+ Jobs::DiscourseRssPolling::PollFeed
+ .any_instance
+ .expects(:execute)
+ .with(has_entries(feed_url: "https://example.com/feed", user_id: user.id))
+
+ feed.poll(inline: true)
+ end
+ end
+end
diff --git a/plugins/discourse-rss-polling/spec/requests/feed_settings_controller_spec.rb b/plugins/discourse-rss-polling/spec/requests/feed_settings_controller_spec.rb
index 43707cbed77..86404df729e 100644
--- a/plugins/discourse-rss-polling/spec/requests/feed_settings_controller_spec.rb
+++ b/plugins/discourse-rss-polling/spec/requests/feed_settings_controller_spec.rb
@@ -1,19 +1,19 @@
# frozen_string_literal: true
-describe DiscourseRssPolling::FeedSettingsController do
- let!(:admin) { Fabricate(:admin) }
+RSpec.describe DiscourseRssPolling::FeedSettingsController do
+ fab!(:admin)
before do
sign_in(admin)
-
SiteSetting.rss_polling_enabled = true
end
describe "#show" do
before do
- DiscourseRssPolling::RssFeed.create!(
+ Fabricate(
+ :rss_feed,
url: "https://blog.discourse.org/feed",
- author: "system",
+ user: Discourse.system_user,
category_id: 4,
tags: nil,
category_filter: "updates",
@@ -21,21 +21,23 @@ describe DiscourseRssPolling::FeedSettingsController do
end
it "returns the serialized feed settings" do
- expected_json =
- ActiveModel::ArraySerializer.new(
- DiscourseRssPolling::FeedSettingFinder.all,
- root: :feed_settings,
- ).to_json
-
get "/admin/plugins/rss_polling/feed_settings.json"
expect(response.status).to eq(200)
- expect(response.body).to eq(expected_json)
+ body = response.parsed_body
+ expect(body["feed_settings"].length).to eq(1)
+ expect(body["feed_settings"].first).to include(
+ "feed_url" => "https://blog.discourse.org/feed",
+ "user_id" => Discourse.system_user.id,
+ "author_username" => Discourse.system_user.username,
+ "discourse_category_id" => 4,
+ "feed_category_filter" => "updates",
+ )
end
end
describe "#update" do
- it "updates rss feeds" do
+ it "creates a feed setting" do
put "/admin/plugins/rss_polling/feed_settings.json",
params: {
feed_setting: {
@@ -46,8 +48,50 @@ describe DiscourseRssPolling::FeedSettingsController do
}
expect(response.status).to eq(200)
- feeds = DiscourseRssPolling::FeedSettingFinder.all
- expect(feeds.count).to eq(1)
+ expect(DiscourseRssPolling::RssFeed.count).to eq(1)
+ end
+
+ it "persists the resolved user_id so renames don't break polling" do
+ user = Fabricate(:user, username: "blogauthor")
+
+ put "/admin/plugins/rss_polling/feed_settings.json",
+ params: {
+ feed_setting: {
+ feed_url: "https://www.newsite.com/feed",
+ author_username: user.username,
+ feed_category_filter: "updates",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(DiscourseRssPolling::RssFeed.last.user_id).to eq(user.id)
+ end
+
+ it "returns 422 with a human-readable error when the author_username does not match a user" do
+ put "/admin/plugins/rss_polling/feed_settings.json",
+ params: {
+ feed_setting: {
+ feed_url: "https://www.newsite.com/feed",
+ author_username: "nope_not_real",
+ feed_category_filter: "updates",
+ },
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to contain_exactly(match(/nope_not_real/))
+ end
+
+ it "returns 400 when the contract is invalid" do
+ put "/admin/plugins/rss_polling/feed_settings.json",
+ params: {
+ feed_setting: {
+ feed_url: "",
+ author_username: "system",
+ },
+ }
+
+ expect(response.status).to eq(400)
+ expect(response.parsed_body["errors"]).to be_present
end
it "allows duplicate rss feed urls" do
@@ -60,7 +104,6 @@ describe DiscourseRssPolling::FeedSettingsController do
feed_category_filter: "updates",
},
}
-
expect(response.status).to eq(200)
put "/admin/plugins/rss_polling/feed_settings.json",
@@ -72,10 +115,9 @@ describe DiscourseRssPolling::FeedSettingsController do
feed_category_filter: "updates",
},
}
-
expect(response.status).to eq(200)
- feeds = DiscourseRssPolling::FeedSettingFinder.all
- expect(feeds.count).to eq(2)
+
+ expect(DiscourseRssPolling::RssFeed.count).to eq(2)
end
end
end
diff --git a/plugins/discourse-rss-polling/spec/services/feed_setting/update_spec.rb b/plugins/discourse-rss-polling/spec/services/feed_setting/update_spec.rb
new file mode 100644
index 00000000000..1c341105f6d
--- /dev/null
+++ b/plugins/discourse-rss-polling/spec/services/feed_setting/update_spec.rb
@@ -0,0 +1,99 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseRssPolling::FeedSetting::Update do
+ describe described_class::Contract, type: :model do
+ it { is_expected.to validate_presence_of(:feed_url) }
+ it { is_expected.to validate_presence_of(:author_username) }
+ end
+
+ describe ".call" do
+ subject(:result) { described_class.call(params:, **dependencies) }
+
+ fab!(:user)
+ fab!(:category)
+ fab!(:tag_1, :tag)
+ fab!(:tag_2, :tag)
+
+ let(:params) do
+ {
+ id: nil,
+ feed_url: "https://blog.example.com/feed",
+ author_username: user.username,
+ discourse_category_id: category.id,
+ discourse_tags: [tag_1.name, tag_2.name],
+ feed_category_filter: "news",
+ }
+ end
+ let(:dependencies) { {} }
+
+ context "when the contract is invalid" do
+ before { params[:feed_url] = nil }
+
+ it { is_expected.to fail_a_contract }
+ end
+
+ context "when the author does not exist" do
+ before { params[:author_username] = "ghost" }
+
+ it { is_expected.to fail_to_find_a_model(:user) }
+ end
+
+ context "when everything is ok" do
+ context "when creating a new feed" do
+ it { is_expected.to run_successfully }
+
+ it "creates a new rss_feed with the provided attributes" do
+ expect { result }.to change { DiscourseRssPolling::RssFeed.count }.by(1)
+
+ feed = result[:rss_feed]
+ expect(feed).to have_attributes(
+ url: "https://blog.example.com/feed",
+ user_id: user.id,
+ category_id: category.id,
+ tags: "#{tag_1.name},#{tag_2.name}",
+ category_filter: "news",
+ )
+ end
+ end
+
+ context "when updating an existing feed" do
+ fab!(:rss_feed) { Fabricate(:rss_feed, url: "https://old.example.com/feed", user: user) }
+
+ before { params[:id] = rss_feed.id }
+
+ it { is_expected.to run_successfully }
+
+ it "updates the existing rss_feed" do
+ expect { result }.not_to change { DiscourseRssPolling::RssFeed.count }
+ expect(rss_feed.reload).to have_attributes(
+ url: "https://blog.example.com/feed",
+ user_id: user.id,
+ category_id: category.id,
+ tags: "#{tag_1.name},#{tag_2.name}",
+ category_filter: "news",
+ )
+ end
+ end
+
+ context "when discourse_tags is an array of hashes" do
+ before { params[:discourse_tags] = [{ "name" => tag_1.name }, { "name" => tag_2.name }] }
+
+ it { is_expected.to run_successfully }
+
+ it "normalizes tags into a comma-separated string" do
+ expect(result[:rss_feed].tags).to eq("#{tag_1.name},#{tag_2.name}")
+ end
+ end
+
+ context "when discourse_tags is blank" do
+ before { params[:discourse_tags] = nil }
+
+ it { is_expected.to run_successfully }
+
+ it "stores nil tags" do
+ expect(result[:rss_feed].tags).to be_nil
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-rss-polling/spec/services/feed_setting_finder_spec.rb b/plugins/discourse-rss-polling/spec/services/feed_setting_finder_spec.rb
deleted file mode 100644
index 67ab0023905..00000000000
--- a/plugins/discourse-rss-polling/spec/services/feed_setting_finder_spec.rb
+++ /dev/null
@@ -1,26 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe DiscourseRssPolling::FeedSettingFinder do
- before do
- DiscourseRssPolling::RssFeed.create!(url: "https://blog.discourse.org/feed/", author: "system")
- DiscourseRssPolling::RssFeed.create!(url: "https://www.withwww.com/feed", author: "system")
- DiscourseRssPolling::RssFeed.create!(url: "https://withoutwww.com/feed", author: "system")
- end
-
- describe ".by_embed_url" do
- it "finds the feed setting with the same host" do
- setting =
- DiscourseRssPolling::FeedSettingFinder.by_embed_url("https://blog.discourse.org/2018/03/")
- expect(setting.feed_url).to eq("https://blog.discourse.org/feed/")
- end
-
- it "neglects www in the url" do
- setting = DiscourseRssPolling::FeedSettingFinder.by_embed_url("https://withwww.com/a-post/")
- expect(setting.feed_url).to eq("https://www.withwww.com/feed")
-
- setting =
- DiscourseRssPolling::FeedSettingFinder.by_embed_url("https://www.withoutwww.com/a-post/")
- expect(setting.feed_url).to eq("https://withoutwww.com/feed")
- end
- end
-end
diff --git a/plugins/discourse-rss-polling/spec/system/admin_spec.rb b/plugins/discourse-rss-polling/spec/system/admin_spec.rb
index b795b9df0cf..c2985ef8c58 100644
--- a/plugins/discourse-rss-polling/spec/system/admin_spec.rb
+++ b/plugins/discourse-rss-polling/spec/system/admin_spec.rb
@@ -31,7 +31,7 @@ RSpec.describe "Rss Polling - admin" do
expect(DiscourseRssPolling::RssFeed.last).to have_attributes(
url:,
- author: current_user.username,
+ user_id: current_user.id,
category_filter: "updates",
category_id: category_1.id,
tags: tag_1.name,