From f874925f0ed72ff450d711191bceb823f1865a5f Mon Sep 17 00:00:00 2001 From: Murtaza Bagwala Date: Wed, 6 Oct 2021 00:10:29 +0530 Subject: [PATCH] fix: set default content type as text for message (#3060) Fixes #2974 --- app/models/message.rb | 9 ++++++++- ...3132659_set_content_type_text_for_the_old_messages.rb | 5 +++++ db/schema.rb | 4 ++-- .../accounts/conversations/messages_controller_spec.rb | 3 ++- spec/models/message_spec.rb | 9 +++++++++ 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20210923132659_set_content_type_text_for_the_old_messages.rb diff --git a/app/models/message.rb b/app/models/message.rb index d731bf488..83705f4ea 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/db/migrate/20210923132659_set_content_type_text_for_the_old_messages.rb b/db/migrate/20210923132659_set_content_type_text_for_the_old_messages.rb new file mode 100644 index 000000000..d9af6f19b --- /dev/null +++ b/db/migrate/20210923132659_set_content_type_text_for_the_old_messages.rb @@ -0,0 +1,5 @@ +class SetContentTypeTextForTheOldMessages < ActiveRecord::Migration[6.1] + def change + change_column_null(:messages, :content_type, false, 0) + end +end diff --git a/db/schema.rb b/db/schema.rb index b065ead82..283018559 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb index d32608ba2..605e75b56 100644 --- a/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb @@ -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 diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 43b997b47..4e0933ec2 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -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