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.
This commit is contained in:
Vinoth Kannan 2024-04-04 00:22:28 +05:30 committed by GitHub
parent 3e0898e498
commit 9dc6325821
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 21 additions and 43 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -47,8 +47,4 @@ class Statistics
count: User.real.count,
}
end
def self.discourse_discover
{ enrolled: SiteSetting.include_in_discourse_discover? }
end
end

View File

@ -103,7 +103,6 @@ TEXT
:likes_7_days,
:likes_30_days,
:likes_count,
:discourse_discover_enrolled,
)
end

View File

@ -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"

View File

@ -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