Add AlgoliaSearchable to Comment (#20899)

* Add AlgoliaSearchable to Comment

* Improve test

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Refinement

* Fix broken spec

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Mac Siri 2024-04-26 13:30:00 -04:00 committed by GitHub
parent d5ba407629
commit 7890944a9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 51 additions and 5 deletions

View file

@ -4,6 +4,7 @@ class Comment < ApplicationRecord
include PgSearch::Model
include Reactable
include AlgoliaSearchable
BODY_MARKDOWN_SIZE_RANGE = (1..25_000)

View file

@ -0,0 +1,34 @@
module AlgoliaSearchable
module SearchableComment
extend ActiveSupport::Concern
included do
include AlgoliaSearch
algoliasearch(**DEFAULT_ALGOLIA_SETTINGS, unless: :bad_comment?) do
attribute :commentable_id, :commentable_type, :path, :parent_id
attribute :body do
title
end
attribute :published_at do
readable_publish_date
end
attribute :user do
{ name: user.name, username: user.username, profile_image: user.profile_image_90 }
end
end
end
class_methods do
def trigger_sidekiq_worker(record, delete)
AlgoliaSearch::SearchIndexWorker.perform_async(record.class.name, record.id, delete)
end
end
def bad_comment?
score.negative?
end
end
end

View file

@ -612,4 +612,13 @@ RSpec.describe Comment do
expect(comment.by_staff_account?).to be(false)
end
end
context "when indexing with Algolia", :algolia do
it "indexes on create" do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:comment)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Comment", kind_of(Integer),
false).once
end
end
end

View file

@ -445,7 +445,7 @@ RSpec.describe Organization do
search_index_worker = AlgoliaSearch::SearchIndexWorker
allow(search_index_worker).to receive(:perform_async)
create(:organization)
expect(search_index_worker).to have_received(:perform_async).with("Organization", kind_of(Integer), false)
expect(search_index_worker).to have_received(:perform_async).with("Organization", kind_of(Integer), false).once
end
end
end

View file

@ -137,7 +137,7 @@ RSpec.describe PodcastEpisode do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:podcast_episode)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("PodcastEpisode",
kind_of(Integer), false)
kind_of(Integer), false).once
end
end
end

View file

@ -294,7 +294,8 @@ RSpec.describe Tag do
it "indexes on create" do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:tag)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Tag", kind_of(Integer), false)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("Tag", kind_of(Integer),
false).once
end
end
end

View file

@ -1027,14 +1027,15 @@ RSpec.describe User do
it "indexes the user on create" do
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
create(:user)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", kind_of(Integer), false)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", kind_of(Integer),
false).once
end
it "updates user index if user's name has changed" do
user = create(:user)
allow(AlgoliaSearch::SearchIndexWorker).to receive(:perform_async)
user.update(name: "New Name")
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", user.id, false)
expect(AlgoliaSearch::SearchIndexWorker).to have_received(:perform_async).with("User", user.id, false).once
end
describe "#bad_actor?" do