Index ChatChannelMemberships to Elasticsearch (#6162) [deploy]

This commit is contained in:
Molly Struve 2020-02-19 14:29:16 -05:00 committed by GitHub
parent c50c59c250
commit e752ff98af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 13 deletions

View file

@ -1,5 +1,11 @@
class ChatChannelMembership < ApplicationRecord
include AlgoliaSearch
include Searchable
SEARCH_INDEX_WORKER = Search::ChatChannelMembershipEsIndexWorker
SEARCH_SERIALIZER = Search::ChatChannelMembershipSerializer
SEARCH_CLASS = Search::ChatChannelMembership
belongs_to :chat_channel
belongs_to :user
@ -9,6 +15,8 @@ class ChatChannelMembership < ApplicationRecord
validates :role, inclusion: { in: %w[member mod] }
validate :permission
after_commit :index_to_elasticsearch
algoliasearch index_name: "SecuredChatChannelMembership_#{Rails.env}", auto_index: false do
attribute :id, :status, :viewable_by, :chat_channel_id, :last_opened_at,
:channel_text, :channel_last_message_at, :channel_status, :channel_type, :channel_username,
@ -18,7 +26,7 @@ class ChatChannelMembership < ApplicationRecord
ranking ["desc(channel_last_message_at)"]
end
private
delegate :channel_type, to: :chat_channel
def channel_last_message_at
chat_channel.last_message_at
@ -28,10 +36,6 @@ class ChatChannelMembership < ApplicationRecord
chat_channel.status
end
def channel_type
chat_channel.channel_type
end
def channel_text
"#{chat_channel.channel_name} #{chat_channel.slug} #{chat_channel.channel_human_names}"
end
@ -60,14 +64,6 @@ class ChatChannelMembership < ApplicationRecord
other_user&.username if chat_channel.channel_type == "direct"
end
def channel_color
if chat_channel.channel_type == "direct"
other_user&.decorate&.darker_color
else
"#111111"
end
end
def channel_modified_slug
if chat_channel.channel_type == "direct"
"@" + other_user&.username
@ -80,6 +76,16 @@ class ChatChannelMembership < ApplicationRecord
user_id
end
private
def channel_color
if chat_channel.channel_type == "direct"
other_user&.decorate&.darker_color
else
"#111111"
end
end
def other_user
chat_channel.users.where.not(id: user_id).first
end

View file

@ -0,0 +1,10 @@
module Search
class ChatChannelMembershipSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :status, :viewable_by, :chat_channel_id, :last_opened_at,
:channel_text, :channel_last_message_at, :channel_status,
:channel_status, :channel_type, :channel_username, :channel_name,
:channel_image, :channel_modified_slug, :channel_messages_count
end
end

View file

@ -0,0 +1,12 @@
module Search
class ChatChannelMembershipEsIndexWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority
def perform(chat_channel_membership_id)
chat_channel_membership = ::ChatChannelMembership.find(chat_channel_membership_id)
chat_channel_membership.index_to_elasticsearch_inline
end
end
end

View file

@ -0,0 +1,7 @@
module DataUpdateScripts
class IndexChatChannelMembershipsToElasticsearch
def run
ChatChannelMembership.find_each(&:index_to_elasticsearch_inline)
end
end
end

View file

@ -0,0 +1,11 @@
require "rails_helper"
require Rails.root.join("lib/data_update_scripts/20200218195023_index_chat_channel_memberships_to_elasticsearch.rb")
describe DataUpdateScripts::IndexChatChannelMembershipsToElasticsearch, elasticsearch: true do
it "indexes chat channel memberships to Elasticsearch" do
chat_channel_membership = FactoryBot.create(:chat_channel_membership)
expect { chat_channel_membership.elasticsearch_doc }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
described_class.new.run
expect(chat_channel_membership.elasticsearch_doc).not_to be_nil
end
end

View file

@ -0,0 +1,45 @@
require "rails_helper"
RSpec.describe ChatChannelMembership, type: :model do
let(:chat_channel_membership) { FactoryBot.create(:chat_channel_membership) }
describe "#index_to_elasticsearch" do
it "enqueues job to index tag to elasticsearch" do
sidekiq_assert_enqueued_with(job: Search::ChatChannelMembershipEsIndexWorker, args: [chat_channel_membership.id]) do
chat_channel_membership.index_to_elasticsearch
end
end
end
describe "#index_to_elasticsearch_inline" do
it "indexed chat_channel_membership to elasticsearch inline" do
allow(Search::ChatChannelMembership).to receive(:index)
chat_channel_membership.index_to_elasticsearch_inline
expect(Search::ChatChannelMembership).to have_received(:index).with(chat_channel_membership.id, hash_including(:id, :channel_name))
end
end
describe "#after_commit" do
it "enqueues job to index chat_channel_membership to elasticsearch" do
chat_channel_membership.save
sidekiq_assert_enqueued_with(job: Search::ChatChannelMembershipEsIndexWorker, args: [chat_channel_membership.id]) do
chat_channel_membership.save
end
end
end
describe "#serialized_search_hash" do
it "creates a valid serialized hash to send to elasticsearch" do
mapping_keys = Search::ChatChannelMembership::MAPPINGS.dig(:properties).keys
expect(chat_channel_membership.serialized_search_hash.symbolize_keys.keys).to eq(mapping_keys)
end
end
describe "#elasticsearch_doc" do
it "finds document in elasticsearch", elasticsearch: true do
allow(Search::ChatChannelMembership).to receive(:find_document)
chat_channel_membership.elasticsearch_doc
expect(Search::ChatChannelMembership).to have_received(:find_document)
end
end
end

View file

@ -0,0 +1,21 @@
require "rails_helper"
RSpec.describe Search::ChatChannelMembershipEsIndexWorker, type: :worker, elasticsearch: true do
let(:worker) { subject }
include_examples "#enqueues_on_correct_queue", "high_priority", [1]
it "raises an error if record is not found" do
expect { worker.perform(1234) }.to raise_error(ActiveRecord::RecordNotFound)
end
it "indexes chat_channel_membership" do
chat_channel_membership = FactoryBot.create(:chat_channel_membership)
expect { chat_channel_membership.elasticsearch_doc }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
worker.perform(chat_channel_membership.id)
expected_hash = chat_channel_membership.serialized_search_hash.stringify_keys
expect(chat_channel_membership.elasticsearch_doc.dig("_source")).to include(
expected_hash,
)
end
end