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,58 @@
require 'rails_helper'
shared_examples_for 'liqudable' do
context 'when liquid is present in content' do
let(:contact) { create(:contact, name: 'john', phone_number: '+912883') }
let(:conversation) { create(:conversation, id: 1, contact: contact) }
context 'when message is incoming' do
let(:message) { build(:message, conversation: conversation, message_type: 'incoming') }
it 'will not process liquid in content' do
message.content = 'hey {{contact.name}} how are you?'
message.save!
expect(message.content).to eq 'hey {{contact.name}} how are you?'
end
end
context 'when message is outgoing' do
let(:message) { build(:message, conversation: conversation, message_type: 'outgoing') }
it 'set replaces liquid variables in message' do
message.content = 'hey {{contact.name}} how are you?'
message.save!
expect(message.content).to eq 'hey john how are you?'
end
it 'process liquid operators like default value' do
message.content = 'Can we send you an email at {{ contact.email | default: "default" }} ?'
message.save!
expect(message.content).to eq 'Can we send you an email at default ?'
end
it 'return empty string when value is not available' do
message.content = 'Can we send you an email at {{contact.email}}?'
message.save!
expect(message.content).to eq 'Can we send you an email at ?'
end
it 'will not process liquid tags in multiple code blocks' do
message.content = 'hey {{contact.name}} how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`'
message.save!
expect(message.content).to eq 'hey john how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`'
end
it 'will not process liquid tags in single ticks' do
message.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test'
message.save!
expect(message.content).to eq 'hey john how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test'
end
it 'will not throw error for broken quotes' do
message.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} test'
message.save!
expect(message.content).to eq 'hey john how are you? ` code: {{contact.name}} ` ` code: john test'
end
end
end
end

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'rails_helper'
require Rails.root.join 'spec/models/concerns/liquidable_shared.rb'
RSpec.describe Message, type: :model do
context 'with validations' do
@@ -9,6 +10,10 @@ RSpec.describe Message, type: :model do
it { is_expected.to validate_presence_of(:account_id) }
end
describe 'concerns' do
it_behaves_like 'liqudable'
end
describe '#reopen_conversation' do
let(:conversation) { create(:conversation) }
let(:message) { build(:message, message_type: :incoming, conversation: conversation) }