diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index dbbeab8e7b9..f86e45b9691 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -3,7 +3,7 @@ require_dependency 'site_serializer' class SiteController < ApplicationController layout false skip_before_filter :preload_json, :check_xhr - skip_before_filter :redirect_to_login_if_required, only: ['basic_info'] + skip_before_filter :redirect_to_login_if_required, only: ['basic_info', 'statistics'] def site render json: Site.json_for(guardian) @@ -42,4 +42,9 @@ class SiteController < ApplicationController # this info is always available cause it can be scraped from a 404 page render json: results end + + def statistics + return redirect_to path('/') unless SiteSetting.share_anonymized_statistics? + render json: About.fetch_cached_stats + end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b2eb4e45dae..f68d72b22e4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1395,6 +1395,8 @@ en: native_app_install_banner: "Asks recurring visitors to install Discourse native app." + share_anonymized_statistics: "Share anonymized usage statistics." + max_prints_per_hour_per_user: "Maximum number of /print page impressions (set to 0 to disable)" full_name_required: "Full name is a required field of a user's profile." diff --git a/config/routes.rb b/config/routes.rb index fc53ffb18f2..7c42b21eb7b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ Discourse::Application.routes.draw do end get "site/basic-info" => 'site#basic_info' + get "site/statistics" => 'site#statistics' get "site_customizations/:key" => "site_customizations#show" diff --git a/config/site_settings.yml b/config/site_settings.yml index 1fbc4120561..b82e2b8be83 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1308,6 +1308,8 @@ uncategorized: native_app_install_banner: false + share_anonymized_statistics: true + user_preferences: default_email_digest_frequency: diff --git a/spec/controllers/site_controller_spec.rb b/spec/controllers/site_controller_spec.rb index 5e328f5a3e1..1dccba86ad9 100644 --- a/spec/controllers/site_controller_spec.rb +++ b/spec/controllers/site_controller_spec.rb @@ -24,4 +24,38 @@ describe SiteController do expect(json["mobile_logo_url"]).to eq("https://a.a/a.png") end end + + describe '.statistics' do + + it 'is visible for sites requiring login' do + SiteSetting.login_required = true + SiteSetting.share_anonymized_statistics = true + + xhr :get, :statistics + json = JSON.parse(response.body) + + expect(response).to be_success + expect(json["topic_count"]).to be_present + expect(json["post_count"]).to be_present + expect(json["user_count"]).to be_present + expect(json["topics_7_days"]).to be_present + expect(json["topics_30_days"]).to be_present + expect(json["posts_7_days"]).to be_present + expect(json["posts_30_days"]).to be_present + expect(json["users_7_days"]).to be_present + expect(json["users_30_days"]).to be_present + expect(json["active_users_7_days"]).to be_present + expect(json["active_users_30_days"]).to be_present + expect(json["like_count"]).to be_present + expect(json["likes_7_days"]).to be_present + expect(json["likes_30_days"]).to be_present + end + + it 'is not visible if site setting share_anonymized_statistics is disabled' do + SiteSetting.share_anonymized_statistics = false + + xhr :get, :statistics + expect(response).to redirect_to '/' + end + end end