2019-08-14 09:48:44 +00:00
|
|
|
require 'uri'
|
|
|
|
require 'open-uri'
|
|
|
|
class Attachment < ApplicationRecord
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :message
|
2019-10-20 08:47:26 +00:00
|
|
|
mount_uploader :file, AttachmentUploader # used for images
|
2019-08-14 09:48:44 +00:00
|
|
|
enum file_type: [:image, :audio, :video, :file, :location, :fallback]
|
|
|
|
|
|
|
|
before_create :set_file_extension
|
|
|
|
|
|
|
|
def push_event_data
|
2019-10-02 06:14:34 +00:00
|
|
|
return base_data.merge(location_metadata) if file_type.to_sym == :location
|
|
|
|
return base_data.merge(fallback_data) if file_type.to_sym == :fallback
|
|
|
|
|
|
|
|
base_data.merge(file_metadata)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def file_metadata
|
|
|
|
{
|
|
|
|
extension: extension,
|
|
|
|
data_url: file_url,
|
|
|
|
thumb_url: file.try(:thumb).try(:url) # will exist only for images
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def location_metadata
|
|
|
|
{
|
|
|
|
coordinates_lat: coordinates_lat,
|
|
|
|
coordinates_long: coordinates_long,
|
|
|
|
fallback_title: fallback_title,
|
|
|
|
data_url: external_url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_data
|
|
|
|
{
|
|
|
|
fallback_title: fallback_title,
|
|
|
|
data_url: external_url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_data
|
|
|
|
{
|
2019-08-14 09:48:44 +00:00
|
|
|
id: id,
|
|
|
|
message_id: message_id,
|
|
|
|
file_type: file_type,
|
|
|
|
account_id: account_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_file_extension
|
2019-10-20 08:47:26 +00:00
|
|
|
if external_url && !fallback?
|
|
|
|
self.extension = begin
|
|
|
|
Pathname.new(URI(external_url).path).extname
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|