todos_finder.rb 3.68 KB
Newer Older
1 2
# frozen_string_literal: true

3
# TodosFinder
4
#
5
# Used to filter Todos by set of params
6 7 8 9 10 11 12
#
# Arguments:
#   current_user - which user use
#   params:
#     action_id: integer
#     author_id: integer
#     project_id; integer
13
#     state: 'pending' (default) or 'done'
14 15 16
#     type: 'Issue' or 'MergeRequest'
#

17
class TodosFinder
18 19
  prepend FinderWithCrossProjectAccess
  include FinderMethods
20
  include Gitlab::Utils::StrongMemoize
21 22 23

  requires_cross_project_access unless: -> { project? }

Douwe Maan's avatar
Douwe Maan committed
24
  NONE = '0'.freeze
25 26 27

  attr_accessor :current_user, :params

28
  def initialize(current_user, params = {})
29 30 31 32 33
    @current_user = current_user
    @params = params
  end

  def execute
34
    items = current_user.todos
35
    items = by_action_id(items)
36
    items = by_action(items)
37 38 39
    items = by_author(items)
    items = by_state(items)
    items = by_type(items)
40
    items = by_group(items)
41 42 43
    # 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)
44

Felipe Artur's avatar
Felipe Artur committed
45
    sort(items)
46 47 48 49 50
  end

  private

  def action_id?
51
    action_id.present? && Todo::ACTION_NAMES.key?(action_id.to_i)
52 53 54 55 56 57
  end

  def action_id
    params[:action_id]
  end

58 59 60 61 62 63 64 65 66 67 68 69
  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

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  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

89 90 91 92
  def group?
    params[:group_id].present?
  end

93 94 95 96 97 98
  def project
    return @project if defined?(@project)

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

99
      @project = nil if @project.pending_delete?
100 101 102 103 104 105 106
    else
      @project = nil
    end

    @project
  end

107 108 109
  def group
    strong_memoize(:group) do
      Group.find(params[:group_id])
110 111 112
    end
  end

113
  def type?
114
    type.present? && %w(Issue MergeRequest Epic).include?(type)
115 116 117 118 119 120
  end

  def type
    params[:type]
  end

Felipe Artur's avatar
Felipe Artur committed
121
  def sort(items)
122
    params[:sort] ? items.sort_by_attribute(params[:sort]) : items.order_id_desc
Felipe Artur's avatar
Felipe Artur committed
123 124
  end

125
  # rubocop: disable CodeReuse/ActiveRecord
126 127 128 129 130 131 132
  def by_action(items)
    if action?
      items = items.where(action: to_action_id)
    end

    items
  end
133
  # rubocop: enable CodeReuse/ActiveRecord
134

135
  # rubocop: disable CodeReuse/ActiveRecord
136 137 138 139 140 141 142
  def by_action_id(items)
    if action_id?
      items = items.where(action: action_id)
    end

    items
  end
143
  # rubocop: enable CodeReuse/ActiveRecord
144

145
  # rubocop: disable CodeReuse/ActiveRecord
146 147 148 149 150 151 152
  def by_author(items)
    if author?
      items = items.where(author_id: author.try(:id))
    end

    items
  end
153
  # rubocop: enable CodeReuse/ActiveRecord
154

155
  # rubocop: disable CodeReuse/ActiveRecord
156 157
  def by_project(items)
    if project?
158 159 160 161 162
      items = items.where(project: project)
    end

    items
  end
163
  # rubocop: enable CodeReuse/ActiveRecord
164

165
  # rubocop: disable CodeReuse/ActiveRecord
166
  def by_group(items)
167
    return items unless group?
Jarka Kadlecová's avatar
Jarka Kadlecová committed
168

169 170 171
    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))
172

173
    Todo.from_union([project_todos, group_todos])
174
  end
175
  # rubocop: enable CodeReuse/ActiveRecord
176

177
  def by_state(items)
178
    case params[:state].to_s
179 180 181 182 183 184 185
    when 'done'
      items.done
    else
      items.pending
    end
  end

186
  # rubocop: disable CodeReuse/ActiveRecord
187 188 189 190 191 192 193
  def by_type(items)
    if type?
      items = items.where(target_type: type)
    end

    items
  end
194
  # rubocop: enable CodeReuse/ActiveRecord
195
end