forked from chatwoot/chatwoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Website live chat (chatwoot#187)
Co-authored-by: Nithin David Thomas <[email protected]> Co-authored-by: Sojan Jose <[email protected]>
- Loading branch information
1 parent
a411428
commit 16fe912
Showing
80 changed files
with
2,039 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
linters: | ||
LeadingZero: | ||
enabled: false | ||
|
||
exclude: | ||
- 'app/javascript/widget/assets/scss/_reset.scss' | ||
- 'app/javascript/widget/assets/scss/sdk.css' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ApiClient from '../ApiClient'; | ||
|
||
class WebChannel extends ApiClient { | ||
constructor() { | ||
super('widget/inboxes'); | ||
} | ||
} | ||
|
||
export default new WebChannel(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 40 additions & 12 deletions
52
app/javascript/dashboard/components/widgets/ChannelItem.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.