The code powering m.abunchtell.com https://m.abunchtell.com
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

73 rindas
2.1 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, domain: 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. scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
  24. class << self
  25. def suspend?(domain)
  26. !!rule_for(domain)&.suspend?
  27. end
  28. def silence?(domain)
  29. !!rule_for(domain)&.silence?
  30. end
  31. def reject_media?(domain)
  32. !!rule_for(domain)&.reject_media?
  33. end
  34. def reject_reports?(domain)
  35. !!rule_for(domain)&.reject_reports?
  36. end
  37. alias blocked? suspend?
  38. def rule_for(domain)
  39. return if domain.blank?
  40. uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
  41. segments = uri.normalized_host.split('.')
  42. variants = segments.map.with_index { |_, i| segments[i..-1].join('.') }
  43. where(domain: variants[0..-2]).order(Arel.sql('char_length(domain) desc')).first
  44. end
  45. end
  46. def stricter_than?(other_block)
  47. return true if suspend?
  48. return false if other_block.suspend? && (silence? || noop?)
  49. return false if other_block.silence? && noop?
  50. (reject_media || !other_block.reject_media) && (reject_reports || !other_block.reject_reports)
  51. end
  52. def affected_accounts_count
  53. scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
  54. scope.count
  55. end
  56. end