debian-mirror-gitlab/app/controllers/projects/notes_controller.rb

182 lines
4.7 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Projects::NotesController < Projects::ApplicationController
include ToggleAwardEmoji
2014-09-02 18:07:02 +05:30
# Authorize
2015-09-11 14:41:01 +05:30
before_action :authorize_read_note!
before_action :authorize_create_note!, only: [:create]
before_action :authorize_admin_note!, only: [:update, :destroy]
before_action :find_current_user_notes, only: [:index]
2014-09-02 18:07:02 +05:30
def index
current_fetched_at = Time.now.to_i
notes_json = { notes: [], last_fetched_at: current_fetched_at }
@notes.each do |note|
2016-04-02 18:10:28 +05:30
next if note.cross_reference_not_visible_for?(current_user)
notes_json[:notes] << note_json(note)
2014-09-02 18:07:02 +05:30
end
render json: notes_json
end
def create
@note = Notes::CreateService.new(project, current_user, note_params).execute
2016-08-24 12:49:21 +05:30
if @note.is_a?(Note)
Banzai::NoteRenderer.render([@note], @project, current_user)
end
2014-09-02 18:07:02 +05:30
respond_to do |format|
2016-04-02 18:10:28 +05:30
format.json { render json: note_json(@note) }
2015-10-24 18:46:33 +05:30
format.html { redirect_back_or_default }
2014-09-02 18:07:02 +05:30
end
end
def update
2015-09-11 14:41:01 +05:30
@note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
if @note.is_a?(Note)
Banzai::NoteRenderer.render([@note], @project, current_user)
end
2014-09-02 18:07:02 +05:30
respond_to do |format|
2016-04-02 18:10:28 +05:30
format.json { render json: note_json(@note) }
2015-10-24 18:46:33 +05:30
format.html { redirect_back_or_default }
2014-09-02 18:07:02 +05:30
end
end
def destroy
2015-04-26 12:48:37 +05:30
if note.editable?
2016-06-02 11:05:42 +05:30
Notes::DeleteService.new(project, current_user).execute(note)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
respond_to do |format|
2016-06-02 11:05:42 +05:30
format.js { head :ok }
2014-09-02 18:07:02 +05:30
end
end
def delete_attachment
note.remove_attachment!
note.update_attribute(:attachment, nil)
respond_to do |format|
2016-06-02 11:05:42 +05:30
format.js { head :ok }
2014-09-02 18:07:02 +05:30
end
end
private
def note
@note ||= @project.notes.find(params[:id])
end
alias_method :awardable, :note
2014-09-02 18:07:02 +05:30
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)
2016-06-02 11:05:42 +05:30
return unless note.diff_note?
2016-04-02 18:10:28 +05:30
2015-09-11 14:41:01 +05:30
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] }
2016-04-02 18:10:28 +05:30
end
2015-09-11 14:41:01 +05:30
else
template = "projects/notes/_diff_notes_with_reply"
locals = { notes: [note] }
end
2014-09-02 18:07:02 +05:30
render_to_string(
2015-09-11 14:41:01 +05:30
template,
2014-09-02 18:07:02 +05:30
layout: false,
formats: [:html],
2015-09-11 14:41:01 +05:30
locals: locals
2014-09-02 18:07:02 +05:30
)
end
def note_to_discussion_with_diff_html(note)
2016-06-02 11:05:42 +05:30
return unless note.diff_note?
2014-09-02 18:07:02 +05:30
render_to_string(
"projects/notes/_discussion",
layout: false,
formats: [:html],
locals: { discussion_notes: [note] }
)
end
2016-04-02 18:10:28 +05:30
def note_json(note)
if note.is_a?(AwardEmoji)
{
valid: note.valid?,
award: true,
id: note.id,
name: note.name
}
elsif note.valid?
2016-08-24 12:49:21 +05:30
Banzai::NoteRenderer.render([note], @project, current_user)
attrs = {
2015-12-23 02:04:40 +05:30
valid: true,
id: note.id,
discussion_id: note.discussion_id,
html: note_to_html(note),
award: false,
2015-12-23 02:04:40 +05:30
note: note.note,
discussion_html: note_to_discussion_html(note),
discussion_with_diff_html: note_to_discussion_with_diff_html(note)
}
2016-08-24 12:49:21 +05:30
# The discussion_id is used to add the comment to the correct discussion
# element on the merge request page. Among other things, the discussion_id
# contains the sha of head commit of the merge request.
# When new commits are pushed into the merge request after the initial
# load of the merge request page, the discussion elements will still have
# the old discussion_ids, with the old head commit sha. The new comment,
# however, will have the new discussion_id with the new commit sha.
# To ensure that these new comments will still end up in the correct
# discussion element, we also send the original discussion_id, with the
# old commit sha, along, and fall back on this value when no discussion
# element with the new discussion_id could be found.
if note.new_diff_note? && note.position != note.original_position
attrs[:original_discussion_id] = note.original_discussion_id
end
attrs
2015-12-23 02:04:40 +05:30
else
2016-04-02 18:10:28 +05:30
{
2015-12-23 02:04:40 +05:30
valid: false,
award: false,
2015-12-23 02:04:40 +05:30
errors: note.errors
}
end
2014-09-02 18:07:02 +05:30
end
def authorize_admin_note!
return access_denied! unless can?(current_user, :admin_note, note)
end
def note_params
params.require(:note).permit(
:note, :noteable, :noteable_id, :noteable_type, :project_id,
2016-08-24 12:49:21 +05:30
:attachment, :line_code, :commit_id, :type, :position
2014-09-02 18:07:02 +05:30
)
end
2015-04-26 12:48:37 +05:30
def find_current_user_notes
@notes = NotesFinder.new.execute(project, current_user, params)
end
2014-09-02 18:07:02 +05:30
end