Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit bc6ad10

Browse files
authored
Reaction report (#2)
1 parent 906f9d6 commit bc6ad10

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseRewind
4+
class Rewind::Action::Base < Service::ActionBase
5+
option :user
6+
option :date
7+
8+
def call
9+
raise NotImplementedError
10+
end
11+
12+
def enabled?
13+
true
14+
end
15+
end
16+
end

app/services/discourse_rewind/rewind/action/posting_calendar.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
# For a GitHub like calendar
44
# https://docs.github.com/assets/cb-35216/mw-1440/images/help/profile/contributions-graph.webp
55
module DiscourseRewind
6-
class Rewind::Action::PostingCalendar < Service::ActionBase
7-
option :user
8-
option :date
9-
6+
class Rewind::Action::PostingCalendar < Action::Base
107
def call
118
calendar =
129
Post
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# For a most user / received reactions cards
4+
module DiscourseRewind
5+
class Rewind::Action::Reactions < Action::Base
6+
def call
7+
post_used_reactions = {}
8+
post_received_reactions = {}
9+
chat_used_reactions = {}
10+
chat_received_reactions = {}
11+
12+
if defined?(DiscourseReactions::Reaction)
13+
# This is missing heart reactions (default like)
14+
post_used_reactions =
15+
DiscourseReactions::Reaction
16+
.by_user(user)
17+
.where(created_at: date)
18+
.group(:reaction_value)
19+
.count
20+
21+
post_received_reactions =
22+
DiscourseReactions::Reaction
23+
.includes(:post)
24+
.where(posts: { user_id: user.id })
25+
.where(created_at: date)
26+
.group(:reaction_value)
27+
.count
28+
end
29+
30+
if SiteSetting.chat_enabled
31+
chat_used_reactions =
32+
Chat::MessageReaction.where(user: user).where(created_at: date).group(:emoji).count
33+
34+
chat_received_reactions =
35+
Chat::MessageReaction
36+
.includes(:chat_message)
37+
.where(chat_message: { user_id: user.id })
38+
.where(created_at: date)
39+
.group(:emoji)
40+
.count
41+
end
42+
43+
{
44+
data: {
45+
post_used_reactions: sort_and_limit(post_used_reactions),
46+
post_received_reactions: sort_and_limit(post_received_reactions),
47+
chat_used_reactions: sort_and_limit(chat_used_reactions),
48+
chat_received_reactions: sort_and_limit(chat_received_reactions),
49+
},
50+
identifier: "reactions",
51+
}
52+
end
53+
54+
def enabled?
55+
SiteSetting.discourse_reaction_enabled || SiteSetting.chat_enabled
56+
end
57+
58+
def sort_and_limit(reactions)
59+
reactions.sort_by { |_, v| -v }.first(6).to_h
60+
end
61+
end
62+
end

app/services/discourse_rewind/rewind/action/reading_time.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
# For showcasing the reading time of a user
44
# Should we show book covers or just the names?
55
module DiscourseRewind
6-
class Rewind::Action::ReadingTime < Service::ActionBase
7-
option :user
8-
option :date
9-
6+
class Rewind::Action::ReadingTime < Action::Base
107
def call
118
reading_time = UserVisit.where(user: user).where(visited_at: date).sum(:time_read)
129

app/services/discourse_rewind/rewind/fetch.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ class Rewind::Fetch
2222
# @option params [Integer] :username of the rewind
2323
# @return [Service::Base::Context]
2424

25-
# order matters, rewinds are displayed in the order they are defined
26-
REPORTS = [
27-
DiscourseRewind::Rewind::Action::ReadingTime,
28-
DiscourseRewind::Rewind::Action::PostingCalendar,
29-
].freeze
25+
# Do we need a custom order?
26+
available_reports = DiscourseRewind::Rewind::Action::Base.descendants
3027

3128
CACHE_DURATION = 5.minutes
3229

@@ -57,7 +54,10 @@ def fetch_reports(params:, date:, user:, guardian:)
5754
reports = Discourse.redis.get(key)
5855

5956
if Rails.env.development? || !reports
60-
reports = REPORTS.map { |report| report.call(params:, date:, user:, guardian:) }
57+
reports =
58+
available_reports
59+
.filter { _1.enabled? }
60+
.map { |report| report.call(params:, date:, user:, guardian:) }
6161
Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
6262
else
6363
reports = MultiJson.load(reports, symbolize_keys: true)

0 commit comments

Comments
 (0)