[deploy] Optimization:Index Chat Channel Memberships Async on Message Creation (#9491)
This commit is contained in:
parent
cd11ebefb9
commit
330009995d
4 changed files with 43 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
12
app/workers/chat_channels/indexes_memberships_worker.rb
Normal file
12
app/workers/chat_channels/indexes_memberships_worker.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue