2013-07-30 03:36:34 -04:00
|
|
|
require_dependency 'trust_level'
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
class AdminUserIndexQuery
|
2014-11-24 13:34:04 -05:00
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
|
|
|
|
@params = params
|
|
|
|
@query = initialize_query_with_order(klass)
|
|
|
|
@trust_levels = trust_levels
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :params, :trust_levels
|
|
|
|
|
2017-02-25 13:51:40 -05:00
|
|
|
SORTABLE_MAPPING = {
|
|
|
|
'created' => 'created_at',
|
|
|
|
'last_emailed' => "COALESCE(last_emailed_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
|
|
|
'seen' => "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD'))",
|
|
|
|
'username' => 'username',
|
|
|
|
'email' => 'email',
|
|
|
|
'trust_level' => 'trust_level',
|
|
|
|
'days_visited' => 'user_stats.days_visited',
|
|
|
|
'posts_read' => 'user_stats.posts_read_count',
|
|
|
|
'topics_viewed' => 'user_stats.topics_entered',
|
|
|
|
'posts' => 'user_stats.post_count',
|
|
|
|
'read_time' => 'user_stats.time_read'
|
|
|
|
}
|
|
|
|
|
2014-11-24 13:34:04 -05:00
|
|
|
def find_users(limit=100)
|
2017-05-12 16:28:35 -04:00
|
|
|
page = params[:page].to_i - 1
|
|
|
|
if page < 0
|
|
|
|
page = 0
|
|
|
|
end
|
|
|
|
find_users_query.limit(limit).offset(page * limit)
|
2014-11-24 13:34:04 -05:00
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2014-11-24 13:34:04 -05:00
|
|
|
def count_users
|
|
|
|
find_users_query.count
|
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
def custom_direction
|
2017-02-25 11:13:31 -05:00
|
|
|
asc = params[:ascending]
|
2017-02-24 19:11:17 -05:00
|
|
|
asc.present? && asc ? "ASC" : "DESC"
|
|
|
|
end
|
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def initialize_query_with_order(klass)
|
2016-07-28 11:42:06 -04:00
|
|
|
order = []
|
|
|
|
|
|
|
|
custom_order = params[:order]
|
|
|
|
if custom_order.present? &&
|
2017-02-25 13:51:40 -05:00
|
|
|
without_dir = SORTABLE_MAPPING[custom_order.downcase.sub(/ (asc|desc)$/, '')]
|
|
|
|
order << "#{without_dir} #{custom_direction}"
|
2016-07-28 11:42:06 -04:00
|
|
|
end
|
2017-02-25 13:51:40 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
if !custom_order.present?
|
|
|
|
if params[:query] == "active"
|
2017-02-25 13:51:40 -05:00
|
|
|
order << "COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
|
2017-02-24 19:11:17 -05:00
|
|
|
else
|
|
|
|
order << "users.created_at DESC"
|
|
|
|
end
|
2014-11-19 15:38:53 -05:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
order << "users.username"
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
if params[:stats].present? && params[:stats] == false
|
|
|
|
klass.order(order.reject(&:blank?).join(","))
|
|
|
|
else
|
|
|
|
klass.includes(:user_stat).order(order.reject(&:blank?).join(","))
|
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_trust
|
|
|
|
levels = trust_levels.map { |key, _| key.to_s }
|
|
|
|
if levels.include?(params[:query])
|
|
|
|
@query.where('trust_level = ?', trust_levels[params[:query].to_sym])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-26 13:56:12 -05:00
|
|
|
def suspect_users
|
|
|
|
where_conds = []
|
|
|
|
|
|
|
|
# One signal: no reading yet the user has bio text
|
2014-12-01 17:15:07 -05:00
|
|
|
where_conds << "user_stats.posts_read_count <= 1 AND user_stats.topics_entered <= 1"
|
2014-11-26 13:56:12 -05:00
|
|
|
|
|
|
|
@query.activated
|
|
|
|
.references(:user_stats)
|
|
|
|
.includes(:user_profile)
|
2014-11-26 16:29:58 -05:00
|
|
|
.where("COALESCE(user_profiles.bio_raw, '') != ''")
|
2014-12-03 12:21:43 -05:00
|
|
|
.where('users.created_at <= ?', 1.day.ago)
|
2014-11-26 16:29:58 -05:00
|
|
|
.where(where_conds.map {|c| "(#{c})"}.join(" OR "))
|
2014-11-26 13:56:12 -05:00
|
|
|
end
|
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_query_classification
|
|
|
|
case params[:query]
|
2014-11-26 13:10:12 -05:00
|
|
|
when 'staff' then @query.where("admin or moderator")
|
2014-11-24 13:48:08 -05:00
|
|
|
when 'admins' then @query.where(admin: true)
|
|
|
|
when 'moderators' then @query.where(moderator: true)
|
|
|
|
when 'blocked' then @query.blocked
|
|
|
|
when 'suspended' then @query.suspended
|
|
|
|
when 'pending' then @query.not_suspended.where(approved: false)
|
2014-11-26 13:56:12 -05:00
|
|
|
when 'suspect' then suspect_users
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_search
|
|
|
|
if params[:filter].present?
|
2016-10-10 10:18:57 -04:00
|
|
|
params[:filter].strip!
|
2014-11-24 13:48:08 -05:00
|
|
|
if ip = IPAddr.new(params[:filter]) rescue nil
|
|
|
|
@query.where('ip_address <<= :ip OR registration_ip_address <<= :ip', ip: ip.to_cidr_s)
|
|
|
|
else
|
|
|
|
@query.where('username_lower ILIKE :filter OR email ILIKE :filter', filter: "%#{params[:filter]}%")
|
2014-11-24 13:34:04 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_by_ip
|
|
|
|
if params[:ip].present?
|
2016-10-10 10:18:57 -04:00
|
|
|
@query.where('ip_address = :ip OR registration_ip_address = :ip', ip: params[:ip].strip)
|
2014-06-15 12:14:19 -04:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-06-15 12:14:19 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def filter_exclude
|
|
|
|
if params[:exclude].present?
|
|
|
|
@query.where('id != ?', params[:exclude])
|
2014-11-24 13:34:04 -05:00
|
|
|
end
|
2014-11-24 13:48:08 -05:00
|
|
|
end
|
2014-11-24 12:05:40 -05:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
# this might not be needed in rails 4 ?
|
|
|
|
def append(active_relation)
|
|
|
|
@query = active_relation if active_relation
|
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2014-11-24 13:48:08 -05:00
|
|
|
def find_users_query
|
|
|
|
append filter_by_trust
|
|
|
|
append filter_by_query_classification
|
|
|
|
append filter_by_ip
|
|
|
|
append filter_exclude
|
|
|
|
append filter_by_search
|
|
|
|
@query
|
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2013-07-30 03:36:34 -04:00
|
|
|
end
|