docbrown/app/services/data_sync/elasticsearch/user.rb
Molly Struve 44d0be9c76
[deploy] Setup User Elasticsearch Doc Syncing (#7049)
* sync related Elasticsearch docs when a user is updated

* check for changes in all user nested keys

* put user fields into a constant for easy access
2020-04-03 10:07:32 -04:00

51 lines
1 KiB
Ruby

module DataSync
module Elasticsearch
class User
RELATED_DOCS = %i[
articles
reactions
podcast_episodes
chat_channel_memberships
comments
].freeze
SHARED_USER_FIELDS = %i[
username
name
pro
profile_image_url
].freeze
attr_accessor :user, :updated_fields
delegate :articles, :chat_channel_memberships, :comments, to: :@user
def initialize(user, updated_fields)
@user = user
@updated_fields = updated_fields.deep_symbolize_keys
end
def sync_documents
return unless sync_needed?
RELATED_DOCS.each do |relation_name|
send(relation_name).find_each(&:index_to_elasticsearch)
end
end
private
def sync_needed?
updated_fields.slice(*SHARED_USER_FIELDS).any?
end
def reactions
user.reactions.readinglist
end
def podcast_episodes
PodcastEpisode.for_user(user)
end
end
end
end