feat: Enable Markdown Parsing in emails (#1663)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose 2021-01-18 11:43:31 +05:30 committed by GitHub
parent 54f15b73d3
commit 13073629bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 15 deletions

View file

@ -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

View file

@ -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

View file

@ -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!(
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

View file

@ -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

View file

@ -1,3 +1,4 @@
<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %>
<% @messages.each do |message| %>
<tr>
<td>
@ -7,7 +8,7 @@
<tr>
<td style="padding-bottom: 16px;">
<% if message.content %>
<%= message.content %>
<%= markdown.render(message.content).html_safe %>
<% end %>
<% if message.attachments %>
<% message.attachments.each do |attachment| %>

View file

@ -1,3 +1,4 @@
<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %>
<p>Hi <%= @contact.name %>,</p>
<p>You have new messages on your conversation.</p>
@ -11,7 +12,7 @@
<tr>
<td style="padding: 10px 20px; margin: 5px 0 20px 0; background: #F2F3F7; border-radius: 10px; display: inline-block; font-family: "Helvetica Neue",Tahoma,Arial,sans-serif; text-align: start; unicode-bidi: plaintext;">
<% if message.content %>
<%= message.content %>
<%= markdown.render(message.content).html_safe %>
<% end %>
<% if message.attachments %>
<% message.attachments.each do |attachment| %>

View file

@ -1,7 +1,9 @@
<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %>
<% @messages.each do |message| %>
<p style="font-family: Roboto,"Helvetica Neue",Tahoma,Arial,sans-serif; text-align: start; unicode-bidi: plaintext;">
<% if message.content %>
<%= message.content.gsub("\n", "<br/>").html_safe %>
<%= markdown.render(message.content).html_safe %>
<% end %>
<% if message.attachments %>
<% message.attachments.each do |attachment| %>