2021-07-20 19:56:32 +00:00
|
|
|
# TODO : Delete this and associated spec once 'api/widget/config' end point is merged
|
2019-10-29 07:20:54 +00:00
|
|
|
class WidgetsController < ActionController::Base
|
2022-06-23 10:18:56 +00:00
|
|
|
include WidgetHelper
|
|
|
|
|
2020-05-12 07:15:28 +00:00
|
|
|
before_action :set_global_config
|
2019-10-29 07:20:54 +00:00
|
|
|
before_action :set_web_widget
|
2022-08-03 06:10:03 +00:00
|
|
|
before_action :ensure_account_is_active
|
2019-10-30 05:13:11 +00:00
|
|
|
before_action :set_token
|
2019-10-29 07:20:54 +00:00
|
|
|
before_action :set_contact
|
|
|
|
before_action :build_contact
|
2020-06-25 18:05:16 +00:00
|
|
|
after_action :allow_iframe_requests
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-12 07:15:28 +00:00
|
|
|
def set_global_config
|
2022-02-15 10:46:54 +00:00
|
|
|
@global_config = GlobalConfig.get('LOGO_THUMBNAIL', 'BRAND_NAME', 'WIDGET_BRAND_URL', 'DIRECT_UPLOADS_ENABLED')
|
2020-05-12 07:15:28 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def set_web_widget
|
|
|
|
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_token
|
|
|
|
@token = permitted_params[:cw_conversation]
|
|
|
|
@auth_token_params = if @token.present?
|
|
|
|
::Widget::TokenService.new(token: @token).decode_token
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def set_contact
|
2020-01-09 07:36:40 +00:00
|
|
|
return if @auth_token_params[:source_id].nil?
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2021-11-22 18:02:17 +00:00
|
|
|
@contact_inbox = ::ContactInbox.find_by(
|
2019-10-29 07:20:54 +00:00
|
|
|
inbox_id: @web_widget.inbox.id,
|
2020-01-09 07:36:40 +00:00
|
|
|
source_id: @auth_token_params[:source_id]
|
2019-10-29 07:20:54 +00:00
|
|
|
)
|
|
|
|
|
2021-11-22 18:02:17 +00:00
|
|
|
@contact = @contact_inbox ? @contact_inbox.contact : nil
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_contact
|
|
|
|
return if @contact.present?
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
@contact_inbox, @token = build_contact_inbox_with_token(@web_widget, additional_attributes)
|
2021-11-22 18:02:17 +00:00
|
|
|
@contact = @contact_inbox.contact
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
2022-08-03 06:10:03 +00:00
|
|
|
def ensure_account_is_active
|
|
|
|
render json: { error: 'Account is suspended' }, status: :unauthorized unless @web_widget.inbox.account.active?
|
|
|
|
end
|
|
|
|
|
2020-12-13 16:53:56 +00:00
|
|
|
def additional_attributes
|
|
|
|
if @web_widget.inbox.account.feature_enabled?('ip_lookup')
|
|
|
|
{ created_at_ip: request.remote_ip }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def permitted_params
|
2019-10-30 05:13:11 +00:00
|
|
|
params.permit(:website_token, :cw_conversation)
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
2020-06-25 18:05:16 +00:00
|
|
|
|
|
|
|
def allow_iframe_requests
|
|
|
|
response.headers.delete('X-Frame-Options')
|
|
|
|
end
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|