2014-02-25 12:15:08 -05:00
|
|
|
class NotesFinder
|
2014-04-28 06:13:29 -04:00
|
|
|
FETCH_OVERLAP = 5.seconds
|
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
def execute(project, current_user, params)
|
|
|
|
target_type = params[:target_type]
|
|
|
|
target_id = params[:target_id]
|
2014-04-28 10:37:19 -04:00
|
|
|
# Default to 0 to remain compatible with old clients
|
|
|
|
last_fetched_at = Time.at(params.fetch(:last_fetched_at, 0).to_i)
|
2014-02-25 12:15:08 -05:00
|
|
|
|
2015-02-03 00:26:40 -05:00
|
|
|
notes =
|
|
|
|
case target_type
|
|
|
|
when "commit"
|
2016-05-10 18:41:46 -04:00
|
|
|
project.notes.for_commit_id(target_id).non_diff_notes
|
2015-02-03 00:26:40 -05:00
|
|
|
when "issue"
|
2016-06-14 13:37:41 -04:00
|
|
|
project.issues.visible_to_user(current_user).find(target_id).notes.inc_author
|
2015-02-03 00:26:40 -05:00
|
|
|
when "merge_request"
|
2016-04-25 04:09:39 -04:00
|
|
|
project.merge_requests.find(target_id).mr_and_commit_notes.inc_author
|
2015-02-03 00:26:40 -05:00
|
|
|
when "snippet", "project_snippet"
|
2015-02-06 13:21:48 -05:00
|
|
|
project.snippets.find(target_id).notes
|
2015-02-03 00:26:40 -05:00
|
|
|
else
|
|
|
|
raise 'invalid target_type'
|
|
|
|
end
|
2014-04-28 06:13:29 -04:00
|
|
|
|
|
|
|
# Use overlapping intervals to avoid worrying about race conditions
|
2015-02-06 13:21:48 -05:00
|
|
|
notes.where('updated_at > ?', last_fetched_at - FETCH_OVERLAP).fresh
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
|
|
|
end
|