summaryrefslogtreecommitdiff
path: root/app/finders/todos_finder.rb
blob: 6481893a195063058e0ebed81fc649b14fad91bf (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# TodosFinder
#
# Used to filter Todos by set of params
#
# Arguments:
#   current_user - which user use
#   params:
#     action_id: integer
#     author_id: integer
#     project_id; integer
#     state: 'pending' (default) or 'done'
#     type: 'Issue' or 'MergeRequest'
#

class TodosFinder
  prepend FinderWithCrossProjectAccess
  include FinderMethods
  include Gitlab::Utils::StrongMemoize

  requires_cross_project_access unless: -> { project? }

  NONE = '0'.freeze

  attr_accessor :current_user, :params

  def initialize(current_user, params = {})
    @current_user = current_user
    @params = params
  end

  def execute
    items = current_user.todos
    items = by_action_id(items)
    items = by_action(items)
    items = by_author(items)
    items = by_state(items)
    items = by_type(items)
    items = by_group(items)
    # Filtering by project HAS TO be the last because we use
    # the project IDs yielded by the todos query thus far
    items = by_project(items)

    sort(items)
  end

  private

  def action_id?
    action_id.present? && Todo::ACTION_NAMES.key?(action_id.to_i)
  end

  def action_id
    params[:action_id]
  end

  def to_action_id
    Todo::ACTION_NAMES.key(action.to_sym)
  end

  def action?
    action.present? && to_action_id
  end

  def action
    params[:action]
  end

  def author?
    params[:author_id].present?
  end

  def author
    return @author if defined?(@author)

    @author =
      if author? && params[:author_id] != NONE
        User.find(params[:author_id])
      else
        nil
      end
  end

  def project?
    params[:project_id].present?
  end

  def group?
    params[:group_id].present?
  end

  def project
    return @project if defined?(@project)

    if project?
      @project = Project.find(params[:project_id])

      @project = nil if @project.pending_delete?
    else
      @project = nil
    end

    @project
  end

  def group
    strong_memoize(:group) do
      Group.find(params[:group_id])
    end
  end

  def type?
    type.present? && %w(Issue MergeRequest Epic).include?(type)
  end

  def type
    params[:type]
  end

  def sort(items)
    params[:sort] ? items.sort_by_attribute(params[:sort]) : items.order_id_desc
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def by_action(items)
    if action?
      items = items.where(action: to_action_id)
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # rubocop: disable CodeReuse/ActiveRecord
  def by_action_id(items)
    if action_id?
      items = items.where(action: action_id)
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # rubocop: disable CodeReuse/ActiveRecord
  def by_author(items)
    if author?
      items = items.where(author_id: author.try(:id))
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # rubocop: disable CodeReuse/ActiveRecord
  def by_project(items)
    if project?
      items = items.where(project: project)
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # rubocop: disable CodeReuse/ActiveRecord
  def by_group(items)
    if group?
      groups = group.self_and_descendants
      project_todos = items.where(project_id: Project.where(group: groups).select(:id))
      group_todos = items.where(group_id: groups.select(:id))

      union = Gitlab::SQL::Union.new([project_todos, group_todos])
      items = Todo.from("(#{union.to_sql}) #{Todo.table_name}")
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def by_state(items)
    case params[:state].to_s
    when 'done'
      items.done
    else
      items.pending
    end
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def by_type(items)
    if type?
      items = items.where(target_type: type)
    end

    items
  end
  # rubocop: enable CodeReuse/ActiveRecord
end