diff --git a/app/lib/event_bus.rb b/app/lib/event_bus.rb index 559fd4b6f..a05ed2741 100644 --- a/app/lib/event_bus.rb +++ b/app/lib/event_bus.rb @@ -42,6 +42,14 @@ def self.reset! EventBus.subscribe :message_thread_changed, ->(message_thread) { Searchable::ReindexMessageThreadJob.perform_later(message_thread.id) } + +EventBus.subscribe :message_thread_note_created, ->(note) { + Searchable::ReindexMessageThreadJob.perform_later(note.message_thread_id) +} +EventBus.subscribe :message_thread_note_changed, ->(note) { + Searchable::ReindexMessageThreadJob.perform_later(note.message_thread_id) +} + EventBus.subscribe :message_thread_tag_changed, ->(message_thread_tag) { Searchable::ReindexMessageThreadJob.perform_later(message_thread_tag.message_thread_id) } EventBus.subscribe :tag_renamed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) } diff --git a/app/models/message_thread_note.rb b/app/models/message_thread_note.rb index 1828bf61f..cfa9c3a87 100644 --- a/app/models/message_thread_note.rb +++ b/app/models/message_thread_note.rb @@ -1,3 +1,6 @@ class MessageThreadNote < ApplicationRecord belongs_to :message_thread + + after_create_commit ->(note) { EventBus.publish(:message_thread_note_created, note) } + after_update_commit ->(note) { EventBus.publish(:message_thread_note_changed, note) } end diff --git a/app/models/searchable/indexer.rb b/app/models/searchable/indexer.rb index fa36ef381..259123989 100644 --- a/app/models/searchable/indexer.rb +++ b/app/models/searchable/indexer.rb @@ -8,6 +8,7 @@ class Searchable::Indexer def self.index_message_thread(message_thread) record = ::Searchable::MessageThread.find_or_initialize_by(message_thread_id: message_thread.id) record.title = Searchable::IndexHelpers.searchable_string(message_thread.title) + record.note = Searchable::IndexHelpers.searchable_string(message_thread.message_thread_note&.note.to_s) record.tag_ids = message_thread.tags.map(&:id) record.tag_names = Searchable::IndexHelpers.searchable_string(message_thread.tags.map(&:name).join(' ').gsub(/[:\/]/, " ")) record.content = message_thread.messages.map { |message| message_to_searchable_string(message) }.join(' ') diff --git a/app/models/searchable/message_thread.rb b/app/models/searchable/message_thread.rb index b4022ea1e..5de325e61 100644 --- a/app/models/searchable/message_thread.rb +++ b/app/models/searchable/message_thread.rb @@ -4,7 +4,7 @@ class Searchable::MessageThread < ApplicationRecord include PgSearch::Model pg_search_scope :pg_search_all, - against: [:title, :content, :tag_names], + against: [:title, :content, :note, :tag_names], using: { tsearch: { highlight: { diff --git a/db/migrate/20231016140436_add_note_to_searchable_message_thread.rb b/db/migrate/20231016140436_add_note_to_searchable_message_thread.rb new file mode 100644 index 000000000..2304e76d8 --- /dev/null +++ b/db/migrate/20231016140436_add_note_to_searchable_message_thread.rb @@ -0,0 +1,11 @@ +class AddNoteToSearchableMessageThread < ActiveRecord::Migration[7.0] + def change + add_column :searchable_message_threads, :note, :string + + Searchable::MessageThread.reset_column_information + + Searchable::MessageThread.update_all(note: "") + + change_column_null :searchable_message_threads, :note, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 79c0a9b30..ebc5c6a59 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_12_094539) do +ActiveRecord::Schema[7.0].define(version: 2023_10_16_140436) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -65,10 +65,7 @@ t.bigint "automation_rule_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "condition_object_type" - t.bigint "condition_object_id" t.index ["automation_rule_id"], name: "index_automation_conditions_on_automation_rule_id" - t.index ["condition_object_type", "condition_object_id"], name: "index_automation_conditions_on_condition_object" end create_table "automation_rules", force: :cascade do |t| @@ -78,9 +75,6 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id", null: false - t.string "rule_object_type" - t.bigint "rule_object_id" - t.index ["rule_object_type", "rule_object_id"], name: "index_automation_rules_on_rule_object" t.index ["tenant_id"], name: "index_automation_rules_on_tenant_id" t.index ["user_id"], name: "index_automation_rules_on_user_id" end @@ -372,6 +366,7 @@ t.datetime "updated_at", null: false t.integer "tenant_id", null: false t.integer "box_id", null: false + t.string "note", null: false t.index ["message_thread_id"], name: "index_searchable_message_threads_on_message_thread_id", unique: true end