2014-09-02 18:07:02 +05:30
|
|
|
class Projects::NotesController < Projects::ApplicationController
|
2016-06-16 23:09:34 +05:30
|
|
|
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]
|
2016-09-13 17:45:13 +05:30
|
|
|
before_action :authorize_resolve_note!, only: [:resolve, :unresolve]
|
2016-06-16 23:09:34 +05:30
|
|
|
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
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def resolve
|
|
|
|
return render_404 unless note.resolvable?
|
|
|
|
|
|
|
|
note.resolve!(current_user)
|
|
|
|
|
|
|
|
MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(note.noteable)
|
|
|
|
|
|
|
|
discussion = note.discussion
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
resolved_by: note.resolved_by.try(:name),
|
|
|
|
discussion_headline_html: (view_to_html_string('discussions/_headline', discussion: discussion) if discussion)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def unresolve
|
|
|
|
return render_404 unless note.resolvable?
|
|
|
|
|
|
|
|
note.unresolve!
|
|
|
|
|
|
|
|
discussion = note.discussion
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
discussion_headline_html: (view_to_html_string('discussions/_headline', discussion: discussion) if discussion)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def note
|
|
|
|
@note ||= @project.notes.find(params[:id])
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
alias_method :awardable, :note
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def note_html(note)
|
2014-09-02 18:07:02 +05:30
|
|
|
render_to_string(
|
|
|
|
"projects/notes/_note",
|
|
|
|
layout: false,
|
|
|
|
formats: [:html],
|
|
|
|
locals: { note: note }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def diff_discussion_html(discussion)
|
|
|
|
return unless discussion.diff_discussion?
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
if params[:view] == 'parallel'
|
2016-09-13 17:45:13 +05:30
|
|
|
template = "discussions/_parallel_diff_discussion"
|
2015-09-11 14:41:01 +05:30
|
|
|
locals =
|
|
|
|
if params[:line_type] == 'old'
|
2016-09-13 17:45:13 +05:30
|
|
|
{ discussion_left: discussion, discussion_right: nil }
|
2015-09-11 14:41:01 +05:30
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
{ discussion_left: nil, discussion_right: discussion }
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
template = "discussions/_diff_discussion"
|
|
|
|
locals = { discussion: discussion }
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def discussion_html(discussion)
|
|
|
|
return unless discussion.diff_discussion?
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
render_to_string(
|
2016-09-13 17:45:13 +05:30
|
|
|
"discussions/_discussion",
|
2014-09-02 18:07:02 +05:30
|
|
|
layout: false,
|
|
|
|
formats: [:html],
|
2016-09-13 17:45:13 +05:30
|
|
|
locals: { discussion: discussion }
|
2014-09-02 18:07:02 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def note_json(note)
|
2016-06-16 23:09:34 +05:30
|
|
|
if note.is_a?(AwardEmoji)
|
|
|
|
{
|
|
|
|
valid: note.valid?,
|
|
|
|
award: true,
|
|
|
|
id: note.id,
|
|
|
|
name: note.name
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
elsif note.persisted?
|
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,
|
2016-09-13 17:45:13 +05:30
|
|
|
html: note_html(note),
|
2016-06-16 23:09:34 +05:30
|
|
|
award: false,
|
2016-09-13 17:45:13 +05:30
|
|
|
note: note.note
|
2015-12-23 02:04:40 +05:30
|
|
|
}
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
if note.diff_note?
|
|
|
|
discussion = note.to_discussion
|
|
|
|
|
|
|
|
attrs.merge!(
|
|
|
|
diff_discussion_html: diff_discussion_html(discussion),
|
|
|
|
discussion_html: discussion_html(discussion)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2016-08-24 12:49:21 +05:30
|
|
|
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,
|
2016-06-16 23:09:34 +05:30
|
|
|
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
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def authorize_resolve_note!
|
|
|
|
return access_denied! unless can?(current_user, :resolve_note, note)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
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
|