notes_controller.rb 4.72 KB
Newer Older
1
class Projects::NotesController < Projects::ApplicationController
2 3
  include ToggleAwardEmoji

gitlabhq's avatar
gitlabhq committed
4
  # Authorize
5
  before_action :authorize_read_note!
6
  before_action :authorize_create_note!, only: [:create]
7
  before_action :authorize_admin_note!, only: [:update, :destroy]
8
  before_action :find_current_user_notes, only: [:index]
gitlabhq's avatar
gitlabhq committed
9

10
  def index
11
    current_fetched_at = Time.now.to_i
12

13
    notes_json = { notes: [], last_fetched_at: current_fetched_at }
14

15
    @notes.each do |note|
16 17 18
      next if note.cross_reference_not_visible_for?(current_user)

      notes_json[:notes] << note_json(note)
19
    end
20 21

    render json: notes_json
22 23
  end

gitlabhq's avatar
gitlabhq committed
24
  def create
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25
    @note = Notes::CreateService.new(project, current_user, note_params).execute
gitlabhq's avatar
gitlabhq committed
26

27 28 29 30
    if @note.is_a?(Note)
      Banzai::NoteRenderer.render([@note], @project, current_user)
    end

gitlabhq's avatar
gitlabhq committed
31
    respond_to do |format|
ZJ van de Weg's avatar
ZJ van de Weg committed
32
      format.json { render json: note_json(@note) }
33
      format.html { redirect_back_or_default }
gitlabhq's avatar
gitlabhq committed
34 35 36
    end
  end

37
  def update
38
    @note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
gitlabhq's avatar
gitlabhq committed
39

40 41 42 43
    if @note.is_a?(Note)
      Banzai::NoteRenderer.render([@note], @project, current_user)
    end

gitlabhq's avatar
gitlabhq committed
44
    respond_to do |format|
45
      format.json { render json: note_json(@note) }
46
      format.html { redirect_back_or_default }
gitlabhq's avatar
gitlabhq committed
47 48 49
    end
  end

50
  def destroy
51
    if note.editable?
Robert Schilling's avatar
Robert Schilling committed
52
      Notes::DeleteService.new(project, current_user).execute(note)
53
    end
54 55

    respond_to do |format|
56
      format.js { head :ok }
57 58 59 60
    end
  end

  def delete_attachment
61 62
    note.remove_attachment!
    note.update_attribute(:attachment, nil)
63 64

    respond_to do |format|
65
      format.js { head :ok }
66 67 68
    end
  end

69 70 71 72 73
  private

  def note
    @note ||= @project.notes.find(params[:id])
  end
74
  alias_method :awardable, :note
75 76 77 78 79 80 81 82 83 84 85

  def note_to_html(note)
    render_to_string(
      "projects/notes/_note",
      layout: false,
      formats: [:html],
      locals: { note: note }
    )
  end

  def note_to_discussion_html(note)
86
    return unless note.diff_note?
87

88 89 90 91 92 93 94
    if params[:view] == 'parallel'
      template = "projects/notes/_diff_notes_with_reply_parallel"
      locals =
        if params[:line_type] == 'old'
          { notes_left: [note], notes_right: [] }
        else
          { notes_left: [], notes_right: [note] }
95
        end
96 97 98 99 100
    else
      template = "projects/notes/_diff_notes_with_reply"
      locals = { notes: [note] }
    end

101
    render_to_string(
102
      template,
103 104
      layout: false,
      formats: [:html],
105
      locals: locals
106 107 108
    )
  end

109
  def note_to_discussion_with_diff_html(note)
110
    return unless note.diff_note?
111

112 113 114 115 116 117 118 119
    render_to_string(
      "projects/notes/_discussion",
      layout: false,
      formats: [:html],
      locals: { discussion_notes: [note] }
    )
  end

120
  def note_json(note)
ZJ van de Weg's avatar
ZJ van de Weg committed
121 122 123 124 125 126 127 128
    if note.is_a?(AwardEmoji)
      {
        valid:  note.valid?,
        award:  true,
        id:     note.id,
        name:   note.name
      }
    elsif note.valid?
129 130
      Banzai::NoteRenderer.render([note], @project, current_user)

131
      attrs = {
132
        valid: true,
133 134 135
        id: note.id,
        discussion_id: note.discussion_id,
        html: note_to_html(note),
136
        award: false,
137 138 139 140
        note: note.note,
        discussion_html: note_to_discussion_html(note),
        discussion_with_diff_html: note_to_discussion_with_diff_html(note)
      }
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

      # The discussion_id is used to add the comment to the correct discussion
      # element on the merge request page. Among other things, the discussion_id
      # contains the sha of head commit of the merge request.
      # When new commits are pushed into the merge request after the initial
      # load of the merge request page, the discussion elements will still have
      # the old discussion_ids, with the old head commit sha. The new comment,
      # however, will have the new discussion_id with the new commit sha.
      # To ensure that these new comments will still end up in the correct
      # discussion element, we also send the original discussion_id, with the
      # old commit sha, along, and fall back on this value when no discussion
      # element with the new discussion_id could be found.
      if note.new_diff_note? && note.position != note.original_position
        attrs[:original_discussion_id] = note.original_discussion_id
      end

      attrs
158
    else
159
      {
160
        valid: false,
161
        award: false,
162 163
        errors: note.errors
      }
164
    end
165 166 167 168 169
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
170 171 172 173

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
174
      :attachment, :line_code, :commit_id, :type, :position
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
175 176
    )
  end
177 178 179 180

  def find_current_user_notes
    @notes = NotesFinder.new.execute(project, current_user, params)
  end
gitlabhq's avatar
gitlabhq committed
181
end