diff --git a/app/controllers/discourse_rewind/rewinds_controller.rb b/app/controllers/discourse_rewind/rewinds_controller.rb index f1a9cf7..00a15dd 100644 --- a/app/controllers/discourse_rewind/rewinds_controller.rb +++ b/app/controllers/discourse_rewind/rewinds_controller.rb @@ -6,6 +6,8 @@ module ::DiscourseRewind def show DiscourseRewind::Rewind::Fetch.call(service_params) do + on_model_not_found(:year) { raise Discourse::NotFound } + on_model_not_found(:user) { raise Discourse::NotFound } on_success do |reports:| @reports = reports render json: MultiJson.dump(reports), status: 200 diff --git a/app/services/discourse_rewind/rewind/action/fbff.rb b/app/services/discourse_rewind/rewind/action/fbff.rb index 24dbef5..2d03a82 100644 --- a/app/services/discourse_rewind/rewind/action/fbff.rb +++ b/app/services/discourse_rewind/rewind/action/fbff.rb @@ -62,9 +62,11 @@ module DiscourseRewind fbffs .flatten .inject { |h1, h2| h1.merge(h2) { |_, v1, v2| v1 + v2 } } - .sort_by { |_, v| -v } - .first - .first + &.sort_by { |_, v| -v } + &.first + &.first + + return if !fbff_id { data: { diff --git a/app/services/discourse_rewind/rewind/action/reactions.rb b/app/services/discourse_rewind/rewind/action/reactions.rb index 6641edc..bcf9667 100644 --- a/app/services/discourse_rewind/rewind/action/reactions.rb +++ b/app/services/discourse_rewind/rewind/action/reactions.rb @@ -85,7 +85,7 @@ module DiscourseRewind end def sort_and_limit(reactions) - reactions.sort_by { |_, v| -v }.first(5).to_h + reactions.sort_by { |_, v| v }.first(5).to_h end end end diff --git a/app/services/discourse_rewind/rewind/action/reading_time.rb b/app/services/discourse_rewind/rewind/action/reading_time.rb index 94e5732..265fdc8 100644 --- a/app/services/discourse_rewind/rewind/action/reading_time.rb +++ b/app/services/discourse_rewind/rewind/action/reading_time.rb @@ -6,12 +6,15 @@ module DiscourseRewind class Rewind::Action::ReadingTime < Rewind::Action::BaseReport def call reading_time = UserVisit.where(user_id: user.id).where(visited_at: date).sum(:time_read) + book = best_book_fit(reading_time) + + return if book.nil? { data: { reading_time: reading_time, - book: best_book_fit(reading_time)[:title], - isbn: best_book_fit(reading_time)[:isbn], + book: book[:title], + isbn: book[:isbn], }, identifier: "reading-time", } @@ -114,6 +117,8 @@ module DiscourseRewind reading_time_rest -= best_fit.last[:reading_time] end + return if books.empty? + book_title = books.group_by { |book| book }.transform_values(&:count).max_by { |_, count| count }.first diff --git a/app/services/discourse_rewind/rewind/fetch.rb b/app/services/discourse_rewind/rewind/fetch.rb index 830a58d..c03f9d6 100644 --- a/app/services/discourse_rewind/rewind/fetch.rb +++ b/app/services/discourse_rewind/rewind/fetch.rb @@ -20,8 +20,7 @@ module DiscourseRewind CACHE_DURATION = 5.minutes - YEAR = 2024 - + model :year model :user model :date model :reports @@ -32,12 +31,27 @@ module DiscourseRewind User.find_by_username(guardian.user.username) end - def fetch_date(params:) - Date.new(YEAR).all_year + 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 + false + end end - def fetch_reports(date:, user:, guardian:) - key = "rewind:#{guardian.user.username}:#{YEAR}" + 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 Rails.env.development? || !reports @@ -48,7 +62,7 @@ module DiscourseRewind .map { |report| report.call(date:, user:, guardian:) } Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports)) else - reports = MultiJson.load(reports, symbolize_keys: true) + reports = MultiJson.load(reports.compact, symbolize_keys: true) end reports diff --git a/assets/javascripts/discourse/components/reports/favorite-tags.gjs b/assets/javascripts/discourse/components/reports/favorite-tags.gjs index a185225..9d60bfd 100644 --- a/assets/javascripts/discourse/components/reports/favorite-tags.gjs +++ b/assets/javascripts/discourse/components/reports/favorite-tags.gjs @@ -13,7 +13,7 @@ const FavoriteTags =