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.

37 lines
994 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
  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. # Regenerate feed if needed
  15. regenerate_feed! if user_needs_feed_update?
  16. end
  17. def user_needs_sign_in_update?
  18. user_signed_in? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < UPDATE_SIGN_IN_HOURS.hours.ago)
  19. end
  20. def user_needs_feed_update?
  21. current_user.last_sign_in_at < REGENERATE_FEED_DAYS.days.ago
  22. end
  23. def regenerate_feed!
  24. Redis.current.setnx("account:#{current_user.account_id}:regeneration", true) == 1 && Redis.current.expire("account:#{current_user.account_id}:regeneration", 3_600 * 24)
  25. RegenerationWorker.perform_async(current_user.account_id)
  26. end
  27. end