about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-04-27 19:19:32 -0500
committermultiple creatures <dev@multiple-creature.party>2019-05-21 03:16:22 -0500
commit26d90a36ff07aa905db63985886d5c08c934b7b6 (patch)
tree4a4ac8b60bcd3a02655465e00d8e885918f3959c /app
parent2423830e3c08d319317dd193b7d3ad450016fd4a (diff)
Custom filters: add ability to create filters that exclude or are exclusive to roars with attachments.
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/filters_controller.rb2
-rw-r--r--app/controllers/filters_controller.rb2
-rw-r--r--app/lib/feed_manager.rb6
-rw-r--r--app/models/custom_filter.rb20
-rw-r--r--app/serializers/rest/filter_serializer.rb2
-rw-r--r--app/views/filters/_fields.html.haml4
6 files changed, 24 insertions, 12 deletions
diff --git a/app/controllers/api/v1/filters_controller.rb b/app/controllers/api/v1/filters_controller.rb
index e5ebaff4d..496964394 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, :whole_word, context: [])
+    params.permit(:phrase, :expires_in, :irreversible, :whole_word, :exclude_media, :media_only, context: [])
   end
 end
diff --git a/app/controllers/filters_controller.rb b/app/controllers/filters_controller.rb
index f1e110d87..2f6f98272 100644
--- a/app/controllers/filters_controller.rb
+++ b/app/controllers/filters_controller.rb
@@ -58,7 +58,7 @@ class FiltersController < ApplicationController
   end
 
   def resource_params
-    params.require(:custom_filter).permit(:phrase, :expires_in, :irreversible, :whole_word, context: [])
+    params.require(:custom_filter).permit(:phrase, :expires_in, :irreversible, :whole_word, :exclude_media, :media_only, context: [])
   end
 
   def set_body_classes
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index 064903d71..bd418366c 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -206,6 +206,12 @@ class FeedManager
 
     active_filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
 
+    if status.media_attachments.any?
+      active_filters.delete_if { |filter| filter.exclude_media }
+    else
+      active_filters.delete_if { |filter| filter.media_only }
+    end
+
     active_filters.map! do |filter|
       if filter.whole_word
         sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : ''
diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb
index 342207a55..d4cb2206e 100644
--- a/app/models/custom_filter.rb
+++ b/app/models/custom_filter.rb
@@ -3,15 +3,17 @@
 #
 # Table name: custom_filters
 #
-#  id           :bigint(8)        not null, primary key
-#  account_id   :bigint(8)
-#  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
+#  id            :bigint(8)        not null, primary key
+#  account_id    :bigint(8)
+#  expires_at    :datetime
+#  phrase        :text             default(""), not null
+#  context       :string           default([]), not null, is an Array
+#  irreversible  :boolean          default(FALSE), not null
+#  created_at    :datetime         not null
+#  updated_at    :datetime         not null
+#  whole_word    :boolean          default(TRUE), not null
+#  exclude_media :boolean          default(FALSE), not null
+#  media_only    :boolean          default(FALSE), not null
 #
 
 class CustomFilter < ApplicationRecord
diff --git a/app/serializers/rest/filter_serializer.rb b/app/serializers/rest/filter_serializer.rb
index 57205630b..f83d55577 100644
--- a/app/serializers/rest/filter_serializer.rb
+++ b/app/serializers/rest/filter_serializer.rb
@@ -2,7 +2,7 @@
 
 class REST::FilterSerializer < ActiveModel::Serializer
   attributes :id, :phrase, :context, :whole_word, :expires_at,
-             :irreversible
+             :irreversible, :exclude_media, :media_only
 
   def id
     object.id.to_s
diff --git a/app/views/filters/_fields.html.haml b/app/views/filters/_fields.html.haml
index fb94a07fc..84b380f13 100644
--- a/app/views/filters/_fields.html.haml
+++ b/app/views/filters/_fields.html.haml
@@ -14,3 +14,7 @@
 
 .fields-group
   = f.input :whole_word, wrapper: :with_label
+
+.fields-group
+  = f.input :exclude_media, wrapper: :with_label
+  = f.input :media_only, wrapper: :with_label