notes_controller.rb 5.81 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 :authorize_resolve_note!, only: [:resolve, :unresolve]
9
  before_action :find_current_user_notes, only: [:index]
gitlabhq's avatar
gitlabhq committed
10

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

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

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

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

    render json: notes_json
23 24
  end

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

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

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

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

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

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

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

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

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

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

70
  def resolve
71 72 73 74
    return render_404 unless note.resolvable?

    note.resolve!(current_user)

Douwe Maan's avatar
Douwe Maan committed
75
    MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(note.noteable)
76

Douwe Maan's avatar
Douwe Maan committed
77
    discussion = note.discussion
78

79
    render json: {
80
      resolved_by: note.resolved_by.try(:name),
Douwe Maan's avatar
Douwe Maan committed
81
      discussion_headline_html: (view_to_html_string('discussions/_headline', discussion: discussion) if discussion)
82
    }
83 84
  end

85 86 87 88 89
  def unresolve
    return render_404 unless note.resolvable?

    note.unresolve!

Douwe Maan's avatar
Douwe Maan committed
90
    discussion = note.discussion
91 92

    render json: {
Douwe Maan's avatar
Douwe Maan committed
93
      discussion_headline_html: (view_to_html_string('discussions/_headline', discussion: discussion) if discussion)
94
    }
Phil Hughes's avatar
Phil Hughes committed
95 96
  end

97 98 99 100 101
  private

  def note
    @note ||= @project.notes.find(params[:id])
  end
102
  alias_method :awardable, :note
103

104
  def note_html(note)
105 106 107 108 109 110 111 112
    render_to_string(
      "projects/notes/_note",
      layout: false,
      formats: [:html],
      locals: { note: note }
    )
  end

113 114
  def diff_discussion_html(discussion)
    return unless discussion.diff_discussion?
115

116
    if params[:view] == 'parallel'
117
      template = "discussions/_parallel_diff_discussion"
118 119
      locals =
        if params[:line_type] == 'old'
120
          { discussion_left: discussion, discussion_right: nil }
121
        else
122
          { discussion_left: nil, discussion_right: discussion }
123
        end
124
    else
125 126
      template = "discussions/_diff_discussion"
      locals = { discussion: discussion }
127 128
    end

129
    render_to_string(
130
      template,
131 132
      layout: false,
      formats: [:html],
133
      locals: locals
134 135 136
    )
  end

137 138
  def discussion_html(discussion)
    return unless discussion.diff_discussion?
139

140
    render_to_string(
141
      "discussions/_discussion",
142 143
      layout: false,
      formats: [:html],
144
      locals: { discussion: discussion }
145 146 147
    )
  end

148
  def note_json(note)
149 150 151 152 153
    attrs = {
      award: false,
      id: note.id
    }

ZJ van de Weg's avatar
ZJ van de Weg committed
154
    if note.is_a?(AwardEmoji)
155
      attrs.merge!(
ZJ van de Weg's avatar
ZJ van de Weg committed
156 157 158
        valid:  note.valid?,
        award:  true,
        name:   note.name
159
      )
160
    elsif note.persisted?
161 162
      Banzai::NoteRenderer.render([note], @project, current_user)

163
      attrs.merge!(
164
        valid: true,
165
        discussion_id: note.discussion_id,
166 167
        html: note_html(note),
        note: note.note
168
      )
169

170
      if note.diff_note?
Douwe Maan's avatar
Douwe Maan committed
171
        discussion = note.to_discussion
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

        attrs.merge!(
          diff_discussion_html: diff_discussion_html(discussion),
          discussion_html: discussion_html(discussion)
        )

        # 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
192
      end
193
    else
194
      attrs.merge!(
195 196
        valid: false,
        errors: note.errors
197
      )
198
    end
199

200
    attrs[:commands_changes] = note.commands_changes unless attrs[:award]
201
    attrs
202 203 204 205 206
  end

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

208 209 210 211
  def authorize_resolve_note!
    return access_denied! unless can?(current_user, :resolve_note, note)
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
212 213 214
  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
215
      :attachment, :line_code, :commit_id, :type, :position
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
216 217
    )
  end
218 219

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