From 7890944a9d819d859e07033af469e4a347fe0cfb Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Fri, 26 Apr 2024 13:30:00 -0400 Subject: [PATCH] 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> --- app/models/comment.rb | 1 + .../algolia_searchable/searchable_comment.rb | 34 +++++++++++++++++++ spec/models/comment_spec.rb | 9 +++++ spec/models/organization_spec.rb | 2 +- spec/models/podcast_episode_spec.rb | 2 +- spec/models/tag_spec.rb | 3 +- spec/models/user_spec.rb | 5 +-- 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 app/models/concerns/algolia_searchable/searchable_comment.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 4f269249d..b7bf1a5a2 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,6 +4,7 @@ class Comment < ApplicationRecord include PgSearch::Model include Reactable + include AlgoliaSearchable BODY_MARKDOWN_SIZE_RANGE = (1..25_000) diff --git a/app/models/concerns/algolia_searchable/searchable_comment.rb b/app/models/concerns/algolia_searchable/searchable_comment.rb new file mode 100644 index 000000000..fd3849a81 --- /dev/null +++ b/app/models/concerns/algolia_searchable/searchable_comment.rb @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 05026aab0..e2c403949 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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 diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index c4d51b620..5b74b2633 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -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 diff --git a/spec/models/podcast_episode_spec.rb b/spec/models/podcast_episode_spec.rb index b77c16db1..ca4b78594 100644 --- a/spec/models/podcast_episode_spec.rb +++ b/spec/models/podcast_episode_spec.rb @@ -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 diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 37b5a9e8c..804887a9c 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c9c6bc23f..3ee8a095a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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