about summary refs log tree commit diff
path: root/app/helpers/filter_helper.rb
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-08-15 22:40:20 -0500
committermultiple creatures <dev@multiple-creature.party>2019-08-15 22:40:20 -0500
commit7bbcf793bc75b44f2b20de77b7774759af60cd90 (patch)
treed8d441c2d8cf245b28651efa14f6460841a8e304 /app/helpers/filter_helper.rb
parentf783ec279d03b7b56c96af5e18e0dd8c7a0710a1 (diff)
custom filters now have an option to add or override content warnings; filter caching has been fixed
Diffstat (limited to 'app/helpers/filter_helper.rb')
-rw-r--r--app/helpers/filter_helper.rb24
1 files changed, 21 insertions, 3 deletions
diff --git a/app/helpers/filter_helper.rb b/app/helpers/filter_helper.rb
index 3de27168d..ca5b32d1a 100644
--- a/app/helpers/filter_helper.rb
+++ b/app/helpers/filter_helper.rb
@@ -1,7 +1,10 @@
 module FilterHelper
+  include Redisable
+
 	def phrase_filtered?(status, receiver_id, context)
-    filters = Rails.cache.fetch("filters:#{receiver_id}") { CustomFilter.where(account_id: receiver_id).active_irreversible.to_a }.to_a
+    return true if redis.sismember("filtered_statuses:#{receiver_id}", status.id)
 
+    filters = cached_filters(receiver_id)
     filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
 
     if status.media_attachments.any?
@@ -34,6 +37,17 @@ module FilterHelper
 
       if matched
         filter_thread(receiver_id, status.conversation_id) if filter.thread
+
+        unless filter.custom_cw.nil?
+          cw = if filter.override_cw || status.spoiler_text.blank?
+                 filter.custom_cw
+               else
+                 "[#{filter.custom_cw}] #{status.spoiler_text}".rstrip
+               end
+          redis.hset("custom_cw:#{receiver_id}", status.id, cw)
+        end
+
+        redis.sadd("filtered_statuses:#{receiver_id}", status.id)
         return true
       end
     end
@@ -43,10 +57,14 @@ module FilterHelper
 
   def filter_thread(account_id, conversation_id)
     return if Status.where(account_id: account_id, conversation_id: conversation_id).exists?
-    Redis.current.sadd("filtered_threads:#{account_id}", conversation_id)
+    redis.sadd("filtered_threads:#{account_id}", conversation_id)
   end
 
   def filtering_thread?(account_id, conversation_id)
-    Redis.current.sismember("filtered_threads:#{account_id}", conversation_id)
+    redis.sismember("filtered_threads:#{account_id}", conversation_id)
+  end
+
+  def cached_filters(account_id)
+    Rails.cache.fetch("filters:#{account_id}") { CustomFilter.where(account_id: account_id).to_a }.to_a
   end
 end