You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0 KiB

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