[Feature] Website live chat (#187)
Co-authored-by: Nithin David Thomas <webofnithin@gmail.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
26
app/controllers/api/v1/widget/inboxes_controller.rb
Normal file
26
app/controllers/api/v1/widget/inboxes_controller.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
class Api::V1::Widget::InboxesController < ApplicationController
|
||||
def create
|
||||
ActiveRecord::Base.transaction do
|
||||
channel = web_widgets.create!(
|
||||
website_name: permitted_params[:website_name],
|
||||
website_url: permitted_params[:website_url]
|
||||
)
|
||||
inbox = inboxes.create!(name: permitted_params[:website_name], channel: channel)
|
||||
render json: inbox
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def inboxes
|
||||
current_account.inboxes
|
||||
end
|
||||
|
||||
def web_widgets
|
||||
current_account.web_widgets
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.fetch(:website).permit(:website_name, :website_url)
|
||||
end
|
||||
end
|
||||
@@ -1,28 +1,68 @@
|
||||
class Api::V1::Widget::MessagesController < ApplicationController
|
||||
# TODO: move widget apis to different controller.
|
||||
skip_before_action :set_current_user, only: [:create_incoming]
|
||||
skip_before_action :check_subscription, only: [:create_incoming]
|
||||
skip_around_action :handle_with_exception, only: [:create_incoming]
|
||||
class Api::V1::Widget::MessagesController < ActionController::Base
|
||||
before_action :set_conversation, only: [:create]
|
||||
|
||||
def create_incoming
|
||||
builder = Integrations::Widget::IncomingMessageBuilder.new(incoming_message_params)
|
||||
builder.perform
|
||||
render json: builder.message
|
||||
def index
|
||||
@messages = conversation.nil? ? [] : message_finder.perform
|
||||
end
|
||||
|
||||
def create_outgoing
|
||||
builder = Integrations::Widget::OutgoingMessageBuilder.new(outgoing_message_params)
|
||||
builder.perform
|
||||
render json: builder.message
|
||||
def create
|
||||
@message = conversation.messages.new(message_params)
|
||||
@message.save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def incoming_message_params
|
||||
params.require(:message).permit(:contact_id, :inbox_id, :content)
|
||||
def conversation
|
||||
@conversation ||= ::Conversation.find_by(
|
||||
contact_id: cookie_params[:contact_id],
|
||||
inbox_id: cookie_params[:inbox_id]
|
||||
)
|
||||
end
|
||||
|
||||
def outgoing_message_params
|
||||
params.require(:message).permit(:user_id, :inbox_id, :content, :conversation_id)
|
||||
def set_conversation
|
||||
@conversation = ::Conversation.create!(conversation_params) if conversation.nil?
|
||||
end
|
||||
|
||||
def message_params
|
||||
{
|
||||
account_id: conversation.account_id,
|
||||
inbox_id: conversation.inbox_id,
|
||||
message_type: :incoming,
|
||||
content: permitted_params[:content]
|
||||
}
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: inbox.account_id,
|
||||
inbox_id: inbox.id,
|
||||
contact_id: cookie_params[:contact_id]
|
||||
}
|
||||
end
|
||||
|
||||
def inbox
|
||||
@inbox ||= ::Inbox.find_by(id: cookie_params[:inbox_id])
|
||||
end
|
||||
|
||||
def cookie_params
|
||||
JSON.parse(cookies.signed[cookie_name]).symbolize_keys
|
||||
end
|
||||
|
||||
def message_finder_params
|
||||
{
|
||||
filter_internal_messages: true
|
||||
}
|
||||
end
|
||||
|
||||
def message_finder
|
||||
@message_finder ||= MessageFinder.new(conversation, message_finder_params)
|
||||
end
|
||||
|
||||
def cookie_name
|
||||
'cw_conversation_' + params[:website_token]
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.fetch(:message).permit(:content)
|
||||
end
|
||||
end
|
||||
|
||||
47
app/controllers/widgets_controller.rb
Normal file
47
app/controllers/widgets_controller.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
class WidgetsController < ActionController::Base
|
||||
before_action :set_web_widget
|
||||
before_action :set_contact
|
||||
before_action :build_contact
|
||||
|
||||
private
|
||||
|
||||
def set_web_widget
|
||||
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def build_contact
|
||||
return if @contact.present?
|
||||
|
||||
contact_inbox = @web_widget.create_contact_inbox
|
||||
@contact = contact_inbox.contact
|
||||
|
||||
cookies.signed[cookie_name] = JSON.generate(
|
||||
source_id: contact_inbox.source_id,
|
||||
contact_id: @contact.id,
|
||||
inbox_id: @web_widget.inbox.id
|
||||
).to_s
|
||||
end
|
||||
|
||||
def cookie_params
|
||||
cookies.signed[cookie_name] ? JSON.parse(cookies.signed[cookie_name]).symbolize_keys : {}
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.permit(:website_token)
|
||||
end
|
||||
|
||||
def cookie_name
|
||||
'cw_conversation_' + permitted_params[:website_token]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user