From 1132af15151713f52d8d1e320271185865a79633 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Sat, 16 Nov 2019 21:01:07 -0600 Subject: Moved to using a normalized text column for searches. Admins using an FTS-enabled version of Monsterfork will need to apply the migration from `dist/search.sql` then run `bundle exec rails monsterfork:index_statuses`. --- app/models/status.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/status.rb b/app/models/status.rb index 29c4f6bd1..a2d2a8f28 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -31,9 +31,9 @@ # edited :boolean # imported :boolean # origin :string -# tsv :tsvector # boostable :boolean # reject_replies :boolean +# normalized_text :text default(""), not null # class Status < ApplicationRecord @@ -43,6 +43,7 @@ class Status < ApplicationRecord include Streamable include Cacheable include StatusThreadingConcern + include TextHelper # match both with and without U+FE0F (the emoji variation selector) LOCAL_ONLY_TOKENS = /(?:#!|\u{1f441}\ufe0f?)\u200b?\z/ @@ -324,6 +325,7 @@ class Status < ApplicationRecord around_create Mastodon::Snowflake::Callbacks before_create :set_locality + before_create :update_normalized_text before_validation :prepare_contents, if: :local? before_validation :set_reblog @@ -334,6 +336,9 @@ class Status < ApplicationRecord after_create :set_poll_id after_create :process_bangtags, if: :local? + after_create :update_normalized_text + + after_update :update_normalized_text class << self include SearchHelper @@ -350,7 +355,7 @@ class Status < ApplicationRecord end return none if term.blank? || term.length < 3 query = query.without_reblogs - .where('text ~* ?', expand_search_query(term)) + .where('normalized_text ~ ?', expand_search_query(term)) .offset(offset).limit(limit) apply_timeline_filters(query, account, true) rescue ActiveRecord::StatementInvalid @@ -618,6 +623,12 @@ class Status < ApplicationRecord Bangtags.new(self).process end + def update_normalized_text + return unless (normalized_text.blank? && !text.blank?) || saved_change_to_text? + Rails.cache.delete("formatted_status:#{status.id}") + self.normalized_text = normalize_status(self) + end + def set_conversation self.thread = thread.reblog if thread&.reblog? -- cgit