BigW Consortium Gitlab

notes_controller.rb 4.87 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 :find_current_user_notes, only: [:index]
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 committed
24
  def create
Dmitriy Zaporozhets committed
25
    @note = Notes::CreateService.new(project, current_user, note_params).execute
gitlabhq committed
26

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

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

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

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

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

50
  def destroy
51
    if note.editable?
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
  def note_html(note)
77 78 79 80 81 82 83 84
    render_to_string(
      "projects/notes/_note",
      layout: false,
      formats: [:html],
      locals: { note: note }
    )
  end

85 86
  def diff_discussion_html(discussion)
    return unless discussion.diff_discussion?
87

88
    if params[:view] == 'parallel'
89
      template = "discussions/_parallel_diff_discussion"
90 91
      locals =
        if params[:line_type] == 'old'
92
          { discussion_left: discussion, discussion_right: nil }
93
        else
94
          { discussion_left: nil, discussion_right: discussion }
95
        end
96
    else
97 98
      template = "discussions/_diff_discussion"
      locals = { discussion: discussion }
99 100
    end

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

109 110
  def discussion_html(discussion)
    return unless discussion.diff_discussion?
111

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

120
  def note_json(note)
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
        id: note.id,
        discussion_id: note.discussion_id,
135
        html: note_html(note),
136
        award: false,
137
        note: note.note
138
      }
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      if note.diff_note?
        discussion = Discussion.new([note])

        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
162 163 164
      end

      attrs
165
    else
166
      {
167
        valid: false,
168
        award: false,
169 170
        errors: note.errors
      }
171
    end
172 173 174 175 176
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets committed
177 178 179 180

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
181
      :attachment, :line_code, :commit_id, :type, :position
Dmitriy Zaporozhets committed
182 183
    )
  end
184 185 186 187

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