diff --git a/app/jobs/domain/fa/job/user_page_job.rb b/app/jobs/domain/fa/job/user_page_job.rb index 0de99ff2..b57e8b78 100644 --- a/app/jobs/domain/fa/job/user_page_job.rb +++ b/app/jobs/domain/fa/job/user_page_job.rb @@ -1,5 +1,7 @@ # typed: strict class Domain::Fa::Job::UserPageJob < Domain::Fa::Job::Base + RECENT_USERS_THRESHOLD = T.let(12, Integer) + queue_as :fa_user_page queue_with_priority do T.bind(self, Domain::Fa::Job::UserPageJob) @@ -167,7 +169,7 @@ class Domain::Fa::Job::UserPageJob < Domain::Fa::Job::Base if recent_watching.empty? logger.info(format_tags("skipping followed users scan, 0 watching")) user.scanned_follows_at = Time.current - elsif recent_watching.count < 12 + elsif recent_watching.count < RECENT_USERS_THRESHOLD logger.info( format_tags( "skipping followed users scan, #{recent_watching.count} watching < threshold", @@ -215,10 +217,16 @@ class Domain::Fa::Job::UserPageJob < Domain::Fa::Job::Base end def check_skip_followed_by_users_scan(user, user_page) recent_watchers = user_page.recent_watchers + page_num_watched_by = user_page.num_watched_by + if recent_watchers.empty? logger.info(format_tags("skipping followed by scan, 0 watched")) user.scanned_followed_by_at = Time.current - elsif recent_watchers.count < 12 + return + end + + # by now, we know that there is at least one watcher + if recent_watchers.count < RECENT_USERS_THRESHOLD logger.info( format_tags( "skipping followed by scan, #{recent_watchers.count} watchers < threshold", @@ -239,36 +247,34 @@ class Domain::Fa::Job::UserPageJob < Domain::Fa::Job::Base unique_by: %i[from_id to_id], ) user.scanned_followed_by_at = Time.current - elsif recent_watchers.any? - # if there are watchers, find the ones we've already recorded. if - # all of them are known, then we can skip scanning favs the next time. - known_watchers = - Domain::User::FaUser.where(url_name: recent_watchers.map(&:url_name)) - if known_watchers.count == recent_watchers.count - logger.info( - format_tags("skipping followed by scan, all watchers already known"), - ) - user.scanned_followed_by_at = Time.current - end - elsif recent_watchers.any? - # if there are watchers, find the ones we've already recorded. if - # all of them are known, then we can assume watched users are up to date. - num_recent_watchers = recent_watchers.count - num_recent_watchers_known = - user - .followed_by_users - .where(url_name: recent_watchers.map(&:url_name)) - .count + return + end - if (num_recent_watchers == num_recent_watchers_known) && - (user.followed_by_users.count == user_page.num_watched_by) - logger.info( - format_tags( - "skipping UserFollowsJob, all watched users already known", - ), - ) - user.scanned_followed_by_at = Time.current + # if there are watchers, find the ones we've already recorded. if + # all of them are known, then we can skip scanning favs the next time. + known_watchers = + Domain::User::FaUser.where(url_name: recent_watchers.map(&:url_name)) + if known_watchers.count == recent_watchers.count + logger.info( + format_tags("skipping followed by scan, all watchers already known"), + ) + user.scanned_followed_by_at = Time.current + return + end + + # is the recent watcher in the last position known? + last_recent_watcher = T.must(recent_watchers.last) + last_recent_watcher_is_known = + known_watchers.any? do |user| + user.url_name == last_recent_watcher.url_name end + + if last_recent_watcher_is_known + logger.info( + format_tags("skipping followed by scan, last watcher already known"), + ) + user.scanned_followed_by_at = Time.current + return end end end diff --git a/config/routes.rb b/config/routes.rb index 93226524..34c87c14 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ # typed: strict +ID_CONSTRAINT = %r{([^/]+)} Rails.application.routes.draw do mount ActionCable.server => "/cable" @@ -24,6 +25,9 @@ Rails.application.routes.draw do resources :users, as: :domain_users, only: %i[show], + constraints: { + id: ID_CONSTRAINT, + }, controller: "domain/users" do get :search_by_name, on: :collection @@ -48,7 +52,13 @@ Rails.application.routes.draw do as: :visual_results, to: "domain/posts#visual_results" end - resources :users, only: %i[], controller: "domain/users", path: "" do + resources :users, + only: %i[], + controller: "domain/users", + path: "", + constraints: { + id: ID_CONSTRAINT, + } do get :faved_by, on: :collection, action: :users_faving_post end end