diff options
Diffstat (limited to 'app/helpers')
-rw-r--r-- | app/helpers/filter_helper.rb | 78 |
1 files changed, 6 insertions, 72 deletions
diff --git a/app/helpers/filter_helper.rb b/app/helpers/filter_helper.rb index 77ffa98be..b2f1b00fc 100644 --- a/app/helpers/filter_helper.rb +++ b/app/helpers/filter_helper.rb @@ -1,83 +1,17 @@ module FilterHelper include Redisable - def phrase_filtered?(status, receiver_id, context) - if redis.sismember("filtered_statuses:#{receiver_id}", status.id) - return !(redis.hexists("custom_cw:#{receiver_id}", status.id) || redis.hexists("custom_cw:#{receiver_id}", "c#{status.conversation_id}")) - end - - filters = cached_filters(receiver_id).select { |filter| !filter.expired? } - - unless context.nil? - filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? } - end - - 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? + def phrase_filtered?(status, receiver_id) + return true if redis.sismember("filtered_statuses:#{receiver_id}", status.id) + return false unless CustomFilter.where(account_id: receiver_id).exists? status = status.reblog if status.reblog? - status_text = status.normalized_text - spoiler_text = status.spoiler_text - tags = status.tags.pluck(:name).join("\n") - descs = status.media_attachments.map { |a| a.description }.join("\n").strip - - 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 ||= regex.match(status_text).present? if filter.status_text - matched ||= regex.match(spoiler_text).present? if filter.spoiler && spoiler_text.present? - matched ||= regex.match(tags).present? if filter.tags && tags.present? - matched ||= regex.match(descs).present? if filter.desc && descs.present? - matched ||= status.media_attachments.all { |a| a.description.blank? } if filter.no_desc && status.media_attachments.any? - if matched - filter_thread(receiver_id, status.conversation_id) if filter.thread && filter.custom_cw.blank? - - unless filter.custom_cw.blank? - cw = if filter.override_cw || status.spoiler_text.blank? - filter.custom_cw - else - "[#{filter.custom_cw}] #{status.spoiler_text}".rstrip - end - - if filter.thread - redis.hset("custom_cw:#{receiver_id}", "c#{status.conversation_id}", cw) - else - redis.hset("custom_cw:#{receiver_id}", status.id, cw) - end - end - - redis.sadd("filtered_statuses:#{receiver_id}", status.id) - return filter.custom_cw.blank? - end + if Status.where(id: status.id).where("statuses.normalized_text ~ ANY(ARRAY(SELECT unaccent(lower(phrase)) FROM custom_filters WHERE account_id = ?))", receiver_id).exists? + redis.sadd("filtered_statuses:#{receiver_id}", status.id) + return true end false end - - def filter_thread(account_id, conversation_id) - return if Status.where(account_id: account_id, conversation_id: conversation_id).exists? - redis.sadd("filtered_threads:#{account_id}", conversation_id) - end - - def filtering_thread?(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 |