The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

72 lines
2.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. #
  16. class DomainBlock < ApplicationRecord
  17. include DomainNormalizable
  18. enum severity: [:silence, :suspend, :noop]
  19. validates :domain, presence: true, uniqueness: true
  20. has_many :accounts, foreign_key: :domain, primary_key: :domain
  21. delegate :count, to: :accounts, prefix: true
  22. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  23. class << self
  24. def suspend?(domain)
  25. !!rule_for(domain)&.suspend?
  26. end
  27. def silence?(domain)
  28. !!rule_for(domain)&.silence?
  29. end
  30. def reject_media?(domain)
  31. !!rule_for(domain)&.reject_media?
  32. end
  33. def reject_reports?(domain)
  34. !!rule_for(domain)&.reject_reports?
  35. end
  36. alias blocked? suspend?
  37. def rule_for(domain)
  38. return if domain.blank?
  39. uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
  40. segments = uri.normalized_host.split('.')
  41. variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
  42. where(domain: variants[0..-2]).order(Arel.sql('char_length(domain) desc')).first
  43. end
  44. end
  45. def stricter_than?(other_block)
  46. return true if suspend?
  47. return false if other_block.suspend? && (silence? || noop?)
  48. return false if other_block.silence? && noop?
  49. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  50. end
  51. def affected_accounts_count
  52. scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
  53. scope.count
  54. end
  55. end