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:
Sojan Jose
2022-04-20 22:42:13 +05:30
committed by GitHub
parent 2b2252b66e
commit 2c73df4292
11 changed files with 29 additions and 11 deletions

View File

@@ -44,12 +44,15 @@ class Attachment < ApplicationRecord
base_data.merge(file_metadata)
end
# NOTE: the URl returned does a 301 redirect to the actual file
def file_url
file.attached? ? url_for(file) : ''
end
# NOTE: for External services use this methods since redirect doesn't work effectively in a lot of cases
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
def thumb_url

View File

@@ -95,7 +95,7 @@ class Channel::Telegram < ApplicationRecord
when 'file'
telegram_attachment[:type] = 'document'
end
telegram_attachment[:media] = attachment.file_url
telegram_attachment[:media] = attachment.download_url
telegram_attachments << telegram_attachment
end

View File

@@ -85,7 +85,7 @@ class Channel::Whatsapp < ApplicationRecord
def send_attachment_message(phone_number, message)
attachment = message.attachments.first
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(
"#{api_base_path}/messages",
headers: api_headers,