闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. module UserTrackingConcern
  3. extend ActiveSupport::Concern
  4. REGENERATE_FEED_DAYS = 14
  5. UPDATE_SIGN_IN_HOURS = 24
  6. included do
  7. before_action :set_user_activity
  8. end
  9. private
  10. def set_user_activity
  11. return unless user_needs_sign_in_update?
  12. # Mark as signed-in today
  13. current_user.update_tracked_fields!(request)
  14. ActivityTracker.record('activity:logins', current_user.id)
  15. # Regenerate feed if needed
  16. regenerate_feed! if user_needs_feed_update?
  17. end
  18. def user_needs_sign_in_update?
  19. user_signed_in? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < UPDATE_SIGN_IN_HOURS.hours.ago)
  20. end
  21. def user_needs_feed_update?
  22. current_user.last_sign_in_at < REGENERATE_FEED_DAYS.days.ago
  23. end
  24. def regenerate_feed!
  25. Redis.current.setnx("account:#{current_user.account_id}:regeneration", true) && Redis.current.expire("account:#{current_user.account_id}:regeneration", 1.day.seconds)
  26. RegenerationWorker.perform_async(current_user.account_id)
  27. end
  28. end