domain_block.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 DomainNormalizable
  19. include DomainMaterializable
  20. enum severity: [:silence, :suspend, :noop]
  21. validates :domain, presence: true, uniqueness: true, domain: true
  22. has_many :accounts, foreign_key: :domain, primary_key: :domain
  23. delegate :count, to: :accounts, prefix: true
  24. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  25. scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
  26. scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), reject_media, domain')) }
  27. class << self
  28. def suspend?(domain)
  29. !!rule_for(domain)&.suspend?
  30. end
  31. def silence?(domain)
  32. !!rule_for(domain)&.silence?
  33. end
  34. def reject_media?(domain)
  35. !!rule_for(domain)&.reject_media?
  36. end
  37. def reject_reports?(domain)
  38. !!rule_for(domain)&.reject_reports?
  39. end
  40. alias blocked? suspend?
  41. def rule_for(domain)
  42. return if domain.blank?
  43. uri = Addressable::URI.new.tap { |u| u.host = domain.strip.gsub(/[\/]/, '') }
  44. segments = uri.normalized_host.split('.')
  45. variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
  46. where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
  47. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  48. nil
  49. end
  50. end
  51. def stricter_than?(other_block)
  52. return true if suspend?
  53. return false if other_block.suspend? && (silence? || noop?)
  54. return false if other_block.silence? && noop?
  55. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  56. end
  57. def affected_accounts_count
  58. scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
  59. scope.count
  60. end
  61. def public_domain
  62. return domain unless obfuscate?
  63. length = domain.size
  64. visible_ratio = length / 4
  65. domain.chars.map.with_index do |chr, i|
  66. if i > visible_ratio && i < length - visible_ratio && chr != '.'
  67. '*'
  68. else
  69. chr
  70. end
  71. end.join
  72. end
  73. def domain_digest
  74. Digest::SHA256.hexdigest(domain)
  75. end
  76. end