2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
module Notes
|
2017-04-04 18:27:23 -04:00
|
|
|
class BuildService < ::BaseService
|
2017-03-09 20:29:11 -05:00
|
|
|
def execute
|
|
|
|
in_reply_to_discussion_id = params.delete(:in_reply_to_discussion_id)
|
2021-02-24 07:10:54 -05:00
|
|
|
discussion = nil
|
2017-03-21 11:28:47 -04:00
|
|
|
|
2017-05-03 04:48:01 -04:00
|
|
|
if in_reply_to_discussion_id.present?
|
|
|
|
discussion = find_discussion(in_reply_to_discussion_id)
|
2017-03-09 20:29:11 -05:00
|
|
|
|
2021-02-24 07:10:54 -05:00
|
|
|
return discussion_not_found unless discussion && can?(current_user, :create_note, discussion.noteable)
|
2017-03-09 20:29:11 -05:00
|
|
|
|
2019-02-06 05:31:46 -05:00
|
|
|
discussion = discussion.convert_to_discussion! if discussion.can_convert_to_discussion?
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
params.merge!(discussion.reply_attributes)
|
|
|
|
end
|
|
|
|
|
2021-02-24 07:10:54 -05:00
|
|
|
new_note(params, discussion)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def new_note(params, discussion)
|
2017-03-09 20:29:11 -05:00
|
|
|
note = Note.new(params)
|
|
|
|
note.project = project
|
|
|
|
note.author = current_user
|
|
|
|
|
2021-02-24 07:10:54 -05:00
|
|
|
parent_confidential = discussion&.confidential?
|
|
|
|
can_set_confidential = can?(current_user, :mark_note_as_confidential, note)
|
2018-09-23 17:15:29 -04:00
|
|
|
|
2021-02-24 07:10:54 -05:00
|
|
|
return discussion_not_found if parent_confidential && !can_set_confidential
|
|
|
|
|
|
|
|
note.confidential = (parent_confidential.nil? && can_set_confidential ? params.delete(:confidential) : parent_confidential)
|
|
|
|
note.resolve_without_save(current_user) if discussion&.resolved?
|
2017-03-09 20:29:11 -05:00
|
|
|
note
|
|
|
|
end
|
2017-05-03 04:48:01 -04:00
|
|
|
|
|
|
|
def find_discussion(discussion_id)
|
|
|
|
if project
|
|
|
|
project.notes.find_discussion(discussion_id)
|
|
|
|
else
|
2019-01-15 13:53:24 -05:00
|
|
|
Note.find_discussion(discussion_id)
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
end
|
2021-02-24 07:10:54 -05:00
|
|
|
|
|
|
|
def discussion_not_found
|
|
|
|
note = Note.new
|
|
|
|
note.errors.add(:base, _('Discussion to reply to cannot be found'))
|
|
|
|
note
|
|
|
|
end
|
2017-03-09 20:29:11 -05:00
|
|
|
end
|
|
|
|
end
|