2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
class EventsFinder
|
2017-12-11 09:21:06 -05:00
|
|
|
prepend FinderMethods
|
|
|
|
prepend FinderWithCrossProjectAccess
|
2018-12-07 12:09:00 -05:00
|
|
|
|
|
|
|
MAX_PER_PAGE = 100
|
|
|
|
|
2019-12-18 13:08:04 -05:00
|
|
|
attr_reader :source, :params, :current_user, :scope
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-12-07 12:09:00 -05:00
|
|
|
requires_cross_project_access unless: -> { source.is_a?(Project) }, model: Event
|
2017-12-11 09:21:06 -05:00
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
# 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
|
2019-12-18 13:08:04 -05:00
|
|
|
# scope - return all events across a user's projects
|
2017-05-29 01:49:17 -04:00
|
|
|
# params:
|
|
|
|
# action: string
|
|
|
|
# target_type: string
|
|
|
|
# before: datetime
|
|
|
|
# after: datetime
|
2018-12-07 12:09:00 -05:00
|
|
|
# per_page: integer (max. 100)
|
|
|
|
# page: integer
|
|
|
|
# with_associations: boolean
|
|
|
|
# sort: 'asc' or 'desc'
|
2017-05-29 01:49:17 -04:00
|
|
|
def initialize(params = {})
|
|
|
|
@source = params.delete(:source)
|
|
|
|
@current_user = params.delete(:current_user)
|
2019-12-18 13:08:04 -05:00
|
|
|
@scope = params.delete(:scope)
|
2017-05-29 01:49:17 -04:00
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-07-01 17:08:51 -04:00
|
|
|
return Event.none if cannot_access_private_profile?
|
|
|
|
|
2019-12-18 13:08:04 -05:00
|
|
|
events = get_events
|
2017-05-29 01:49:17 -04:00
|
|
|
|
|
|
|
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)
|
2018-12-07 12:09:00 -05:00
|
|
|
events = sort(events)
|
|
|
|
|
|
|
|
events = events.with_associations if params[:with_associations]
|
|
|
|
paginated_filtered_by_user_visibility(events)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-18 13:08:04 -05:00
|
|
|
def get_events
|
2020-01-20 10:09:18 -05:00
|
|
|
if current_user && scope == 'all'
|
|
|
|
EventCollection.new(current_user.authorized_projects).all_project_events
|
|
|
|
else
|
2020-07-14 11:09:05 -04:00
|
|
|
source.events
|
2020-01-20 10:09:18 -05:00
|
|
|
end
|
2019-12-18 13:08:04 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_current_user_access(events)
|
2018-12-07 12:09:00 -05:00
|
|
|
events.merge(Project.public_or_visible_to_user(current_user))
|
2017-06-21 09:48:12 -04:00
|
|
|
.joins(:project)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_action(events)
|
2020-05-28 11:08:02 -04:00
|
|
|
safe_action = Event.actions[params[:action]]
|
|
|
|
return events unless safe_action
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2020-05-28 11:08:02 -04:00
|
|
|
events.where(action: safe_action)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_target_type(events)
|
|
|
|
return events unless Event::TARGET_TYPES[params[:target_type]]
|
|
|
|
|
2018-11-17 10:14:36 -05:00
|
|
|
events.where(target_type: Event::TARGET_TYPES[params[:target_type]].name)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_created_at_before(events)
|
|
|
|
return events unless params[:before]
|
|
|
|
|
|
|
|
events.where('events.created_at < ?', params[:before].beginning_of_day)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_created_at_after(events)
|
|
|
|
return events unless params[:after]
|
|
|
|
|
|
|
|
events.where('events.created_at > ?', params[:after].end_of_day)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-12-07 12:09:00 -05:00
|
|
|
|
2020-07-01 17:08:51 -04:00
|
|
|
def cannot_access_private_profile?
|
|
|
|
source.is_a?(User) && !Ability.allowed?(current_user, :read_user_profile, source)
|
|
|
|
end
|
|
|
|
|
2018-12-07 12:09:00 -05:00
|
|
|
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)
|
|
|
|
limited_events = events.page(page).per(per_page)
|
|
|
|
visible_events = limited_events.select { |event| event.visible_to_user?(current_user) }
|
|
|
|
|
|
|
|
Kaminari.paginate_array(visible_events, total_count: events.count)
|
|
|
|
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-05-29 01:49:17 -04:00
|
|
|
end
|