debian-mirror-gitlab/app/serializers/note_entity.rb

111 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
class NoteEntity < API::Entities::Note
include RequestAwareEntity
2018-11-08 19:23:39 +05:30
include NotesHelper
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
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
2018-03-17 18:26:18 +05:30
expose :type
expose :author, using: NoteUserEntity
unexpose :note, as: :body
expose :note
expose :redacted_note_html, as: :note_html
expose :last_edited_at, if: -> (note, _) { note.edited? }
expose :last_edited_by, using: NoteUserEntity, if: -> (note, _) { note.edited? }
expose :current_user do
expose :can_edit do |note|
2018-11-08 19:23:39 +05:30
can?(current_user, :admin_note, note)
2018-05-09 12:01:36 +05:30
end
expose :can_award_emoji do |note|
2018-11-08 19:23:39 +05:30
can?(current_user, :award_emoji, note)
end
expose :can_resolve do |note|
note.resolvable? && can?(current_user, :resolve_note, note)
2018-03-17 18:26:18 +05:30
end
2021-01-29 00:20:46 +05:30
expose :can_resolve_discussion do |note|
2021-04-29 21:17:54 +05:30
discussion = options.fetch(:discussion, nil) || note.discussion
discussion.resolvable? && discussion.can_resolve?(current_user)
2021-01-29 00:20:46 +05:30
end
2018-03-17 18:26:18 +05:30
end
2019-02-15 15:39:39 +05:30
expose :suggestions, using: SuggestionEntity
2018-03-27 19:54:05 +05:30
expose :resolved?, as: :resolved
expose :resolvable?, as: :resolvable
2018-11-08 19:23:39 +05:30
2018-03-27 19:54:05 +05:30
expose :resolved_by, using: NoteUserEntity
2018-03-17 18:26:18 +05:30
expose :system_note_icon_name, if: -> (note, _) { note.system? } do |note|
SystemNoteHelper.system_note_icon_name(note)
end
2021-12-11 22:18:48 +05:30
expose :outdated_line_change_path, if: -> (note, _) { note.system? && note.change_position&.line_range && Feature.enabled?(:display_outdated_line_diff, note.project, default_enabled: :yaml) } do |note|
outdated_line_change_namespace_project_note_path(namespace_id: note.project.namespace, project_id: note.project, id: note)
end
2020-11-24 15:15:51 +05:30
expose :is_noteable_author do |note|
note.noteable_author?(request.noteable)
end
2018-03-17 18:26:18 +05:30
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-11-20 20:47:30 +05:30
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))
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
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
2018-03-17 18:26:18 +05:30
expose :attachment, using: NoteAttachmentEntity, if: -> (note, _) { note.attachment? }
2018-11-08 19:23:39 +05:30
expose :cached_markdown_version
2021-01-29 00:20:46 +05:30
# 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-11-08 19:23:39 +05:30
private
2021-01-29 00:20:46 +05:30
def discussion
@discussion ||= object.to_discussion(request.noteable)
end
2018-11-08 19:23:39 +05:30
def current_user
request.current_user
end
2021-01-29 00:20:46 +05:30
def with_base_discussion?
options.fetch(:with_base_discussion, true)
end
2018-03-17 18:26:18 +05:30
end
2019-12-26 22:10:19 +05:30
2021-06-08 01:23:25 +05:30
NoteEntity.prepend_mod_with('NoteEntity')