From 9dc63258211ff0d55989f437ca2bd4740d556576 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 4 Apr 2024 00:22:28 +0530 Subject: [PATCH] DEV: add logo URL and locale details to the Discover stats. (#26320) We will be collecting the logo URL and the site's default locale values along with existing basic details to display the site on the Discourse Discover listing page. It will be included only if the site is opted-in by enabling the "`include_in_discourse_discover`" site setting. Also, we no longer going to use `about.json` and `site/statistics.json` endpoints retrieve these data. We will be using only the `site/basic-info.json` endpoint. --- app/controllers/site_controller.rb | 10 +++++++--- app/models/about.rb | 8 ++++++++ app/models/stat.rb | 3 --- app/serializers/about_serializer.rb | 11 +---------- lib/guardian.rb | 4 ++++ lib/statistics.rb | 4 ---- spec/lib/plugin/instance_spec.rb | 1 - spec/requests/about_controller_spec.rb | 11 ----------- spec/requests/site_controller_spec.rb | 12 +----------- 9 files changed, 21 insertions(+), 43 deletions(-) diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 11ea1d58c0b..35f73888d9e 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -42,9 +42,13 @@ class SiteController < ApplicationController results[:mobile_logo_url] = UrlHelper.absolute(mobile_logo_url) end - results[:discourse_discover_enrolled] = true if SiteSetting.include_in_discourse_discover? - - DiscourseHub.stats_fetched_at = Time.zone.now if request.user_agent == "Discourse Hub" + if guardian.is_discourse_hub_request? + DiscourseHub.stats_fetched_at = Time.zone.now + discover_data = About.discourse_discover + discover_data.each_key do |key| + results["discourse_discover_#{key}".to_sym] = discover_data[key] + end + end # this info is always available cause it can be scraped from a 404 page render json: results diff --git a/app/models/about.rb b/app/models/about.rb index 4a1821d1c0a..a830c77d887 100644 --- a/app/models/about.rb +++ b/app/models/about.rb @@ -97,4 +97,12 @@ class About def category_mods_limit=(number) @category_mods_limit = number end + + def self.discourse_discover + @discourse_discover ||= { + enrolled: SiteSetting.include_in_discourse_discover?, + logo_url: UrlHelper.absolute(SiteSetting.site_logo_url), + locale: SiteSetting.default_locale, + } + end end diff --git a/app/models/stat.rb b/app/models/stat.rb index 70e9b773e4b..b993d7326a8 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -46,9 +46,6 @@ class Stat Stat.new("users", show_in_ui: true, expose_via_api: true) { Statistics.users }, Stat.new("active_users", show_in_ui: true, expose_via_api: true) { Statistics.active_users }, Stat.new("likes", show_in_ui: true, expose_via_api: true) { Statistics.likes }, - Stat.new("discourse_discover", show_in_ui: false, expose_via_api: true) do - Statistics.discourse_discover - end, ] end diff --git a/app/serializers/about_serializer.rb b/app/serializers/about_serializer.rb index 89a2efdb557..ea76852ab63 100644 --- a/app/serializers/about_serializer.rb +++ b/app/serializers/about_serializer.rb @@ -23,8 +23,7 @@ class AboutSerializer < ApplicationSerializer :https, :can_see_about_stats, :contact_url, - :contact_email, - :discourse_discover_enrolled + :contact_email def include_stats? can_see_about_stats @@ -50,14 +49,6 @@ class AboutSerializer < ApplicationSerializer SiteSetting.contact_email end - def discourse_discover_enrolled - SiteSetting.include_in_discourse_discover? - end - - def include_discourse_discover_enrolled? - SiteSetting.include_in_discourse_discover? - end - private def can_see_about_stats diff --git a/lib/guardian.rb b/lib/guardian.rb index 8d82d94df8a..1c1ba0296d4 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -637,6 +637,10 @@ class Guardian other && authenticated? && other.is_a?(User) && @user == other end + def is_discourse_hub_request? + request&.user_agent == "Discourse Hub" + end + private def is_my_own?(obj) diff --git a/lib/statistics.rb b/lib/statistics.rb index bf4779d11db..3ae1fc36f89 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -47,8 +47,4 @@ class Statistics count: User.real.count, } end - - def self.discourse_discover - { enrolled: SiteSetting.include_in_discourse_discover? } - end end diff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rb index 60485e36d60..7c8050fb32c 100644 --- a/spec/lib/plugin/instance_spec.rb +++ b/spec/lib/plugin/instance_spec.rb @@ -103,7 +103,6 @@ TEXT :likes_7_days, :likes_30_days, :likes_count, - :discourse_discover_enrolled, ) end diff --git a/spec/requests/about_controller_spec.rb b/spec/requests/about_controller_spec.rb index 54d1945e176..eb719ae9f7d 100644 --- a/spec/requests/about_controller_spec.rb +++ b/spec/requests/about_controller_spec.rb @@ -48,17 +48,6 @@ RSpec.describe AboutController do expect(response.parsed_body["about"].keys).to include("stats") end - it "adds Discourse Discover status if enabled" do - get "/about.json" - - expect(response.parsed_body["about"].keys).not_to include("discourse_discover_enrolled") - - SiteSetting.include_in_discourse_discover = true - get "/about.json" - - expect(response.parsed_body["about"]["discourse_discover_enrolled"]).to eq(true) - end - it "does not serialize stats when 'Guardian#can_see_about_stats?' is false" do Guardian.any_instance.stubs(:can_see_about_stats?).returns(false) get "/about.json" diff --git a/spec/requests/site_controller_spec.rb b/spec/requests/site_controller_spec.rb index a53353774b2..9a2db4a0a43 100644 --- a/spec/requests/site_controller_spec.rb +++ b/spec/requests/site_controller_spec.rb @@ -15,7 +15,7 @@ RSpec.describe SiteController do SiteSetting.include_in_discourse_discover = true Theme.clear_default! - get "/site/basic-info.json" + get "/site/basic-info.json", headers: { "HTTP_USER_AGENT" => "Discourse Hub" } json = response.parsed_body expected_url = UrlHelper.absolute(upload.url) @@ -67,16 +67,6 @@ RSpec.describe SiteController do expect(json["likes_30_days"]).to be_present end - it "returns Discourse Discover stats" do - SiteSetting.include_in_discourse_discover = true - About.refresh_stats - - get "/site/statistics.json" - json = response.parsed_body - - expect(json["discourse_discover_enrolled"]).to be_truthy - end - it "is not visible if site setting share_anonymized_statistics is disabled" do SiteSetting.share_anonymized_statistics = false