2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
class NotesFinder
|
2014-04-28 06:13:29 -04:00
|
|
|
FETCH_OVERLAP = 5.seconds
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
attr_reader :target_type
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
# 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:
|
2019-07-30 14:25:49 -04:00
|
|
|
# target: noteable
|
2016-12-08 20:56:31 -05:00
|
|
|
# target_type: string
|
|
|
|
# target_id: integer
|
|
|
|
# last_fetched_at: time
|
|
|
|
# search: string
|
|
|
|
#
|
2019-07-30 14:25:49 -04:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@project = params[:project]
|
2016-12-08 20:56:31 -05:00
|
|
|
@current_user = current_user
|
2019-07-30 14:25:49 -04:00
|
|
|
@params = params.dup
|
|
|
|
@target_type = @params[:target_type]
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2017-03-30 23:06:09 -04:00
|
|
|
notes = init_collection
|
|
|
|
notes = since_fetch_at(notes)
|
2018-10-23 05:49:45 -04:00
|
|
|
notes = notes.with_notes_filter(@params[:notes_filter]) if notes_filter?
|
|
|
|
|
2017-03-30 23:06:09 -04:00
|
|
|
notes.fresh
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def target
|
|
|
|
return @target if defined?(@target)
|
2016-12-08 20:56:31 -05:00
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
if target_given?
|
|
|
|
use_explicit_target
|
|
|
|
else
|
|
|
|
find_target_by_type_and_ids
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def target_given?
|
|
|
|
@params.key?(:target)
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_explicit_target
|
|
|
|
@target = @params[:target]
|
|
|
|
@target_type = @target.class.name.underscore
|
|
|
|
|
|
|
|
@target
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_target_by_type_and_ids
|
2017-03-09 20:29:11 -05:00
|
|
|
target_id = @params[:target_id]
|
2019-06-15 01:10:58 -04:00
|
|
|
target_iid = @params[:target_iid]
|
2017-03-09 20:29:11 -05:00
|
|
|
|
2019-06-15 01:19:54 -04:00
|
|
|
return @target = nil unless target_type
|
2019-06-15 03:05:45 -04:00
|
|
|
return @target = nil unless target_id || target_iid
|
2017-03-09 20:29:11 -05:00
|
|
|
|
|
|
|
@target =
|
|
|
|
if target_type == "commit"
|
|
|
|
if Ability.allowed?(@current_user, :download_code, @project)
|
|
|
|
@project.commit(target_id)
|
|
|
|
end
|
2017-02-22 10:10:32 -05:00
|
|
|
else
|
2019-07-30 14:25:49 -04:00
|
|
|
noteable_for_type_by_id(target_type, target_id, target_iid)
|
2017-02-22 10:10:32 -05:00
|
|
|
end
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
def noteable_for_type_by_id(type, id, iid)
|
2019-06-15 01:10:58 -04:00
|
|
|
query = if id
|
|
|
|
{ id: id }
|
|
|
|
else
|
|
|
|
{ iid: iid }
|
|
|
|
end
|
|
|
|
|
|
|
|
noteables_for_type(type).find_by!(query) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def init_collection
|
|
|
|
if target
|
|
|
|
notes_on_target
|
2018-03-05 08:25:56 -05:00
|
|
|
elsif target_type
|
|
|
|
notes_of_target_type
|
2017-03-09 20:29:11 -05:00
|
|
|
else
|
|
|
|
notes_of_any_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:25:56 -05:00
|
|
|
def notes_of_target_type
|
|
|
|
notes = notes_for_type(target_type)
|
|
|
|
|
|
|
|
search(notes)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-12-08 20:56:31 -05:00
|
|
|
def notes_of_any_type
|
|
|
|
types = %w(commit issue merge_request snippet)
|
|
|
|
note_relations = types.map { |t| notes_for_type(t) }
|
2017-03-30 23:06:09 -04:00
|
|
|
note_relations.map! { |notes| search(notes) }
|
2018-08-27 11:31:01 -04:00
|
|
|
UnionFinder.new.find_union(note_relations, Note.includes(:author)) # rubocop: disable CodeReuse/Finder
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
def noteables_for_type(noteable_type)
|
|
|
|
case noteable_type
|
|
|
|
when "issue"
|
2018-08-27 11:31:01 -04:00
|
|
|
IssuesFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder
|
2016-12-08 20:56:31 -05:00
|
|
|
when "merge_request"
|
2018-08-27 11:31:01 -04:00
|
|
|
MergeRequestsFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder
|
2016-12-08 20:56:31 -05:00
|
|
|
when "snippet", "project_snippet"
|
2018-08-27 11:31:01 -04:00
|
|
|
SnippetsFinder.new(@current_user, project: @project).execute # rubocop: disable CodeReuse/Finder
|
2017-04-27 06:41:26 -04:00
|
|
|
when "personal_snippet"
|
|
|
|
PersonalSnippet.all
|
2016-12-08 20:56:31 -05:00
|
|
|
else
|
2018-06-21 08:22:40 -04:00
|
|
|
raise "invalid target_type '#{noteable_type}'"
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-12-08 20:56:31 -05:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-12-08 20:56:31 -05:00
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def notes_on_target
|
|
|
|
if target.respond_to?(:related_notes)
|
|
|
|
target.related_notes
|
2016-12-08 20:56:31 -05:00
|
|
|
else
|
2017-03-09 20:29:11 -05:00
|
|
|
target.notes
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Searches for notes matching the given query.
|
|
|
|
#
|
2020-06-15 23:08:24 -04:00
|
|
|
# This method uses ILIKE on PostgreSQL.
|
2016-12-08 20:56:31 -05:00
|
|
|
#
|
2017-03-31 17:52:38 -04:00
|
|
|
def search(notes)
|
2017-03-30 23:06:09 -04:00
|
|
|
query = @params[:search]
|
2017-03-31 17:52:38 -04:00
|
|
|
return notes unless query
|
2017-03-30 23:06:09 -04:00
|
|
|
|
2017-11-24 05:45:19 -05:00
|
|
|
notes.search(query)
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Notes changed since last fetch
|
|
|
|
# Uses overlapping intervals to avoid worrying about race conditions
|
2017-03-30 23:06:09 -04:00
|
|
|
def since_fetch_at(notes)
|
|
|
|
return notes unless @params[:last_fetched_at]
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
# Default to 0 to remain compatible with old clients
|
|
|
|
last_fetched_at = Time.at(@params.fetch(:last_fetched_at, 0).to_i)
|
2017-03-30 23:06:09 -04:00
|
|
|
notes.updated_after(last_fetched_at - FETCH_OVERLAP)
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
2018-10-23 05:49:45 -04:00
|
|
|
|
|
|
|
def notes_filter?
|
|
|
|
@params[:notes_filter].present?
|
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
NotesFinder.prepend_if_ee('EE::NotesFinder')
|