fix: set default content type as text for message (#3060)

Fixes #2974
This commit is contained in:
Murtaza Bagwala
2021-10-06 00:10:29 +05:30
committed by GitHub
parent bd7aeba484
commit f874925f0e
5 changed files with 26 additions and 4 deletions

View File

@@ -5,7 +5,7 @@
# id :integer not null, primary key
# content :text
# content_attributes :json
# content_type :integer default("text")
# content_type :integer default("text"), not null
# external_source_ids :jsonb
# message_type :integer not null
# private :boolean default(FALSE)
@@ -32,10 +32,13 @@ class Message < ApplicationRecord
include MessageFilterHelpers
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
before_validation :ensure_content_type
validates :account_id, presence: true
validates :inbox_id, presence: true
validates :conversation_id, presence: true
validates_with ContentAttributeValidator
validates :content_type, presence: true
# when you have a temperory id in your frontend and want it echoed back via action cable
attr_accessor :echo_id
@@ -133,6 +136,10 @@ class Message < ApplicationRecord
private
def ensure_content_type
self.content_type ||= Message.content_types[:text]
end
def execute_after_create_commit_callbacks
# rails issue with order of active record callbacks being executed https://github.com/rails/rails/issues/20911
reopen_conversation

View File

@@ -0,0 +1,5 @@
class SetContentTypeTextForTheOldMessages < ActiveRecord::Migration[6.1]
def change
change_column_null(:messages, :content_type, false, 0)
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_09_22_082754) do
ActiveRecord::Schema.define(version: 2021_09_23_132659) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
@@ -519,7 +519,7 @@ ActiveRecord::Schema.define(version: 2021_09_22_082754) do
t.boolean "private", default: false
t.integer "status", default: 0
t.string "source_id"
t.integer "content_type", default: 0
t.integer "content_type", default: 0, null: false
t.json "content_attributes", default: {}
t.string "sender_type"
t.bigint "sender_id"

View File

@@ -35,7 +35,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
expect(conversation.messages.first.content).to eq(params[:content])
end
it 'creates an outgoing message with a specific bot sender' do
it 'creates an outgoing text message with a specific bot sender' do
agent_bot = create(:agent_bot)
time_stamp = Time.now.utc.to_s
params = { content: 'test-message', external_created_at: time_stamp, sender_type: 'AgentBot', sender_id: agent_bot.id }
@@ -50,6 +50,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
expect(response_data['content_attributes']['external_created_at']).to eq time_stamp
expect(conversation.messages.count).to eq(1)
expect(conversation.messages.last.sender_id).to eq(agent_bot.id)
expect(conversation.messages.last.content_type).to eq('text')
end
it 'creates a new outgoing message with attachment' do

View File

@@ -77,4 +77,13 @@ RSpec.describe Message, type: :model do
expect(ConversationReplyEmailWorker).not_to have_received(:perform_in)
end
end
context 'when content_type is blank' do
let(:message) { build(:message, content_type: nil, account: create(:account)) }
it 'sets content_type as text' do
message.save!
expect(message.content_type).to eq 'text'
end
end
end