Chore: Switch from Carrierwave to ActiveStorage (#393)
This commit is contained in:
@@ -36,19 +36,26 @@ module Messages
|
||||
def build_contact
|
||||
return if contact.present?
|
||||
|
||||
@contact = Contact.create!(contact_params)
|
||||
@contact = Contact.create!(contact_params.except(:remote_avatar_url))
|
||||
avatar_resource = LocalResource.new(contact_params[:remote_avatar_url])
|
||||
@contact.avatar.attach(io: avatar_resource.file, filename: avatar_resource.tmp_filename, content_type: avatar_resource.encoding)
|
||||
|
||||
ContactInbox.create(contact: contact, inbox: @inbox, source_id: @sender_id)
|
||||
end
|
||||
|
||||
def build_message
|
||||
@message = conversation.messages.new(message_params)
|
||||
@message = conversation.messages.create!(message_params)
|
||||
(response.attachments || []).each do |attachment|
|
||||
@message.build_attachment(attachment_params(attachment))
|
||||
attachment_obj = @message.build_attachment(attachment_params(attachment).except(:remote_file_url))
|
||||
attachment_obj.save!
|
||||
attach_file(attachment_obj, attachment_params(attachment)[:remote_file_url]) if attachment_params(attachment)[:remote_file_url]
|
||||
end
|
||||
@message.save!
|
||||
end
|
||||
|
||||
def build_attachment; end
|
||||
def attach_file(attachment, file_url)
|
||||
file_resource = LocalResource.new(file_url)
|
||||
attachment.file.attach(io: file_resource.file, filename: file_resource.tmp_filename, content_type: file_resource.encoding)
|
||||
end
|
||||
|
||||
def conversation
|
||||
@conversation ||= Conversation.find_by(conversation_params) || Conversation.create!(conversation_params)
|
||||
@@ -123,7 +130,7 @@ module Messages
|
||||
{
|
||||
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
||||
account_id: @inbox.account_id,
|
||||
remote_avatar_url: result['profile_pic'] || nil
|
||||
remote_avatar_url: result['profile_pic'] || ''
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,8 +12,9 @@ class Api::V1::CallbacksController < ApplicationController
|
||||
inbox_name = params[:inbox_name]
|
||||
facebook_channel = current_account.facebook_pages.create!(
|
||||
name: page_name, page_id: page_id, user_access_token: user_access_token,
|
||||
page_access_token: page_access_token, remote_avatar_url: set_avatar(page_id)
|
||||
page_access_token: page_access_token
|
||||
)
|
||||
set_avatar(facebook_channel, page_id)
|
||||
inbox = current_account.inboxes.create!(name: inbox_name, channel: facebook_channel)
|
||||
render json: inbox
|
||||
end
|
||||
@@ -79,7 +80,12 @@ class Api::V1::CallbacksController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def set_avatar(page_id)
|
||||
def set_avatar(facebook_channel, page_id)
|
||||
avatar_resource = LocalResource.new(get_avatar_url(page_id))
|
||||
facebook_channel.avatar.attach(io: avatar_resource.file, filename: avatar_resource.tmp_filename, content_type: avatar_resource.encoding)
|
||||
end
|
||||
|
||||
def get_avatar_url(page_id)
|
||||
begin
|
||||
url = 'http://graph.facebook.com/' << page_id << '/picture?type=large'
|
||||
uri = URI.parse(url)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<div class="contact--profile">
|
||||
<div class="contact--info">
|
||||
<thumbnail
|
||||
:src="contact.avatar_url"
|
||||
:src="contact.thumbnail"
|
||||
size="56px"
|
||||
:badge="contact.channel"
|
||||
:username="contact.name"
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
# extension :string
|
||||
# external_url :string
|
||||
# fallback_title :string
|
||||
# file :string
|
||||
# file_type :integer default("image")
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
@@ -19,12 +18,12 @@
|
||||
require 'uri'
|
||||
require 'open-uri'
|
||||
class Attachment < ApplicationRecord
|
||||
include Rails.application.routes.url_helpers
|
||||
belongs_to :account
|
||||
belongs_to :message
|
||||
mount_uploader :file, AttachmentUploader # used for images
|
||||
enum file_type: [:image, :audio, :video, :file, :location, :fallback]
|
||||
has_one_attached :file
|
||||
|
||||
before_create :set_file_extension
|
||||
enum file_type: [:image, :audio, :video, :file, :location, :fallback]
|
||||
|
||||
def push_event_data
|
||||
return base_data.merge(location_metadata) if file_type.to_sym == :location
|
||||
@@ -68,13 +67,7 @@ class Attachment < ApplicationRecord
|
||||
}
|
||||
end
|
||||
|
||||
def set_file_extension
|
||||
if external_url && !fallback?
|
||||
self.extension = begin
|
||||
Pathname.new(URI(external_url).path).extname
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
def file_url
|
||||
file.attached? ? url_for(file) : ''
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
# Table name: channel_facebook_pages
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# avatar :string
|
||||
# name :string not null
|
||||
# page_access_token :string not null
|
||||
# user_access_token :string not null
|
||||
@@ -20,11 +19,13 @@
|
||||
|
||||
module Channel
|
||||
class FacebookPage < ApplicationRecord
|
||||
include Avatarable
|
||||
|
||||
self.table_name = 'channel_facebook_pages'
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :page_id, uniqueness: { scope: :account_id }
|
||||
mount_uploader :avatar, AvatarUploader
|
||||
has_one_attached :avatar
|
||||
belongs_to :account
|
||||
|
||||
has_one :inbox, as: :channel, dependent: :destroy
|
||||
|
||||
18
app/models/concerns/avatarable.rb
Normal file
18
app/models/concerns/avatarable.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Avatarable
|
||||
extend ActiveSupport::Concern
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
included do
|
||||
has_one_attached :avatar
|
||||
end
|
||||
|
||||
def avatar_url
|
||||
if avatar.attached? && avatar.representable?
|
||||
url_for(avatar.representation(resize: '250x250'))
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,6 @@
|
||||
# Table name: contacts
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# avatar :string
|
||||
# email :string
|
||||
# name :string
|
||||
# phone_number :string
|
||||
@@ -20,13 +19,13 @@
|
||||
|
||||
class Contact < ApplicationRecord
|
||||
include Pubsubable
|
||||
include Avatarable
|
||||
validates :account_id, presence: true
|
||||
|
||||
belongs_to :account
|
||||
has_many :conversations, dependent: :destroy
|
||||
has_many :contact_inboxes, dependent: :destroy
|
||||
has_many :inboxes, through: :contact_inboxes
|
||||
mount_uploader :avatar, AvatarUploader
|
||||
|
||||
def get_source_id(inbox_id)
|
||||
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
||||
@@ -36,7 +35,7 @@ class Contact < ApplicationRecord
|
||||
{
|
||||
id: id,
|
||||
name: name,
|
||||
thumbnail: avatar.thumb.url,
|
||||
thumbnail: avatar_url,
|
||||
pubsub_token: pubsub_token
|
||||
}
|
||||
end
|
||||
|
||||
@@ -68,11 +68,10 @@ class Inbox < ApplicationRecord
|
||||
Facebook::Messenger::Subscriptions.subscribe(
|
||||
access_token: channel.page_access_token,
|
||||
subscribed_fields: %w[
|
||||
message_mention messages messaging_account_linking messaging_checkout_updates
|
||||
message_echoes message_deliveries messaging_game_plays messaging_optins messaging_optouts
|
||||
messaging_payments messaging_postbacks messaging_pre_checkouts message_reads messaging_referrals
|
||||
messaging_handovers messaging_policy_enforcement messaging_page_feedback
|
||||
messaging_appointments messaging_direct_sends
|
||||
messages messaging_postbacks messaging_optins message_deliveries
|
||||
message_reads messaging_payments messaging_pre_checkouts messaging_checkout_updates
|
||||
messaging_account_linking messaging_referrals message_echoes messaging_game_plays
|
||||
standby messaging_handovers messaging_policy_enforcement message_reactions
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
@@ -47,6 +47,7 @@ class User < ApplicationRecord
|
||||
include DeviseTokenAuth::Concerns::User
|
||||
include Events::Types
|
||||
include Pubsubable
|
||||
include Avatarable
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
devise :database_authenticatable,
|
||||
@@ -57,12 +58,6 @@ class User < ApplicationRecord
|
||||
:validatable,
|
||||
:confirmable
|
||||
|
||||
# Used by the actionCable/PubSub Service we use for real time communications
|
||||
has_secure_token :pubsub_token
|
||||
|
||||
# Uses active storage for the avatar
|
||||
has_one_attached :avatar
|
||||
|
||||
# The validation below has been commented out as it does not
|
||||
# work because :validatable in devise overrides this.
|
||||
# validates_uniqueness_of :email, scope: :account_id
|
||||
@@ -108,14 +103,6 @@ class User < ApplicationRecord
|
||||
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
|
||||
end
|
||||
|
||||
def avatar_url
|
||||
if avatar.attached? && avatar.representable?
|
||||
url_for(avatar.representation(resize: '250x250'))
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
def push_event_data
|
||||
{
|
||||
name: name,
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
class AttachmentUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
def store_dir
|
||||
if Rails.env.test?
|
||||
"#{Rails.root}/spec/support/uploads/attachments/#{model.class.to_s.underscore}/#{model.id}"
|
||||
else
|
||||
"uploads/attachments/#{model.class.to_s.underscore}/#{model.id}"
|
||||
end
|
||||
end
|
||||
|
||||
version :thumb, if: :image? do
|
||||
process resize_to_fill: [280, 280]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def image?(_new_file)
|
||||
model.image?
|
||||
end
|
||||
end
|
||||
@@ -1,19 +0,0 @@
|
||||
class AvatarUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
def store_dir
|
||||
if Rails.env.test?
|
||||
"#{Rails.root}/spec/support/uploads/avatar/#{model.class.to_s.underscore}/#{model.id}"
|
||||
else
|
||||
"uploads/avatar/#{model.class.to_s.underscore}/#{model.id}"
|
||||
end
|
||||
end
|
||||
|
||||
version :thumb do
|
||||
process resize_to_fill: [64, 64]
|
||||
end
|
||||
|
||||
version :profile_thumb do
|
||||
process resize_to_fill: [128, 128]
|
||||
end
|
||||
end
|
||||
@@ -3,5 +3,5 @@ json.payload do
|
||||
json.name @contact.name
|
||||
json.email @contact.email
|
||||
json.phone_number @contact.phone_number
|
||||
json.thumbnail @contact.avatar.thumb.url
|
||||
json.thumbnail @contact.avatar_url
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ json.data do
|
||||
json.sender do
|
||||
json.id conversation.contact.id
|
||||
json.name conversation.contact.name
|
||||
json.thumbnail conversation.contact.avatar.thumb.url
|
||||
json.thumbnail conversation.contact.avatar_url
|
||||
json.channel conversation.inbox.try(:channel_type)
|
||||
end
|
||||
json.assignee conversation.assignee
|
||||
|
||||
@@ -4,7 +4,7 @@ json.payload do
|
||||
json.channel_id inbox.channel_id
|
||||
json.name inbox.name
|
||||
json.channel_type inbox.channel_type
|
||||
json.avatar_url inbox.channel.try(:avatar).try(:url)
|
||||
json.avatar_url inbox.channel.try(:avatar_url)
|
||||
json.page_id inbox.channel.try(:page_id)
|
||||
json.widget_color inbox.channel.try(:widget_color)
|
||||
json.website_token inbox.channel.try(:website_token)
|
||||
|
||||
Reference in New Issue
Block a user