91 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

2024-12-12 17:45:22 -03:00
# frozen_string_literal: true
# For a most user / received reactions cards
module DiscourseRewind
2024-12-13 17:00:17 +01:00
class Rewind::Action::Reactions < Rewind::Action::BaseReport
2025-01-09 16:52:32 +01:00
FakeData = {
data: {
post_received_reactions: {
2025-01-09 17:55:58 +01:00
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
2025-01-09 16:52:32 +01:00
},
post_used_reactions: {
2025-01-09 17:55:58 +01:00
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
},
chat_used_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
},
chat_received_reactions: {
"open_mouth" => 2,
"cat" => 32,
"dog" => 34,
"heart" => 45,
"grinning" => 82,
2025-01-09 16:52:32 +01:00
},
},
identifier: "reactions",
}
2024-12-12 17:45:22 -03:00
def call
2025-01-09 16:52:32 +01:00
return FakeData if Rails.env.development?
2024-12-12 17:45:22 -03:00
2025-01-09 16:52:32 +01:00
data = {}
2024-12-12 17:45:22 -03:00
if defined?(DiscourseReactions::Reaction)
# This is missing heart reactions (default like)
2025-01-09 17:55:58 +01:00
data[:post_used_reactions] = sort_and_limit(
DiscourseReactions::Reaction
.by_user(user)
.where(created_at: date)
.group(:reaction_value)
.count,
)
2025-01-02 22:15:10 +01:00
2025-01-09 17:55:58 +01:00
data[:post_received_reactions] = sort_and_limit(
DiscourseReactions::Reaction
.includes(:post)
.where(posts: { user_id: user.id })
.where(created_at: date)
.group(:reaction_value)
.count,
)
2024-12-12 17:45:22 -03:00
end
if SiteSetting.chat_enabled
2025-01-09 17:55:58 +01:00
data[:chat_used_reactions] = sort_and_limit(
Chat::MessageReaction.where(user: user).where(created_at: date).group(:emoji).count,
)
2025-01-02 22:15:10 +01:00
2025-01-09 17:55:58 +01:00
data[:chat_received_reactions] = sort_and_limit(
Chat::MessageReaction
.includes(:chat_message)
.where(chat_message: { user_id: user.id })
.where(created_at: date)
.group(:emoji)
.count,
)
2024-12-12 17:45:22 -03:00
end
2025-01-02 22:15:10 +01:00
{ data:, identifier: "reactions" }
2024-12-12 17:45:22 -03:00
end
def enabled?
2025-01-07 23:26:17 +01:00
SiteSetting.discourse_reactions_enabled || SiteSetting.chat_enabled
2024-12-12 17:45:22 -03:00
end
def sort_and_limit(reactions)
2025-01-13 18:56:40 -03:00
reactions.sort_by { |_, v| -v }.first(5).reverse.to_h
2024-12-12 17:45:22 -03:00
end
end
end