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.

30 lines
755 B

  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, if: %i(user_signed_in? user_needs_sign_in_update?)
  8. end
  9. private
  10. def set_user_activity
  11. # Mark as signed-in today
  12. current_user.update_tracked_fields!(request)
  13. # Regenerate feed if needed
  14. RegenerationWorker.perform_async(current_user.account_id) if user_needs_feed_update?
  15. end
  16. def user_needs_sign_in_update?
  17. current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < UPDATE_SIGN_IN_HOURS.hours.ago
  18. end
  19. def user_needs_feed_update?
  20. current_user.last_sign_in_at < REGENERATE_FEED_DAYS.days.ago
  21. end
  22. end