about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2018-07-09 02:22:09 +0200
committerEugen Rochko <eugen@zeonfederated.com>2018-07-09 02:22:09 +0200
commit1ca4e51eb38de6de81cedf3ddcdaa626f1d1c569 (patch)
tree3cf44ef95b178d233fd34d2ec2aae2a7021c159d /app
parent451e585b9720c61ef9a155a16484b25ded79d512 (diff)
Add option to not consider word boundaries when processing keyword filtering (#7975)
* Add option to not consider word boundaries when filtering phrases

* Add a few tests for keyword/phrase filtering
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/filters_controller.rb2
-rw-r--r--app/javascript/mastodon/selectors/index.js5
-rw-r--r--app/lib/feed_manager.rb11
-rw-r--r--app/models/custom_filter.rb1
-rw-r--r--app/serializers/rest/filter_serializer.rb2
-rw-r--r--app/views/filters/_fields.html.haml3
6 files changed, 20 insertions, 4 deletions
diff --git a/app/controllers/api/v1/filters_controller.rb b/app/controllers/api/v1/filters_controller.rb
index a98080d1d..e5ebaff4d 100644
--- a/app/controllers/api/v1/filters_controller.rb
+++ b/app/controllers/api/v1/filters_controller.rb
@@ -43,6 +43,6 @@ class Api::V1::FiltersController < Api::BaseController
   end
 
   def resource_params
-    params.permit(:phrase, :expires_in, :irreversible, context: [])
+    params.permit(:phrase, :expires_in, :irreversible, :whole_word, context: [])
   end
 end
diff --git a/app/javascript/mastodon/selectors/index.js b/app/javascript/mastodon/selectors/index.js
index 7aa7569a0..d0212c379 100644
--- a/app/javascript/mastodon/selectors/index.js
+++ b/app/javascript/mastodon/selectors/index.js
@@ -45,7 +45,10 @@ export const regexFromFilters = filters => {
     return null;
   }
 
-  return new RegExp(filters.map(filter => escapeRegExp(filter.get('phrase'))).map(expr => `\\b${expr}\\b`).join('|'), 'i');
+  return new RegExp(filters.map(filter => {
+    let expr = escapeRegExp(filter.get('phrase'));
+    return filter.get('whole_word') ? `\\b${expr}\\b` : expr;
+  }).join('|'), 'i');
 };
 
 export const makeGetStatus = () => {
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index 55c72d0ea..c247ab21d 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -200,7 +200,16 @@ class FeedManager
     active_filters = Rails.cache.fetch("filters:#{receiver_id}") { CustomFilter.where(account_id: receiver_id).active_irreversible.to_a }.to_a
 
     active_filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
-    active_filters.map! { |filter| Regexp.new("\\b#{Regexp.escape(filter.phrase)}\\b", true) }
+    active_filters.map! do |filter|
+      if filter.whole_word
+        sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : ''
+        eb = filter.phrase =~ /[[:word:]]\Z/ ? '\b' : ''
+
+        /(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/
+      else
+        /#{Regexp.escape(filter.phrase)}/i
+      end
+    end
 
     return false if active_filters.empty?
 
diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb
index 2c1a54375..342207a55 100644
--- a/app/models/custom_filter.rb
+++ b/app/models/custom_filter.rb
@@ -8,6 +8,7 @@
 #  expires_at   :datetime
 #  phrase       :text             default(""), not null
 #  context      :string           default([]), not null, is an Array
+#  whole_word   :boolean          default(TRUE), not null
 #  irreversible :boolean          default(FALSE), not null
 #  created_at   :datetime         not null
 #  updated_at   :datetime         not null
diff --git a/app/serializers/rest/filter_serializer.rb b/app/serializers/rest/filter_serializer.rb
index 51340aa79..3134be371 100644
--- a/app/serializers/rest/filter_serializer.rb
+++ b/app/serializers/rest/filter_serializer.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
 class REST::FilterSerializer < ActiveModel::Serializer
-  attributes :id, :phrase, :context, :expires_at,
+  attributes :id, :phrase, :context, :whole_word, :expires_at,
              :irreversible
 end
diff --git a/app/views/filters/_fields.html.haml b/app/views/filters/_fields.html.haml
index af5d648b8..a5a3f0337 100644
--- a/app/views/filters/_fields.html.haml
+++ b/app/views/filters/_fields.html.haml
@@ -8,4 +8,7 @@
   = f.input :irreversible, wrapper: :with_label
 
 .fields-group
+  = f.input :whole_word, wrapper: :with_label
+
+.fields-group
   = f.input :expires_in, wrapper: :with_label, collection: [30.minutes, 1.hour, 6.hours, 12.hours, 1.day, 1.week].map(&:to_i), label_method: lambda { |i| I18n.t("invites.expires_in.#{i}") }, prompt: I18n.t('invites.expires_in_prompt')