about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb1
-rw-r--r--app/models/custom_emoji.rb1
-rw-r--r--app/models/domain_block.rb36
3 files changed, 33 insertions, 5 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 28ba35148..f50be9d4c 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -122,6 +122,7 @@ class Account < ApplicationRecord
   scope :popular, -> { order('account_stats.followers_count desc') }
   scope :without_hidden, -> { where(hidden: false) }
   scope :without_unlisted, -> { where(unlisted: false) }
+  scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches('%.' + domain))) }
 
   delegate :email,
            :unconfirmed_email,
diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb
index 1aa1b5e34..33f59bc3d 100644
--- a/app/models/custom_emoji.rb
+++ b/app/models/custom_emoji.rb
@@ -39,6 +39,7 @@ class CustomEmoji < ApplicationRecord
   scope :local,      -> { where(domain: nil) }
   scope :remote,     -> { where.not(domain: nil) }
   scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
+  scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches('%.' + domain))) }
 
   remotable_attachment :image, LIMIT
 
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)