Kris 972cc6e5d9
UX: restyling for 2025 (#28)
This generally updates the styles for the 2025 version. I also made more
features work in development environments (so it's easier to test) and
fixed an issue with invites that was causing the report to not show.


<img width="400" alt="image"
src="https://github.com/user-attachments/assets/69a62b79-12e3-4539-a82c-9bbf9673abe2"
/>

<img width="1554" height="896" alt="image"
src="https://github.com/user-attachments/assets/60fabac5-6f20-4e01-9679-fd228c6850c2"
/>

<img width="754" height="459" alt="image"
src="https://github.com/user-attachments/assets/6725c6e2-1bac-417e-b4ee-190841ab656b"
/>

<img width="1472" height="490" alt="image"
src="https://github.com/user-attachments/assets/87c95e6a-5d78-4fb6-9f2d-dad13bcc9788"
/>

<img width="1480" height="702" alt="image"
src="https://github.com/user-attachments/assets/67bec8f9-dce5-4077-a07d-fe9e13696958"
/>
2025-11-18 09:03:41 -05:00

94 lines
2.2 KiB
Ruby

# frozen_string_literal: true
module DiscourseRewind
# Service responsible to fetch a rewind for a username/year.
#
# @example
# ::DiscourseRewind::Rewind::Fetch.call(
# guardian: guardian
# )
#
class FetchReports
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]
CACHE_DURATION = Rails.env.development? ? 10.seconds : 5.minutes
# order matters
REPORTS = [
Action::TopWords,
Action::ReadingTime,
Action::Reactions,
Action::Fbff,
Action::MostViewedTags,
Action::MostViewedCategories,
Action::BestTopics,
Action::BestPosts,
Action::ActivityCalendar,
Action::TimeOfDayActivity,
Action::NewUserInteractions,
Action::ChatUsage,
Action::AiUsage,
Action::FavoriteGifs,
Action::Assignments,
Action::Invites,
]
model :year
model :user
model :date
model :reports
private
def fetch_user(guardian:)
User.find_by_username(guardian.user.username)
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 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_reports(date:, user:, guardian:, year:)
key = "rewind:#{guardian.user.username}:#{year}"
reports = Discourse.redis.get(key)
if !reports
reports = REPORTS.map { |report| report.call(date:, user:, guardian:) }.compact
Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
else
reports = MultiJson.load(reports, symbolize_keys: true)
end
reports
end
end
end