about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-11-16 07:17:15 -0600
committermultiple creatures <dev@multiple-creature.party>2019-11-16 07:17:15 -0600
commit6abae51e47753cc80b440c4234c14cd8232e235f (patch)
treeeb3d71fece5419b882d6128c1ab0e130e121c40a /app
parent9de5952f4e92528a1dd664c188d730229493b707 (diff)
Regular expressions can now be used in full-text searches. Wrapping a search term in double-quotes treats it as a whole-word match.
Diffstat (limited to 'app')
-rw-r--r--app/helpers/search_helper.rb6
-rw-r--r--app/lib/bangtags.rb4
-rw-r--r--app/models/status.rb4
3 files changed, 12 insertions, 2 deletions
diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb
new file mode 100644
index 000000000..96da161f1
--- /dev/null
+++ b/app/helpers/search_helper.rb
@@ -0,0 +1,6 @@
+module SearchHelper
+
+	def expand_search_query(query)
+    query.gsub(/"(.*)"/, '\\y\1\\y')
+  end
+end
diff --git a/app/lib/bangtags.rb b/app/lib/bangtags.rb
index 681fa4abc..60fb426b3 100644
--- a/app/lib/bangtags.rb
+++ b/app/lib/bangtags.rb
@@ -2,6 +2,8 @@
 
 class Bangtags
   include ModerationHelper
+  include SearchHelper
+
   attr_reader :status, :account
 
   def initialize(status)
@@ -718,7 +720,7 @@ class Bangtags
           q = cmd[1..-1].join.strip
           next if q.blank?
           begin
-            data = @account.statuses.where('text ILIKE ?', "%#{sanitize_sql_like(q)}%")
+            data = @account.statuses.where('text ~* ?', expand_search_query(q))
               .reorder(:created_at)
               .pluck(:created_at)
               .map { |d| d.strftime('%Y-%m') }
diff --git a/app/models/status.rb b/app/models/status.rb
index 47d5f982e..045a4a31a 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -336,6 +336,8 @@ class Status < ApplicationRecord
   after_create :process_bangtags, if: :local?
 
   class << self
+    include SearchHelper
+
     def search_for(term, account = nil, limit = 33, offset = 0)
       return none if account.nil?
       if term.start_with?('me:')
@@ -349,7 +351,7 @@ class Status < ApplicationRecord
       end
       return none if term.blank? || term.length < 3
       query = query.without_reblogs
-        .where('text ILIKE ?', "%#{sanitize_sql_like(term)}%")
+        .where('text ~* ?', expand_search_query(term))
         .offset(offset).limit(limit)
       apply_timeline_filters(query, account, true)
     rescue ActiveRecord::StatementInvalid