From 9863d70b57490dc2dcf0b1d4153283dae154b025 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Thu, 27 Feb 2020 09:59:12 -0500 Subject: [PATCH] Remove algolia from chat channel memberships (#6323) --- .../chat_channel_memberships_controller.rb | 2 -- app/controllers/chat_channels_controller.rb | 30 ++++++------------- app/models/chat_channel.rb | 1 - app/models/chat_channel_membership.rb | 11 ------- app/models/message.rb | 2 +- app/services/user_blocks/channel_handler.rb | 2 -- app/services/users/cleanup_chat_channels.rb | 5 +--- app/views/chat_channels/index.html.erb | 3 -- 8 files changed, 11 insertions(+), 45 deletions(-) diff --git a/app/controllers/chat_channel_memberships_controller.rb b/app/controllers/chat_channel_memberships_controller.rb index b57d22498..0ceeced98 100644 --- a/app/controllers/chat_channel_memberships_controller.rb +++ b/app/controllers/chat_channel_memberships_controller.rb @@ -26,7 +26,6 @@ class ChatChannelMembershipsController < ApplicationController authorize @chat_channel_membership if permitted_params[:user_action] == "accept" @chat_channel_membership.update(status: "active") - @chat_channel_membership.index! else @chat_channel_membership.update(status: "rejected") end @@ -42,7 +41,6 @@ class ChatChannelMembershipsController < ApplicationController chat_channel_memberships.where(user_id: current_user.id).first authorize @chat_channel_membership @chat_channel_membership.update(status: "left_channel") - @chat_channel_membership.remove_from_index! @chat_channels_memberships = [] render json: { result: "left channel" }, status: :created end diff --git a/app/controllers/chat_channels_controller.rb b/app/controllers/chat_channels_controller.rb index 69c9c64ff..a70b83dcd 100644 --- a/app/controllers/chat_channels_controller.rb +++ b/app/controllers/chat_channels_controller.rb @@ -38,7 +38,6 @@ class ChatChannelsController < ApplicationController membership = @chat_channel.chat_channel_memberships.where(user_id: current_user.id).first membership.update(last_opened_at: 1.second.from_now, has_unopened_messages: false) send_open_notification - membership.index! render json: { status: "success", channel: params[:id] }, status: :ok end @@ -95,7 +94,7 @@ class ChatChannelsController < ApplicationController authorize chat_channel chat_channel.status = "blocked" chat_channel.save - chat_channel.chat_channel_memberships.map(&:remove_from_index!) + chat_channel.chat_channel_memberships.map(&:index_to_elasticsearch) render json: { status: "success", message: "chat channel blocked" }, status: :ok end @@ -142,26 +141,15 @@ class ChatChannelsController < ApplicationController end def render_channels_html - return unless current_user + return unless current_user && params[:slug] - if params[:slug] - slug = if params[:slug]&.start_with?("@") - [current_user.username, params[:slug].delete("@")].sort.join("/") - else - params[:slug] - end - @active_channel = ChatChannel.find_by(slug: slug) - @active_channel.current_user = current_user if @active_channel - end - generate_algolia_search_key - end - - def generate_algolia_search_key - current_user_id = current_user.id - params = { filters: "viewable_by:#{current_user_id} AND status: active" } - @secured_algolia_key = Algolia.generate_secured_api_key( - ApplicationConfig["ALGOLIASEARCH_SEARCH_ONLY_KEY"], params - ) + slug = if params[:slug]&.start_with?("@") + [current_user.username, params[:slug].delete("@")].sort.join("/") + else + params[:slug] + end + @active_channel = ChatChannel.find_by(slug: slug) + @active_channel.current_user = current_user if @active_channel end def generate_github_token diff --git a/app/models/chat_channel.rb b/app/models/chat_channel.rb index 76ac46a17..b0120d134 100644 --- a/app/models/chat_channel.rb +++ b/app/models/chat_channel.rb @@ -77,7 +77,6 @@ class ChatChannel < ApplicationRecord status: "active", ) channel.add_users(users) - channel.chat_channel_memberships.map(&:index!) end channel end diff --git a/app/models/chat_channel_membership.rb b/app/models/chat_channel_membership.rb index b0c1bcbb3..5efe16e11 100644 --- a/app/models/chat_channel_membership.rb +++ b/app/models/chat_channel_membership.rb @@ -1,6 +1,4 @@ class ChatChannelMembership < ApplicationRecord - include AlgoliaSearch - include Searchable SEARCH_SERIALIZER = Search::ChatChannelMembershipSerializer SEARCH_CLASS = Search::ChatChannelMembership @@ -17,15 +15,6 @@ class ChatChannelMembership < ApplicationRecord after_commit :index_to_elasticsearch, on: %i[create update] after_commit :remove_from_elasticsearch, on: [:destroy] - 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, - :channel_name, :channel_image, :channel_modified_slug, :channel_messages_count - searchableAttributes %i[channel_text] - attributesForFaceting ["filterOnly(viewable_by)", "filterOnly(status)", "filterOnly(channel_status)", "filterOnly(channel_type)"] - ranking ["desc(channel_last_message_at)"] - end - delegate :channel_type, to: :chat_channel def channel_last_message_at diff --git a/app/models/message.rb b/app/models/message.rb index 7de20f45e..4aeb6f1f9 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.reindex! + chat_channel.chat_channel_memberships.each(&:index_to_elasticsearch) end def update_all_has_unopened_messages_statuses diff --git a/app/services/user_blocks/channel_handler.rb b/app/services/user_blocks/channel_handler.rb index af97a35e5..ba525cfe4 100644 --- a/app/services/user_blocks/channel_handler.rb +++ b/app/services/user_blocks/channel_handler.rb @@ -20,7 +20,6 @@ module UserBlocks chat_channel.update(status: "blocked") chat_channel.chat_channel_memberships.includes([:user]).each do |membership| membership.update(status: "left_channel") - membership.remove_from_index! end end @@ -31,7 +30,6 @@ module UserBlocks chat_channel.update(status: "active") chat_channel.chat_channel_memberships.includes([:user]).each do |membership| membership.update(status: "active") - membership.index! end end end diff --git a/app/services/users/cleanup_chat_channels.rb b/app/services/users/cleanup_chat_channels.rb index 4e9cb480f..7b617860c 100644 --- a/app/services/users/cleanup_chat_channels.rb +++ b/app/services/users/cleanup_chat_channels.rb @@ -13,10 +13,7 @@ module Users end def self.cleanup_memberships(chat_channel_memberships) - chat_channel_memberships.each do |ccm| - ccm.remove_from_index! - ccm.destroy! - end + chat_channel_memberships.each(&:destroy!) end private_class_method :cleanup_memberships end diff --git a/app/views/chat_channels/index.html.erb b/app/views/chat_channels/index.html.erb index cf95281d0..8776dfc43 100644 --- a/app/views/chat_channels/index.html.erb +++ b/app/views/chat_channels/index.html.erb @@ -12,9 +12,6 @@ id="chat" class="live-chat" data-pusher-key="<%= ApplicationConfig["PUSHER_KEY"] %>" - data-algolia-id="<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>" - data-algolia-key="<%= @secured_algolia_key %>" - data-algolia-index="<%= "SecuredChatChannelMembership_#{Rails.env}" %>" data-github-token="<%= @github_token %>" data-chat-channels="<%= [] %>" data-chat-options="<%= { showChannelsList: true, showTimestamp: true, activeChannelId: @active_channel&.id, currentUserId: current_user.id }.to_json %>">