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.

35 lines
979 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. regenerate_feed! 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. def regenerate_feed!
  23. Redis.current.setnx("account:#{current_user.account_id}:regeneration", true) == 1 && Redis.current.expire("account:#{current_user.account_id}:regeneration", 3_600 * 24)
  24. RegenerationWorker.perform_async(current_user.account_id)
  25. end
  26. end