about summary refs log tree commit diff
path: root/app/helpers
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-08-02 02:30:35 -0500
committermultiple creatures <dev@multiple-creature.party>2019-08-02 02:30:35 -0500
commit65c42e5398304273b54916cc90441ade23317d85 (patch)
tree466c63aa34c1680c49e66a3c7b0961297f1d8929 /app/helpers
parent3813810cac8eb138ed1819058d122503628d6cba (diff)
filters now have options to separately match post text, content warnings/titles, & hashtags + option to filter threads containing a matching post
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/filter_helper.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/app/helpers/filter_helper.rb b/app/helpers/filter_helper.rb
new file mode 100644
index 000000000..861f37551
--- /dev/null
+++ b/app/helpers/filter_helper.rb
@@ -0,0 +1,51 @@
+module FilterHelper
+	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
+
+    filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
+
+    if status.media_attachments.any?
+      filters.delete_if { |filter| filter.exclude_media }
+    else
+      filters.delete_if { |filter| filter.media_only }
+    end
+
+    return false if filters.empty?
+
+    status = status.reblog if status.reblog?
+    status_text = Formatter.instance.plaintext(status)
+    spoiler_text = status.spoiler_text
+    tags = status.tags.pluck(:name).join("\n")
+
+    filters.each do |filter|
+      if filter.whole_word
+        sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : ''
+        eb = filter.phrase =~ /[[:word:]]\z/ ? '\b' : ''
+
+        regex = /(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/
+      else
+        regex = /#{Regexp.escape(filter.phrase)}/i
+      end
+
+      matched = false
+      matched = true unless regex.match(status_text).nil?
+      matched = true unless spoiler_text.blank? || regex.match(spoiler_text).nil?
+      matched = true unless tags.empty? || tags_regex.match(tags).nil?
+
+      if matched
+        filter_thread(receiver_id, status.conversation_id) if filter.thread
+        return true
+      end
+    end
+
+    false
+  end
+
+  def filter_thread(account_id, conversation_id)
+    Redis.cache.sadd("filtered_threads:#{account_id}", conversation_id)
+  end
+
+  def filtering_thread?(account_id, conversation_id)
+    Redis.cache.sismember("filtered_threads:#{account_id}", conversation_id)
+  end
+end