Reaction report (#2)

This commit is contained in:
Rafael dos Santos Silva
2024-12-12 21:45:22 +01:00
committed by GitHub
parent 906f9d6e9e
commit bc6ad108ea
5 changed files with 86 additions and 14 deletions
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -22,11 +22,8 @@ module DiscourseRewind
# @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 @@ module DiscourseRewind
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)