logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

account_filter.rb (1257B)


  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. when 'staff'
  42. accounts_with_users.merge User.staff
  43. else
  44. raise "Unknown filter: #{key}"
  45. end
  46. end
  47. def accounts_with_users
  48. Account.joins(:user)
  49. end
  50. def valid_ip?(value)
  51. IPAddr.new(value)
  52. true
  53. rescue IPAddr::InvalidAddressError
  54. false
  55. end
  56. end