2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-11 16:59:00 -04:00
|
|
|
class About
|
2019-07-31 09:46:58 -04:00
|
|
|
class CategoryMods
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
attr_reader :category_id, :moderators
|
|
|
|
|
|
|
|
def initialize(category_id, moderators)
|
|
|
|
@category_id = category_id
|
|
|
|
@moderators = moderators
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-11 16:59:00 -04:00
|
|
|
include ActiveModel::Serialization
|
2015-07-07 00:52:19 -04:00
|
|
|
include StatsCacheable
|
2014-08-11 16:59:00 -04:00
|
|
|
|
|
|
|
attr_accessor :moderators,
|
|
|
|
:admins
|
|
|
|
|
2015-07-07 00:52:19 -04:00
|
|
|
def self.stats_cache_key
|
|
|
|
'about-stats'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetch_stats
|
|
|
|
About.new.stats
|
|
|
|
end
|
|
|
|
|
2019-07-31 09:46:58 -04:00
|
|
|
def initialize(user = nil)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2014-08-11 18:15:35 -04:00
|
|
|
def version
|
|
|
|
Discourse::VERSION::STRING
|
|
|
|
end
|
|
|
|
|
2014-11-24 01:54:17 -05:00
|
|
|
def https
|
2016-06-27 05:26:43 -04:00
|
|
|
SiteSetting.force_https
|
2014-11-24 01:54:17 -05:00
|
|
|
end
|
|
|
|
|
2014-08-11 18:15:35 -04:00
|
|
|
def title
|
|
|
|
SiteSetting.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def locale
|
|
|
|
SiteSetting.default_locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
SiteSetting.site_description
|
|
|
|
end
|
|
|
|
|
2014-08-11 16:59:00 -04:00
|
|
|
def moderators
|
2014-08-20 12:42:33 -04:00
|
|
|
@moderators ||= User.where(moderator: true, admin: false)
|
2017-07-27 21:20:09 -04:00
|
|
|
.human_users
|
2019-01-15 06:55:22 -05:00
|
|
|
.order("last_seen_at DESC")
|
2014-08-11 16:59:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def admins
|
2019-01-15 06:55:22 -05:00
|
|
|
@admins ||= User.where(admin: true)
|
|
|
|
.human_users
|
|
|
|
.order("last_seen_at DESC")
|
2014-08-11 16:59:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats
|
|
|
|
@stats ||= {
|
|
|
|
topic_count: Topic.listable_topics.count,
|
|
|
|
post_count: Post.count,
|
2015-02-17 15:31:50 -05:00
|
|
|
user_count: User.real.count,
|
2014-08-11 16:59:00 -04:00
|
|
|
topics_7_days: Topic.listable_topics.where('created_at > ?', 7.days.ago).count,
|
2015-02-02 12:27:49 -05:00
|
|
|
topics_30_days: Topic.listable_topics.where('created_at > ?', 30.days.ago).count,
|
2014-08-11 16:59:00 -04:00
|
|
|
posts_7_days: Post.where('created_at > ?', 7.days.ago).count,
|
2015-02-02 12:27:49 -05:00
|
|
|
posts_30_days: Post.where('created_at > ?', 30.days.ago).count,
|
2014-08-11 18:15:35 -04:00
|
|
|
users_7_days: User.where('created_at > ?', 7.days.ago).count,
|
2015-02-02 12:27:49 -05:00
|
|
|
users_30_days: User.where('created_at > ?', 30.days.ago).count,
|
2015-01-30 17:23:52 -05:00
|
|
|
active_users_7_days: User.where('last_seen_at > ?', 7.days.ago).count,
|
2015-02-02 12:27:49 -05:00
|
|
|
active_users_30_days: User.where('last_seen_at > ?', 30.days.ago).count,
|
2014-08-11 18:15:35 -04:00
|
|
|
like_count: UserAction.where(action_type: UserAction::LIKE).count,
|
2015-02-02 12:27:49 -05:00
|
|
|
likes_7_days: UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 7.days.ago).count,
|
|
|
|
likes_30_days: UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 30.days.ago).count
|
2014-08-11 16:59:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-31 09:46:58 -04:00
|
|
|
def category_moderators
|
2019-10-03 14:48:56 -04:00
|
|
|
allowed_cats = Guardian.new(@user).allowed_category_ids
|
|
|
|
return [] if allowed_cats.blank?
|
|
|
|
cats_with_mods = Category.where.not(reviewable_by_group_id: nil).pluck(:id)
|
|
|
|
category_ids = cats_with_mods & allowed_cats
|
2019-07-31 09:46:58 -04:00
|
|
|
return [] if category_ids.blank?
|
2019-10-03 14:48:56 -04:00
|
|
|
|
|
|
|
per_cat_limit = category_mods_limit / category_ids.size
|
|
|
|
per_cat_limit = 1 if per_cat_limit < 1
|
|
|
|
results = DB.query(<<~SQL, category_ids: category_ids, per_cat_limit: per_cat_limit)
|
|
|
|
SELECT c.id category_id, user_ids
|
2019-07-31 09:46:58 -04:00
|
|
|
FROM categories c
|
2019-10-03 14:48:56 -04:00
|
|
|
CROSS JOIN LATERAL (
|
|
|
|
SELECT ARRAY(
|
|
|
|
SELECT u.id
|
|
|
|
FROM users u
|
|
|
|
JOIN group_users gu
|
|
|
|
ON gu.group_id = c.reviewable_by_group_id AND gu.user_id = u.id
|
|
|
|
ORDER BY last_seen_at DESC
|
|
|
|
LIMIT :per_cat_limit
|
|
|
|
) AS user_ids
|
|
|
|
) user_ids
|
2019-07-31 09:46:58 -04:00
|
|
|
WHERE c.id IN (:category_ids)
|
|
|
|
SQL
|
|
|
|
moderators = {}
|
2019-10-03 14:48:56 -04:00
|
|
|
User.where(id: results.map(&:user_ids).flatten.uniq).each do |user|
|
2019-07-31 09:46:58 -04:00
|
|
|
moderators[user.id] = user
|
|
|
|
end
|
|
|
|
moderators
|
|
|
|
results.map do |row|
|
|
|
|
CategoryMods.new(row.category_id, row.user_ids.map { |id| moderators[id] })
|
|
|
|
end
|
|
|
|
end
|
2019-10-03 14:48:56 -04:00
|
|
|
|
|
|
|
def category_mods_limit
|
|
|
|
@category_mods_limit || 100
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_mods_limit=(number)
|
|
|
|
@category_mods_limit = number
|
|
|
|
end
|
2014-08-11 16:59:00 -04:00
|
|
|
end
|