2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-14 12:33:31 +00:00
|
|
|
module API
|
2020-04-28 03:09:53 +00:00
|
|
|
class Notes < Grape::API
|
2016-12-04 17:11:19 +00:00
|
|
|
include PaginationParams
|
2018-02-28 07:48:23 +00:00
|
|
|
helpers ::API::Helpers::NotesHelpers
|
2016-12-04 17:11:19 +00:00
|
|
|
|
2012-11-27 19:43:39 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
2019-02-21 13:34:40 +00:00
|
|
|
Helpers::NotesHelpers.noteable_types.each do |noteable_type|
|
2018-02-28 07:48:23 +00:00
|
|
|
parent_type = noteable_type.parent_class.to_s.underscore
|
|
|
|
noteables_str = noteable_type.to_s.underscore.pluralize
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The ID of a #{parent_type}"
|
|
|
|
end
|
2018-11-08 12:18:17 +00:00
|
|
|
resource parent_type.pluralize.to_sym, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2018-02-28 07:48:23 +00:00
|
|
|
desc "Get a list of #{noteable_type.to_s.downcase} notes" do
|
2016-11-09 14:26:27 +00:00
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
2017-11-29 16:22:22 +00:00
|
|
|
optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
|
|
|
|
desc: 'Return notes ordered by `created_at` or `updated_at` fields.'
|
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc',
|
|
|
|
desc: 'Return notes sorted in `asc` or `desc` order.'
|
2020-01-03 09:07:33 +00:00
|
|
|
optional :activity_filter, type: String, values: UserPreference::NOTES_FILTERS.stringify_keys.keys, default: 'all_notes',
|
|
|
|
desc: 'The type of notables which are returned.'
|
2016-12-04 17:11:19 +00:00
|
|
|
use :pagination
|
2016-11-09 14:26:27 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-09 14:26:27 +00:00
|
|
|
get ":id/#{noteables_str}/:noteable_id/notes" do
|
2020-01-24 21:09:09 +00:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2016-11-09 14:26:27 +00:00
|
|
|
|
2018-05-01 12:39:44 +00:00
|
|
|
# We exclude notes that are cross-references and that cannot be viewed
|
|
|
|
# by the current user. By doing this exclusion at this level and not
|
|
|
|
# at the DB query level (which we cannot in that case), the current
|
|
|
|
# page can have less elements than :per_page even if
|
|
|
|
# there's more than one page.
|
2020-01-03 09:07:33 +00:00
|
|
|
notes_filter = UserPreference::NOTES_FILTERS[params[:activity_filter].to_sym]
|
|
|
|
raw_notes = noteable.notes.with_metadata.with_notes_filter(notes_filter).reorder(order_options_with_tie_breaker)
|
2019-08-22 07:19:44 +00:00
|
|
|
|
|
|
|
# paginate() only works with a relation. This could lead to a
|
|
|
|
# mismatch between the pagination headers info and the actual notes
|
|
|
|
# array returned, but this is really a edge-case.
|
|
|
|
notes = paginate(raw_notes)
|
|
|
|
notes = prepare_notes_for_rendering(notes)
|
2020-02-24 03:09:05 +00:00
|
|
|
notes = notes.select { |note| note.readable_by?(current_user) }
|
2018-05-01 12:39:44 +00:00
|
|
|
present notes, with: Entities::Note
|
2012-11-27 19:43:39 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2012-11-29 19:33:41 +00:00
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
desc "Get a single #{noteable_type.to_s.downcase} note" do
|
2016-11-09 14:26:27 +00:00
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
end
|
|
|
|
get ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
2020-01-24 21:09:09 +00:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 07:48:23 +00:00
|
|
|
get_note(noteable, params[:note_id])
|
2012-11-29 19:33:41 +00:00
|
|
|
end
|
2012-11-29 20:06:24 +00:00
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
desc "Create a new #{noteable_type.to_s.downcase} note" do
|
2016-11-09 14:26:27 +00:00
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
optional :created_at, type: String, desc: 'The creation date of the note'
|
|
|
|
end
|
|
|
|
post ":id/#{noteables_str}/:noteable_id/notes" do
|
2020-01-24 21:09:09 +00:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2017-03-27 13:01:45 +00:00
|
|
|
|
2014-06-18 06:26:04 +00:00
|
|
|
opts = {
|
2017-01-19 21:00:47 +00:00
|
|
|
note: params[:body],
|
|
|
|
noteable_type: noteables_str.classify,
|
2018-02-28 07:48:23 +00:00
|
|
|
noteable_id: noteable.id,
|
|
|
|
created_at: params[:created_at]
|
2014-06-18 06:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
note = create_note(noteable, opts)
|
2017-09-01 12:03:57 +00:00
|
|
|
|
2020-03-23 12:09:47 +00:00
|
|
|
if note.errors.keys == [:commands_only]
|
|
|
|
status 202
|
|
|
|
present note, with: Entities::NoteCommands
|
|
|
|
elsif note.valid?
|
2019-10-16 03:06:12 +00:00
|
|
|
present note, with: Entities.const_get(note.class.name, false)
|
2014-06-10 14:56:35 +00:00
|
|
|
else
|
2020-03-23 12:09:47 +00:00
|
|
|
note.errors.delete(:commands_only) if note.errors.has_key?(:commands)
|
2018-02-28 07:48:23 +00:00
|
|
|
bad_request!("Note #{note.errors.messages}")
|
2012-11-29 20:06:24 +00:00
|
|
|
end
|
|
|
|
end
|
2014-09-02 15:12:13 +00:00
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
desc "Update an existing #{noteable_type.to_s.downcase} note" do
|
2016-11-09 14:26:27 +00:00
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
end
|
|
|
|
put ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
2020-01-24 21:09:09 +00:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2015-07-30 12:45:54 +00:00
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
update_note(noteable, params[:note_id])
|
2014-09-02 15:12:13 +00:00
|
|
|
end
|
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
desc "Delete a #{noteable_type.to_s.downcase} note" do
|
2016-11-09 14:26:27 +00:00
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
end
|
|
|
|
delete ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
2020-01-24 21:09:09 +00:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2016-04-12 13:43:29 +00:00
|
|
|
|
2018-02-28 07:48:23 +00:00
|
|
|
delete_note(noteable, params[:note_id])
|
2016-04-05 23:21:02 +00:00
|
|
|
end
|
2012-11-27 19:43:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|