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

71 lines
1.8 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
attr_reader :current_user, :target_user, :params
2020-09-03 11:15:55 +05:30
DEFAULT_LIMIT = 20
MAX_LIMIT = 100
2018-03-27 19:54:05 +05:30
def initialize(current_user, target_user, params = {})
@current_user = current_user
@target_user = target_user
@params = params
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
def execute
2018-11-18 11:00:15 +05:30
return Event.none unless can?(current_user, :read_user_profile, target_user)
2018-03-27 19:54:05 +05:30
recent_events(params[:offset] || 0)
.joins(:project)
.with_associations
2020-09-03 11:15:55 +05:30
.limit_recent(limit, params[:offset])
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
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
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
Event.from([Arel.sql(sql)])
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
def projects
2018-11-20 20:47:30 +05:30
target_user.project_interactions.to_sql
2018-03-27 19:54:05 +05:30
end
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