2017-04-30 08:28:16 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserTrackingConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
REGENERATE_FEED_DAYS = 14
|
|
|
|
UPDATE_SIGN_IN_HOURS = 24
|
|
|
|
|
|
|
|
included do
|
2017-10-14 01:44:29 +11:00
|
|
|
before_action :set_user_activity
|
2017-04-30 08:28:16 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_user_activity
|
2017-10-14 01:44:29 +11:00
|
|
|
return unless user_needs_sign_in_update?
|
|
|
|
|
2017-04-30 08:28:16 +10:00
|
|
|
# Mark as signed-in today
|
|
|
|
current_user.update_tracked_fields!(request)
|
2017-12-30 05:52:04 +11:00
|
|
|
ActivityTracker.record('activity:logins', current_user.id)
|
2017-04-30 08:28:16 +10:00
|
|
|
|
|
|
|
# Regenerate feed if needed
|
2017-06-14 21:37:03 +10:00
|
|
|
regenerate_feed! if user_needs_feed_update?
|
2017-04-30 08:28:16 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_needs_sign_in_update?
|
2017-10-14 01:44:29 +11:00
|
|
|
user_signed_in? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < UPDATE_SIGN_IN_HOURS.hours.ago)
|
2017-04-30 08:28:16 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_needs_feed_update?
|
|
|
|
current_user.last_sign_in_at < REGENERATE_FEED_DAYS.days.ago
|
|
|
|
end
|
2017-06-14 21:37:03 +10:00
|
|
|
|
|
|
|
def regenerate_feed!
|
|
|
|
Redis.current.setnx("account:#{current_user.account_id}:regeneration", true) == 1 && Redis.current.expire("account:#{current_user.account_id}:regeneration", 3_600 * 24)
|
|
|
|
RegenerationWorker.perform_async(current_user.account_id)
|
|
|
|
end
|
2017-04-30 08:28:16 +10:00
|
|
|
end
|