Module: DiscourseHub

Defined in:
lib/discourse_hub.rb

Constant Summary collapse

STATS_FETCHED_AT_KEY =
"stats_fetched_at"

Class Method Summary collapse

Class Method Details

.acceptsObject



126
127
128
# File 'lib/discourse_hub.rb', line 126

def self.accepts
  %w[application/json application/vnd.discoursehub.v1]
end

.collection_action(action, rel_url, params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/discourse_hub.rb', line 77

def self.collection_action(action, rel_url, params = {})
  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",
        },
        omit_default_port: true,
      }.merge(connect_opts),
    )

  if (status = response.status) != 200
    Rails.logger.warn(response_status_log_message(rel_url, status))
  end

  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    Rails.logger.error(response_body_log_message(response.body))
  end
end

.connect_opts(params = {}) ⇒ Object



114
115
116
# File 'lib/discourse_hub.rb', line 114

def self.connect_opts(params = {})
  params.delete(:connect_opts)&.except(:body, :headers, :query) || {}
end

.delete(rel_url, params = {}) ⇒ Object



54
55
56
# File 'lib/discourse_hub.rb', line 54

def self.delete(rel_url, params = {})
  singular_action :delete, rel_url, params
end

.discourse_version_checkObject



13
14
15
# File 'lib/discourse_hub.rb', line 13

def self.discourse_version_check
  get("/version_check", version_check_payload)
end

.discover_enrollmentObject



26
27
28
# File 'lib/discourse_hub.rb', line 26

def self.discover_enrollment
  post("/discover/enroll", discover_enrollment_payload)
end

.discover_enrollment_payloadObject



17
18
19
20
21
22
23
24
# File 'lib/discourse_hub.rb', line 17

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

.get(rel_url, params = {}) ⇒ Object



42
43
44
# File 'lib/discourse_hub.rb', line 42

def self.get(rel_url, params = {})
  singular_action :get, rel_url, params
end

.get_payloadObject



34
35
36
37
38
39
40
# File 'lib/discourse_hub.rb', line 34

def self.get_payload
  if SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago
    About.fetch_cached_stats.symbolize_keys
  else
    {}
  end
end

.hub_base_urlObject



118
119
120
121
122
123
124
# File 'lib/discourse_hub.rb', line 118

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

.post(rel_url, params = {}) ⇒ Object



46
47
48
# File 'lib/discourse_hub.rb', line 46

def self.post(rel_url, params = {})
  collection_action :post, rel_url, params
end

.put(rel_url, params = {}) ⇒ Object



50
51
52
# File 'lib/discourse_hub.rb', line 50

def self.put(rel_url, params = {})
  collection_action :put, rel_url, params
end

.refererObject



130
131
132
# File 'lib/discourse_hub.rb', line 130

def self.referer
  Discourse.base_url
end

.response_body_log_message(body) ⇒ Object



110
111
112
# File 'lib/discourse_hub.rb', line 110

def self.response_body_log_message(body)
  "Discourse Hub returned a bad response body: #{body}"
end

.response_status_log_message(rel_url, status) ⇒ Object



106
107
108
# File 'lib/discourse_hub.rb', line 106

def self.response_status_log_message(rel_url, status)
  "Discourse Hub (#{hub_base_url}#{rel_url}) returned a bad status #{status}."
end

.singular_action(action, rel_url, params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/discourse_hub.rb', line 58

def self.singular_action(action, rel_url, params = {})
  connect_opts = connect_opts(params)

  JSON.parse(
    Excon.public_send(
      action,
      "#{hub_base_url}#{rel_url}",
      {
        headers: {
          "Referer" => referer,
          "Accept" => accepts.join(", "),
        },
        query: params,
        omit_default_port: true,
      }.merge(connect_opts),
    ).body,
  )
end

.stats_fetched_atObject



134
135
136
137
# File 'lib/discourse_hub.rb', line 134

def self.stats_fetched_at
  t = Discourse.redis.get(STATS_FETCHED_AT_KEY)
  t ? Time.zone.at(t.to_i) : 1.year.ago
end

.stats_fetched_at=(time_with_zone) ⇒ Object



30
31
32
# File 'lib/discourse_hub.rb', line 30

def self.stats_fetched_at=(time_with_zone)
  Discourse.redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i
end

.version_check_payloadObject



6
7
8
9
10
11
# File 'lib/discourse_hub.rb', line 6

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