domain_block.rb 3.0 KB

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