From 4c9a0b9f693462a937826366ecb5c410ab484e0f Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 1 Aug 2024 15:25:51 -0400 Subject: [PATCH] Add `User::Activity` concern to hold activity window features --- app/models/concerns/user/activity.rb | 24 ++++++++++++++++++++++++ app/models/user.rb | 18 ++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 app/models/concerns/user/activity.rb diff --git a/app/models/concerns/user/activity.rb b/app/models/concerns/user/activity.rb new file mode 100644 index 00000000000..c183fea7d62 --- /dev/null +++ b/app/models/concerns/user/activity.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module User::Activity + extend ActiveSupport::Concern + + # The home and list feeds will be stored for this amount of time, and status + # fan-out to followers will include only people active within this time frame. + # + # Lowering the duration may improve performance if many people sign up, but + # most will not check their feed every day. Raising the duration reduces the + # amount of background processing that happens when people become active. + ACTIVE_DURATION = ENV.fetch('USER_ACTIVE_DAYS', 7).to_i.days.freeze + + included do + scope :signed_in_recently, -> { where(current_sign_in_at: ACTIVE_DURATION.ago..) } + scope :not_signed_in_recently, -> { where(current_sign_in_at: ...ACTIVE_DURATION.ago) } + end + + private + + def inactive_since_duration? + last_sign_in_at < ACTIVE_DURATION.ago + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 72854569260..79fc58d6a5c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,20 +56,12 @@ class User < ApplicationRecord include LanguagesHelper include Redisable + include User::Activity include User::HasSettings include User::LdapAuthenticable include User::Omniauthable include User::PamAuthenticable - # The home and list feeds will be stored in Redis for this amount - # of time, and status fan-out to followers will include only people - # within this time frame. Lowering the duration may improve performance - # if lots of people sign up, but not a lot of them check their feed - # every day. Raising the duration reduces the amount of expensive - # RegenerationWorker jobs that need to be run when those people come - # to check their feed - ACTIVE_DURATION = ENV.fetch('USER_ACTIVE_DAYS', 7).to_i.days.freeze - devise :two_factor_authenticatable, otp_secret_encryption_key: Rails.configuration.x.otp_secret @@ -121,8 +113,6 @@ class User < ApplicationRecord scope :enabled, -> { where(disabled: false) } scope :disabled, -> { where(disabled: true) } scope :active, -> { confirmed.signed_in_recently.account_not_suspended } - scope :signed_in_recently, -> { where(current_sign_in_at: ACTIVE_DURATION.ago..) } - scope :not_signed_in_recently, -> { where(current_sign_in_at: ...ACTIVE_DURATION.ago) } scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) } scope :matches_ip, ->(value) { left_joins(:ips).where('user_ips.ip <<= ?', value).group('users.id') } @@ -499,7 +489,7 @@ class User < ApplicationRecord return unless confirmed? ActivityTracker.record('activity:logins', id) - regenerate_feed! if needs_feed_update? + regenerate_feed! if inactive_since_duration? end def notify_staff_about_pending_account! @@ -514,10 +504,6 @@ class User < ApplicationRecord RegenerationWorker.perform_async(account_id) if redis.set("account:#{account_id}:regeneration", true, nx: true, ex: 1.day.seconds) end - def needs_feed_update? - last_sign_in_at < ACTIVE_DURATION.ago - end - def validate_email_dns? email_changed? && !external? && !self.class.skip_mx_check? end