2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
# Get user activity feed for projects common for a user and a logged in user
|
|
|
|
#
|
|
|
|
# - current_user: The user viewing the events
|
2018-09-21 04:44:34 -04:00
|
|
|
# WARNING: does not consider project feature visibility!
|
2017-12-11 09:21:06 -05:00
|
|
|
# - user: The user for which to load the events
|
|
|
|
# - params:
|
2020-09-02 11:10:54 -04:00
|
|
|
# - limit: Number of items that to be returned. Defaults to 20 and limited to 100.
|
2017-12-11 09:21:06 -05:00
|
|
|
# - offset: The page of events to return
|
|
|
|
class UserRecentEventsFinder
|
|
|
|
prepend FinderWithCrossProjectAccess
|
|
|
|
include FinderMethods
|
2018-07-24 08:46:19 -04:00
|
|
|
include Gitlab::Allowable
|
2017-12-11 09:21:06 -05:00
|
|
|
|
|
|
|
requires_cross_project_access
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
attr_reader :current_user, :target_user, :params, :event_filter
|
2017-12-11 09:21:06 -05:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
DEFAULT_LIMIT = 20
|
|
|
|
MAX_LIMIT = 100
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
def initialize(current_user, target_user, event_filter, params = {})
|
2017-12-11 09:21:06 -05:00
|
|
|
@current_user = current_user
|
|
|
|
@target_user = target_user
|
|
|
|
@params = params
|
2021-02-15 16:08:59 -05:00
|
|
|
@event_filter = event_filter || EventFilter.new(EventFilter::ALL)
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-02-15 16:08:59 -05:00
|
|
|
if target_user.is_a? User
|
|
|
|
execute_single
|
|
|
|
else
|
|
|
|
execute_multi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_single
|
2018-07-24 08:46:19 -04:00
|
|
|
return Event.none unless can?(current_user, :read_user_profile, target_user)
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
event_filter.apply_filter(target_events
|
2017-12-11 09:21:06 -05:00
|
|
|
.with_associations
|
2020-09-02 11:10:54 -04:00
|
|
|
.limit_recent(limit, params[:offset])
|
2021-02-15 16:08:59 -05:00
|
|
|
.order_created_desc)
|
2018-03-01 06:26:45 -05:00
|
|
|
end
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute_multi
|
|
|
|
users = []
|
|
|
|
@target_user.each do |user|
|
|
|
|
users.append(user.id) if can?(current_user, :read_user_profile, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
return Event.none if users.empty?
|
|
|
|
|
|
|
|
event_filter.apply_filter(Event.where(author: users).limit_recent(limit, params[:offset] || 0))
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
def target_events
|
|
|
|
Event.where(author: target_user)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
def limit
|
|
|
|
return DEFAULT_LIMIT unless params[:limit].present?
|
|
|
|
|
|
|
|
[params[:limit].to_i, MAX_LIMIT].min
|
|
|
|
end
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|