2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
module DiscourseHub
|
2017-03-27 20:47:07 +08:00
|
|
|
STATS_FETCHED_AT_KEY = "stats_fetched_at"
|
|
|
|
|
2014-05-12 07:06:28 +08:00
|
|
|
def self.version_check_payload
|
2017-03-27 20:47:07 +08:00
|
|
|
default_payload = { installed_version: Discourse::VERSION::STRING }.merge!(
|
2023-04-18 18:51:54 +08:00
|
|
|
Discourse.git_branch == "unknown" && !Rails.env.test? ? {} : { branch: Discourse.git_branch },
|
2017-03-27 20:47:07 +08:00
|
|
|
)
|
|
|
|
default_payload.merge!(get_payload)
|
2014-05-12 07:06:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.discourse_version_check
|
|
|
|
get("/version_check", version_check_payload)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2024-02-23 14:12:28 +08:00
|
|
|
def self.discover_enrollment_payload
|
2024-02-25 13:12:50 +08:00
|
|
|
{
|
|
|
|
include_in_discourse_discover: SiteSetting.include_in_discourse_discover?,
|
|
|
|
forum_url: Discourse.base_url,
|
|
|
|
forum_title: SiteSetting.title,
|
|
|
|
locale: I18n.locale,
|
|
|
|
}
|
2024-02-23 14:12:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.discover_enrollment
|
|
|
|
post("/discover/enroll", discover_enrollment_payload)
|
|
|
|
end
|
|
|
|
|
2017-03-27 20:47:07 +08:00
|
|
|
def self.stats_fetched_at=(time_with_zone)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i
|
2017-03-27 20:47:07 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-03-27 20:47:07 +08:00
|
|
|
def self.get_payload
|
|
|
|
if SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago
|
|
|
|
About.fetch_cached_stats.symbolize_keys
|
2023-01-09 20:10:19 +08:00
|
|
|
else
|
2017-03-27 20:47:07 +08:00
|
|
|
{}
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2017-03-27 20:47:07 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.get(rel_url, params = {})
|
2013-04-16 02:52:07 +08:00
|
|
|
singular_action :get, rel_url, params
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post(rel_url, params = {})
|
2013-04-16 02:52:07 +08:00
|
|
|
collection_action :post, rel_url, params
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:13:09 +08:00
|
|
|
def self.put(rel_url, params = {})
|
2013-04-16 02:52:07 +08:00
|
|
|
collection_action :put, rel_url, params
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.delete(rel_url, params = {})
|
|
|
|
singular_action :delete, rel_url, params
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.singular_action(action, rel_url, params = {})
|
2018-08-31 03:57:11 +08:00
|
|
|
connect_opts = connect_opts(params)
|
2019-05-07 09:27:05 +08:00
|
|
|
|
|
|
|
JSON.parse(
|
|
|
|
Excon.public_send(
|
|
|
|
action,
|
2017-06-16 08:08:15 +08:00
|
|
|
"#{hub_base_url}#{rel_url}",
|
2018-08-31 03:57:11 +08:00
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"Referer" => referer,
|
|
|
|
"Accept" => accepts.join(", "),
|
|
|
|
},
|
|
|
|
query: params,
|
|
|
|
omit_default_port: true,
|
|
|
|
}.merge(connect_opts),
|
2017-06-16 08:08:15 +08:00
|
|
|
).body,
|
|
|
|
)
|
2013-04-16 02:52:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.collection_action(action, rel_url, params = {})
|
2018-08-31 03:57:11 +08:00
|
|
|
connect_opts = connect_opts(params)
|
2018-10-09 22:05:31 +08:00
|
|
|
|
2019-05-07 09:27:05 +08:00
|
|
|
response =
|
|
|
|
Excon.public_send(
|
|
|
|
action,
|
2017-06-16 08:08:15 +08:00
|
|
|
"#{hub_base_url}#{rel_url}",
|
2018-08-31 03:57:11 +08:00
|
|
|
{
|
|
|
|
body: JSON[params],
|
|
|
|
headers: {
|
|
|
|
"Referer" => referer,
|
|
|
|
"Accept" => accepts.join(", "),
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
},
|
|
|
|
omit_default_port: true,
|
|
|
|
}.merge(connect_opts),
|
2018-10-09 22:05:31 +08:00
|
|
|
)
|
|
|
|
|
2018-10-10 09:34:50 +08:00
|
|
|
if (status = response.status) != 200
|
|
|
|
Rails.logger.warn(response_status_log_message(rel_url, status))
|
2018-10-09 22:05:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
JSON.parse(response.body)
|
|
|
|
rescue JSON::ParserError
|
2018-10-10 09:34:50 +08:00
|
|
|
Rails.logger.error(response_body_log_message(response.body))
|
2018-10-09 22:05:31 +08:00
|
|
|
end
|
2013-02-12 23:13:09 +08:00
|
|
|
end
|
|
|
|
|
2018-10-10 09:34:50 +08:00
|
|
|
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
|
|
|
|
|
2018-08-31 03:57:11 +08:00
|
|
|
def self.connect_opts(params = {})
|
|
|
|
params.delete(:connect_opts)&.except(:body, :headers, :query) || {}
|
|
|
|
end
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
def self.hub_base_url
|
2014-08-18 14:42:48 +08:00
|
|
|
if Rails.env.production?
|
2017-01-07 06:54:38 +08:00
|
|
|
ENV["HUB_BASE_URL"] || "https://api.discourse.org/api"
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
2013-11-20 03:15:05 +08:00
|
|
|
ENV["HUB_BASE_URL"] || "http://local.hub:3000/api"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
2017-06-16 08:08:15 +08:00
|
|
|
%w[application/json application/vnd.discoursehub.v1]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-08-28 15:18:31 +08:00
|
|
|
|
2014-08-15 05:29:58 +08:00
|
|
|
def self.referer
|
|
|
|
Discourse.base_url
|
|
|
|
end
|
|
|
|
|
2017-03-27 20:47:07 +08:00
|
|
|
def self.stats_fetched_at
|
2019-12-03 17:05:53 +08:00
|
|
|
t = Discourse.redis.get(STATS_FETCHED_AT_KEY)
|
2017-03-27 20:47:07 +08:00
|
|
|
t ? Time.zone.at(t.to_i) : 1.year.ago
|
|
|
|
end
|
2013-08-26 06:41:17 +08:00
|
|
|
end
|