diff --git a/app/models/message.rb b/app/models/message.rb index a3b8d0e55..8a8c795da 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -27,7 +27,7 @@ class Message < ApplicationRecord def update_chat_channel_last_message_at chat_channel.touch(:last_message_at) - chat_channel.chat_channel_memberships.each(&:index_to_elasticsearch) + ChatChannels::IndexesMembershipsWorker.perform_async(chat_channel.id) end def update_all_has_unopened_messages_statuses @@ -195,7 +195,7 @@ class Message < ApplicationRecord def channel_permission errors.add(:base, "Must be part of channel.") if chat_channel_id.blank? - channel = ChatChannel.find(chat_channel_id) + channel = chat_channel || ChatChannel.find(chat_channel_id) return if channel.open? errors.add(:base, "You are not a participant of this chat channel.") unless channel.has_member?(user) diff --git a/app/workers/chat_channels/indexes_memberships_worker.rb b/app/workers/chat_channels/indexes_memberships_worker.rb new file mode 100644 index 000000000..31f0fbcea --- /dev/null +++ b/app/workers/chat_channels/indexes_memberships_worker.rb @@ -0,0 +1,12 @@ +module ChatChannels + class IndexesMembershipsWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority, lock: :until_executing + + def perform(chat_channel_id) + chat_channel = ChatChannel.find(chat_channel_id) + chat_channel.chat_channel_memberships.each(&:index_to_elasticsearch) + end + end +end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 8b32c03fb..a75f6a3a4 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -143,4 +143,15 @@ RSpec.describe Message, type: :model do end.to change(EmailMessage, :count).by(0) end end + + describe "#after_create" do + it "enqueues ChatChannels::IndexesMembershipsWorker" do + chat_channel.add_users([user]) + allow(ChatChannels::IndexesMembershipsWorker).to receive(:perform_async) + + create(:message, chat_channel: chat_channel, user: user) + + expect(ChatChannels::IndexesMembershipsWorker).to have_received(:perform_async) + end + end end diff --git a/spec/workers/chat_channels/indexes_memberships_worker_spec.rb b/spec/workers/chat_channels/indexes_memberships_worker_spec.rb new file mode 100644 index 000000000..e7d146877 --- /dev/null +++ b/spec/workers/chat_channels/indexes_memberships_worker_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" + +RSpec.describe ChatChannels::IndexesMembershipsWorker, type: :worker do + describe "#perform" do + let(:chat_channel) { create(:chat_channel) } + let(:chat_channel_membership) { create(:chat_channel_membership) } + + it "indexes chat channel memberships" do + allow(ChatChannel).to receive(:find).and_return(chat_channel) + allow(chat_channel).to receive(:chat_channel_memberships).and_return([chat_channel_membership]) + allow(chat_channel_membership).to receive(:index_to_elasticsearch) + + described_class.new.perform(chat_channel.id) + + expect(chat_channel_membership).to have_received(:index_to_elasticsearch) + end + end +end