Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/services/discourse_rewind/rewind/action/base.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions app/services/discourse_rewind/rewind/action/reactions.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 1 addition & 4 deletions app/services/discourse_rewind/rewind/action/reading_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions app/services/discourse_rewind/rewind/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Loading