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
|
|
|
|
|
|
|
|
attr_reader :current_user, :target_user, :params
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
DEFAULT_LIMIT = 20
|
|
|
|
MAX_LIMIT = 100
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
def initialize(current_user, target_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@target_user = target_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-12-11 09:21:06 -05:00
|
|
|
def execute
|
2018-07-24 08:46:19 -04:00
|
|
|
return Event.none unless can?(current_user, :read_user_profile, target_user)
|
|
|
|
|
2018-03-01 06:26:45 -05:00
|
|
|
recent_events(params[:offset] || 0)
|
|
|
|
.joins(:project)
|
2017-12-11 09:21:06 -05:00
|
|
|
.with_associations
|
2020-09-02 11:10:54 -04:00
|
|
|
.limit_recent(limit, params[:offset])
|
2018-03-01 06:26:45 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
def recent_events(offset)
|
|
|
|
sql = <<~SQL
|
|
|
|
(#{projects}) AS projects_for_join
|
|
|
|
JOIN (#{target_events.to_sql}) AS #{Event.table_name}
|
|
|
|
ON #{Event.table_name}.project_id = projects_for_join.id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
# Workaround for https://github.com/rails/rails/issues/24193
|
2020-07-16 20:09:37 -04:00
|
|
|
Event.from([Arel.sql(sql)])
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-12-11 09:21:06 -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
|
|
|
|
|
|
|
def projects
|
2018-09-07 11:08:55 -04:00
|
|
|
target_user.project_interactions.to_sql
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
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
|