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.
feat: Add Public APIs for API Channel (chatwoot#2375)
- Loading branch information
1 parent
0f377da
commit 853db60
Showing
29 changed files
with
404 additions
and
11 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
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
48 changes: 48 additions & 0 deletions
48
app/controllers/public/api/v1/inboxes/contacts_controller.rb
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,48 @@ | ||
class Public::Api::V1::Inboxes::ContactsController < Public::Api::V1::InboxesController | ||
before_action :contact_inbox, except: [:create] | ||
before_action :process_hmac | ||
|
||
def create | ||
source_id = params[:source_id] || SecureRandom.uuid | ||
@contact_inbox = ::ContactBuilder.new( | ||
source_id: source_id, | ||
inbox: @inbox_channel.inbox, | ||
contact_attributes: permitted_params.except(:identifier, :identifier_hash) | ||
).perform | ||
end | ||
|
||
def show; end | ||
|
||
def update | ||
contact_identify_action = ContactIdentifyAction.new( | ||
contact: @contact_inbox.contact, | ||
params: permitted_params.to_h.deep_symbolize_keys.except(:identifier) | ||
) | ||
render json: contact_identify_action.perform | ||
end | ||
|
||
private | ||
|
||
def contact_inbox | ||
@contact_inbox = @inbox_channel.inbox.contact_inboxes.find_by!(source_id: params[:id]) | ||
end | ||
|
||
def process_hmac | ||
return if params[:identifier_hash].blank? && !@inbox_channel.hmac_mandatory | ||
raise StandardError, 'HMAC failed: Invalid Identifier Hash Provided' unless valid_hmac? | ||
|
||
@contact_inbox.update(hmac_verified: true) if @contact_inbox.present? | ||
end | ||
|
||
def valid_hmac? | ||
params[:identifier_hash] == OpenSSL::HMAC.hexdigest( | ||
'sha256', | ||
@inbox_channel.hmac_token, | ||
params[:identifier].to_s | ||
) | ||
end | ||
|
||
def permitted_params | ||
params.permit(:identifier, :identifier_hash, :email, :name, :avatar_url, custom_attributes: {}) | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
app/controllers/public/api/v1/inboxes/conversations_controller.rb
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,24 @@ | ||
class Public::Api::V1::Inboxes::ConversationsController < Public::Api::V1::InboxesController | ||
def index | ||
@conversations = @contact_inbox.hmac_verified? ? @contact.conversations : @contact_inbox.conversations | ||
end | ||
|
||
def create | ||
@conversation = create_conversation | ||
end | ||
|
||
private | ||
|
||
def create_conversation | ||
::Conversation.create!(conversation_params) | ||
end | ||
|
||
def conversation_params | ||
{ | ||
account_id: @contact_inbox.contact.account_id, | ||
inbox_id: @contact_inbox.inbox_id, | ||
contact_id: @contact_inbox.contact_id, | ||
contact_inbox_id: @contact_inbox.id | ||
} | ||
end | ||
end |
68 changes: 68 additions & 0 deletions
68
app/controllers/public/api/v1/inboxes/messages_controller.rb
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,68 @@ | ||
class Public::Api::V1::Inboxes::MessagesController < Public::Api::V1::InboxesController | ||
before_action :set_message, only: [:update] | ||
|
||
def index | ||
@messages = @conversation.nil? ? [] : message_finder.perform | ||
end | ||
|
||
def create | ||
@message = @conversation.messages.new(message_params) | ||
@message.save | ||
build_attachment | ||
end | ||
|
||
def update | ||
@message.update!(message_update_params) | ||
rescue StandardError => e | ||
render json: { error: @contact.errors, message: e.message }.to_json, status: 500 | ||
end | ||
|
||
private | ||
|
||
def build_attachment | ||
return if params[:attachments].blank? | ||
|
||
params[:attachments].each do |uploaded_attachment| | ||
attachment = @message.attachments.new( | ||
account_id: @message.account_id, | ||
file_type: helpers.file_type(uploaded_attachment&.content_type) | ||
) | ||
attachment.file.attach(uploaded_attachment) | ||
end | ||
@message.save! | ||
end | ||
|
||
def message_finder_params | ||
{ | ||
filter_internal_messages: true, | ||
before: params[:before] | ||
} | ||
end | ||
|
||
def message_finder | ||
@message_finder ||= MessageFinder.new(@conversation, message_finder_params) | ||
end | ||
|
||
def message_update_params | ||
params.permit(submitted_values: [:name, :title, :value]) | ||
end | ||
|
||
def permitted_params | ||
params.permit(:content, :echo_id) | ||
end | ||
|
||
def set_message | ||
@message = @conversation.messages.find(params[:id]) | ||
end | ||
|
||
def message_params | ||
{ | ||
account_id: @conversation.account_id, | ||
sender: @contact_inbox.contact, | ||
content: permitted_params[:content], | ||
inbox_id: @conversation.inbox_id, | ||
echo_id: permitted_params[:echo_id], | ||
message_type: :incoming | ||
} | ||
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,23 @@ | ||
class Public::Api::V1::InboxesController < PublicController | ||
before_action :set_inbox_channel | ||
before_action :set_contact_inbox | ||
before_action :set_conversation | ||
|
||
private | ||
|
||
def set_inbox_channel | ||
@inbox_channel = ::Channel::Api.find_by!(identifier: params[:inbox_id]) | ||
end | ||
|
||
def set_contact_inbox | ||
return if params[:contact_id].blank? | ||
|
||
@contact_inbox = @inbox_channel.inbox.contact_inboxes.find_by!(source_id: params[:contact_id]) | ||
end | ||
|
||
def set_conversation | ||
return if params[:conversation_id].blank? | ||
|
||
@conversation = @contact_inbox.contact.conversations.find_by!(display_id: params[:conversation_id]) | ||
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,3 @@ | ||
class PublicController < ActionController::Base | ||
skip_before_action :verify_authenticity_token | ||
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
2 changes: 2 additions & 0 deletions
2
app/views/public/api/v1/inboxes/contacts/create.json.jbuilder
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,2 @@ | ||
json.source_id @contact_inbox.source_id | ||
json.partial! 'public/api/v1/models/contact.json.jbuilder', resource: @contact_inbox.contact |
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,2 @@ | ||
json.source_id @contact_inbox.source_id | ||
json.partial! 'public/api/v1/models/contact.json.jbuilder', resource: @contact_inbox.contact |
2 changes: 2 additions & 0 deletions
2
app/views/public/api/v1/inboxes/contacts/update.json.jbuilder
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,2 @@ | ||
json.source_id @contact_inbox.source_id | ||
json.partial! 'public/api/v1/models/contact.json.jbuilder', resource: @contact_inbox.contact |
1 change: 1 addition & 0 deletions
1
app/views/public/api/v1/inboxes/conversations/create.json.jbuilder
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 @@ | ||
json.partial! 'public/api/v1/models/conversation.json.jbuilder', resource: @conversation |
3 changes: 3 additions & 0 deletions
3
app/views/public/api/v1/inboxes/conversations/index.json.jbuilder
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,3 @@ | ||
json.array! @conversations do |conversation| | ||
json.partial! 'public/api/v1/models/conversation.json.jbuilder', resource: conversation | ||
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 @@ | ||
json.partial! 'public/api/v1/models/message.json.jbuilder', resource: @message |
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,3 @@ | ||
json.array! @messages do |message| | ||
json.partial! 'public/api/v1/models/message.json.jbuilder', resource: message | ||
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 @@ | ||
json.partial! 'public/api/v1/models/message.json.jbuilder', resource: @message |
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,4 @@ | ||
json.id resource.id | ||
json.name resource.name | ||
json.email resource.email | ||
json.pubsub_token resource.pubsub_token |
10 changes: 10 additions & 0 deletions
10
app/views/public/api/v1/models/_conversation.json.jbuilder
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,10 @@ | ||
json.id resource.display_id | ||
json.inbox_id resource.inbox_id | ||
json.contact_last_seen_at resource.contact_last_seen_at.to_i | ||
json.status resource.status | ||
json.messages do | ||
json.array! resource.messages do |message| | ||
json.partial! 'public/api/v1/models/message.json.jbuilder', resource: message | ||
end | ||
end | ||
json.contact resource.contact |
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 @@ | ||
json.id resource.id | ||
json.content resource.content | ||
json.message_type resource.message_type_before_type_cast | ||
json.content_type resource.content_type | ||
json.content_attributes resource.content_attributes | ||
json.created_at resource.created_at.to_i | ||
json.conversation_id resource.conversation.display_id | ||
json.attachments resource.attachments.map(&:push_event_data) if resource.attachments.present? | ||
json.sender resource.sender.push_event_data if resource.sender |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class AddHmacToApiChannel < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :channel_api, :identifier, :string | ||
add_index :channel_api, :identifier, unique: true | ||
add_column :channel_api, :hmac_token, :string | ||
add_index :channel_api, :hmac_token, unique: true | ||
add_column :channel_api, :hmac_mandatory, :boolean, default: false | ||
add_column :channel_web_widgets, :hmac_mandatory, :boolean, default: false | ||
set_up_existing_api_channels | ||
end | ||
|
||
def set_up_existing_api_channels | ||
::Channel::Api.find_in_batches do |api_channels_batch| | ||
Rails.logger.info "migrated till #{api_channels_batch.first.id}\n" | ||
api_channels_batch.map(&:regenerate_hmac_token) | ||
api_channels_batch.map(&:regenerate_identifier) | ||
end | ||
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
Oops, something went wrong.