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

74 lines
1.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
# Get user activity feed for projects common for a user and a logged in user
#
# - current_user: The user viewing the events
2018-11-08 19:23:39 +05:30
# WARNING: does not consider project feature visibility!
2018-03-27 19:54:05 +05:30
# - user: The user for which to load the events
# - params:
2020-09-03 11:15:55 +05:30
# - limit: Number of items that to be returned. Defaults to 20 and limited to 100.
2018-03-27 19:54:05 +05:30
# - offset: The page of events to return
class UserRecentEventsFinder
prepend FinderWithCrossProjectAccess
include FinderMethods
2018-11-18 11:00:15 +05:30
include Gitlab::Allowable
2018-03-27 19:54:05 +05:30
requires_cross_project_access
2021-03-11 19:13:27 +05:30
attr_reader :current_user, :target_user, :params, :event_filter
2018-03-27 19:54:05 +05:30
2020-09-03 11:15:55 +05:30
DEFAULT_LIMIT = 20
MAX_LIMIT = 100
2018-03-27 19:54:05 +05:30
2021-03-11 19:13:27 +05:30
def initialize(current_user, target_user, event_filter, params = {})
2018-03-27 19:54:05 +05:30
@current_user = current_user
@target_user = target_user
@params = params
2021-03-11 19:13:27 +05:30
@event_filter = event_filter || EventFilter.new(EventFilter::ALL)
2018-03-27 19:54:05 +05:30
end
def execute
2021-03-11 19:13:27 +05:30
if target_user.is_a? User
execute_single
else
execute_multi
end
end
private
def execute_single
2018-11-18 11:00:15 +05:30
return Event.none unless can?(current_user, :read_user_profile, target_user)
2021-03-11 19:13:27 +05:30
event_filter.apply_filter(target_events
2018-03-27 19:54:05 +05:30
.with_associations
2020-09-03 11:15:55 +05:30
.limit_recent(limit, params[:offset])
2021-03-11 19:13:27 +05:30
.order_created_desc)
2018-03-27 19:54:05 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-03-11 19:13:27 +05:30
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?
2018-03-27 19:54:05 +05:30
2021-03-11 19:13:27 +05:30
event_filter.apply_filter(Event.where(author: users).limit_recent(limit, params[:offset] || 0))
2018-03-27 19:54:05 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
def target_events
Event.where(author: target_user)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
2020-09-03 11:15:55 +05:30
def limit
return DEFAULT_LIMIT unless params[:limit].present?
[params[:limit].to_i, MAX_LIMIT].min
end
2018-03-27 19:54:05 +05:30
end