where i deviate from standard mastodon assets
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.

account_filter.rb 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # frozen_string_literal: true
  2. class AccountFilter
  3. KEYS = %i(
  4. local
  5. remote
  6. by_domain
  7. active
  8. pending
  9. silenced
  10. suspended
  11. username
  12. display_name
  13. email
  14. ip
  15. staff
  16. ).freeze
  17. attr_reader :params
  18. def initialize(params)
  19. @params = params
  20. set_defaults!
  21. end
  22. def results
  23. scope = Account.recent.includes(:user)
  24. params.each do |key, value|
  25. scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
  26. end
  27. scope
  28. end
  29. private
  30. def set_defaults!
  31. params['local'] = '1' if params['remote'].blank?
  32. params['active'] = '1' if params['suspended'].blank? && params['silenced'].blank? && params['pending'].blank?
  33. end
  34. def scope_for(key, value)
  35. case key.to_s
  36. when 'local'
  37. Account.local
  38. when 'remote'
  39. Account.remote
  40. when 'by_domain'
  41. Account.where(domain: value)
  42. when 'active'
  43. Account.without_suspended
  44. when 'pending'
  45. accounts_with_users.merge User.pending
  46. when 'disabled'
  47. accounts_with_users.merge User.disabled
  48. when 'silenced'
  49. Account.silenced
  50. when 'suspended'
  51. Account.suspended
  52. when 'username'
  53. Account.matches_username(value)
  54. when 'display_name'
  55. Account.matches_display_name(value)
  56. when 'email'
  57. accounts_with_users.merge User.matches_email(value)
  58. when 'ip'
  59. valid_ip?(value) ? accounts_with_users.merge(User.matches_ip(value)) : Account.none
  60. when 'staff'
  61. accounts_with_users.merge User.staff
  62. else
  63. raise "Unknown filter: #{key}"
  64. end
  65. end
  66. def accounts_with_users
  67. Account.joins(:user)
  68. end
  69. def valid_ip?(value)
  70. IPAddr.new(value) && true
  71. rescue IPAddr::InvalidAddressError
  72. false
  73. end
  74. end