From b855335d15dc4cfc21ad3bd1ef8aaa48422da8dd Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Thu, 8 May 2025 12:41:29 -0400 Subject: [PATCH] Fix recent users list for setup social proof --- app/controllers/static_pages_controller.rb | 10 ++++- app/jobs/cache/setup_social_proof_job.rb | 50 ++++++++++++++-------- app/views/static_pages/index.html.erb | 16 +++---- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index b49296e..9edc648 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -27,8 +27,14 @@ class StaticPagesController < ApplicationController redirect_to FlavorText.random_time_video.sample, allow_other_host: allowed_hosts end - @show_wakatime_setup_notice = current_user.heartbeats.empty? || params[:show_wakatime_setup_notice] - @setup_social_proof = Cache::SetupSocialProofJob.perform_now if @show_wakatime_setup_notice + if current_user.heartbeats.empty? || params[:show_wakatime_setup_notice] + @show_wakatime_setup_notice = true + + setup_social_proof = Cache::SetupSocialProofJob.perform_now + @ssp_message = setup_social_proof[:message] + @ssp_users_recent = setup_social_proof[:users_recent] + @ssp_users_size = setup_social_proof[:users_size] + end # Get languages and editors in a single query using window functions Time.use_zone(current_user.timezone) do diff --git a/app/jobs/cache/setup_social_proof_job.rb b/app/jobs/cache/setup_social_proof_job.rb index e59c9a2..c685f41 100644 --- a/app/jobs/cache/setup_social_proof_job.rb +++ b/app/jobs/cache/setup_social_proof_job.rb @@ -5,25 +5,39 @@ class Cache::SetupSocialProofJob < Cache::ActivityJob def calculate # Only run queries as needed, starting with the smallest time range - if (past_5min_count = users_in_past(5.minutes)) >= 1 - "#{past_5min_count} #{'Hack Clubber'.pluralize(past_5min_count)} set up Hackatime in the last 5 minutes" - elsif (past_hour_count = users_in_past(1.hour)) >= 3 - "#{past_hour_count} #{'Hack Clubber'.pluralize(past_hour_count)} set up Hackatime in the last hour" - elsif (past_day_count = users_in_past(1.day)) >= 5 - "#{past_day_count} #{'Hack Clubber'.pluralize(past_day_count)} set up Hackatime today" - elsif (past_week_count = users_in_past(1.week)) >= 5 - "#{past_week_count} #{'Hack Clubber'.pluralize(past_week_count)} set up Hackatime in the past week" - elsif (past_month_count = users_in_past(1.month)) >= 5 - "#{past_month_count} #{'Hack Clubber'.pluralize(past_month_count)} set up Hackatime in the past month" - elsif (year_count = users_in_past(Time.current.beginning_of_year)) >= 5 - "#{year_count} #{'Hack Clubber'.pluralize(year_count)} set up Hackatime this year" - end + check_social_proof(5.minutes, 1, "in the last 5 minutes") || + check_social_proof(1.hour, 3, "in the last hour") || + check_social_proof(1.day, 5, "today") || + check_social_proof(1.week, 5, "in the past week") || + check_social_proof(1.month, 5, "in the past month") || + check_social_proof(Time.current.beginning_of_year, 5, "this year") end - def users_in_past(time_period) - Heartbeat.where("time > ?", time_period.to_f) - .where(source_type: :test_entry) - .distinct - .count(:user_id) + def check_social_proof(time_period, threshold, humanized_time_period) + user_ids = Heartbeat.where("time > ?", time_period.ago.to_f) + .where("time < ?", Time.current.to_f) + .with_valid_timestamps + .where(source_type: :test_entry) + .distinct + .pluck(:user_id) + + user_count = user_ids.size + return nil if user_count < threshold + + recent_setup_users = User.where(id: user_ids).limit(5).map do |user| + { + id: user.id, + avatar_url: user.avatar_url, + display_name: user.display_name || "Hack Clubber" + } + end + + message = "#{user_count.to_s + ' Hack Clubber'.pluralize(user_count)} set up Hackatime #{humanized_time_period}" + + { + message: message, + users_size: user_count, + users_recent: recent_setup_users + } end end \ No newline at end of file diff --git a/app/views/static_pages/index.html.erb b/app/views/static_pages/index.html.erb index 4884320..c91dc23 100644 --- a/app/views/static_pages/index.html.erb +++ b/app/views/static_pages/index.html.erb @@ -35,21 +35,21 @@
<%= link_to "Set up Hackatime! Click me.", my_wakatime_setup_path, class: "auth-button setup-button primary-action" %>
- <% if @recent_setup_users&.any? %> + <% if @ssp_users_recent&.any? %>
- <% @recent_setup_users.each do |user| %> + <% @ssp_users_recent.each do |user| %>
<%= user[:display_name] %>
<%= user[:display_name] %>
<% end %> - <% if @all_setup_users && @all_setup_users.size > 5 %> -
-
+<%= @all_setup_users.size - 5 %>
+ <% if @ssp_users_size && @ssp_users_size > 5 %> +
+
+<%= @ssp_users_size - 5 %>

All users who set up Hackatime

- <% @all_setup_users.each do |user| %> + <% @ssp_users_recent.each do |user| %>
<%= user[:display_name] %> <%= user[:display_name] %> @@ -61,8 +61,8 @@ <% end %>
<% end %> - <% if @setup_social_proof %> -

<%= @setup_social_proof %> (this is real data)

+ <% if @ssp_message %> +

<%= @ssp_message %> (this is real data)

<% end %>