diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index f86e45b9691..587c9c79345 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -34,10 +34,9 @@ class SiteController < ApplicationController title: SiteSetting.title, description: SiteSetting.site_description } + results[:mobile_logo_url] = SiteSetting.mobile_logo_url if SiteSetting.mobile_logo_url.present? - if SiteSetting.mobile_logo_url.present? - results[:mobile_logo_url] = SiteSetting.mobile_logo_url - end + DiscourseHub.stats_fetched_at = Time.zone.now if request.user_agent == "Discourse Hub" # this info is always available cause it can be scraped from a 404 page render json: results diff --git a/config/site_settings.yml b/config/site_settings.yml index b82e2b8be83..cffbd0ce650 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1310,7 +1310,6 @@ uncategorized: share_anonymized_statistics: true - user_preferences: default_email_digest_frequency: enum: 'DigestEmailSiteSetting' diff --git a/lib/discourse_hub.rb b/lib/discourse_hub.rb index 3176902c849..dc97b1748d6 100644 --- a/lib/discourse_hub.rb +++ b/lib/discourse_hub.rb @@ -3,19 +3,27 @@ require_dependency 'version' module DiscourseHub + STATS_FETCHED_AT_KEY = "stats_fetched_at" + def self.version_check_payload - { - installed_version: Discourse::VERSION::STRING - }.merge!( Discourse.git_branch == "unknown" ? {} : {branch: Discourse.git_branch}) + default_payload = { installed_version: Discourse::VERSION::STRING }.merge!(Discourse.git_branch == "unknown" ? {} : {branch: Discourse.git_branch}) + default_payload.merge!(get_payload) end def self.discourse_version_check get('/version_check', version_check_payload) end + def self.stats_fetched_at=(time_with_zone) + $redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i + end private + def self.get_payload + SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago ? About.fetch_cached_stats.symbolize_keys : {} + end + def self.get(rel_url, params={}) singular_action :get, rel_url, params end @@ -56,4 +64,9 @@ module DiscourseHub Discourse.base_url end + def self.stats_fetched_at + t = $redis.get(STATS_FETCHED_AT_KEY) + t ? Time.zone.at(t.to_i) : 1.year.ago + end + end diff --git a/spec/components/discourse_hub_spec.rb b/spec/components/discourse_hub_spec.rb index b9ebdee2828..29699ebeec2 100644 --- a/spec/components/discourse_hub_spec.rb +++ b/spec/components/discourse_hub_spec.rb @@ -2,11 +2,73 @@ require 'rails_helper' require_dependency 'discourse_hub' describe DiscourseHub do - describe '#discourse_version_check' do + describe '.discourse_version_check' do it 'should return just return the json that the hub returns' do hub_response = {'success' => 'OK', 'latest_version' => '0.8.1', 'critical_updates' => false} RestClient.stubs(:get).returns( hub_response.to_json ) expect(DiscourseHub.discourse_version_check).to eq(hub_response) end end + + describe '.version_check_payload' do + + describe 'when Discourse Hub has not fetched stats since past 7 days' do + it 'should include stats' do + DiscourseHub.stats_fetched_at = 8.days.ago + json = JSON.parse(DiscourseHub.version_check_payload.to_json) + + 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 + expect(json["installed_version"]).to be_present + expect(json["branch"]).to be_present + end + end + + describe 'when Discourse Hub has fetched stats in past 7 days' do + it 'should not include stats' do + DiscourseHub.stats_fetched_at = 2.days.ago + json = JSON.parse(DiscourseHub.version_check_payload.to_json) + + expect(json["topic_count"]).not_to be_present + expect(json["post_count"]).not_to be_present + expect(json["user_count"]).not_to be_present + expect(json["like_count"]).not_to be_present + expect(json["likes_7_days"]).not_to be_present + expect(json["likes_30_days"]).not_to be_present + expect(json["installed_version"]).to be_present + expect(json["branch"]).to be_present + end + end + + describe 'when send_anonymize_stats is disabled' do + describe 'when Discourse Hub has not fetched stats for the past year' do + it 'should not include stats' do + DiscourseHub.stats_fetched_at = 1.year.ago + SiteSetting.share_anonymized_statistics = false + json = JSON.parse(DiscourseHub.version_check_payload.to_json) + + expect(json["topic_count"]).not_to be_present + expect(json["post_count"]).not_to be_present + expect(json["user_count"]).not_to be_present + expect(json["like_count"]).not_to be_present + expect(json["likes_7_days"]).not_to be_present + expect(json["likes_30_days"]).not_to be_present + expect(json["installed_version"]).to be_present + expect(json["branch"]).to be_present + end + end + end + end end