Class: About

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, StatsCacheable
Defined in:
app/models/about.rb

Defined Under Namespace

Classes: CategoryMods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ About

Returns a new instance of About.



34
35
36
# File 'app/models/about.rb', line 34

def initialize(user = nil)
  @user = user
end

Instance Attribute Details

#adminsObject

Returns the value of attribute admins.



24
25
26
# File 'app/models/about.rb', line 24

def admins
  @admins
end

#moderatorsObject

Returns the value of attribute moderators.



24
25
26
# File 'app/models/about.rb', line 24

def moderators
  @moderators
end

Class Method Details

.displayed_plugin_stat_groupsObject



4
5
6
7
8
9
# File 'app/models/about.rb', line 4

def self.displayed_plugin_stat_groups
  DiscoursePluginRegistry
    .about_stat_groups
    .select { |stat_group| stat_group[:show_in_ui] }
    .map { |stat_group| stat_group[:name] }
end

.fetch_statsObject



30
31
32
# File 'app/models/about.rb', line 30

def self.fetch_stats
  About.new.stats
end

.stats_cache_keyObject



26
27
28
# File 'app/models/about.rb', line 26

def self.stats_cache_key
  "about-stats"
end

Instance Method Details

#category_moderatorsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/about.rb', line 120

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 { |row| CategoryMods.new(row.category_id, mods.values_at(*row.user_ids)) }
end

#category_mods_limitObject



148
149
150
# File 'app/models/about.rb', line 148

def category_mods_limit
  @category_mods_limit || 100
end

#category_mods_limit=(number) ⇒ Object



152
153
154
# File 'app/models/about.rb', line 152

def category_mods_limit=(number)
  @category_mods_limit = number
end

#descriptionObject



54
55
56
# File 'app/models/about.rb', line 54

def description
  SiteSetting.site_description
end

#httpsObject



42
43
44
# File 'app/models/about.rb', line 42

def https
  SiteSetting.force_https
end

#localeObject



50
51
52
# File 'app/models/about.rb', line 50

def locale
  SiteSetting.default_locale
end

#plugin_statsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/about.rb', line 93

def plugin_stats
  final_plugin_stats = {}
  DiscoursePluginRegistry.about_stat_groups.each do |stat_group|
    begin
      stats = stat_group[:block].call
    rescue StandardError => err
      Discourse.warn_exception(
        err,
        message: "Unexpected error when collecting #{stat_group[:name]} About stats.",
      )
      next
    end

    if !stats.key?(:last_day) || !stats.key?("7_days") || !stats.key?("30_days") ||
         !stats.key?(:count)
      Rails.logger.warn(
        "Plugin stat group #{stat_group[:name]} for About stats does not have all required keys, skipping.",
      )
    else
      final_plugin_stats.merge!(
        stats.transform_keys { |key| "#{stat_group[:name]}_#{key}".to_sym },
      )
    end
  end
  final_plugin_stats
end

#statsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/about.rb', line 66

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,
  }.merge(plugin_stats)
end

#titleObject



46
47
48
# File 'app/models/about.rb', line 46

def title
  SiteSetting.title
end

#versionObject



38
39
40
# File 'app/models/about.rb', line 38

def version
  Discourse::VERSION::STRING
end