FEATURE: use new site setting for onebox custom user agent. (#28045)

Previously, we couldn't change the user agent name dynamically for onebox requests. In this commit, a new hidden site setting `onebox_user_agent` is created to override the default user agent value specified in the [initializer](c333e9d6e6/config/initializers/100-onebox_options.rb (L15)).

Co-authored-by: Régis Hanol <regis@hanol.fr>
This commit is contained in:
Vinoth Kannan
2024-07-24 04:45:30 +05:30
committed by GitHub
parent 73ce3589ad
commit d681decf01
6 changed files with 66 additions and 26 deletions

View File

@ -104,9 +104,7 @@ module Onebox
headers ||= {}
if Onebox.options.user_agent && !headers["User-Agent"]
headers["User-Agent"] = Onebox.options.user_agent
end
headers["User-Agent"] ||= user_agent if user_agent
request = Net::HTTP::Get.new(uri.request_uri, headers)
start_time = Time.now
@ -232,6 +230,12 @@ module Onebox
end
end
def self.user_agent
user_agent = SiteSetting.onebox_user_agent.presence || Onebox.options.user_agent
user_agent = "#{user_agent} v#{Discourse::VERSION::STRING}"
user_agent
end
# Percent-encodes a URI string per RFC3986 - https://tools.ietf.org/html/rfc3986
def self.uri_encode(url)
return "" unless url