notes_finder.rb 4.08 KB
Newer Older
1 2
# frozen_string_literal: true

3
class NotesFinder
4 5
  FETCH_OVERLAP = 5.seconds

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  # Used to filter Notes
  # When used with target_type and target_id this returns notes specifically for the controller
  #
  # Arguments:
  #   current_user - which user check authorizations with
  #   project - which project to look for notes on
  #   params:
  #     target_type: string
  #     target_id: integer
  #     last_fetched_at: time
  #     search: string
  #
  def initialize(project, current_user, params = {})
    @project = project
    @current_user = current_user
21
    @params = params
22 23 24
  end

  def execute
Douwe Maan's avatar
Douwe Maan committed
25 26
    notes = init_collection
    notes = since_fetch_at(notes)
27 28
    notes = notes.with_notes_filter(@params[:notes_filter]) if notes_filter?

Douwe Maan's avatar
Douwe Maan committed
29
    notes.fresh
30 31
  end

32 33
  def target
    return @target if defined?(@target)
34

35
    target_type = @params[:target_type]
36
    target_id   = @params[:target_id]
37
    target_iid  = @params[:target_iid]
38

39
    return @target = nil unless target_type
40
    return @target = nil unless target_id || target_iid
41 42 43 44 45 46

    @target =
      if target_type == "commit"
        if Ability.allowed?(@current_user, :download_code, @project)
          @project.commit(target_id)
        end
Douwe Maan's avatar
Douwe Maan committed
47
      else
48
        noteables_for_type_by_id(target_type, target_id, target_iid)
Douwe Maan's avatar
Douwe Maan committed
49
      end
50 51
  end

52 53 54
  private

  def noteables_for_type_by_id(type, id, iid)
55 56 57 58 59 60 61 62 63
    query = if id
              { id: id }
            else
              { iid: iid }
            end

    noteables_for_type(type).find_by!(query) # rubocop: disable CodeReuse/ActiveRecord
  end

64 65 66
  def init_collection
    if target
      notes_on_target
67 68
    elsif target_type
      notes_of_target_type
69 70 71 72 73
    else
      notes_of_any_type
    end
  end

74 75 76 77 78 79
  def notes_of_target_type
    notes = notes_for_type(target_type)

    search(notes)
  end

80 81 82 83
  def target_type
    @params[:target_type]
  end

84
  # rubocop: disable CodeReuse/ActiveRecord
85 86 87
  def notes_of_any_type
    types = %w(commit issue merge_request snippet)
    note_relations = types.map { |t| notes_for_type(t) }
Douwe Maan's avatar
Douwe Maan committed
88
    note_relations.map! { |notes| search(notes) }
89
    UnionFinder.new.find_union(note_relations, Note.includes(:author)) # rubocop: disable CodeReuse/Finder
90
  end
91
  # rubocop: enable CodeReuse/ActiveRecord
92 93 94 95

  def noteables_for_type(noteable_type)
    case noteable_type
    when "issue"
96
      IssuesFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder
97
    when "merge_request"
98
      MergeRequestsFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder
99
    when "snippet", "project_snippet"
100
      SnippetsFinder.new(@current_user, project: @project).execute # rubocop: disable CodeReuse/Finder
101 102
    when "personal_snippet"
      PersonalSnippet.all
103
    else
104
      raise "invalid target_type '#{noteable_type}'"
105 106 107
    end
  end

108
  # rubocop: disable CodeReuse/ActiveRecord
109 110 111 112 113 114 115 116 117 118 119 120
  def notes_for_type(noteable_type)
    if noteable_type == "commit"
      if Ability.allowed?(@current_user, :download_code, @project)
        @project.notes.where(noteable_type: 'Commit')
      else
        Note.none
      end
    else
      finder = noteables_for_type(noteable_type)
      @project.notes.where(noteable_type: finder.base_class.name, noteable_id: finder.reorder(nil))
    end
  end
121
  # rubocop: enable CodeReuse/ActiveRecord
122

123 124 125
  def notes_on_target
    if target.respond_to?(:related_notes)
      target.related_notes
126
    else
127
      target.notes
128 129 130 131 132 133 134
    end
  end

  # Searches for notes matching the given query.
  #
  # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
  #
Douwe Maan's avatar
Douwe Maan committed
135
  def search(notes)
Douwe Maan's avatar
Douwe Maan committed
136
    query = @params[:search]
Douwe Maan's avatar
Douwe Maan committed
137
    return notes unless query
Douwe Maan's avatar
Douwe Maan committed
138

139
    notes.search(query)
140 141 142 143
  end

  # Notes changed since last fetch
  # Uses overlapping intervals to avoid worrying about race conditions
Douwe Maan's avatar
Douwe Maan committed
144 145 146
  def since_fetch_at(notes)
    return notes unless @params[:last_fetched_at]

147 148
    # Default to 0 to remain compatible with old clients
    last_fetched_at = Time.at(@params.fetch(:last_fetched_at, 0).to_i)
Douwe Maan's avatar
Douwe Maan committed
149
    notes.updated_after(last_fetched_at - FETCH_OVERLAP)
150
  end
151 152 153 154

  def notes_filter?
    @params[:notes_filter].present?
  end
155
end
156

157
NotesFinder.prepend_if_ee('EE::NotesFinder')