1
0

domain_block.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, dependent: nil
  24. delegate :count, to: :accounts, prefix: true
  25. scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]) }
  26. scope :with_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
  27. scope :by_severity, -> { in_order_of(:severity, %w(noop silence suspend)).order(: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]
  36. .reject { |policy| policy == :noop }
  37. .compact
  38. end
  39. end
  40. class << self
  41. def suspend?(domain)
  42. !!rule_for(domain)&.suspend?
  43. end
  44. def silence?(domain)
  45. !!rule_for(domain)&.silence?
  46. end
  47. def reject_media?(domain)
  48. !!rule_for(domain)&.reject_media?
  49. end
  50. def reject_reports?(domain)
  51. !!rule_for(domain)&.reject_reports?
  52. end
  53. alias blocked? suspend?
  54. def rule_for(domain)
  55. return if domain.blank?
  56. uri = Addressable::URI.new.tap { |u| u.host = domain.strip.delete('/') }
  57. segments = uri.normalized_host.split('.')
  58. variants = segments.map.with_index { |_, i| segments[i..].join('.') }
  59. where(domain: variants).by_domain_length.first
  60. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  61. nil
  62. end
  63. end
  64. def stricter_than?(other_block)
  65. return true if suspend?
  66. return false if other_block.suspend? && (silence? || noop?)
  67. return false if other_block.silence? && noop?
  68. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  69. end
  70. def public_domain
  71. return domain unless obfuscate?
  72. length = domain.size
  73. visible_ratio = length / 4
  74. domain.chars.map.with_index do |chr, i|
  75. if i > visible_ratio && i < length - visible_ratio && chr != '.'
  76. '*'
  77. else
  78. chr
  79. end
  80. end.join
  81. end
  82. def domain_digest
  83. Digest::SHA256.hexdigest(domain)
  84. end
  85. end