notes_controller.rb 3.06 KB
Newer Older
1
class Projects::NotesController < Projects::ApplicationController
gitlabhq's avatar
gitlabhq committed
2
  # Authorize
3
  before_action :authorize_read_note!
4
  before_action :authorize_create_note!, only: [:create]
5 6
  before_action :authorize_admin_note!, only: [:update, :destroy]
  before_action :find_current_user_notes, except: [:destroy, :delete_attachment]
gitlabhq's avatar
gitlabhq committed
7

8
  def index
9
    current_fetched_at = Time.now.to_i
10

11
    notes_json = { notes: [], last_fetched_at: current_fetched_at }
12

13 14 15 16 17
    @notes.each do |note|
      notes_json[:notes] << {
        id: note.id,
        html: note_to_html(note)
      }
18
    end
19 20

    render json: notes_json
21 22
  end

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

    respond_to do |format|
27
      format.json { render_note_json(@note) }
28
      format.html { redirect_back_or_default }
gitlabhq's avatar
gitlabhq committed
29 30 31
    end
  end

32
  def update
33
    @note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
gitlabhq's avatar
gitlabhq committed
34 35

    respond_to do |format|
36
      format.json { render_note_json(@note) }
37
      format.html { redirect_back_or_default }
gitlabhq's avatar
gitlabhq committed
38 39 40
    end
  end

41
  def destroy
42 43 44 45
    if note.editable?
      note.destroy
      note.reset_events_cache
    end
46 47

    respond_to do |format|
48
      format.js { render nothing: true }
49 50 51 52
    end
  end

  def delete_attachment
53 54
    note.remove_attachment!
    note.update_attribute(:attachment, nil)
55 56 57 58 59 60

    respond_to do |format|
      format.js { render nothing: true }
    end
  end

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  private

  def note
    @note ||= @project.notes.find(params[:id])
  end

  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)
77 78 79 80 81 82 83 84 85 86 87 88 89
    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] }
       end
    else
      template = "projects/notes/_diff_notes_with_reply"
      locals = { notes: [note] }
    end

90
    render_to_string(
91
      template,
92 93
      layout: false,
      formats: [:html],
94
      locals: locals
95 96 97
    )
  end

98
  def note_to_discussion_with_diff_html(note)
99 100
    return unless note.for_diff_line?

101 102 103 104 105 106 107 108
    render_to_string(
      "projects/notes/_discussion",
      layout: false,
      formats: [:html],
      locals: { discussion_notes: [note] }
    )
  end

109 110 111 112 113
  def render_note_json(note)
    render json: {
      id: note.id,
      discussion_id: note.discussion_id,
      html: note_to_html(note),
114 115
      discussion_html: note_to_discussion_html(note),
      discussion_with_diff_html: note_to_discussion_with_diff_html(note)
116 117 118 119 120 121
    }
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
122 123 124 125 126 127 128

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
      :attachment, :line_code, :commit_id
    )
  end
129 130 131 132 133 134

  private

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