notes_controller.rb 3.69 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, :delete_attachment, :award_toggle]
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
  def award_toggle
    noteable = note_params[:noteable_type] == "issue" ? Issue : MergeRequest
    noteable = noteable.find_by!(id: note_params[:noteable_id], project: project)

    data = {
      author: current_user,
      is_award: true,
Valery Sizov's avatar
Valery Sizov committed
68
      note: note_params[:note].gsub(":", '')
69 70 71 72 73 74 75 76 77 78 79 80 81
    }

    note = noteable.notes.find_by(data)

    if note
      note.destroy
    else
      Notes::CreateService.new(project, current_user, note_params).execute
    end

    render json: { ok: true }
  end

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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)
98 99 100 101 102 103 104 105 106 107 108 109 110
    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

111
    render_to_string(
112
      template,
113 114
      layout: false,
      formats: [:html],
115
      locals: locals
116 117 118
    )
  end

119
  def note_to_discussion_with_diff_html(note)
120 121
    return unless note.for_diff_line?

122 123 124 125 126 127 128 129
    render_to_string(
      "projects/notes/_discussion",
      layout: false,
      formats: [:html],
      locals: { discussion_notes: [note] }
    )
  end

130 131 132 133 134
  def render_note_json(note)
    render json: {
      id: note.id,
      discussion_id: note.discussion_id,
      html: note_to_html(note),
135 136 137
      award: note.is_award,
      emoji_path: note.is_award ? ::AwardEmoji.path_to_emoji_image(note.note) : "",
      note: note.note,
138 139
      discussion_html: note_to_discussion_html(note),
      discussion_with_diff_html: note_to_discussion_with_diff_html(note)
140 141 142 143 144 145
    }
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
146 147 148 149 150 151 152

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
      :attachment, :line_code, :commit_id
    )
  end
153 154 155 156 157 158

  private

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