diff --git a/Gemfile b/Gemfile index 711e744c3..7e7e6ea97 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,8 @@ gem 'flag_shih_tzu' gem 'haikunator' # Template parsing safetly gem 'liquid' +# Parse Markdown to HTML +gem 'redcarpet' ##-- for active storage --## gem 'aws-sdk-s3', require: false diff --git a/Gemfile.lock b/Gemfile.lock index e0b382569..d9da65980 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -384,6 +384,7 @@ GEM rb-fsevent (0.10.4) rb-inotify (0.10.1) ffi (~> 1.0) + redcarpet (3.5.1) redis (4.2.1) redis-namespace (1.8.0) redis (>= 3.0.4) @@ -628,6 +629,7 @@ DEPENDENCIES pundit rack-cors rails + redcarpet redis redis-namespace redis-rack-cache diff --git a/app/builders/contact_builder.rb b/app/builders/contact_builder.rb index 3e362c9b5..dc15a299a 100644 --- a/app/builders/contact_builder.rb +++ b/app/builders/contact_builder.rb @@ -5,7 +5,7 @@ class ContactBuilder contact_inbox = inbox.contact_inboxes.find_by(source_id: source_id) return contact_inbox if contact_inbox - build_contact + build_contact_inbox end private @@ -26,16 +26,29 @@ class ContactBuilder ::ContactAvatarJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url] end - def build_contact - ActiveRecord::Base.transaction do - contact = account.contacts.create!( - name: contact_attributes[:name], - phone_number: contact_attributes[:phone_number], - email: contact_attributes[:email], - identifier: contact_attributes[:identifier], - additional_attributes: contact_attributes[:additional_attributes] - ) + def create_contact + account.contacts.create!( + name: contact_attributes[:name], + phone_number: contact_attributes[:phone_number], + email: contact_attributes[:email], + identifier: contact_attributes[:identifier], + additional_attributes: contact_attributes[:additional_attributes] + ) + end + def find_contact + contact = nil + + contact = account.contacts.find_by(identifier: contact_attributes[:identifier]) if contact_attributes[:identifier].present? + + contact ||= account.contacts.find_by(email: contact_attributes[:email]) if contact_attributes[:email].present? + + contact + end + + def build_contact_inbox + ActiveRecord::Base.transaction do + contact = find_contact || create_contact contact_inbox = create_contact_inbox(contact) update_contact_avatar(contact) contact_inbox diff --git a/app/mailers/conversation_reply_mailer.rb b/app/mailers/conversation_reply_mailer.rb index ee3ef8239..165e9cff7 100644 --- a/app/mailers/conversation_reply_mailer.rb +++ b/app/mailers/conversation_reply_mailer.rb @@ -130,8 +130,8 @@ class ConversationReplyMailer < ApplicationMailer def conversation_reply_email_id content_attributes = @conversation.messages.incoming.last&.content_attributes - if content_attributes && content_attributes['email'] && content_attributes['message_id'] - "<#{@conversation.messages.incoming.last.content_attributes['email']['message_id']}>" + if content_attributes && content_attributes['email'] && content_attributes['email']['message_id'] + return "<#{content_attributes['email']['message_id']}>" end nil diff --git a/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb b/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb index 825d5ce3f..d8a3e0e9b 100644 --- a/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb +++ b/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb @@ -1,3 +1,4 @@ +<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %> <% @messages.each do |message| %> @@ -7,7 +8,7 @@ <% if message.content %> - <%= message.content %> + <%= markdown.render(message.content).html_safe %> <% end %> <% if message.attachments %> <% message.attachments.each do |attachment| %> diff --git a/app/views/mailers/conversation_reply_mailer/reply_with_summary.html.erb b/app/views/mailers/conversation_reply_mailer/reply_with_summary.html.erb index 5e182a273..d0b38a021 100644 --- a/app/views/mailers/conversation_reply_mailer/reply_with_summary.html.erb +++ b/app/views/mailers/conversation_reply_mailer/reply_with_summary.html.erb @@ -1,3 +1,4 @@ +<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %>

Hi <%= @contact.name %>,

You have new messages on your conversation.

@@ -11,7 +12,7 @@ <% if message.content %> - <%= message.content %> + <%= markdown.render(message.content).html_safe %> <% end %> <% if message.attachments %> <% message.attachments.each do |attachment| %> diff --git a/app/views/mailers/conversation_reply_mailer/reply_without_summary.html.erb b/app/views/mailers/conversation_reply_mailer/reply_without_summary.html.erb index 54f607c33..ed084b2ca 100644 --- a/app/views/mailers/conversation_reply_mailer/reply_without_summary.html.erb +++ b/app/views/mailers/conversation_reply_mailer/reply_without_summary.html.erb @@ -1,7 +1,9 @@ +<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %> + <% @messages.each do |message| %>

<% if message.content %> - <%= message.content.gsub("\n", "
").html_safe %> + <%= markdown.render(message.content).html_safe %> <% end %> <% if message.attachments %> <% message.attachments.each do |attachment| %>