mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-01 14:54:42 +08:00
This change won't affect functionality in core. It is only for plugins which are calling these DiscourseHub APIs.
154 lines
4 KiB
Ruby
154 lines
4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseHub
|
|
STATS_FETCHED_AT_KEY = "stats_fetched_at"
|
|
|
|
class Error < StandardError
|
|
attr_reader :status, :body
|
|
|
|
def initialize(rel_url, status, body)
|
|
@status = status
|
|
@body = body
|
|
super("Discourse Hub (#{rel_url}) returned status #{status}")
|
|
end
|
|
end
|
|
|
|
def self.version_check_payload
|
|
default_payload = { installed_version: Discourse::VERSION::STRING }.merge!(
|
|
Discourse.git_branch == "unknown" && !Rails.env.test? ? {} : { branch: Discourse.git_branch },
|
|
)
|
|
default_payload.merge!(get_payload)
|
|
end
|
|
|
|
def self.discourse_version_check
|
|
get("/version_check", version_check_payload)
|
|
end
|
|
|
|
def self.discover_enrollment_payload
|
|
{
|
|
include_in_discourse_discover: SiteSetting.include_in_discourse_discover?,
|
|
forum_url: Discourse.base_url,
|
|
forum_title: SiteSetting.title,
|
|
locale: I18n.locale,
|
|
}
|
|
end
|
|
|
|
def self.discover_enrollment
|
|
post("/discover/enroll", discover_enrollment_payload)
|
|
end
|
|
|
|
def self.stats_fetched_at=(time_with_zone)
|
|
Discourse.redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i
|
|
end
|
|
|
|
def self.get_payload
|
|
if SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago
|
|
About.fetch_cached_stats.symbolize_keys
|
|
else
|
|
{}
|
|
end
|
|
end
|
|
|
|
def self.get(rel_url, params = {}, raise_on_error: false)
|
|
singular_action :get, rel_url, params, raise_on_error: raise_on_error
|
|
end
|
|
|
|
def self.post(rel_url, params = {}, raise_on_error: false)
|
|
collection_action :post, rel_url, params, raise_on_error: raise_on_error
|
|
end
|
|
|
|
def self.put(rel_url, params = {}, raise_on_error: false)
|
|
collection_action :put, rel_url, params, raise_on_error: raise_on_error
|
|
end
|
|
|
|
def self.delete(rel_url, params = {}, raise_on_error: false)
|
|
singular_action :delete, rel_url, params, raise_on_error: raise_on_error
|
|
end
|
|
|
|
def self.singular_action(action, rel_url, params = {}, raise_on_error: false)
|
|
connect_opts = connect_opts(params)
|
|
|
|
response =
|
|
Excon.public_send(
|
|
action,
|
|
"#{hub_base_url}#{rel_url}",
|
|
{ headers: { "Referer" => referer, "Accept" => accepts.join(", ") }, query: params }.merge(
|
|
connect_opts,
|
|
),
|
|
)
|
|
|
|
if raise_on_error && response.status != 200
|
|
raise Error.new(rel_url, response.status, parse_body(response.body))
|
|
end
|
|
|
|
JSON.parse(response.body)
|
|
end
|
|
|
|
def self.collection_action(action, rel_url, params = {}, raise_on_error: false)
|
|
connect_opts = connect_opts(params)
|
|
|
|
response =
|
|
Excon.public_send(
|
|
action,
|
|
"#{hub_base_url}#{rel_url}",
|
|
{
|
|
body: JSON[params],
|
|
headers: {
|
|
"Referer" => referer,
|
|
"Accept" => accepts.join(", "),
|
|
"Content-Type" => "application/json",
|
|
},
|
|
}.merge(connect_opts),
|
|
)
|
|
|
|
if (status = response.status) != 200
|
|
Rails.logger.warn(response_status_log_message(rel_url, status))
|
|
end
|
|
|
|
body = parse_body(response.body)
|
|
|
|
raise Error.new(rel_url, status, body) if raise_on_error && status != 200
|
|
|
|
body
|
|
end
|
|
|
|
def self.parse_body(raw)
|
|
JSON.parse(raw)
|
|
rescue JSON::ParserError
|
|
Rails.logger.error(response_body_log_message(raw))
|
|
nil
|
|
end
|
|
|
|
def self.response_status_log_message(rel_url, status)
|
|
"Discourse Hub (#{hub_base_url}#{rel_url}) returned a bad status #{status}."
|
|
end
|
|
|
|
def self.response_body_log_message(body)
|
|
"Discourse Hub returned a bad response body: #{body}"
|
|
end
|
|
|
|
def self.connect_opts(params = {})
|
|
params.delete(:connect_opts)&.except(:body, :headers, :query) || {}
|
|
end
|
|
|
|
def self.hub_base_url
|
|
if Rails.env.production?
|
|
ENV["HUB_BASE_URL"] || "https://api.discourse.org/api"
|
|
else
|
|
ENV["HUB_BASE_URL"] || "http://local.hub:3000/api"
|
|
end
|
|
end
|
|
|
|
def self.accepts
|
|
%w[application/json application/vnd.discoursehub.v1]
|
|
end
|
|
|
|
def self.referer
|
|
Discourse.base_url
|
|
end
|
|
|
|
def self.stats_fetched_at
|
|
t = Discourse.redis.get(STATS_FETCHED_AT_KEY)
|
|
t ? Time.zone.at(t.to_i) : 1.year.ago
|
|
end
|
|
end
|