BigW Consortium Gitlab

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

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 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 committed
25
  def create
Dmitriy Zaporozhets committed
26
    @note = Notes::CreateService.new(project, current_user, note_params).execute
gitlabhq committed
27

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

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

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

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

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

51
  def destroy
52
    if note.editable?
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 committed
75
    MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(note.noteable)
76

Douwe Maan committed
77
    discussion = note.discussion
78

79
    render json: {
80
      resolved_by: note.resolved_by.try(:name),
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 committed
90
    discussion = note.discussion
91 92

    render json: {
Douwe Maan committed
93
      discussion_headline_html: (view_to_html_string('discussions/_headline', discussion: discussion) if discussion)
94
    }
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)
ZJ van de Weg committed
149 150 151 152 153 154 155
    if note.is_a?(AwardEmoji)
      {
        valid:  note.valid?,
        award:  true,
        id:     note.id,
        name:   note.name
      }
156
    elsif note.persisted?
157 158
      Banzai::NoteRenderer.render([note], @project, current_user)

159
      attrs = {
160
        valid: true,
161 162
        id: note.id,
        discussion_id: note.discussion_id,
163
        html: note_html(note),
164
        award: false,
165
        note: note.note
166
      }
167

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

        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
190 191 192
      end

      attrs
193
    else
194
      {
195
        valid: false,
196
        award: false,
197 198
        errors: note.errors
      }
199
    end
200 201 202 203 204
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets committed
205

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

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

  def find_current_user_notes
    @notes = NotesFinder.new.execute(project, current_user, params)
  end
gitlabhq committed
220
end