The code powering m.abunchtell.com https://m.abunchtell.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

67 строки
1.4 KiB

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