57 lines
1.4 KiB
Ruby
Raw Normal View History

2024-12-07 18:40:23 +01:00
# frozen_string_literal: true
module DiscourseRewind
# Service responsible to fetch a rewind for a username/year.
#
# @example
# ::DiscourseRewind::Rewind::Fetch.call(
# guardian: guardian,
# params: {
# username: "falco",
# year: 2024,
# }
# )
#
class Rewind::Fetch
include Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params [Integer] :year of the rewind
# @option params [Integer] :username of the rewind
# @return [Service::Base::Context]
2024-12-07 18:42:49 +01:00
# order matters, rewinds are displayed in the order they are defined
2024-12-07 18:40:23 +01:00
REPORTS = [DiscourseRewind::Rewind::Action::CreatePostsCountReport]
CACHE_DURATION = 5.minutes
params do
attribute :year, :integer
attribute :username, :string
validates :year, presence: true
validates :username, presence: true
end
model :reports
private
def fetch_reports(params:, guardian:)
key = "rewind:#{params.username}:#{params.year}"
reports = Discourse.redis.get(key)
if Rails.env.development? || !reports
reports = REPORTS.map { |report| report.call(params:, guardian:) }
Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
else
2024-12-11 22:38:44 +01:00
reports = MultiJson.load(reports, symbolize_keys: true)
2024-12-07 18:40:23 +01:00
end
reports
end
end
end