notes_controller.rb 3.97 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]
Valery Sizov's avatar
Valery Sizov committed
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

Valery Sizov's avatar
Valery Sizov committed
61
  def award_toggle
Valery Sizov's avatar
Valery Sizov committed
62 63 64 65 66
    noteable = if note_params[:noteable_type] == "issue"
                 project.issues.find(note_params[:noteable_id])
               else
                 project.merge_requests.find(note_params[:noteable_id])
               end
Valery Sizov's avatar
Valery Sizov committed
67

Valery Sizov's avatar
Valery Sizov committed
68 69 70
    data = {
      author: current_user,
      is_award: true,
Valery Sizov's avatar
Valery Sizov committed
71
      note: note_params[:note].gsub(":", '')
Valery Sizov's avatar
Valery Sizov committed
72 73
    }

Valery Sizov's avatar
Valery Sizov committed
74
    note = noteable.notes.find_by(data)
Valery Sizov's avatar
Valery Sizov committed
75 76 77 78

    if note
      note.destroy
    else
Valery Sizov's avatar
Valery Sizov committed
79
      Notes::CreateService.new(project, current_user, note_params).execute
Valery Sizov's avatar
Valery Sizov committed
80 81
    end

Valery Sizov's avatar
Valery Sizov committed
82
    render json: { ok: true }
Valery Sizov's avatar
Valery Sizov committed
83 84
  end

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

114
    render_to_string(
115
      template,
116 117
      layout: false,
      formats: [:html],
118
      locals: locals
119 120 121
    )
  end

122
  def note_to_discussion_with_diff_html(note)
123 124
    return unless note.for_diff_line?

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

133
  def render_note_json(note)
134 135
    if note.valid?
      render json: {
136
        valid: true,
137 138 139 140 141 142 143 144 145 146
        id: note.id,
        discussion_id: note.discussion_id,
        html: note_to_html(note),
        award: note.is_award,
        emoji_path: note.is_award ? view_context.image_url(::AwardEmoji.path_to_emoji_image(note.note)) : "",
        note: note.note,
        discussion_html: note_to_discussion_html(note),
        discussion_with_diff_html: note_to_discussion_with_diff_html(note)
      }
    else
147 148 149 150 151
      render json: {
        valid: false,
        award: note.is_award,
        errors: note.errors
      }
152
    end
153 154 155 156 157
  end

  def authorize_admin_note!
    return access_denied! unless can?(current_user, :admin_note, note)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
158 159 160 161 162 163 164

  def note_params
    params.require(:note).permit(
      :note, :noteable, :noteable_id, :noteable_type, :project_id,
      :attachment, :line_code, :commit_id
    )
  end
165 166 167 168 169 170

  private

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