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.
 
 
 
 

62 line
1.2 KiB

  1. # frozen_string_literal: true
  2. class AccountFilter
  3. attr_reader :params
  4. def initialize(params)
  5. @params = params
  6. end
  7. def results
  8. scope = Account.alphabetic
  9. params.each do |key, value|
  10. scope.merge!(scope_for(key, value)) if value.present?
  11. end
  12. scope
  13. end
  14. private
  15. def scope_for(key, value)
  16. case key.to_s
  17. when 'local'
  18. Account.local
  19. when 'remote'
  20. Account.remote
  21. when 'by_domain'
  22. Account.where(domain: value)
  23. when 'silenced'
  24. Account.silenced
  25. when 'recent'
  26. Account.recent
  27. when 'suspended'
  28. Account.suspended
  29. when 'username'
  30. Account.matches_username(value)
  31. when 'display_name'
  32. Account.matches_display_name(value)
  33. when 'email'
  34. accounts_with_users.merge User.matches_email(value)
  35. when 'ip'
  36. if valid_ip?(value)
  37. accounts_with_users.merge User.with_recent_ip_address(value)
  38. else
  39. Account.default_scoped
  40. end
  41. else
  42. raise "Unknown filter: #{key}"
  43. end
  44. end
  45. def accounts_with_users
  46. Account.joins(:user)
  47. end
  48. def valid_ip?(value)
  49. IPAddr.new(value)
  50. true
  51. rescue IPAddr::InvalidAddressError
  52. false
  53. end
  54. end