about summary refs log tree commit diff
path: root/app/models/relationship_filter.rb
blob: 9135ff144c9a66ab9095c97fce61fa3fe37907ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

class RelationshipFilter
  KEYS = %i(
    relationship
    status
    by_domain
    activity
    order
    location
  ).freeze

  attr_reader :params, :account

  def initialize(account, params)
    @account = account
    @params  = params

    set_defaults!
  end

  def results
    scope = scope_for('relationship', params['relationship'].to_s.strip)

    params.each do |key, value|
      next if %w(relationship page).include?(key)

      scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
    end

    scope
  end

  private

  def set_defaults!
    params['relationship'] = 'following' if params['relationship'].blank?
    params['order']        = 'recent' if params['order'].blank?
  end

  def scope_for(key, value)
    case key
    when 'relationship'
      relationship_scope(value)
    when 'by_domain'
      by_domain_scope(value)
    when 'location'
      location_scope(value)
    when 'status'
      status_scope(value)
    when 'order'
      order_scope(value)
    when 'activity'
      activity_scope(value)
    else
      raise "Unknown filter: #{key}"
    end
  end

  def relationship_scope(value)
    case value
    when 'following'
      account.following.eager_load(:account_stat).reorder(nil)
    when 'followed_by'
      account.followers.eager_load(:account_stat).reorder(nil)
    when 'mutual'
      account.followers.eager_load(:account_stat).reorder(nil).merge(Account.where(id: account.following))
    when 'invited'
      Account.joins(user: :invite).merge(Invite.where(user: account.user)).eager_load(:account_stat).reorder(nil)
    else
      raise "Unknown relationship: #{value}"
    end
  end

  def by_domain_scope(value)
    Account.where(domain: value)
  end

  def location_scope(value)
    case value
    when 'local'
      Account.local
    when 'remote'
      Account.remote
    else
      raise "Unknown location: #{value}"
    end
  end

  def status_scope(value)
    case value
    when 'moved'
      Account.where.not(moved_to_account_id: nil)
    when 'primary'
      Account.where(moved_to_account_id: nil)
    else
      raise "Unknown status: #{value}"
    end
  end

  def order_scope(value)
    case value
    when 'active'
      Account.by_recent_status
    when 'recent'
      params[:relationship] == 'invited' ? Account.recent : Follow.recent
    else
      raise "Unknown order: #{value}"
    end
  end

  def activity_scope(value)
    case value
    when 'dormant'
      AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago)))
    else
      raise "Unknown activity: #{value}"
    end
  end
end