discourse/app/models/about.rb

126 lines
3.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2014-08-11 15:59:00 -05:00
class About
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 15:59:00 -05:00
include ActiveModel::Serialization
2015-07-06 23:52:19 -05:00
include StatsCacheable
2014-08-11 15:59:00 -05:00
attr_accessor :moderators,
:admins
2015-07-06 23:52:19 -05:00
def self.stats_cache_key
'about-stats'
end
def self.fetch_stats
About.new.stats
end
def initialize(user = nil)
@user = user
end
2014-08-11 17:15:35 -05:00
def version
Discourse::VERSION::STRING
end
def https
SiteSetting.force_https
end
2014-08-11 17:15:35 -05:00
def title
SiteSetting.title
end
def locale
SiteSetting.default_locale
end
def description
SiteSetting.site_description
end
2014-08-11 15:59:00 -05:00
def moderators
@moderators ||= User.where(moderator: true, admin: false)
2017-07-27 20:20:09 -05:00
.human_users
.order("last_seen_at DESC")
2014-08-11 15:59:00 -05:00
end
def admins
@admins ||= User.where(admin: true)
.human_users
.order("last_seen_at DESC")
2014-08-11 15:59:00 -05:00
end
def stats
@stats ||= {
topic_count: Topic.listable_topics.count,
topics_last_day: Topic.listable_topics.where('created_at > ?', 1.days.ago).count,
topics_7_days: Topic.listable_topics.where('created_at > ?', 7.days.ago).count,
topics_30_days: Topic.listable_topics.where('created_at > ?', 30.days.ago).count,
post_count: Post.count,
posts_last_day: Post.where('created_at > ?', 1.days.ago).count,
posts_7_days: Post.where('created_at > ?', 7.days.ago).count,
posts_30_days: Post.where('created_at > ?', 30.days.ago).count,
user_count: User.real.count,
users_last_day: User.real.where('created_at > ?', 1.days.ago).count,
users_7_days: User.real.where('created_at > ?', 7.days.ago).count,
users_30_days: User.real.where('created_at > ?', 30.days.ago).count,
active_users_last_day: User.where('last_seen_at > ?', 1.days.ago).count,
active_users_7_days: User.where('last_seen_at > ?', 7.days.ago).count,
active_users_30_days: User.where('last_seen_at > ?', 30.days.ago).count,
like_count: UserAction.where(action_type: UserAction::LIKE).count,
likes_last_day: UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 1.days.ago).count,
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 15:59:00 -05:00
}
end
def category_moderators
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
return [] if category_ids.blank?
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)
SELECT c.id category_id
, (ARRAY_AGG(u.id ORDER BY u.last_seen_at DESC))[:#{per_cat_limit}] user_ids
FROM categories c
JOIN group_users gu ON gu.group_id = c.reviewable_by_group_id
JOIN users u ON u.id = gu.user_id
WHERE c.id IN (:category_ids)
GROUP BY c.id
ORDER BY c.position
SQL
mods = User.where(id: results.map(&:user_ids).flatten.uniq).index_by(&:id)
results.map do |row|
CategoryMods.new(row.category_id, mods.values_at(*row.user_ids))
end
end
def category_mods_limit
@category_mods_limit || 100
end
def category_mods_limit=(number)
@category_mods_limit = number
end
2014-08-11 15:59:00 -05:00
end