2013-02-19 15:16:50 -05:00
|
|
|
require_dependency 'version'
|
2017-10-05 23:39:00 -04:00
|
|
|
require_dependency 'site_setting'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-14 12:57:26 -05:00
|
|
|
module DiscourseHub
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-03-27 08:47:07 -04:00
|
|
|
STATS_FETCHED_AT_KEY = "stats_fetched_at"
|
|
|
|
|
2014-05-11 19:06:28 -04:00
|
|
|
def self.version_check_payload
|
2017-07-27 21:20:09 -04:00
|
|
|
default_payload = { installed_version: Discourse::VERSION::STRING }.merge!(Discourse.git_branch == "unknown" ? {} : { branch: Discourse.git_branch })
|
2017-03-27 08:47:07 -04:00
|
|
|
default_payload.merge!(get_payload)
|
2014-05-11 19:06:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.discourse_version_check
|
|
|
|
get('/version_check', version_check_payload)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-03-27 08:47:07 -04:00
|
|
|
def self.stats_fetched_at=(time_with_zone)
|
|
|
|
$redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-03-27 08:47:07 -04:00
|
|
|
def self.get_payload
|
|
|
|
SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago ? About.fetch_cached_stats.symbolize_keys : {}
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.get(rel_url, params = {})
|
2013-04-15 14:52:07 -04:00
|
|
|
singular_action :get, rel_url, params
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.post(rel_url, params = {})
|
2013-04-15 14:52:07 -04:00
|
|
|
collection_action :post, rel_url, params
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.put(rel_url, params = {})
|
2013-04-15 14:52:07 -04:00
|
|
|
collection_action :put, rel_url, params
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.delete(rel_url, params = {})
|
2013-04-15 14:52:07 -04:00
|
|
|
singular_action :delete, rel_url, params
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.singular_action(action, rel_url, params = {})
|
2017-06-15 20:08:15 -04:00
|
|
|
JSON.parse(Excon.send(action,
|
|
|
|
"#{hub_base_url}#{rel_url}",
|
2017-06-17 13:17:40 -04:00
|
|
|
headers: { 'Referer' => referer, 'Accept' => accepts.join(', ') },
|
2017-06-18 18:51:06 -04:00
|
|
|
query: params,
|
|
|
|
omit_default_port: true
|
2017-06-15 20:08:15 -04:00
|
|
|
).body)
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.collection_action(action, rel_url, params = {})
|
2017-06-15 20:08:15 -04:00
|
|
|
JSON.parse(Excon.send(action,
|
|
|
|
"#{hub_base_url}#{rel_url}",
|
2017-06-20 17:15:59 -04:00
|
|
|
body: JSON[params],
|
|
|
|
headers: { 'Referer' => referer, 'Accept' => accepts.join(', '), "Content-Type" => "application/json" },
|
2017-06-19 19:32:45 -04:00
|
|
|
omit_default_port: true
|
2017-06-15 20:08:15 -04:00
|
|
|
).body)
|
2013-02-12 10:13:09 -05:00
|
|
|
end
|
|
|
|
|
2013-02-14 12:57:26 -05:00
|
|
|
def self.hub_base_url
|
2014-08-18 02:42:48 -04:00
|
|
|
if Rails.env.production?
|
2017-01-06 17:54:38 -05:00
|
|
|
ENV['HUB_BASE_URL'] || 'https://api.discourse.org/api'
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2013-11-19 14:15:05 -05:00
|
|
|
ENV['HUB_BASE_URL'] || 'http://local.hub:3000/api'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
2017-06-15 20:08:15 -04:00
|
|
|
['application/json', 'application/vnd.discoursehub.v1']
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
|
2014-08-14 17:29:58 -04:00
|
|
|
def self.referer
|
|
|
|
Discourse.base_url
|
|
|
|
end
|
|
|
|
|
2017-03-27 08:47:07 -04:00
|
|
|
def self.stats_fetched_at
|
|
|
|
t = $redis.get(STATS_FETCHED_AT_KEY)
|
|
|
|
t ? Time.zone.at(t.to_i) : 1.year.ago
|
|
|
|
end
|
|
|
|
|
2013-08-25 18:41:17 -04:00
|
|
|
end
|