Skip to content

Commit 03191bc

Browse files
committed
Count profile messages correctly
We accidentally limited it to the currently shown 20 messages. Now we properly display it according to the selected interval.
1 parent 9876553 commit 03191bc

4 files changed

Lines changed: 79 additions & 10 deletions

File tree

app/controllers/concerns/profile_activity.rb

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@ def person_ids_for_query
2222

2323
def load_activity_data(scope: nil, year: nil)
2424
@activity_filters = parse_activity_filters
25-
@activity_entries = build_activity_entries(scope: scope, filters: @activity_filters)
25+
effective_scope = scope || default_recent_scope
26+
@activity_entries = build_activity_entries(scope: effective_scope, filters: @activity_filters)
27+
@activity_summary = build_activity_summary(scope: effective_scope, filters: @activity_filters)
28+
@activity_period ||= { type: :recent } if scope.nil?
2629
@contribution_years = contribution_years
2730
@contribution_year = year || select_contribution_year(@contribution_years)
2831
@contribution_weeks, @contribution_month_spans = build_contribution_weeks(@contribution_year, filters: @activity_filters)
2932
end
3033

34+
def default_recent_scope
35+
ids = person_ids_for_query
36+
start_date = 1.month.ago.beginning_of_day
37+
Message.where(sender_person_id: ids, created_at: start_date..)
38+
end
39+
3140
def build_activity_entries(scope: nil, filters: nil)
3241
ids = person_ids_for_query
3342
return [] if ids.blank?
@@ -73,6 +82,63 @@ def build_activity_entries(scope: nil, filters: nil)
7382
entries.first(20)
7483
end
7584

85+
def build_activity_summary(scope: nil, filters: nil)
86+
ids = person_ids_for_query
87+
return empty_activity_summary if ids.blank?
88+
89+
scope ||= Message.where(sender_person_id: ids)
90+
messages = scope.includes(:topic, :attachments)
91+
92+
return empty_activity_summary if messages.empty?
93+
94+
topic_ids = messages.map(&:topic_id).uniq
95+
first_message_per_topic = Message.where(topic_id: topic_ids).group(:topic_id).minimum(:id)
96+
first_patch_per_topic = Message.joins(:attachments).where(topic_id: topic_ids).group(:topic_id).minimum(:id)
97+
own_topic_ids = Topic.where(id: topic_ids, creator_person_id: ids).pluck(:id).to_set
98+
99+
filter_symbols = filters&.map(&:to_sym)&.to_set
100+
101+
summary = {
102+
total: 0,
103+
started_thread: 0,
104+
replied_own_thread: 0,
105+
replied_other_thread: 0,
106+
sent_first_patch: 0,
107+
sent_followup_patch: 0
108+
}
109+
110+
messages.each do |message|
111+
topic = message.topic
112+
next unless topic
113+
114+
activity_types = compute_activity_types(
115+
message: message,
116+
topic: topic,
117+
first_message_per_topic: first_message_per_topic,
118+
first_patch_per_topic: first_patch_per_topic,
119+
own_topic_ids: own_topic_ids
120+
)
121+
122+
if filter_symbols.blank? || (activity_types.to_set & filter_symbols).any?
123+
summary[:total] += 1
124+
activity_types.each { |type| summary[type] += 1 }
125+
end
126+
end
127+
128+
summary
129+
end
130+
131+
def empty_activity_summary
132+
{
133+
total: 0,
134+
started_thread: 0,
135+
replied_own_thread: 0,
136+
replied_other_thread: 0,
137+
sent_first_patch: 0,
138+
sent_followup_patch: 0
139+
}
140+
end
141+
76142
def compute_activity_types(message:, topic:, first_message_per_topic:, first_patch_per_topic:, own_topic_ids:)
77143
is_first_message = first_message_per_topic[topic.id] == message.id
78144
is_own_thread = own_topic_ids.include?(topic.id)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/recent_threads", activity_entries: @activity_entries, activity_period: @activity_period
1+
= render "shared/profile/recent_threads", activity_entries: @activity_entries, activity_period: @activity_period, activity_summary: @activity_summary

app/views/shared/profile/_recent_threads.html.slim

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
h2 Messages in week #{activity_period[:week]} (#{activity_period[:start_date].strftime('%b %d')} - #{activity_period[:end_date].strftime('%b %d, %Y')})
88
- when :month
99
h2 Messages in #{Date.new(activity_period[:year], activity_period[:month], 1).strftime('%B %Y')}
10+
- when :recent
11+
h2 Recent messages (last month)
1012
- else
11-
h2 Recent messages
13+
h2 Recent messages (last month)
1214

1315
- if activity_entries.any?
14-
- total = activity_entries.size
15-
- started = activity_entries.count { |e| e[:activity_types].include?(:started_thread) }
16-
- replied_own = activity_entries.count { |e| e[:activity_types].include?(:replied_own_thread) }
17-
- replied_other = activity_entries.count { |e| e[:activity_types].include?(:replied_other_thread) }
18-
- first_patch = activity_entries.count { |e| e[:activity_types].include?(:sent_first_patch) }
19-
- followup_patch = activity_entries.count { |e| e[:activity_types].include?(:sent_followup_patch) }
16+
- summary = local_assigns[:activity_summary] || {}
17+
- total = summary[:total] || activity_entries.size
18+
- started = summary[:started_thread] || 0
19+
- replied_own = summary[:replied_own_thread] || 0
20+
- replied_other = summary[:replied_other_thread] || 0
21+
- first_patch = summary[:sent_first_patch] || 0
22+
- followup_patch = summary[:sent_followup_patch] || 0
2023
.activity-summary
2124
span.activity-summary-total #{total} messages
2225
span.activity-summary-item
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/recent_threads", activity_entries: @activity_entries, activity_period: @activity_period
1+
= render "shared/profile/recent_threads", activity_entries: @activity_entries, activity_period: @activity_period, activity_summary: @activity_summary

0 commit comments

Comments
 (0)