2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module NotesHelpers
|
2019-08-22 03:19:44 -04:00
|
|
|
include ::RendersNotes
|
|
|
|
|
2019-02-21 08:34:40 -05:00
|
|
|
def self.noteable_types
|
|
|
|
# This is a method instead of a constant, allowing EE to more easily
|
|
|
|
# extend it.
|
|
|
|
[Issue, MergeRequest, Snippet]
|
|
|
|
end
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
def update_note(noteable, note_id)
|
2019-08-09 08:54:57 -04:00
|
|
|
note = noteable.notes.find(note_id)
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
authorize! :admin_note, note
|
|
|
|
|
|
|
|
opts = {
|
|
|
|
note: params[:body]
|
|
|
|
}
|
|
|
|
parent = noteable_parent(noteable)
|
|
|
|
project = parent if parent.is_a?(Project)
|
|
|
|
|
|
|
|
note = ::Notes::UpdateService.new(project, current_user, opts).execute(note)
|
|
|
|
|
|
|
|
if note.valid?
|
|
|
|
present note, with: Entities::Note
|
|
|
|
else
|
|
|
|
bad_request!("Failed to save note #{note.errors.messages}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-01 08:39:44 -04:00
|
|
|
def resolve_note(noteable, note_id, resolved)
|
|
|
|
note = noteable.notes.find(note_id)
|
|
|
|
|
|
|
|
authorize! :resolve_note, note
|
|
|
|
|
|
|
|
bad_request!("Note is not resolvable") unless note.resolvable?
|
|
|
|
|
|
|
|
if resolved
|
|
|
|
parent = noteable_parent(noteable)
|
|
|
|
::Notes::ResolveService.new(parent, current_user).execute(note)
|
|
|
|
else
|
|
|
|
note.unresolve!
|
|
|
|
end
|
|
|
|
|
|
|
|
present note, with: Entities::Note
|
|
|
|
end
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
def delete_note(noteable, note_id)
|
|
|
|
note = noteable.notes.find(note_id)
|
|
|
|
|
|
|
|
authorize! :admin_note, note
|
|
|
|
|
|
|
|
parent = noteable_parent(noteable)
|
|
|
|
project = parent if parent.is_a?(Project)
|
|
|
|
destroy_conditionally!(note) do |note|
|
|
|
|
::Notes::DestroyService.new(project, current_user).execute(note)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_note(noteable, note_id)
|
2019-08-09 08:54:57 -04:00
|
|
|
note = noteable.notes.with_metadata.find(note_id)
|
2020-02-23 22:09:05 -05:00
|
|
|
can_read_note = note.readable_by?(current_user)
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
if can_read_note
|
|
|
|
present note, with: Entities::Note
|
|
|
|
else
|
|
|
|
not_found!("Note")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def noteable_read_ability_name(noteable)
|
2020-01-23 07:08:38 -05:00
|
|
|
"read_#{ability_name(noteable)}".to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def ability_name(noteable)
|
|
|
|
if noteable.respond_to?(:to_ability_name)
|
|
|
|
noteable.to_ability_name
|
|
|
|
else
|
|
|
|
noteable.class.to_s.underscore
|
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
2020-01-24 16:09:09 -05:00
|
|
|
def find_noteable(noteable_type, noteable_id)
|
|
|
|
params = finder_params_by_noteable_type_and_id(noteable_type, noteable_id)
|
2018-05-01 08:39:44 -04:00
|
|
|
|
2019-08-02 04:55:03 -04:00
|
|
|
noteable = NotesFinder.new(current_user, params).target
|
2019-06-15 01:10:58 -04:00
|
|
|
noteable = nil unless can?(current_user, noteable_read_ability_name(noteable), noteable)
|
2019-06-19 05:07:17 -04:00
|
|
|
noteable || not_found!(noteable_type)
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
2020-01-24 16:09:09 -05:00
|
|
|
def finder_params_by_noteable_type_and_id(type, id)
|
2019-06-15 01:10:58 -04:00
|
|
|
target_type = type.name.underscore
|
|
|
|
{ target_type: target_type }.tap do |h|
|
|
|
|
if %w(issue merge_request).include?(target_type)
|
|
|
|
h[:target_iid] = id
|
|
|
|
else
|
|
|
|
h[:target_id] = id
|
|
|
|
end
|
2019-08-02 04:55:03 -04:00
|
|
|
|
2020-01-24 16:09:09 -05:00
|
|
|
add_parent_to_finder_params(h, type)
|
2019-06-15 01:10:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-24 16:09:09 -05:00
|
|
|
def add_parent_to_finder_params(finder_params, noteable_type)
|
2019-08-02 04:55:03 -04:00
|
|
|
finder_params[:project] = user_project
|
|
|
|
end
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
def noteable_parent(noteable)
|
|
|
|
public_send("user_#{noteable.class.parent_class.to_s.underscore}") # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_note(noteable, opts)
|
2020-03-23 08:09:47 -04:00
|
|
|
whitelist_query_limiting
|
2019-03-04 13:37:08 -05:00
|
|
|
authorize!(:create_note, noteable)
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
parent = noteable_parent(noteable)
|
2018-04-23 11:48:26 -04:00
|
|
|
|
2019-03-04 13:37:08 -05:00
|
|
|
opts.delete(:created_at) unless current_user.can?(:set_note_created_at, noteable)
|
2018-02-28 02:48:23 -05:00
|
|
|
|
2018-07-12 05:26:09 -04:00
|
|
|
opts[:updated_at] = opts[:created_at] if opts[:created_at]
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
project = parent if parent.is_a?(Project)
|
|
|
|
::Notes::CreateService.new(project, current_user, opts).execute
|
|
|
|
end
|
2018-05-01 08:39:44 -04:00
|
|
|
|
|
|
|
def resolve_discussion(noteable, discussion_id, resolved)
|
|
|
|
discussion = noteable.find_discussion(discussion_id)
|
|
|
|
|
|
|
|
forbidden! unless discussion.can_resolve?(current_user)
|
|
|
|
|
|
|
|
if resolved
|
|
|
|
parent = noteable_parent(noteable)
|
|
|
|
::Discussions::ResolveService.new(parent, current_user, merge_request: noteable).execute(discussion)
|
|
|
|
else
|
|
|
|
discussion.unresolve!
|
|
|
|
end
|
|
|
|
|
|
|
|
present discussion, with: Entities::Discussion
|
|
|
|
end
|
2020-03-23 08:09:47 -04:00
|
|
|
|
|
|
|
def whitelist_query_limiting
|
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab/-/issues/211538')
|
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
API::Helpers::NotesHelpers.prepend_if_ee('EE::API::Helpers::NotesHelpers')
|