2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-09 17:24:54 -04:00
|
|
|
class NoteEntity < API::Entities::Note
|
|
|
|
include RequestAwareEntity
|
2018-06-21 08:22:40 -04:00
|
|
|
include NotesHelper
|
2017-06-09 17:24:54 -04:00
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
expose :id do |note|
|
|
|
|
# resource events are represented as notes too, but don't
|
|
|
|
# have ID, discussion ID is used for them instead
|
|
|
|
note.id ? note.id.to_s : note.discussion_id
|
|
|
|
end
|
|
|
|
|
2017-06-09 17:24:54 -04:00
|
|
|
expose :type
|
|
|
|
|
2017-08-17 13:27:11 -04:00
|
|
|
expose :author, using: NoteUserEntity
|
2017-06-09 17:24:54 -04:00
|
|
|
|
|
|
|
unexpose :note, as: :body
|
|
|
|
expose :note
|
|
|
|
|
|
|
|
expose :redacted_note_html, as: :note_html
|
|
|
|
|
2017-08-30 04:43:10 -04:00
|
|
|
expose :last_edited_at, if: -> (note, _) { note.edited? }
|
|
|
|
expose :last_edited_by, using: NoteUserEntity, if: -> (note, _) { note.edited? }
|
2017-06-09 17:24:54 -04:00
|
|
|
|
2017-06-26 20:39:40 -04:00
|
|
|
expose :current_user do
|
|
|
|
expose :can_edit do |note|
|
2018-06-21 08:22:40 -04:00
|
|
|
can?(current_user, :admin_note, note)
|
2017-06-26 20:39:40 -04:00
|
|
|
end
|
2018-04-06 14:19:37 -04:00
|
|
|
|
|
|
|
expose :can_award_emoji do |note|
|
2018-06-21 08:22:40 -04:00
|
|
|
can?(current_user, :award_emoji, note)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :can_resolve do |note|
|
|
|
|
note.resolvable? && can?(current_user, :resolve_note, note)
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
2020-11-09 07:09:24 -05:00
|
|
|
|
|
|
|
expose :can_resolve_discussion do |note|
|
|
|
|
note.discussion.resolvable? && note.discussion.can_resolve?(current_user)
|
|
|
|
end
|
2017-06-09 17:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
expose :suggestions, using: SuggestionEntity
|
2018-02-27 19:10:43 -05:00
|
|
|
expose :resolved?, as: :resolved
|
|
|
|
expose :resolvable?, as: :resolvable
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2018-02-27 19:10:43 -05:00
|
|
|
expose :resolved_by, using: NoteUserEntity
|
|
|
|
|
2017-06-09 17:24:54 -04:00
|
|
|
expose :system_note_icon_name, if: -> (note, _) { note.system? } do |note|
|
|
|
|
SystemNoteHelper.system_note_icon_name(note)
|
|
|
|
end
|
|
|
|
|
2020-09-16 11:09:32 -04:00
|
|
|
expose :is_noteable_author do |note|
|
|
|
|
note.noteable_author?(request.noteable)
|
|
|
|
end
|
|
|
|
|
2017-06-09 17:24:54 -04:00
|
|
|
expose :discussion_id do |note|
|
|
|
|
note.discussion_id(request.noteable)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :emoji_awardable?, as: :emoji_awardable
|
|
|
|
expose :award_emoji, if: -> (note, _) { note.emoji_awardable? }, using: AwardEmojiEntity
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
expose :report_abuse_path, if: -> (note, _) { note.author_id } do |note|
|
|
|
|
new_abuse_report_path(user_id: note.author_id, ref_url: Gitlab::UrlBuilder.build(note))
|
2017-06-09 17:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :noteable_note_url do |note|
|
|
|
|
noteable_note_url(note)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :resolve_path, if: -> (note, _) { note.part_of_discussion? && note.resolvable? } do |note|
|
|
|
|
resolve_project_merge_request_discussion_path(note.project, note.noteable, note.discussion_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :resolve_with_issue_path, if: -> (note, _) { note.part_of_discussion? && note.resolvable? } do |note|
|
|
|
|
new_project_issue_path(note.project, merge_request_to_resolve_discussions_of: note.noteable.iid, discussion_to_resolve: note.discussion_id)
|
|
|
|
end
|
|
|
|
|
2017-08-17 09:00:10 -04:00
|
|
|
expose :attachment, using: NoteAttachmentEntity, if: -> (note, _) { note.attachment? }
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2018-07-06 14:49:33 -04:00
|
|
|
expose :cached_markdown_version
|
|
|
|
|
2020-11-10 10:09:14 -05:00
|
|
|
# Correctly rendering a note requires some background information about any
|
|
|
|
# discussion it is part of. This is essential for the notes endpoint, but
|
|
|
|
# optional for the discussions endpoint, which will include the discussion
|
|
|
|
# along with the note
|
|
|
|
expose :discussion, as: :base_discussion, using: BaseDiscussionEntity, if: -> (_, _) { with_base_discussion? }
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
private
|
|
|
|
|
2020-11-10 10:09:14 -05:00
|
|
|
def discussion
|
|
|
|
@discussion ||= object.to_discussion(request.noteable)
|
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
def current_user
|
|
|
|
request.current_user
|
|
|
|
end
|
2020-11-10 10:09:14 -05:00
|
|
|
|
|
|
|
def with_base_discussion?
|
|
|
|
options.fetch(:with_base_discussion, true)
|
|
|
|
end
|
2017-06-09 17:24:54 -04:00
|
|
|
end
|
2019-11-15 07:06:12 -05:00
|
|
|
|
|
|
|
NoteEntity.prepend_if_ee('EE::NoteEntity')
|