File: todos_controller.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (131 lines) | stat: -rw-r--r-- 3,526 bytes parent folder | download
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
# frozen_string_literal: true

class Dashboard::TodosController < Dashboard::ApplicationController
  include ActionView::Helpers::NumberHelper
  include PaginatedCollection
  include Gitlab::Utils::StrongMemoize

  before_action :authorize_read_project!, only: :index
  before_action :authorize_read_group!, only: :index
  before_action :find_todos, only: [:index, :destroy_all]

  feature_category :notifications
  urgency :low

  def index
    @sort = pagination_params[:sort]
    @todos = @todos.page(pagination_params[:page])
    @todos = @todos.with_entity_associations

    return if redirect_out_of_range(@todos, todos_page_count(@todos))

    @allowed_todos = ::Todos::AllowedTargetFilterService.new(@todos, current_user).execute
  end

  def vue
    redirect_to(dashboard_todos_path, status: :found) unless Feature.enabled?(:todos_vue_application, current_user)
  end

  def destroy
    todo = current_user.todos.find(params[:id])

    TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :mark_done)

    respond_to do |format|
      format.html do
        redirect_to dashboard_todos_path, status: :found, notice: _('To-do item successfully marked as done.')
      end
      format.js { head :ok }
      format.json { render json: todos_counts }
    end
  end

  def destroy_all
    updated_ids = TodoService.new.resolve_todos(@todos, current_user, resolved_by_action: :mark_all_done)

    respond_to do |format|
      format.html do
        redirect_to dashboard_todos_path, status: :found, notice: _('Everything on your to-do list is marked as done.')
      end
      format.js { head :ok }
      format.json { render json: todos_counts.merge(updated_ids: updated_ids) }
    end
  end

  def restore
    TodoService.new.restore_todo(current_user.todos.find(params[:id]), current_user)

    render json: todos_counts
  end

  def bulk_restore
    TodoService.new.restore_todos(current_user.todos.id_in(params[:ids]), current_user)

    render json: todos_counts
  end

  private

  def authorize_read_project!
    project_id = params[:project_id]

    return unless project_id.present?

    project = Project.find(project_id)
    render_404 unless can?(current_user, :read_project, project)
  end

  def authorize_read_group!
    group_id = params[:group_id]

    return unless group_id.present?

    group = Group.find(group_id)
    render_404 unless can?(current_user, :read_group, group)
  end

  def find_todos
    @todos ||= TodosFinder.new(current_user, todo_params).execute
  end

  def todos_counts
    {
      count: current_user.todos_pending_count,
      done_count: current_user.todos_done_count
    }
  end

  def todos_page_count(todos)
    if todo_params.except(:sort, :page).empty?
      (current_user.todos_pending_count.to_f / todos.limit_value).ceil
    else
      todos.total_pages
    end
  end

  def todo_params
    aliased_params(
      params.permit(:action_id, :author_id, :project_id, :type, :sort, :state, :group_id)
    )
  end
  strong_memoize_attr :todo_params

  def aliased_params(original_params)
    alias_issue_type(original_params)
    alias_action_id(original_params)

    original_params
  end

  def alias_issue_type(original_params)
    return unless original_params[:type] == Issue.name

    original_params[:type] = [Issue.name, WorkItem.name]
  end

  def alias_action_id(original_params)
    return unless original_params[:action_id].to_i == ::Todo::MENTIONED

    original_params[:action_id] = [::Todo::MENTIONED, ::Todo::DIRECTLY_ADDRESSED]
  end
end