domain_block.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: 0, suspend: 1, noop: 2 }
  22. validates :domain, presence: true, uniqueness: true, domain: true
  23. has_many :accounts, foreign_key: :domain, primary_key: :domain, inverse_of: false
  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 :with_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
  28. scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
  29. def to_log_human_identifier
  30. domain
  31. end
  32. def policies
  33. if suspend?
  34. [:suspend]
  35. else
  36. [severity.to_sym, reject_media? ? :reject_media : nil, reject_reports? ? :reject_reports : nil].reject { |policy| policy == :noop || policy.nil? }
  37. end
  38. end
  39. class << self
  40. def suspend?(domain)
  41. !!rule_for(domain)&.suspend?
  42. end
  43. def silence?(domain)
  44. !!rule_for(domain)&.silence?
  45. end
  46. def reject_media?(domain)
  47. !!rule_for(domain)&.reject_media?
  48. end
  49. def reject_reports?(domain)
  50. !!rule_for(domain)&.reject_reports?
  51. end
  52. alias blocked? suspend?
  53. def rule_for(domain)
  54. return if domain.blank?
  55. uri = Addressable::URI.new.tap { |u| u.host = domain.strip.delete('/') }
  56. segments = uri.normalized_host.split('.')
  57. variants = segments.map.with_index { |_, i| segments[i..].join('.') }
  58. where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
  59. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  60. nil
  61. end
  62. end
  63. def stricter_than?(other_block)
  64. return true if suspend?
  65. return false if other_block.suspend? && (silence? || noop?)
  66. return false if other_block.silence? && noop?
  67. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  68. end
  69. def affected_accounts_count
  70. scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
  71. scope.count
  72. end
  73. def public_domain
  74. return domain unless obfuscate?
  75. length = domain.size
  76. visible_ratio = length / 4
  77. domain.chars.map.with_index do |chr, i|
  78. if i > visible_ratio && i < length - visible_ratio && chr != '.'
  79. '*'
  80. else
  81. chr
  82. end
  83. end.join
  84. end
  85. def domain_digest
  86. Digest::SHA256.hexdigest(domain)
  87. end
  88. end