2019-10-29 07:20:54 +00:00
|
|
|
class WidgetsController < ActionController::Base
|
|
|
|
before_action :set_web_widget
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_contact
|
|
|
|
return if cookie_params[:source_id].nil?
|
|
|
|
|
|
|
|
contact_inbox = ::ContactInbox.find_by(
|
|
|
|
inbox_id: @web_widget.inbox.id,
|
|
|
|
source_id: cookie_params[:source_id]
|
|
|
|
)
|
|
|
|
|
|
|
|
@contact = contact_inbox.contact
|
|
|
|
end
|
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
def set_token
|
|
|
|
@token = conversation_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_web_widget
|
|
|
|
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def build_contact
|
|
|
|
return if @contact.present?
|
|
|
|
|
|
|
|
contact_inbox = @web_widget.create_contact_inbox
|
|
|
|
@contact = contact_inbox.contact
|
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
payload = {
|
2019-10-29 07:20:54 +00:00
|
|
|
source_id: contact_inbox.source_id,
|
|
|
|
contact_id: @contact.id,
|
|
|
|
inbox_id: @web_widget.inbox.id
|
2019-10-30 05:13:11 +00:00
|
|
|
}
|
|
|
|
@token = JWT.encode payload, secret_key, 'HS256'
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cookie_params
|
2019-10-30 05:13:11 +00:00
|
|
|
return @cookie_params if @cookie_params.present?
|
|
|
|
|
|
|
|
if conversation_token.present?
|
2019-11-09 10:34:02 +00:00
|
|
|
begin
|
|
|
|
@cookie_params = JWT.decode(
|
|
|
|
conversation_token, secret_key, true, algorithm: 'HS256'
|
|
|
|
).first.symbolize_keys
|
|
|
|
rescue StandardError
|
|
|
|
@cookie_params = {}
|
|
|
|
end
|
2019-10-30 05:13:11 +00:00
|
|
|
return @cookie_params
|
|
|
|
end
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_token
|
|
|
|
permitted_params[:cw_conversation]
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-30 05:13:11 +00:00
|
|
|
def secret_key
|
|
|
|
Rails.application.secrets.secret_key_base
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
end
|