notes_controller.rb 1.8 KB
Newer Older
1
class Projects::NotesController < Projects::ApplicationController
G
gitlabhq 已提交
2
  # Authorize
D
Dmitriy Zaporozhets 已提交
3
  before_filter :authorize_read_note!
4
  before_filter :authorize_write_note!, only: [:create]
G
gitlabhq 已提交
5 6 7

  respond_to :js

8
  def index
9
    @notes = Notes::LoadContext.new(project, current_user, params).execute
R
Riyad Preukschas 已提交
10 11
    @target_type = params[:target_type].camelize
    @target_id = params[:target_id]
12

13
    if params[:target_type] == "merge_request"
14
      @discussions   = discussions_from_notes
15 16
    end

17
    respond_with(@notes)
18 19
  end

G
gitlabhq 已提交
20
  def create
D
Dmitriy Zaporozhets 已提交
21
    @note = Notes::CreateContext.new(project, current_user, params).execute
R
Riyad Preukschas 已提交
22 23
    @target_type = params[:target_type].camelize
    @target_id = params[:target_id]
G
gitlabhq 已提交
24 25 26

    respond_to do |format|
      format.html {redirect_to :back}
N
Nihad Abbasov 已提交
27
      format.js
G
gitlabhq 已提交
28 29 30 31 32
    end
  end

  def destroy
    @note = @project.notes.find(params[:id])
G
gitlabhq 已提交
33
    return access_denied! unless can?(current_user, :admin_note, @note)
G
gitlabhq 已提交
34 35 36
    @note.destroy

    respond_to do |format|
37
      format.js { render nothing: true }
G
gitlabhq 已提交
38 39 40
    end
  end

41
  def preview
42
    render text: view_context.markdown(params[:note])
43 44 45
  end

  protected
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60
  def discussion_notes_for(note)
    @notes.select do |other_note|
      note.discussion_id == other_note.discussion_id
    end
  end

  def discussions_from_notes
    discussion_ids = []
    discussions = []

    @notes.each do |note|
      next if discussion_ids.include?(note.discussion_id)

      # don't group notes for the main target
R
Riyad Preukschas 已提交
61
      if note_for_main_target?(note)
62 63 64 65 66 67 68 69 70 71 72
        discussions << [note]
      else
        discussions << discussion_notes_for(note)
        discussion_ids << note.discussion_id
      end
    end

    discussions
  end

  # Helps to distinguish e.g. commit notes in mr notes list
R
Riyad Preukschas 已提交
73
  def note_for_main_target?(note)
74
    (@target_type.camelize == note.noteable_type && !note.for_diff_line?)
75
  end
G
gitlabhq 已提交
76
end