Skip to content

Commit

Permalink
feat: migrate facebook env vars to globalConfig (#3369)
Browse files Browse the repository at this point in the history
Migrate facebook env vars to globalConfig and make it editable from the super admin UI.
  • Loading branch information
vishnu-narayanan committed Nov 24, 2021
1 parent c23e2c2 commit 3a48e08
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/callbacks_controller.rb
Expand Up @@ -74,7 +74,7 @@ def fb_object
end

def long_lived_token(omniauth_token)
koala = Koala::Facebook::OAuth.new(ENV['FB_APP_ID'], ENV['FB_APP_SECRET'])
koala = Koala::Facebook::OAuth.new(GlobalConfigService.load('FB_APP_ID', ''), GlobalConfigService.load('FB_APP_SECRET', ''))
koala.exchange_access_token_info(omniauth_token)['access_token']
rescue StandardError => e
Rails.logger.info e
Expand Down
13 changes: 8 additions & 5 deletions app/controllers/dashboard_controller.rb
Expand Up @@ -26,14 +26,17 @@ def set_global_config
'API_CHANNEL_THUMBNAIL',
'ANALYTICS_TOKEN',
'ANALYTICS_HOST'
).merge(
APP_VERSION: Chatwoot.config[:version],
VAPID_PUBLIC_KEY: VapidService.public_key,
ENABLE_ACCOUNT_SIGNUP: GlobalConfigService.load('ENABLE_ACCOUNT_SIGNUP', 'false')
)
).merge(app_config)
end

def ensure_installation_onboarding
redirect_to '/installation/onboarding' if ::Redis::Alfred.get(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING)
end

def app_config
{ APP_VERSION: Chatwoot.config[:version],
VAPID_PUBLIC_KEY: VapidService.public_key,
ENABLE_ACCOUNT_SIGNUP: GlobalConfigService.load('ENABLE_ACCOUNT_SIGNUP', 'false'),
FB_APP_ID: GlobalConfigService.load('FB_APP_ID', '') }
end
end
2 changes: 1 addition & 1 deletion app/controllers/webhooks/instagram_controller.rb
Expand Up @@ -25,6 +25,6 @@ def events
private

def valid_instagram_token?(token)
token == ENV['IG_VERIFY_TOKEN']
token == GlobalConfigService.load('IG_VERIFY_TOKEN', '')
end
end
6 changes: 6 additions & 0 deletions app/models/installation_config.rb
Expand Up @@ -23,6 +23,8 @@ class InstallationConfig < ApplicationRecord
default_scope { order(created_at: :desc) }
scope :editable, -> { where(locked: false) }

after_commit :clear_cache

def value
serialized_value[:value]
end
Expand All @@ -38,4 +40,8 @@ def value=(value_to_assigned)
def set_lock
self.locked = true if locked.nil?
end

def clear_cache
GlobalConfig.clear_cache
end
end
2 changes: 1 addition & 1 deletion app/services/instagram/send_on_instagram_service.rb
Expand Up @@ -50,7 +50,7 @@ def attachament_message_params
# @see https://developers.facebook.com/docs/messenger-platform/instagram/features/send-message
def send_to_facebook_page(message_content)
access_token = channel.page_access_token
app_secret_proof = calculate_app_secret_proof(ENV['FB_APP_SECRET'], access_token)
app_secret_proof = calculate_app_secret_proof(GlobalConfigService.load('FB_APP_SECRET', ''), access_token)

query = { access_token: access_token }
query[:appsecret_proof] = app_secret_proof if app_secret_proof
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/facebook_messenger.rb
@@ -1,11 +1,11 @@
# ref: https://github.com/jgorset/facebook-messenger#make-a-configuration-provider
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
def valid_verify_token?(_verify_token)
ENV['FB_VERIFY_TOKEN']
GlobalConfigService.load('FB_VERIFY_TOKEN', '')
end

def app_secret_for(_page_id)
ENV['FB_APP_SECRET']
GlobalConfigService.load('FB_APP_SECRET', '')
end

def access_token_for(page_id)
Expand Down
2 changes: 1 addition & 1 deletion lib/integrations/facebook/message_parser.rb
Expand Up @@ -44,7 +44,7 @@ def app_id

# TODO : does this work ?
def sent_from_chatwoot_app?
app_id && app_id == ENV['FB_APP_ID'].to_i
app_id && app_id == GlobalConfigService.load('FB_APP_ID', '').to_i
end
end

Expand Down
7 changes: 6 additions & 1 deletion spec/lib/vapid_service_spec.rb
Expand Up @@ -19,6 +19,9 @@
ENV['VAPID_PUBLIC_KEY'] = 'test'
described_class.public_key

# this call will hit db as after_commit method will clear globalConfig cache
expect(InstallationConfig).to receive(:find_by)
described_class.public_key
# subsequent calls should not hit DB
expect(InstallationConfig).not_to receive(:find_by)
described_class.public_key
Expand All @@ -30,11 +33,13 @@
ENV['VAPID_PRIVATE_KEY'] = 'test'
described_class.private_key

# this call will hit db as after_commit method will clear globalConfig cache
expect(InstallationConfig).to receive(:find_by)
described_class.private_key
# subsequent calls should not hit DB
expect(InstallationConfig).not_to receive(:find_by)
described_class.private_key
ENV['VAPID_PRIVATE_KEY'] = nil
ENV['VAPID_PRIVATE_KEY'] = nil
end

it 'clears cache and fetch from DB next time, when clear_cache is called' do
Expand Down

0 comments on commit 3a48e08

Please sign in to comment.