summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2019-08-01 16:27:02 +0000
committerStan Hu <stanhu@gmail.com>2019-08-01 16:27:02 +0000
commitdbc6c2440348578be0e9dfb2197adfe3e9c457dd (patch)
treec3a9a851c4505946c9bd4dbab460e9c6e9835c14 /app/finders
parent536ebecf7eec95bd2cc1b542f1c35d70f814731e (diff)
parente13b5dc7540b85673e066a7a7c26fc3abbecbfe7 (diff)
downloadgitlab-ce-dbc6c2440348578be0e9dfb2197adfe3e9c457dd.tar.gz
Merge branch '52123-issuable-actions-notesfinder-pderichs' into 'master'
Use NotesFinder to fetch notes on API and Controllers Closes #52123 See merge request gitlab-org/gitlab-ce!31300
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/notes_finder.rb42
1 files changed, 30 insertions, 12 deletions
diff --git a/app/finders/notes_finder.rb b/app/finders/notes_finder.rb
index 8f610d7dddb..f7d9100bb78 100644
--- a/app/finders/notes_finder.rb
+++ b/app/finders/notes_finder.rb
@@ -3,6 +3,8 @@
class NotesFinder
FETCH_OVERLAP = 5.seconds
+ attr_reader :target_type
+
# Used to filter Notes
# When used with target_type and target_id this returns notes specifically for the controller
#
@@ -10,15 +12,17 @@ class NotesFinder
# current_user - which user check authorizations with
# project - which project to look for notes on
# params:
+ # target: noteable
# target_type: string
# target_id: integer
# last_fetched_at: time
# search: string
#
- def initialize(project, current_user, params = {})
- @project = project
+ def initialize(current_user, params = {})
+ @project = params[:project]
@current_user = current_user
- @params = params
+ @params = params.dup
+ @target_type = @params[:target_type]
end
def execute
@@ -32,7 +36,27 @@ class NotesFinder
def target
return @target if defined?(@target)
- target_type = @params[:target_type]
+ 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
target_id = @params[:target_id]
target_iid = @params[:target_iid]
@@ -45,13 +69,11 @@ class NotesFinder
@project.commit(target_id)
end
else
- noteables_for_type_by_id(target_type, target_id, target_iid)
+ noteable_for_type_by_id(target_type, target_id, target_iid)
end
end
- private
-
- def noteables_for_type_by_id(type, id, iid)
+ def noteable_for_type_by_id(type, id, iid)
query = if id
{ id: id }
else
@@ -77,10 +99,6 @@ class NotesFinder
search(notes)
end
- def target_type
- @params[:target_type]
- end
-
# rubocop: disable CodeReuse/ActiveRecord
def notes_of_any_type
types = %w(commit issue merge_request snippet)