Module: NotesActions

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize, RendersNotes
Included in:
Projects::NotesController, Snippets::NotesController
Defined in:
app/controllers/concerns/notes_actions.rb

Constant Summary collapse

MICROSECOND =

last_fetched_at is an integer number of microseconds, which is the same precision as PostgreSQL “timestamp” fields. It's important for them to have identical precision for accurate pagination

1_000_000

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from RendersNotes

#prepare_notes_for_rendering

Instance Method Details

#createObject

rubocop:disable Gitlab/ModuleWithInstanceVariables


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
# File 'app/controllers/concerns/notes_actions.rb', line 38

def create
  @note = Notes::CreateService.new(note_project, current_user, create_note_params).execute

  respond_to do |format|
    format.json do
      json = {
        commands_changes: @note.commands_changes&.slice(:emoji_award, :time_estimate, :spend_time)
      }

      if @note.persisted? && return_discussion?
        json[:valid] = true

        discussion = @note.discussion
        prepare_notes_for_rendering(discussion.notes)
        json[:discussion] = discussion_serializer.represent(discussion, context: self)
      else
        prepare_notes_for_rendering([@note])

        json.merge!(note_json(@note))
      end

      if @note.errors.present? && @note.errors.keys != [:commands_only]
        render json: json, status: :unprocessable_entity
      else
        render json: json
      end
    end
    format.html { redirect_back_or_default }
  end
end

#destroyObject

rubocop:enable Gitlab/ModuleWithInstanceVariables


87
88
89
90
91
92
93
94
95
# File 'app/controllers/concerns/notes_actions.rb', line 87

def destroy
  if note.editable?
    Notes::DestroyService.new(project, current_user).execute(note)
  end

  respond_to do |format|
    format.js { head :ok }
  end
end

#indexObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/concerns/notes_actions.rb', line 20

def index
  notes, meta = gather_notes
  notes = prepare_notes_for_rendering(notes)
  notes = notes.select { |n| n.readable_by?(current_user) }
  notes =
    if use_note_serializer?
      note_serializer.represent(notes)
    else
      notes.map { |note| note_json(note) }
    end

  # We know there's more data, so tell the frontend to poll again after 1ms
  set_polling_interval_header(interval: 1) if meta[:more]

  render json: meta.merge(notes: notes)
end

#updateObject

rubocop:disable Gitlab/ModuleWithInstanceVariables


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/concerns/notes_actions.rb', line 71

def update
  @note = Notes::UpdateService.new(project, current_user, update_note_params).execute(note)
  unless @note
    head :gone
    return
  end

  prepare_notes_for_rendering([@note])

  respond_to do |format|
    format.json { render json: note_json(@note) }
    format.html { redirect_back_or_default }
  end
end