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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
3.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
class EventsFinder
2018-03-27 19:54:05 +05:30
prepend FinderMethods
prepend FinderWithCrossProjectAccess
2019-02-15 15:39:39 +05:30
MAX_PER_PAGE = 100
2020-03-13 15:44:24 +05:30
attr_reader :source, :params, :current_user, :scope
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
requires_cross_project_access unless: -> { source.is_a?(Project) }, model: Event
2018-03-27 19:54:05 +05:30
2017-09-10 17:25:29 +05:30
# Used to filter Events
#
# Arguments:
# source - which user or project to looks for events on
# current_user - only return events for projects visible to this user
2020-03-13 15:44:24 +05:30
# scope - return all events across a user's projects
2017-09-10 17:25:29 +05:30
# params:
# action: string
# target_type: string
# before: datetime
# after: datetime
2019-02-15 15:39:39 +05:30
# per_page: integer (max. 100)
# page: integer
# with_associations: boolean
# sort: 'asc' or 'desc'
2017-09-10 17:25:29 +05:30
def initialize(params = {})
@source = params.delete(:source)
@current_user = params.delete(:current_user)
2020-03-13 15:44:24 +05:30
@scope = params.delete(:scope)
2017-09-10 17:25:29 +05:30
@params = params
end
def execute
2020-07-02 01:45:43 +05:30
return Event.none if cannot_access_private_profile?
2020-03-13 15:44:24 +05:30
events = get_events
2017-09-10 17:25:29 +05:30
events = by_current_user_access(events)
events = by_action(events)
events = by_target_type(events)
events = by_created_at_before(events)
events = by_created_at_after(events)
2019-02-15 15:39:39 +05:30
events = sort(events)
paginated_filtered_by_user_visibility(events)
2017-09-10 17:25:29 +05:30
end
private
2020-03-13 15:44:24 +05:30
def get_events
if current_user && scope == 'all'
EventCollection.new(current_user.authorized_projects).all_project_events
else
2020-07-28 23:09:34 +05:30
source.events
2020-03-13 15:44:24 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_current_user_access(events)
2019-02-15 15:39:39 +05:30
events.merge(Project.public_or_visible_to_user(current_user))
2017-09-10 17:25:29 +05:30
.joins(:project)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_action(events)
2020-06-23 00:09:42 +05:30
safe_action = Event.actions[params[:action]]
return events unless safe_action
2017-09-10 17:25:29 +05:30
2020-06-23 00:09:42 +05:30
events.where(action: safe_action)
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_target_type(events)
return events unless Event::TARGET_TYPES[params[:target_type]]
2019-02-15 15:39:39 +05:30
events.where(target_type: Event::TARGET_TYPES[params[:target_type]].name)
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_created_at_before(events)
return events unless params[:before]
events.where('events.created_at < ?', params[:before].beginning_of_day)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def by_created_at_after(events)
return events unless params[:after]
events.where('events.created_at > ?', params[:after].end_of_day)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-02-15 15:39:39 +05:30
2020-07-02 01:45:43 +05:30
def cannot_access_private_profile?
source.is_a?(User) && !Ability.allowed?(current_user, :read_user_profile, source)
end
2019-02-15 15:39:39 +05:30
def sort(events)
return events unless params[:sort]
if params[:sort] == 'asc'
events.order_id_asc
else
events.order_id_desc
end
end
def paginated_filtered_by_user_visibility(events)
2021-12-11 22:18:48 +05:30
events_count = events.count
events = events.with_associations if params[:with_associations]
2019-02-15 15:39:39 +05:30
limited_events = events.page(page).per(per_page)
visible_events = limited_events.select { |event| event.visible_to_user?(current_user) }
2021-12-11 22:18:48 +05:30
Kaminari.paginate_array(visible_events, total_count: events_count)
2019-02-15 15:39:39 +05:30
end
def per_page
return MAX_PER_PAGE unless params[:per_page]
[params[:per_page], MAX_PER_PAGE].min
end
def page
params[:page] || 1
end
2017-09-10 17:25:29 +05:30
end