2020-01-09 07:36:40 +00:00
|
|
|
class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
|
2019-10-29 07:20:54 +00:00
|
|
|
before_action :set_conversation, only: [:create]
|
2020-01-09 07:36:40 +00:00
|
|
|
before_action :set_message, only: [:update]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def index
|
|
|
|
@messages = conversation.nil? ? [] : message_finder.perform
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def create
|
|
|
|
@message = conversation.messages.new(message_params)
|
2020-04-02 06:58:38 +00:00
|
|
|
build_attachment
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
@message.save!
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def update
|
2020-04-10 11:12:37 +00:00
|
|
|
if @message.content_type == 'input_email'
|
|
|
|
@message.update!(submitted_email: contact_email)
|
|
|
|
update_contact(contact_email)
|
|
|
|
else
|
|
|
|
@message.update!(message_update_params[:message])
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
rescue StandardError => e
|
2021-06-07 11:56:08 +00:00
|
|
|
render json: { error: @contact.errors, message: e.message }.to_json, status: :internal_server_error
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
private
|
|
|
|
|
2020-04-02 06:58:38 +00:00
|
|
|
def build_attachment
|
2020-04-17 15:45:20 +00:00
|
|
|
return if params[:message][:attachments].blank?
|
|
|
|
|
|
|
|
params[:message][:attachments].each do |uploaded_attachment|
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
@message.attachments.new(
|
2020-04-17 15:45:20 +00:00
|
|
|
account_id: @message.account_id,
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
file_type: helpers.file_type(uploaded_attachment&.content_type),
|
|
|
|
file: uploaded_attachment
|
2020-04-17 15:45:20 +00:00
|
|
|
)
|
|
|
|
end
|
2020-04-02 06:58:38 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def set_conversation
|
2021-02-15 18:44:13 +00:00
|
|
|
@conversation = create_conversation if conversation.nil?
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_finder_params
|
|
|
|
{
|
2019-12-11 15:27:06 +00:00
|
|
|
filter_internal_messages: true,
|
|
|
|
before: permitted_params[:before]
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_finder
|
|
|
|
@message_finder ||= MessageFinder.new(conversation, message_finder_params)
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def message_update_params
|
2021-06-29 15:29:41 +00:00
|
|
|
params.permit(message: [{ submitted_values: [:name, :title, :value, { csat_survey_response: [:feedback_message, :rating] }] }])
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def permitted_params
|
2021-06-15 14:39:17 +00:00
|
|
|
# timestamp parameter is used in create conversation method
|
2021-02-15 18:44:13 +00:00
|
|
|
params.permit(:id, :before, :website_token, contact: [:name, :email], message: [:content, :referer_url, :timestamp, :echo_id])
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2019-10-30 05:13:11 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def set_message
|
|
|
|
@message = @web_widget.inbox.messages.find(permitted_params[:id])
|
2019-10-30 05:13:11 +00:00
|
|
|
end
|
2019-10-20 08:47:26 +00:00
|
|
|
end
|