domain_block.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: domain_blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # severity :integer default("silence")
  11. # reject_media :boolean default(FALSE), not null
  12. # reject_reports :boolean default(FALSE), not null
  13. # private_comment :text
  14. # public_comment :text
  15. # obfuscate :boolean default(FALSE), not null
  16. #
  17. class DomainBlock < ApplicationRecord
  18. include Paginable
  19. include DomainNormalizable
  20. include DomainMaterializable
  21. enum severity: [:silence, :suspend, :noop]
  22. validates :domain, presence: true, uniqueness: true, domain: true
  23. has_many :accounts, foreign_key: :domain, primary_key: :domain
  24. delegate :count, to: :accounts, prefix: true
  25. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  26. scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]) }
  27. scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
  28. def to_log_human_identifier
  29. domain
  30. end
  31. def policies
  32. if suspend?
  33. [:suspend]
  34. else
  35. [severity.to_sym, reject_media? ? :reject_media : nil, reject_reports? ? :reject_reports : nil].reject { |policy| policy == :noop || policy.nil? }
  36. end
  37. end
  38. class << self
  39. def suspend?(domain)
  40. !!rule_for(domain)&.suspend?
  41. end
  42. def silence?(domain)
  43. !!rule_for(domain)&.silence?
  44. end
  45. def reject_media?(domain)
  46. !!rule_for(domain)&.reject_media?
  47. end
  48. def reject_reports?(domain)
  49. !!rule_for(domain)&.reject_reports?
  50. end
  51. alias blocked? suspend?
  52. def rule_for(domain)
  53. return if domain.blank?
  54. uri = Addressable::URI.new.tap { |u| u.host = domain.strip.gsub(/[\/]/, '') }
  55. segments = uri.normalized_host.split('.')
  56. variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
  57. where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
  58. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  59. nil
  60. end
  61. end
  62. def stricter_than?(other_block)
  63. return true if suspend?
  64. return false if other_block.suspend? && (silence? || noop?)
  65. return false if other_block.silence? && noop?
  66. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  67. end
  68. def affected_accounts_count
  69. scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
  70. scope.count
  71. end
  72. def public_domain
  73. return domain unless obfuscate?
  74. length = domain.size
  75. visible_ratio = length / 4
  76. domain.chars.map.with_index do |chr, i|
  77. if i > visible_ratio && i < length - visible_ratio && chr != '.'
  78. '*'
  79. else
  80. chr
  81. end
  82. end.join
  83. end
  84. def domain_digest
  85. Digest::SHA256.hexdigest(domain)
  86. end
  87. end