docbrown/app/workers/segmented_user_refresh_worker.rb
Joshua Wehner 375cbfe946
Refresh audience segment for 'active' user (#19470)
* Refresh an individual user's segmentation

* Refresh segment if creating a published article

* Quick refactor

* Refresh segment if update publishing

* Quick refactor

* Refresh segment if update user settings

* Refresh segments after user update + role change

* Using latest_article_updated_at

* Sidekiq uses JSON for arguments, needs basic types

* Fix test issues with last_updated_at and sidekiq params

* Tests update continues

* Consolidate dependencies

* Method names acknowledging manual exclusive

* Fix test name copypasta

* Rubocop

* Try to clarify Creator#series

* Try to avoid naming predicate

* Remove erroneously included helper
2023-05-19 14:16:07 +02:00

30 lines
1.1 KiB
Ruby

class SegmentedUserRefreshWorker
include Sidekiq::Worker
sidekiq_options queue: :low_priority
# Ignore manually-updated segments, remove user if they no longer belong,
# add the user if they now belong
def perform(user_or_id)
user_id = user_or_id.respond_to?(:id) ? user_or_id.id : user_or_id
automated_segments = AudienceSegment.not_manual
matching = SegmentedUser.where(user_id: user_id).pluck(:audience_segment_id)
current_segments = automated_segments.where(id: matching)
future_segments = automated_segments.each_with_object([]) do |audience_segment, collection|
collection << audience_segment if audience_segment.includes?(user_id)
end
# In current / not in future => delete user from segment
SegmentedUser
.where(user_id: user_id)
.delete_by(audience_segment: (current_segments - future_segments))
# In future / not in current => add user to segment
(future_segments - current_segments).each do |audience_segment|
audience_segment.segmented_users.create! user_id: user_id
end
future_segments
end
end