diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2019-06-22 00:13:10 +0200 |
---|---|---|
committer | multiple creatures <dev@multiple-creature.party> | 2020-02-21 01:48:49 -0600 |
commit | 0a238f34b39b606d5f20e1ef1fbdd2a8c56951db (patch) | |
tree | 9192e83c420e085c16c8a65fe6077ca30046dd44 /app/models/domain_block.rb | |
parent | e6e69f091e3414b29271040926cc1d2e7c5f0e41 (diff) |
port tootsuite#11138 to monsterfork: Change domain blocks to automatically support subdomains
* Change domain blocks to automatically support subdomains If a more authoritative domain is blocked (example.com), then the same block will be applied to a subdomain (foo.example.com) * Match subdomains of existing accounts when blocking/unblocking domains * Improve code style
Diffstat (limited to 'app/models/domain_block.rb')
-rw-r--r-- | app/models/domain_block.rb | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 1d0b25772..05264190c 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -33,12 +33,38 @@ class DomainBlock < ApplicationRecord before_save :set_processing - def self.blocked?(domain) - suspend.where(domain: domain).or(suspend.where('domain LIKE ?', "%.#{domain}")).exists? - end + class << self + def suspend?(domain) + !!rule_for(domain)&.suspend? + end + + def silence?(domain) + !!rule_for(domain)&.silence? + end + + def reject_media?(domain) + !!rule_for(domain)&.reject_media? + end + + def reject_reports?(domain) + !!rule_for(domain)&.reject_reports? + end + + def force_unlisted?(domain) + !!rule_for(domain)&.severity == 'force_unlisted' + end + + alias blocked? suspend? + + def rule_for(domain) + return if domain.blank? + + uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') } + segments = uri.normalized_host.split('.') + variants = segments.map.with_index { |_, i| segments[i..-1].join('.') } - def self.force_unlisted?(domain) - where(domain: domain, severity: :force_unlisted).exists? + where(domain: variants[0..-2]).order(Arel.sql('char_length(domain) desc')).first + end end def stricter_than?(other_block) |