discussion_entity.rb 2.57 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
class DiscussionEntity < Grape::Entity
  include RequestAwareEntity
5
  include NotesHelper
6 7

  expose :id, :reply_id
8
  expose :position, if: -> (d, _) { d.diff_discussion? && !d.legacy_diff_discussion? }
9
  expose :original_position, if: -> (d, _) { d.diff_discussion? && !d.legacy_diff_discussion? }
10
  expose :line_code, if: -> (d, _) { d.diff_discussion? }
11
  expose :expanded?, as: :expanded
12 13
  expose :active?, as: :active, if: -> (d, _) { d.diff_discussion? }
  expose :project_id
14

15 16 17
  expose :notes do |discussion, opts|
    request.note_entity.represent(discussion.notes, opts)
  end
18

19 20 21 22
  expose :discussion_path do |discussion|
    discussion_path(discussion)
  end

23
  expose :individual_note?, as: :individual_note
24 25 26 27
  expose :resolvable do |discussion|
    discussion.resolvable?
  end

28
  expose :resolved?, as: :resolved
29
  expose :resolved_by_push?, as: :resolved_by_push
30
  expose :resolved_by, using: NoteUserEntity
31
  expose :resolved_at
32 33 34
  expose :resolve_path, if: -> (d, _) { d.resolvable? } do |discussion|
    resolve_project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion.id)
  end
35
  expose :resolve_with_issue_path, if: -> (d, _) { d.resolvable? } do |discussion|
36 37 38
    new_project_issue_path(discussion.project, merge_request_to_resolve_discussions_of: discussion.noteable.iid, discussion_to_resolve: discussion.id)
  end

39
  expose :diff_file, using: DiffFileEntity, if: -> (d, _) { d.diff_discussion? }
40 41 42

  expose :diff_discussion?, as: :diff_discussion

43 44
  expose :truncated_diff_lines_path, if: -> (d, _) { !d.expanded? && !render_truncated_diff_lines? } do |discussion|
    project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion)
45 46
  end

47
  expose :truncated_diff_lines, using: DiffLineEntity, if: -> (d, _) { d.diff_discussion? && d.on_text? && (d.expanded? || render_truncated_diff_lines?) }
48 49

  expose :image_diff_html, if: -> (d, _) { d.diff_discussion? && d.on_image? } do |discussion|
50 51 52 53 54 55 56 57 58 59 60
    diff_file = discussion.diff_file
    partial = diff_file.new_file? || diff_file.deleted_file? ? 'single_image_diff' : 'replaced_image_diff'
    options[:context].render_to_string(
      partial: "projects/diffs/#{partial}",
      locals: { diff_file: diff_file,
                position: discussion.position.to_json,
                click_to_comment: false },
      layout: false,
      formats: [:html]
    )
  end
61 62 63 64 65 66 67 68 69 70 71 72 73

  expose :for_commit?, as: :for_commit
  expose :commit_id

  private

  def render_truncated_diff_lines?
    options[:render_truncated_diff_lines]
  end

  def current_user
    request.current_user
  end
74
end