Skip to content

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

DEV: Skip Rewind reports whose plugin is disabled

Régis Hanol082c2d18a17a
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/ai_usage.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/ai_usage.rb
index 9926d4a1b90..4f11f0fafa3 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/ai_usage.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/ai_usage.rb
@@ -31,7 +31,6 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
- return if !enabled?
base_query = AiApiRequestStat.where(user_id: user.id).where(bucket_date: date)
@@ -89,8 +88,8 @@ module DiscourseRewind
}
end
- def enabled?
- Discourse.plugins_by_name["discourse-ai"]&.enabled?
+ def self.enabled?
+ plugin_enabled?("discourse-ai")
end
def self.filter_for_viewer(report, guardian:, for_user:)
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/assignments.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/assignments.rb
index 58d43880a6e..e9be4dfcf80 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/assignments.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/assignments.rb
@@ -18,7 +18,6 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
- return if !enabled?
# Assignments made to the user
assignments_scope =
@@ -63,8 +62,8 @@ module DiscourseRewind
}
end
- def enabled?
- Discourse.plugins_by_name["discourse-assign"]&.enabled?
+ def self.enabled?
+ plugin_enabled?("discourse-assign")
end
end
end
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
index ddf7ce97602..39d2a3d4a29 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/chat_usage.rb
@@ -26,7 +26,6 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
- return if !enabled?
messages =
Chat::Message.where(user_id: user.id).where(created_at: date).where(deleted_at: nil)
@@ -111,8 +110,8 @@ module DiscourseRewind
)
end
- def enabled?
- Discourse.plugins_by_name["chat"]&.enabled?
+ def self.enabled?
+ plugin_enabled?("chat")
end
end
end
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
index 1cd2ed3f7fd..c8c2c3de0e1 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
@@ -24,7 +24,6 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
- return if !self.class.enabled?
used = count_by_emoji(DiscourseReactions::Reaction.by_user(user))
received =
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb
index 8ee17efd248..d233f332dd1 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb
@@ -48,6 +48,10 @@ module DiscourseRewind
Action::Invites,
]
+ def self.enabled_reports
+ REPORTS.select(&:enabled?)
+ end
+
params do
attribute :for_user_username, :string
attribute :offset, :integer, default: 0
@@ -55,7 +59,7 @@ module DiscourseRewind
validates :offset,
numericality: {
greater_than_or_equal_to: 0,
- less_than: ->(_) { REPORTS.size },
+ less_than: ->(_) { FetchReports.enabled_reports.size },
}
end
@@ -82,13 +86,13 @@ module DiscourseRewind
end
def fetch_reports(params:, for_user:, year:, guardian:)
- REPORTS[params.offset, PAGE_SIZE].map do |report_class|
+ self.class.enabled_reports[params.offset, PAGE_SIZE].map do |report_class|
fetch_report(report_class, for_user:, year:, guardian:)
end
end
def fetch_total_available
- REPORTS.size
+ self.class.enabled_reports.size
end
def fetch_report(report_class, for_user:, year:, guardian:)
diff --git a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
index 4567ef15b17..dc3b3d35fcc 100644
--- a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
+++ b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
@@ -24,7 +24,7 @@ RSpec.describe(DiscourseRewind::FetchReports) do
include(identifier: "reading-time"),
include(identifier: "writing-analysis"),
],
- total_available: described_class::REPORTS.size,
+ total_available: described_class.enabled_reports.size,
)
end
@@ -87,11 +87,12 @@ RSpec.describe(DiscourseRewind::FetchReports) do
end
end
- context "with every report returning data" do
+ context "with every report enabled" do
let(:params) { { offset: 3 } }
before do
described_class::REPORTS.each do |report_class|
+ report_class.stubs(:enabled?).returns(true)
report = { data: [1], identifier: report_class.name.demodulize }
report_class.stubs(:call).returns(report)
report_class.stubs(:filter_for_viewer).returns(report)
@@ -119,6 +120,15 @@ RSpec.describe(DiscourseRewind::FetchReports) do
[*reports_named("Reactions"), nil, *reports_named("MostViewedTags")],
)
end
+
+ it "skips the reports of disabled plugins" do
+ DiscourseRewind::Action::Reactions.stubs(:enabled?).returns(false)
+
+ expect(result).to have_attributes(
+ reports: reports_named("Fbff", "MostViewedTags", "MostViewedCategories"),
+ total_available: described_class::REPORTS.size - 1,
+ )
+ end
end
context "when the offset is negative" do
@@ -128,7 +138,7 @@ RSpec.describe(DiscourseRewind::FetchReports) do
end
context "when the offset is past the last report" do
- let(:params) { { offset: described_class::REPORTS.size } }
+ let(:params) { { offset: described_class.enabled_reports.size } }
it { is_expected.to fail_a_contract }
end
@@ -138,7 +148,7 @@ RSpec.describe(DiscourseRewind::FetchReports) do
fab!(:shared_draft_topic) { Fabricate(:shared_draft).topic }
let(:params) do
- { offset: described_class::REPORTS.index(DiscourseRewind::Action::BestTopics) }
+ { offset: described_class.enabled_reports.index(DiscourseRewind::Action::BestTopics) }
end
it "filters the report for the viewer" do