Chore: Provide fixed attachment URLs for Channels (#4507)
Prior to this change, The attachment URL sent from Chatwoot to 3rd party integrations like Whatsapp and Facebook involved a 301 redirect before the original content is served. This causes intermittent breakages for the sent attachments. fixes: #3632 ref: https://blog.saeloun.com/2021/09/14/rails-7-adds-expiring-urls-to-active-storage.html
This commit is contained in:
parent
2b2252b66e
commit
2c73df4292
11 changed files with 29 additions and 11 deletions
|
@ -44,12 +44,15 @@ class Attachment < ApplicationRecord
|
||||||
base_data.merge(file_metadata)
|
base_data.merge(file_metadata)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# NOTE: the URl returned does a 301 redirect to the actual file
|
||||||
def file_url
|
def file_url
|
||||||
file.attached? ? url_for(file) : ''
|
file.attached? ? url_for(file) : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# NOTE: for External services use this methods since redirect doesn't work effectively in a lot of cases
|
||||||
def download_url
|
def download_url
|
||||||
file.attached? ? rails_storage_proxy_url(file) : ''
|
ActiveStorage::Current.host = Rails.application.routes.default_url_options[:host] if ActiveStorage::Current.host.blank?
|
||||||
|
file.attached? ? file.blob.url : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
def thumb_url
|
def thumb_url
|
||||||
|
|
|
@ -95,7 +95,7 @@ class Channel::Telegram < ApplicationRecord
|
||||||
when 'file'
|
when 'file'
|
||||||
telegram_attachment[:type] = 'document'
|
telegram_attachment[:type] = 'document'
|
||||||
end
|
end
|
||||||
telegram_attachment[:media] = attachment.file_url
|
telegram_attachment[:media] = attachment.download_url
|
||||||
telegram_attachments << telegram_attachment
|
telegram_attachments << telegram_attachment
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ class Channel::Whatsapp < ApplicationRecord
|
||||||
def send_attachment_message(phone_number, message)
|
def send_attachment_message(phone_number, message)
|
||||||
attachment = message.attachments.first
|
attachment = message.attachments.first
|
||||||
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
||||||
attachment_url = attachment.file_url
|
attachment_url = attachment.download_url
|
||||||
response = HTTParty.post(
|
response = HTTParty.post(
|
||||||
"#{api_base_path}/messages",
|
"#{api_base_path}/messages",
|
||||||
headers: api_headers,
|
headers: api_headers,
|
||||||
|
|
|
@ -36,7 +36,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
||||||
attachment: {
|
attachment: {
|
||||||
type: attachment_type(attachment),
|
type: attachment_type(attachment),
|
||||||
payload: {
|
payload: {
|
||||||
url: attachment.file_url
|
url: attachment.download_url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -39,7 +39,7 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
||||||
attachment: {
|
attachment: {
|
||||||
type: attachment_type(attachment),
|
type: attachment_type(attachment),
|
||||||
payload: {
|
payload: {
|
||||||
url: attachment.file_url
|
url: attachment.download_url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ class Twilio::SendOnTwilioService < Base::SendOnChannelService
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
message.attachments.map(&:file_url)
|
message.attachments.map(&:download_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def inbox
|
def inbox
|
||||||
|
|
|
@ -20,8 +20,8 @@ Rails.application.configure do
|
||||||
config.public_file_server.headers = {
|
config.public_file_server.headers = {
|
||||||
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
|
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
|
||||||
}
|
}
|
||||||
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
|
||||||
Rails.application.routes.default_url_options = { host: 'localhost', port: 3000 }
|
Rails.application.routes.default_url_options = { host: 'http://localhost:3000' }
|
||||||
|
|
||||||
# Show full error reports and disable caching.
|
# Show full error reports and disable caching.
|
||||||
config.consider_all_requests_local = true
|
config.consider_all_requests_local = true
|
||||||
|
|
|
@ -73,7 +73,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_type
|
def file_type
|
||||||
File.extname(message.attachments.first.file_url).strip.downcase[1..]
|
File.extname(message.attachments.first.download_url).strip.downcase[1..]
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_information
|
def file_information
|
||||||
|
|
12
spec/models/attachment_spec.rb
Normal file
12
spec/models/attachment_spec.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Attachment, type: :model do
|
||||||
|
describe 'download_url' do
|
||||||
|
it 'returns valid download url' do
|
||||||
|
message = create(:message)
|
||||||
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||||
|
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
||||||
|
expect(attachment.download_url).not_to eq nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -57,6 +57,7 @@ describe Facebook::SendOnFacebookService do
|
||||||
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||||
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
||||||
message.save!
|
message.save!
|
||||||
|
allow(attachment).to receive(:download_url).and_return('url1')
|
||||||
::Facebook::SendOnFacebookService.new(message: message).perform
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
||||||
expect(bot).to have_received(:deliver).with({
|
expect(bot).to have_received(:deliver).with({
|
||||||
recipient: { id: contact_inbox.source_id },
|
recipient: { id: contact_inbox.source_id },
|
||||||
|
@ -70,7 +71,7 @@ describe Facebook::SendOnFacebookService do
|
||||||
attachment: {
|
attachment: {
|
||||||
type: 'image',
|
type: 'image',
|
||||||
payload: {
|
payload: {
|
||||||
url: attachment.file_url
|
url: 'url1'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -36,12 +36,14 @@ describe Sms::SendOnSmsService do
|
||||||
allow(HTTParty).to receive(:post).and_return(sms_request)
|
allow(HTTParty).to receive(:post).and_return(sms_request)
|
||||||
allow(sms_request).to receive(:success?).and_return(true)
|
allow(sms_request).to receive(:success?).and_return(true)
|
||||||
allow(sms_request).to receive(:parsed_response).and_return({ 'id' => '123456789' })
|
allow(sms_request).to receive(:parsed_response).and_return({ 'id' => '123456789' })
|
||||||
|
allow(attachment).to receive(:download_url).and_return('url1')
|
||||||
|
allow(attachment2).to receive(:download_url).and_return('url2')
|
||||||
expect(HTTParty).to receive(:post).with(
|
expect(HTTParty).to receive(:post).with(
|
||||||
'https://messaging.bandwidth.com/api/v2/users/1/messages',
|
'https://messaging.bandwidth.com/api/v2/users/1/messages',
|
||||||
basic_auth: { username: '1', password: '1' },
|
basic_auth: { username: '1', password: '1' },
|
||||||
headers: { 'Content-Type' => 'application/json' },
|
headers: { 'Content-Type' => 'application/json' },
|
||||||
body: { 'to' => '+123456789', 'from' => sms_channel.phone_number, 'text' => 'test', 'applicationId' => '1',
|
body: { 'to' => '+123456789', 'from' => sms_channel.phone_number, 'text' => 'test', 'applicationId' => '1',
|
||||||
'media' => [attachment.download_url, attachment2.download_url] }.to_json
|
'media' => %w[url1 url2] }.to_json
|
||||||
)
|
)
|
||||||
described_class.new(message: message).perform
|
described_class.new(message: message).perform
|
||||||
expect(message.reload.source_id).to eq('123456789')
|
expect(message.reload.source_id).to eq('123456789')
|
||||||
|
|
Loading…
Reference in a new issue