diff --git a/app/services/discourse_rewind/rewind/action/base.rb b/app/services/discourse_rewind/rewind/action/base.rb new file mode 100644 index 0000000..4c39f70 --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/base.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module DiscourseRewind + class Rewind::Action::Base < Service::ActionBase + option :user + option :date + + def call + raise NotImplementedError + end + + def enabled? + true + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/posting_calendar.rb b/app/services/discourse_rewind/rewind/action/posting_calendar.rb index 2439995..a55dd30 100644 --- a/app/services/discourse_rewind/rewind/action/posting_calendar.rb +++ b/app/services/discourse_rewind/rewind/action/posting_calendar.rb @@ -3,10 +3,7 @@ # For a GitHub like calendar # https://docs.github.com/assets/cb-35216/mw-1440/images/help/profile/contributions-graph.webp module DiscourseRewind - class Rewind::Action::PostingCalendar < Service::ActionBase - option :user - option :date - + class Rewind::Action::PostingCalendar < Action::Base def call calendar = Post diff --git a/app/services/discourse_rewind/rewind/action/reactions.rb b/app/services/discourse_rewind/rewind/action/reactions.rb new file mode 100644 index 0000000..59859b2 --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/reactions.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +# For a most user / received reactions cards +module DiscourseRewind + class Rewind::Action::Reactions < Action::Base + def call + post_used_reactions = {} + post_received_reactions = {} + chat_used_reactions = {} + chat_received_reactions = {} + + if defined?(DiscourseReactions::Reaction) + # This is missing heart reactions (default like) + post_used_reactions = + DiscourseReactions::Reaction + .by_user(user) + .where(created_at: date) + .group(:reaction_value) + .count + + post_received_reactions = + DiscourseReactions::Reaction + .includes(:post) + .where(posts: { user_id: user.id }) + .where(created_at: date) + .group(:reaction_value) + .count + end + + if SiteSetting.chat_enabled + chat_used_reactions = + Chat::MessageReaction.where(user: user).where(created_at: date).group(:emoji).count + + chat_received_reactions = + Chat::MessageReaction + .includes(:chat_message) + .where(chat_message: { user_id: user.id }) + .where(created_at: date) + .group(:emoji) + .count + end + + { + data: { + post_used_reactions: sort_and_limit(post_used_reactions), + post_received_reactions: sort_and_limit(post_received_reactions), + chat_used_reactions: sort_and_limit(chat_used_reactions), + chat_received_reactions: sort_and_limit(chat_received_reactions), + }, + identifier: "reactions", + } + end + + def enabled? + SiteSetting.discourse_reaction_enabled || SiteSetting.chat_enabled + end + + def sort_and_limit(reactions) + reactions.sort_by { |_, v| -v }.first(6).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 787c59c..ebeeb41 100644 --- a/app/services/discourse_rewind/rewind/action/reading_time.rb +++ b/app/services/discourse_rewind/rewind/action/reading_time.rb @@ -3,10 +3,7 @@ # For showcasing the reading time of a user # Should we show book covers or just the names? module DiscourseRewind - class Rewind::Action::ReadingTime < Service::ActionBase - option :user - option :date - + class Rewind::Action::ReadingTime < Action::Base def call reading_time = UserVisit.where(user: user).where(visited_at: date).sum(:time_read) diff --git a/app/services/discourse_rewind/rewind/fetch.rb b/app/services/discourse_rewind/rewind/fetch.rb index 0c48e90..0933721 100644 --- a/app/services/discourse_rewind/rewind/fetch.rb +++ b/app/services/discourse_rewind/rewind/fetch.rb @@ -22,11 +22,8 @@ class Rewind::Fetch # @option params [Integer] :username of the rewind # @return [Service::Base::Context] - # order matters, rewinds are displayed in the order they are defined - REPORTS = [ - DiscourseRewind::Rewind::Action::ReadingTime, - DiscourseRewind::Rewind::Action::PostingCalendar, - ].freeze + # Do we need a custom order? + available_reports = DiscourseRewind::Rewind::Action::Base.descendants CACHE_DURATION = 5.minutes @@ -57,7 +54,10 @@ def fetch_reports(params:, date:, user:, guardian:) reports = Discourse.redis.get(key) if Rails.env.development? || !reports - reports = REPORTS.map { |report| report.call(params:, date:, user:, guardian:) } + reports = + available_reports + .filter { _1.enabled? } + .map { |report| report.call(params:, date:, user:, guardian:) } Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports)) else reports = MultiJson.load(reports, symbolize_keys: true)