Skip to content

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

FIX: Add version number to Rewind cache key (#36745)

Martin Brennane080ff92f165
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
index 8429e142844..b2865e7bb1a 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
@@ -29,6 +29,7 @@ module DiscourseRewind
def call
return FakeData if should_use_fake_data?
+
best_topics =
TopTopic
.includes(:topic)
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb
index 94a151fbfbf..725f5406951 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb
@@ -3,14 +3,19 @@
module DiscourseRewind
# Service responsible to fetch a single report by index.
#
+ # NOTE: When changing any report implementations, please
+ # also update FetchReportsHelper::REWIND_REPORT_VERSION
+ # to invalidate caches.
+ #
# @example
# ::DiscourseRewind::FetchReport.call(
# guardian: guardian,
- # params: { index: 3 }
+ # params: { index: 3, for_user_username: 'codinghorror' }
# )
#
class FetchReport
include Service::Base
+ include DiscourseRewind::FetchReportsHelper
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
@@ -26,53 +31,15 @@ module DiscourseRewind
validates :index, presence: true, numericality: { greater_than_or_equal_to: 0 }
end
- model :for_user
- model :year
+ model :for_user # see FetchReportsHelper#fetch_for_user
+ model :year # see FetchReportsHelper#fetch_year
model :all_reports
model :report
private
- def fetch_for_user(params:, guardian:)
- return guardian.user if params.for_user_username.blank?
-
- user = User.find_by(username: params.for_user_username)
- return if user.nil?
-
- if guardian.user.id != user.id
- if !user.discourse_rewind_and_profile_public?
- return if !guardian.user.admin?
- end
- end
-
- user
- end
-
- def fetch_year
- current_date = Time.zone.now
- current_month = current_date.month
- current_year = current_date.year
-
- case current_month
- when 1
- current_year - 1
- when 12
- current_year
- else
- if Rails.env.development?
- current_year
- else
- false
- end
- end
- end
-
def fetch_all_reports(for_user:, year:)
- key = "rewind:#{for_user.username}:#{year}"
- reports = Discourse.redis.get(key)
- return nil unless reports
-
- MultiJson.load(reports, symbolize_keys: true)
+ load_reports_from_cache(for_user.username, year)
end
def fetch_report(all_reports:, params:)
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 8a367ba8f80..fbaa647703f 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb
@@ -6,11 +6,12 @@ module DiscourseRewind
# @example
# ::DiscourseRewind::Rewind::Fetch.call(
# guardian: guardian,
- # params: { user: User, for_user_username: 'codinghorror' }
+ # params: { for_user_username: 'codinghorror' }
# )
#
class FetchReports
include Service::Base
+ include DiscourseRewind::FetchReportsHelper
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
@@ -18,11 +19,14 @@ module DiscourseRewind
# @option params [String] :for_user_username (optional) username of the user to see the rewind for, otherwise the guardian user is used
# @return [Service::Base::Context]
- CACHE_DURATION = Rails.env.development? ? 10.seconds : 3.days
INITIAL_REPORT_COUNT = 3
# The order here controls the order of reports in the UI,
# so be careful when moving these around.
+ #
+ # NOTE: When changing any report implementations, please
+ # also update FetchReportsHelper::REWIND_REPORT_VERSION
+ # to invalidate caches.
REPORTS = [
Action::TopWords,
Action::ReadingTime,
@@ -38,18 +42,14 @@ module DiscourseRewind
Action::NewUserInteractions,
Action::ChatUsage,
Action::AiUsage,
- #Action::FavoriteGifs,
Action::Assignments,
Action::Invites,
]
- params do
- attribute :user
- attribute :for_user_username, :string
- end
+ params { attribute :for_user_username, :string }
- model :for_user
- model :year
+ model :for_user # see FetchReportsHelper#fetch_for_user
+ model :year # see FetchReportsHelper#fetch_year
model :date
model :all_reports
model :reports
@@ -57,49 +57,12 @@ module DiscourseRewind
private
- def fetch_for_user(params:, guardian:)
- return guardian.user if params.for_user_username.blank?
-
- user = User.find_by(username: params.for_user_username)
- return if user.nil?
-
- if guardian.user.id != user.id
- if !user.discourse_rewind_and_profile_public?
- return if !guardian.user.admin?
- end
- end
-
- user
- end
-
- def fetch_year
- current_date = Time.zone.now
- current_month = current_date.month
- current_year = current_date.year
-
- case current_month
- when 1
- current_year - 1
- when 12
- current_year
- else
- # Otherwise it's impossible to test in browser locally unless you're
- # in December or January
- if Rails.env.development?
- current_year
- else
- false
- end
- end
- end
-
def fetch_date(params:, year:)
Date.new(year).all_year
end
def fetch_all_reports(date:, for_user:, year:)
- key = cache_key(for_user.username, year)
- reports = Discourse.redis.get(key)
+ reports = load_reports_from_cache(for_user.username, year)
if !reports
reports =
@@ -108,9 +71,7 @@ module DiscourseRewind
rescue StandardError
nil
end
- Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
- else
- reports = MultiJson.load(reports, symbolize_keys: true)
+ cache_reports(for_user.username, year, reports)
end
reports
@@ -123,9 +84,5 @@ module DiscourseRewind
def fetch_total_available(all_reports:)
all_reports.size
end
-
- def cache_key(username, year)
- "rewind:#{username}:#{year}"
- end
end
end
diff --git a/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb
new file mode 100644
index 00000000000..b78080db794
--- /dev/null
+++ b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+module DiscourseRewind
+ module FetchReportsHelper
+ extend self
+
+ REWIND_REPORT_VERSION = "1"
+ CACHE_DURATION = Rails.env.development? ? 10.seconds : 3.days
+
+ def cache_key(username, year)
+ "rewind:#{username}:#{year}:v#{REWIND_REPORT_VERSION}"
+ end
+
+ def load_reports_from_cache(username, year)
+ reports = Discourse.redis.get(cache_key(username, year))
+ return nil if !reports
+ MultiJson.load(reports, symbolize_keys: true)
+ end
+
+ def cache_reports(username, year, reports)
+ Discourse.redis.setex(cache_key(username, year), CACHE_DURATION, MultiJson.dump(reports))
+ end
+
+ def fetch_for_user(guardian:, params:)
+ return guardian.user if params.for_user_username.blank?
+
+ user = User.find_by(username: params.for_user_username)
+ return if user.nil?
+
+ if guardian.user.id != user.id
+ if !user.discourse_rewind_and_profile_public?
+ return if !guardian.user.admin?
+ end
+ end
+
+ user
+ end
+
+ def fetch_year
+ current_date = Time.zone.now
+ current_month = current_date.month
+ current_year = current_date.year
+
+ case current_month
+ when 1
+ current_year - 1
+ when 12
+ current_year
+ else
+ # Otherwise it's impossible to test in browser locally unless you're
+ # in December or January
+ if Rails.env.development?
+ current_year
+ else
+ nil
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb
index 0f32181c519..ce3cb1d7fde 100644
--- a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb
+++ b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb
@@ -10,160 +10,130 @@ RSpec.describe(DiscourseRewind::FetchReport) do
let(:params) { { index: 0 } }
let(:dependencies) { { guardian:, params: } }
- before do
- SiteSetting.discourse_rewind_enabled = true
- freeze_time DateTime.parse("2021-12-22")
- key = "rewind:#{current_user.username}:2021"
- reports = [
- { type: "top_words", data: "report 1" },
- { type: "reading_time", data: "report 2" },
- { type: "writing_analysis", data: "report 3" },
- ]
- Discourse.redis.setex(key, 3600, MultiJson.dump(reports))
- end
-
- context "when index is not provided" do
- let(:params) { {} }
-
- it { is_expected.to fail_a_contract }
- end
-
- context "when index is negative" do
- let(:params) { { index: -1 } }
-
- it { is_expected.to fail_a_contract }
- end
-
- context "when fetching a report by index" do
- context "when index is 0" do
- let(:params) { { index: 0 } }
-
- it "returns the first report" do
- expect(result).to be_success
- expect(result.report[:type]).to eq("top_words")
- end
- end
-
- context "when index is 1" do
- let(:params) { { index: 1 } }
-
- it "returns the second report" do
- expect(result).to be_success
- expect(result.report[:type]).to eq("reading_time")
- end
+ context "when reports are cached" do
+ before do
+ SiteSetting.discourse_rewind_enabled = true
+ freeze_time DateTime.parse("2021-12-22")
+ reports = [
+ { type: "top_words", data: "report 1" },
+ { type: "reading_time", data: "report 2" },
+ { type: "writing_analysis", data: "report 3" },
+ ]
+ DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2021, reports)
end
- context "when index is out of bounds" do
- let(:params) { { index: 10 } }
+ context "when index is not provided" do
+ let(:params) { {} }
- it { is_expected.to fail_to_find_a_model(:report) }
+ it { is_expected.to fail_a_contract }
end
- end
- context "when in january" do
- before do
- freeze_time DateTime.parse("2021-01-22")
- key = "rewind:#{current_user.username}:2020"
- reports = [{ type: "top_words", data: "report 1" }]
- Discourse.redis.setex(key, 3600, MultiJson.dump(reports))
- end
+ context "when index is negative" do
+ let(:params) { { index: -1 } }
- it "fetches reports from the previous year" do
- expect(result.year).to eq(2020)
- expect(result).to be_success
+ it { is_expected.to fail_a_contract }
end
- end
- context "when out of valid months" do
- before { freeze_time DateTime.parse("2021-02-22") }
-
- it { is_expected.to fail_to_find_a_model(:year) }
- end
+ context "when fetching a report by index" do
+ context "when index is 0" do
+ let(:params) { { index: 0 } }
- context "in development mode" do
- before do
- Rails.env.stubs(:development?).returns(true)
- freeze_time DateTime.parse("2021-06-22")
- key = "rewind:#{current_user.username}:2021"
- reports = [{ type: "top_words", data: "report 1" }]
- Discourse.redis.setex(key, 3600, MultiJson.dump(reports))
- end
-
- it "finds the year no matter what month" do
- expect(result.year).to eq(2021)
- expect(result).to be_success
- end
- end
+ it "returns the first report" do
+ expect(result).to be_success
+ expect(result.report[:type]).to eq("top_words")
+ end
+ end
- context "when reports are not cached" do
- before { Discourse.redis.del("rewind:#{current_user.username}:2021") }
+ context "when index is 1" do
+ let(:params) { { index: 1 } }
- it { is_expected.to fail_to_find_a_model(:all_reports) }
- end
+ it "returns the second report" do
+ expect(result).to be_success
+ expect(result.report[:type]).to eq("reading_time")
+ end
+ end
- context "with for_user_username parameter" do
- fab!(:other_user, :user)
- fab!(:admin, :admin)
+ context "when index is out of bounds" do
+ let(:params) { { index: 10 } }
- before do
- freeze_time DateTime.parse("2021-12-22")
- key = "rewind:#{other_user.username}:2021"
- reports = [
- { type: "top_words", data: "other user report 1" },
- { type: "reading_time", data: "other user report 2" },
- ]
- Discourse.redis.setex(key, 3600, MultiJson.dump(reports))
+ it { is_expected.to fail_to_find_a_model(:report) }
+ end
end
- context "when for_user_username is blank" do
- let(:params) { { index: 0, for_user_username: "" } }
+ context "when in january" do
+ before do
+ freeze_time DateTime.parse("2021-01-22")
+ reports = [{ type: "top_words", data: "report 1" }]
+ DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2020, reports)
+ end
- it "uses the guardian user" do
+ it "fetches reports from the previous year" do
+ expect(result.year).to eq(2020)
expect(result).to be_success
- expect(result.for_user).to eq(current_user)
end
end
- context "when for_user_username is provided but user does not exist" do
- let(:params) { { index: 0, for_user_username: "nonexistent" } }
+ context "when out of valid months" do
+ before { freeze_time DateTime.parse("2021-02-22") }
- it { is_expected.to fail_to_find_a_model(:for_user) }
+ it { is_expected.to fail_to_find_a_model(:year) }
end
- context "when viewing own rewind" do
- let(:params) { { index: 0, for_user_username: current_user.username } }
+ context "in development mode" do
+ before do
+ Rails.env.stubs(:development?).returns(true)
+ freeze_time DateTime.parse("2021-06-22")
+ reports = [{ type: "top_words", data: "report 1" }]
+ DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2021, reports)
+ end
- it "returns the user's report" do
+ it "finds the year no matter what month" do
+ expect(result.year).to eq(2021)
expect(result).to be_success
- expect(result.for_user).to eq(current_user)
end
end
- context "when viewing another user's rewind" do
- context "when the other user has sharing enabled" do
- before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }
+ context "with for_user_username parameter" do
+ fab!(:other_user, :user)
+ fab!(:admin, :admin)
+
+ before do
+ freeze_time DateTime.parse("2021-12-22")
+ reports = [
+ { type: "top_words", data: "other user report 1" },
+ { type: "reading_time", data: "other user report 2" },
+ ]
+ DiscourseRewind::FetchReportsHelper.cache_reports(other_user.username, 2021, reports)
+ end
- let(:params) { { index: 0, for_user_username: other_user.username } }
+ context "when for_user_username is blank" do
+ let(:params) { { index: 0, for_user_username: "" } }
- it "allows access to the report" do
+ it "uses the guardian user" do
expect(result).to be_success
- expect(result.for_user).to eq(other_user)
- expect(result.report[:type]).to eq("top_words")
- expect(result.report[:data]).to eq("other user report 1")
+ expect(result.for_user).to eq(current_user)
end
+ end
- context "when the other user has hide_profile enabled" do
- before { other_user.user_option.update!(hide_profile: true) }
+ context "when for_user_username is provided but user does not exist" do
+ let(:params) { { index: 0, for_user_username: "nonexistent" } }
- it { is_expected.to fail_to_find_a_model(:for_user) }
+ it { is_expected.to fail_to_find_a_model(:for_user) }
+ end
+
+ context "when viewing own rewind" do
+ let(:params) { { index: 0, for_user_username: current_user.username } }
+
+ it "returns the user's report" do
+ expect(result).to be_success
+ expect(result.for_user).to eq(current_user)
end
end
- context "when the other user has sharing disabled" do
- before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }
+ context "when viewing another user's rewind" do
+ context "when the other user has sharing enabled" do
+ before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }
- context "when guardian is an admin" do
- let(:guardian) { Guardian.new(admin) }
let(:params) { { index: 0, for_user_username: other_user.username } }
it "allows access to the report" do
@@ -172,15 +142,41 @@ RSpec.describe(DiscourseRewind::FetchReport) do
expect(result.report[:type]).to eq("top_words")
expect(result.report[:data]).to eq("other user report 1")
end
+
+ context "when the other user has hide_profile enabled" do
+ before { other_user.user_option.update!(hide_profile: true) }
+
+ it { is_expected.to fail_to_find_a_model(:for_user) }
+ end
end
- context "when guardian is not an admin" do
- let(:params) { { index: 0, for_user_username: other_user.username } }
+ context "when the other user has sharing disabled" do
+ before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }
+
+ context "when guardian is an admin" do
+ let(:guardian) { Guardian.new(admin) }
+ let(:params) { { index: 0, for_user_username: other_user.username } }
- it { is_expected.to fail_to_find_a_model(:for_user) }
+ it "allows access to the report" do
+ expect(result).to be_success
+ expect(result.for_user).to eq(other_user)
+ expect(result.report[:type]).to eq("top_words")
+ expect(result.report[:data]).to eq("other user report 1")
+ end
+ end
+
+ context "when guardian is not an admin" do
+ let(:params) { { index: 0, for_user_username: other_user.username } }
+
+ it { is_expected.to fail_to_find_a_model(:for_user) }
+ end
end
end
end
end
+
+ context "when reports are not cached" do
+ it { is_expected.to fail_to_find_a_model(:all_reports) }
+ end
end
end
diff --git a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
index 1b569ded348..ef14d67c80c 100644
--- a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
+++ b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb
@@ -44,7 +44,7 @@ RSpec.describe(DiscourseRewind::FetchReports) do
end
end
- context "when reports is cached" do
+ context "when reports are cached" do
before { freeze_time DateTime.parse("2021-12-22") }
it "returns the cached reports" do
@@ -57,11 +57,8 @@ RSpec.describe(DiscourseRewind::FetchReports) do
end
end
- context "when reports is not cached" do
- before do
- freeze_time DateTime.parse("2021-01-22")
- Discourse.redis.del("rewind:#{current_user.username}:2020")
- end
+ context "when reports are not cached" do
+ before { freeze_time DateTime.parse("2021-01-22") }
it "returns the reports" do
allow(DiscourseRewind::Action::TopWords).to receive(:call)