debian-mirror-gitlab/app/finders/notes_finder.rb

179 lines
4.4 KiB
Ruby
Raw Normal View History

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