notes_controller.rb 3.12 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
  before_action :authorize_admin_note!, only: [:update, :destroy]
6
  before_action :find_current_user_notes, except: [:destroy, :edit, :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 33 34 35 36
  def edit
    @note = note
    render layout: false
  end

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

    respond_to do |format|
41
      format.json { render_note_json(@note) }
42
      format.html { redirect_back_or_default }
gitlabhq's avatar
gitlabhq committed
43 44 45
    end
  end

46
  def destroy
47 48 49 50
    if note.editable?
      note.destroy
      note.reset_events_cache
    end
51 52

    respond_to do |format|
53
      format.js { render nothing: true }
54 55 56 57
    end
  end

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

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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)
82 83 84 85 86 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] }
       end
    else
      template = "projects/notes/_diff_notes_with_reply"
      locals = { notes: [note] }
    end

95
    render_to_string(
96
      template,
97 98
      layout: false,
      formats: [:html],
99
      locals: locals
100 101 102
    )
  end

103
  def note_to_discussion_with_diff_html(note)
104 105
    return unless note.for_diff_line?

106 107 108 109 110 111 112 113
    render_to_string(
      "projects/notes/_discussion",
      layout: false,
      formats: [:html],
      locals: { discussion_notes: [note] }
    )
  end

114 115 116 117 118
  def render_note_json(note)
    render json: {
      id: note.id,
      discussion_id: note.discussion_id,
      html: note_to_html(note),
119 120
      discussion_html: note_to_discussion_html(note),
      discussion_with_diff_html: note_to_discussion_with_diff_html(note)
121 122 123 124 125 126
    }
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
127 128 129 130 131 132 133

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
      :attachment, :line_code, :commit_id
    )
  end
134 135 136 137 138 139

  private

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