mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 03:27:52 +00:00
When the CC field is generated in the UI, the email values are joined together with ", " but when they are parsed, we currently split by just ",". This causes an error on the backend and on the frontend. It seems reasonable to update the code to allow whitespace in the input and to split by `\s*,\s` and also to trim leading and trailing whitespace from the CC list. --------- Co-authored-by: Sojan <sojan@pepalo.com>
119 lines
3.6 KiB
Ruby
119 lines
3.6 KiB
Ruby
class Messages::MessageBuilder
|
|
include ::FileTypeHelper
|
|
attr_reader :message
|
|
|
|
def initialize(user, conversation, params)
|
|
@params = params
|
|
@private = params[:private] || false
|
|
@conversation = conversation
|
|
@user = user
|
|
@message_type = params[:message_type] || 'outgoing'
|
|
@attachments = params[:attachments]
|
|
@automation_rule = @params&.dig(:content_attributes, :automation_rule_id)
|
|
return unless params.instance_of?(ActionController::Parameters)
|
|
|
|
@in_reply_to = params.to_unsafe_h&.dig(:content_attributes, :in_reply_to)
|
|
@items = params.to_unsafe_h&.dig(:content_attributes, :items)
|
|
end
|
|
|
|
def perform
|
|
@message = @conversation.messages.build(message_params)
|
|
process_attachments
|
|
process_emails
|
|
@message.save!
|
|
@message
|
|
end
|
|
|
|
private
|
|
|
|
def process_attachments
|
|
return if @attachments.blank?
|
|
|
|
@attachments.each do |uploaded_attachment|
|
|
attachment = @message.attachments.build(
|
|
account_id: @message.account_id,
|
|
file: uploaded_attachment
|
|
)
|
|
|
|
attachment.file_type = if uploaded_attachment.is_a?(String)
|
|
file_type_by_signed_id(
|
|
uploaded_attachment
|
|
)
|
|
else
|
|
file_type(uploaded_attachment&.content_type)
|
|
end
|
|
end
|
|
end
|
|
|
|
def process_emails
|
|
return unless @conversation.inbox&.inbox_type == 'Email'
|
|
|
|
cc_emails = []
|
|
cc_emails = @params[:cc_emails].gsub(/\s+/, '').split(',') if @params[:cc_emails].present?
|
|
|
|
bcc_emails = []
|
|
bcc_emails = @params[:bcc_emails].gsub(/\s+/, '').split(',') if @params[:bcc_emails].present?
|
|
|
|
all_email_addresses = cc_emails + bcc_emails
|
|
validate_email_addresses(all_email_addresses)
|
|
|
|
@message.content_attributes[:cc_emails] = cc_emails
|
|
@message.content_attributes[:bcc_emails] = bcc_emails
|
|
end
|
|
|
|
def validate_email_addresses(all_emails)
|
|
all_emails&.each do |email|
|
|
raise StandardError, 'Invalid email address' unless email.match?(URI::MailTo::EMAIL_REGEXP)
|
|
end
|
|
end
|
|
|
|
def message_type
|
|
if @conversation.inbox.channel_type != 'Channel::Api' && @message_type == 'incoming'
|
|
raise StandardError, 'Incoming messages are only allowed in Api inboxes'
|
|
end
|
|
|
|
@message_type
|
|
end
|
|
|
|
def sender
|
|
message_type == 'outgoing' ? (message_sender || @user) : @conversation.contact
|
|
end
|
|
|
|
def external_created_at
|
|
@params[:external_created_at].present? ? { external_created_at: @params[:external_created_at] } : {}
|
|
end
|
|
|
|
def automation_rule_id
|
|
@automation_rule.present? ? { content_attributes: { automation_rule_id: @automation_rule } } : {}
|
|
end
|
|
|
|
def campaign_id
|
|
@params[:campaign_id].present? ? { additional_attributes: { campaign_id: @params[:campaign_id] } } : {}
|
|
end
|
|
|
|
def template_params
|
|
@params[:template_params].present? ? { additional_attributes: { template_params: JSON.parse(@params[:template_params].to_json) } } : {}
|
|
end
|
|
|
|
def message_sender
|
|
return if @params[:sender_type] != 'AgentBot'
|
|
|
|
AgentBot.where(account_id: [nil, @conversation.account.id]).find_by(id: @params[:sender_id])
|
|
end
|
|
|
|
def message_params
|
|
{
|
|
account_id: @conversation.account_id,
|
|
inbox_id: @conversation.inbox_id,
|
|
message_type: message_type,
|
|
content: @params[:content],
|
|
private: @private,
|
|
sender: sender,
|
|
content_type: @params[:content_type],
|
|
items: @items,
|
|
in_reply_to: @in_reply_to,
|
|
echo_id: @params[:echo_id]
|
|
}.merge(external_created_at).merge(automation_rule_id).merge(campaign_id).merge(template_params)
|
|
end
|
|
end
|