feat: Add support for template variables in messages content (#6215)

Fixes: #6078

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Muhsin Keloth
2023-01-10 16:00:34 +05:30
committed by GitHub
parent e7a52c3a46
commit 078ff615ee
8 changed files with 177 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
module Liquidable
extend ActiveSupport::Concern
included do
acts_as_taggable_on :labels
before_create :process_liquid_in_content
end
private
def message_drops
{
'contact' => ContactDrop.new(conversation.contact),
'agent' => UserDrop.new(sender),
'conversation' => ConversationDrop.new(conversation),
'inbox' => InboxDrop.new(inbox)
}
end
def liquid_processable_message?
content.present? && message_type == 'outgoing'
end
def process_liquid_in_content
return unless liquid_processable_message?
template = Liquid::Template.parse(modified_liquid_content)
self.content = template.render(message_drops)
end
def modified_liquid_content
# This regex is used to match the code blocks in the content
# We don't want to process liquid in code blocks
content.gsub(/`(.*?)`/m, '{% raw %}`\\1`{% endraw %}')
end
end