2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'rest_client'
|
2013-02-20 04:16:50 +08:00
|
|
|
require_dependency 'version'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
module DiscourseHub
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
class UsernameUnavailable < RuntimeError
|
|
|
|
def initialize(username)
|
|
|
|
@username = username
|
2013-08-26 06:41:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def response_message
|
|
|
|
{
|
|
|
|
success: false,
|
|
|
|
message: I18n.t(
|
|
|
|
"login.errors",
|
|
|
|
errors:I18n.t(
|
2014-03-13 00:39:27 +08:00
|
|
|
"login.not_available", suggestion: UserNameSuggester.suggest(@username)
|
2013-08-26 06:41:17 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2013-09-12 04:32:49 +08:00
|
|
|
end
|
2013-08-26 06:41:17 +08:00
|
|
|
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.username_available?(username)
|
|
|
|
json = get('/users/username_available', {username: username})
|
2013-04-16 02:52:07 +08:00
|
|
|
[json['available'], json['suggestion']]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.username_match?(username, email)
|
|
|
|
json = get('/users/username_match', {username: username, email: email})
|
2013-04-16 02:52:07 +08:00
|
|
|
[json['match'], json['available'] || false, json['suggestion']]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.username_for_email(email)
|
|
|
|
json = get('/users/username_match', {email: email})
|
2013-11-20 03:15:05 +08:00
|
|
|
json['suggestion']
|
|
|
|
end
|
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.register_username(username, email)
|
|
|
|
json = post('/users', {username: username, email: email})
|
2013-02-06 03:16:51 +08:00
|
|
|
if json.has_key?('success')
|
|
|
|
true
|
|
|
|
else
|
2014-03-13 00:39:27 +08:00
|
|
|
raise UsernameUnavailable.new(username) # TODO: report ALL the errors
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.unregister_username(username)
|
|
|
|
json = delete('/memberships/' + username)
|
2013-04-16 02:52:07 +08:00
|
|
|
json.has_key?('success')
|
|
|
|
end
|
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.change_username(current_username, new_username)
|
|
|
|
json = put("/users/#{current_username}/username", {username: new_username})
|
2013-02-12 23:13:09 +08:00
|
|
|
if json.has_key?('success')
|
|
|
|
true
|
|
|
|
else
|
2014-03-13 00:39:27 +08:00
|
|
|
raise UsernameUnavailable.new(new_username) # TODO: report ALL the errors
|
2013-02-12 23:13:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-20 04:16:50 +08:00
|
|
|
def self.discourse_version_check
|
2013-03-02 05:01:36 +08:00
|
|
|
get('/version_check', {
|
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
forum_title: SiteSetting.title,
|
2013-09-12 04:32:49 +08:00
|
|
|
forum_description: SiteSetting.site_description,
|
2013-05-28 04:30:06 +08:00
|
|
|
forum_url: Discourse.base_url,
|
2013-04-25 02:13:20 +08:00
|
|
|
contact_email: SiteSetting.contact_email,
|
2013-05-28 04:30:06 +08:00
|
|
|
topic_count: Topic.listable_topics.count,
|
2013-07-31 05:52:19 +08:00
|
|
|
user_count: User.count,
|
2013-11-23 04:30:02 +08:00
|
|
|
login_required: SiteSetting.login_required,
|
|
|
|
locale: SiteSetting.default_locale
|
2013-03-02 05:01:36 +08:00
|
|
|
})
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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={})
|
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: {access_token: access_token}.merge(params), accept: accepts } )
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.collection_action(action, rel_url, params={})
|
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts )
|
2013-02-12 23:13:09 +08:00
|
|
|
end
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
def self.hub_base_url
|
2013-02-06 03:16:51 +08:00
|
|
|
if Rails.env == 'production'
|
|
|
|
'http://api.discourse.org/api'
|
|
|
|
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.access_token
|
2013-03-06 02:53:23 +08:00
|
|
|
SiteSetting.discourse_org_access_key
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
|
|
|
[:json, 'application/vnd.discoursehub.v1']
|
|
|
|
end
|
2013-08-28 15:18:31 +08:00
|
|
|
|
2014-03-13 00:39:27 +08:00
|
|
|
def self.username_operation
|
2013-08-28 15:18:31 +08:00
|
|
|
if SiteSetting.call_discourse_hub?
|
|
|
|
begin
|
|
|
|
yield
|
2014-03-13 00:39:27 +08:00
|
|
|
rescue DiscourseHub::UsernameUnavailable
|
2013-08-28 15:18:31 +08:00
|
|
|
false
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-26 06:41:17 +08:00
|
|
|
end
|