2021-01-11 12:04:41 +00:00
|
|
|
class ChatwootHub
|
2022-07-15 02:51:59 +00:00
|
|
|
BASE_URL = ENV.fetch('CHATWOOT_HUB_URL', 'https://hub.2.chatwoot.com')
|
2021-07-15 18:28:54 +00:00
|
|
|
PING_URL = "#{BASE_URL}/ping".freeze
|
|
|
|
REGISTRATION_URL = "#{BASE_URL}/instances".freeze
|
2021-08-16 20:14:16 +00:00
|
|
|
PUSH_NOTIFICATION_URL = "#{BASE_URL}/send_push".freeze
|
2021-07-15 18:28:54 +00:00
|
|
|
EVENTS_URL = "#{BASE_URL}/events".freeze
|
|
|
|
|
|
|
|
def self.installation_identifier
|
|
|
|
identifier = InstallationConfig.find_by(name: 'INSTALLATION_IDENTIFIER')&.value
|
2022-09-13 12:10:06 +00:00
|
|
|
identifier ||= InstallationConfig.create!(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value
|
2021-07-15 18:28:54 +00:00
|
|
|
identifier
|
|
|
|
end
|
2021-01-11 12:04:41 +00:00
|
|
|
|
|
|
|
def self.instance_config
|
|
|
|
{
|
2021-07-15 18:28:54 +00:00
|
|
|
installation_identifier: installation_identifier,
|
|
|
|
installation_version: Chatwoot.config[:version],
|
2021-08-16 18:05:00 +00:00
|
|
|
installation_host: URI.parse(ENV.fetch('FRONTEND_URL', '')).host,
|
2022-04-14 11:45:57 +00:00
|
|
|
installation_env: ENV.fetch('INSTALLATION_ENV', ''),
|
|
|
|
edition: ENV.fetch('CW_EDITION', '')
|
2021-07-15 18:28:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.instance_metrics
|
|
|
|
{
|
|
|
|
accounts_count: Account.count,
|
|
|
|
users_count: User.count,
|
|
|
|
inboxes_count: Inbox.count,
|
|
|
|
conversations_count: Conversation.count,
|
|
|
|
incoming_messages_count: Message.incoming.count,
|
|
|
|
outgoing_messages_count: Message.outgoing.count,
|
|
|
|
additional_information: {}
|
2021-01-11 12:04:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.latest_version
|
|
|
|
begin
|
2021-07-15 18:28:54 +00:00
|
|
|
info = instance_config
|
|
|
|
info = info.merge(instance_metrics) unless ENV['DISABLE_TELEMETRY']
|
|
|
|
response = RestClient.post(PING_URL, info.to_json, { content_type: :json, accept: :json })
|
2021-01-11 12:04:41 +00:00
|
|
|
version = JSON.parse(response)['version']
|
2021-08-11 11:10:28 +00:00
|
|
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS => e
|
2022-03-28 12:44:30 +00:00
|
|
|
Rails.logger.error "Exception: #{e.message}"
|
2021-01-11 12:04:41 +00:00
|
|
|
rescue StandardError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e).capture_exception
|
2021-01-11 12:04:41 +00:00
|
|
|
end
|
|
|
|
version
|
|
|
|
end
|
2021-01-17 08:37:18 +00:00
|
|
|
|
2021-07-15 18:28:54 +00:00
|
|
|
def self.register_instance(company_name, owner_name, owner_email)
|
|
|
|
info = { company_name: company_name, owner_name: owner_name, owner_email: owner_email, subscribed_to_mailers: true }
|
|
|
|
RestClient.post(REGISTRATION_URL, info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
2021-08-11 11:10:28 +00:00
|
|
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS => e
|
2022-03-28 12:44:30 +00:00
|
|
|
Rails.logger.error "Exception: #{e.message}"
|
2021-07-15 18:28:54 +00:00
|
|
|
rescue StandardError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e).capture_exception
|
2021-08-16 20:14:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_browser_push(fcm_token_list, fcm_options)
|
|
|
|
info = { fcm_token_list: fcm_token_list, fcm_options: fcm_options }
|
|
|
|
RestClient.post(PUSH_NOTIFICATION_URL, info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
|
|
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS => e
|
2022-03-28 12:44:30 +00:00
|
|
|
Rails.logger.error "Exception: #{e.message}"
|
2021-08-16 20:14:16 +00:00
|
|
|
rescue StandardError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e).capture_exception
|
2021-07-15 18:28:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.emit_event(event_name, event_data)
|
|
|
|
return if ENV['DISABLE_TELEMETRY']
|
|
|
|
|
|
|
|
info = { event_name: event_name, event_data: event_data }
|
|
|
|
RestClient.post(EVENTS_URL, info.merge(instance_config).to_json, { content_type: :json, accept: :json })
|
2021-08-11 11:10:28 +00:00
|
|
|
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS => e
|
2022-03-28 12:44:30 +00:00
|
|
|
Rails.logger.error "Exception: #{e.message}"
|
2021-01-17 08:37:18 +00:00
|
|
|
rescue StandardError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e).capture_exception
|
2021-01-17 08:37:18 +00:00
|
|
|
end
|
2021-01-11 12:04:41 +00:00
|
|
|
end
|