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.

120 lines
2.6 KiB

  1. # frozen_string_literal: true
  2. class RelationshipFilter
  3. KEYS = %i(
  4. relationship
  5. status
  6. by_domain
  7. activity
  8. order
  9. location
  10. ).freeze
  11. attr_reader :params, :account
  12. def initialize(account, params)
  13. @account = account
  14. @params = params
  15. set_defaults!
  16. end
  17. def results
  18. scope = scope_for('relationship', params['relationship'].to_s.strip)
  19. params.each do |key, value|
  20. next if %w(relationship page).include?(key)
  21. scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
  22. end
  23. scope
  24. end
  25. private
  26. def set_defaults!
  27. params['relationship'] = 'following' if params['relationship'].blank?
  28. params['order'] = 'recent' if params['order'].blank?
  29. end
  30. def scope_for(key, value)
  31. case key
  32. when 'relationship'
  33. relationship_scope(value)
  34. when 'by_domain'
  35. by_domain_scope(value)
  36. when 'location'
  37. location_scope(value)
  38. when 'status'
  39. status_scope(value)
  40. when 'order'
  41. order_scope(value)
  42. when 'activity'
  43. activity_scope(value)
  44. else
  45. raise "Unknown filter: #{key}"
  46. end
  47. end
  48. def relationship_scope(value)
  49. case value
  50. when 'following'
  51. account.following.eager_load(:account_stat).reorder(nil)
  52. when 'followed_by'
  53. account.followers.eager_load(:account_stat).reorder(nil)
  54. when 'mutual'
  55. account.followers.eager_load(:account_stat).reorder(nil).merge(Account.where(id: account.following))
  56. when 'invited'
  57. Account.joins(user: :invite).merge(Invite.where(user: account.user)).eager_load(:account_stat).reorder(nil)
  58. else
  59. raise "Unknown relationship: #{value}"
  60. end
  61. end
  62. def by_domain_scope(value)
  63. Account.where(domain: value)
  64. end
  65. def location_scope(value)
  66. case value
  67. when 'local'
  68. Account.local
  69. when 'remote'
  70. Account.remote
  71. else
  72. raise "Unknown location: #{value}"
  73. end
  74. end
  75. def status_scope(value)
  76. case value
  77. when 'moved'
  78. Account.where.not(moved_to_account_id: nil)
  79. when 'primary'
  80. Account.where(moved_to_account_id: nil)
  81. else
  82. raise "Unknown status: #{value}"
  83. end
  84. end
  85. def order_scope(value)
  86. case value
  87. when 'active'
  88. Account.by_recent_status
  89. when 'recent'
  90. params[:relationship] == 'invited' ? Account.recent : Follow.recent
  91. else
  92. raise "Unknown order: #{value}"
  93. end
  94. end
  95. def activity_scope(value)
  96. case value
  97. when 'dormant'
  98. AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago)))
  99. else
  100. raise "Unknown activity: #{value}"
  101. end
  102. end
  103. end