mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 03:27:52 +00:00
Co-authored-by: Nithin David Thomas <webofnithin@gmail.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
69 lines
1.4 KiB
Ruby
69 lines
1.4 KiB
Ruby
class Api::V1::Widget::MessagesController < ActionController::Base
|
|
before_action :set_conversation, only: [:create]
|
|
|
|
def index
|
|
@messages = conversation.nil? ? [] : message_finder.perform
|
|
end
|
|
|
|
def create
|
|
@message = conversation.messages.new(message_params)
|
|
@message.save!
|
|
end
|
|
|
|
private
|
|
|
|
def conversation
|
|
@conversation ||= ::Conversation.find_by(
|
|
contact_id: cookie_params[:contact_id],
|
|
inbox_id: cookie_params[:inbox_id]
|
|
)
|
|
end
|
|
|
|
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
|