custom_filter.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: custom_filters
  5. #
  6. # id :bigint not null, primary key
  7. # account_id :bigint
  8. # expires_at :datetime
  9. # phrase :text default(""), not null
  10. # context :string default([]), not null, is an Array
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. # action :integer default(0), not null
  14. #
  15. class CustomFilter < ApplicationRecord
  16. self.ignored_columns = %w(whole_word irreversible)
  17. alias_attribute :title, :phrase
  18. alias_attribute :filter_action, :action
  19. VALID_CONTEXTS = %w(
  20. home
  21. notifications
  22. public
  23. thread
  24. account
  25. ).freeze
  26. include Expireable
  27. include Redisable
  28. enum action: [:warn, :hide], _suffix: :action
  29. belongs_to :account
  30. has_many :keywords, class_name: 'CustomFilterKeyword', foreign_key: :custom_filter_id, inverse_of: :custom_filter, dependent: :destroy
  31. accepts_nested_attributes_for :keywords, reject_if: :all_blank, allow_destroy: true
  32. validates :title, :context, presence: true
  33. validate :context_must_be_valid
  34. before_validation :clean_up_contexts
  35. before_save :prepare_cache_invalidation!
  36. before_destroy :prepare_cache_invalidation!
  37. after_commit :invalidate_cache!
  38. def expires_in
  39. return @expires_in if defined?(@expires_in)
  40. return nil if expires_at.nil?
  41. [30.minutes, 1.hour, 6.hours, 12.hours, 1.day, 1.week].find { |expires_in| expires_in.from_now >= expires_at }
  42. end
  43. def irreversible=(value)
  44. self.action = value ? :hide : :warn
  45. end
  46. def irreversible?
  47. hide_action?
  48. end
  49. def self.cached_filters_for(account_id)
  50. active_filters = Rails.cache.fetch("filters:v3:#{account_id}") do
  51. scope = CustomFilterKeyword.includes(:custom_filter).where(custom_filter: { account_id: account_id }).where(Arel.sql('expires_at IS NULL OR expires_at > NOW()'))
  52. scope.to_a.group_by(&:custom_filter).map do |filter, keywords|
  53. keywords.map! do |keyword|
  54. if keyword.whole_word
  55. sb = /\A[[:word:]]/.match?(keyword.keyword) ? '\b' : ''
  56. eb = /[[:word:]]\z/.match?(keyword.keyword) ? '\b' : ''
  57. /(?mix:#{sb}#{Regexp.escape(keyword.keyword)}#{eb})/
  58. else
  59. /#{Regexp.escape(keyword.keyword)}/i
  60. end
  61. end
  62. [filter, { keywords: Regexp.union(keywords) }]
  63. end
  64. end.to_a
  65. active_filters.select { |custom_filter, _| !custom_filter.expired? }
  66. end
  67. def prepare_cache_invalidation!
  68. @should_invalidate_cache = true
  69. end
  70. def invalidate_cache!
  71. return unless @should_invalidate_cache
  72. @should_invalidate_cache = false
  73. Rails.cache.delete("filters:v3:#{account_id}")
  74. redis.publish("timeline:#{account_id}", Oj.dump(event: :filters_changed))
  75. redis.publish("timeline:system:#{account_id}", Oj.dump(event: :filters_changed))
  76. end
  77. private
  78. def clean_up_contexts
  79. self.context = Array(context).map(&:strip).filter_map(&:presence)
  80. end
  81. def context_must_be_valid
  82. errors.add(:context, I18n.t('filters.errors.invalid_context')) if context.empty? || context.any? { |c| !VALID_CONTEXTS.include?(c) }
  83. end
  84. end