chore: Upgrade rails and ruby versions (#2400)
ruby version: 3.0.2 rails version: 6.1.4
This commit is contained in:
@@ -15,11 +15,6 @@ module Chatwoot
|
||||
|
||||
config.autoload_paths << Rails.root.join('lib')
|
||||
config.eager_load_paths << Rails.root.join('lib')
|
||||
Rails.autoloaders.main.ignore(Rails.root.join('lib/azure'))
|
||||
|
||||
# This is required in production for zeitwerk to autoload the file
|
||||
config.paths.add File.join('app', 'bot'), glob: File.join('**', '*.rb')
|
||||
config.autoload_paths << Rails.root.join('app/bot/*')
|
||||
|
||||
# Settings in config/environments/* take precedence over those specified here.
|
||||
# Application configuration can go into files in config/initializers
|
||||
|
||||
1
config/initializers/active_record_query_trace.rb
Normal file
1
config/initializers/active_record_query_trace.rb
Normal file
@@ -0,0 +1 @@
|
||||
ActiveRecordQueryTrace.enabled = true if Rails.env.development?
|
||||
@@ -16,4 +16,5 @@ Rails.application.config.assets.precompile += %w[dashboardChart.js]
|
||||
|
||||
# to take care of fonts in assets pre-compiling
|
||||
# Ref: https://stackoverflow.com/questions/56960709/rails-font-cors-policy
|
||||
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2)$/
|
||||
# https://github.com/rails/sprockets/issues/632#issuecomment-551324428
|
||||
Rails.application.config.assets.precompile << ['*.svg', '*.eot', '*.woff', '*.ttf']
|
||||
|
||||
@@ -1,170 +0,0 @@
|
||||
# TODO: Remove this once the changes comes into rails version
|
||||
# https://github.com/Azure/azure-storage-ruby/issues/166#issuecomment-637696565
|
||||
# Remove this once the changes comes into rails version
|
||||
|
||||
# code from current master : https://github.com/rails/rails/blob/8520cc77133d9ff642e2c393b4ee5eae2a2a28b6/activestorage/lib/active_storage/service/azure_storage_service.rb
|
||||
|
||||
require 'azure/storage/blob'
|
||||
require 'active_storage/service/azure_storage_service'
|
||||
module ActiveStorage
|
||||
# Wraps the Microsoft Azure Storage Blob Service as an Active Storage service.
|
||||
# See ActiveStorage::Service for the generic API documentation that applies to all services.
|
||||
class Service::AzureStorageService < Service
|
||||
attr_reader :client, :container, :signer
|
||||
|
||||
def initialize(storage_account_name:, storage_access_key:, container:, public: false, **options)
|
||||
@client = Azure::Storage::Blob::BlobService.create(storage_account_name: storage_account_name, storage_access_key: storage_access_key, **options)
|
||||
@signer = Azure::Storage::Common::Core::Auth::SharedAccessSignature.new(storage_account_name, storage_access_key)
|
||||
@container = container
|
||||
@public = public
|
||||
end
|
||||
|
||||
def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
|
||||
instrument :upload, key: key, checksum: checksum do
|
||||
handle_errors do
|
||||
content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename
|
||||
|
||||
client.create_block_blob(container, key, IO.try_convert(io) || io, content_md5: checksum, content_type: content_type, content_disposition: content_disposition)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def download(key, &block)
|
||||
if block_given?
|
||||
instrument :streaming_download, key: key do
|
||||
stream(key, &block)
|
||||
end
|
||||
else
|
||||
instrument :download, key: key do
|
||||
handle_errors do
|
||||
_, io = client.get_blob(container, key)
|
||||
io.force_encoding(Encoding::BINARY)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def download_chunk(key, range)
|
||||
instrument :download_chunk, key: key, range: range do
|
||||
handle_errors do
|
||||
_, io = client.get_blob(container, key, start_range: range.begin, end_range: range.exclude_end? ? range.end - 1 : range.end)
|
||||
io.force_encoding(Encoding::BINARY)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def delete(key)
|
||||
instrument :delete, key: key do
|
||||
client.delete_blob(container, key)
|
||||
rescue Azure::Core::Http::HTTPError => e
|
||||
raise unless e.type == "BlobNotFound"
|
||||
# Ignore files already deleted
|
||||
end
|
||||
end
|
||||
|
||||
def delete_prefixed(prefix)
|
||||
instrument :delete_prefixed, prefix: prefix do
|
||||
marker = nil
|
||||
|
||||
loop do
|
||||
results = client.list_blobs(container, prefix: prefix, marker: marker)
|
||||
|
||||
results.each do |blob|
|
||||
client.delete_blob(container, blob.name)
|
||||
end
|
||||
|
||||
break unless marker = results.continuation_token.presence
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def exist?(key)
|
||||
instrument :exist, key: key do |payload|
|
||||
answer = blob_for(key).present?
|
||||
payload[:exist] = answer
|
||||
answer
|
||||
end
|
||||
end
|
||||
|
||||
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
|
||||
instrument :url, key: key do |payload|
|
||||
generated_url = signer.signed_uri(
|
||||
uri_for(key), false,
|
||||
service: "b",
|
||||
permissions: "rw",
|
||||
expiry: format_expiry(expires_in)
|
||||
).to_s
|
||||
|
||||
payload[:url] = generated_url
|
||||
|
||||
generated_url
|
||||
end
|
||||
end
|
||||
|
||||
def headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, **)
|
||||
content_disposition = content_disposition_with(type: disposition, filename: filename) if filename
|
||||
|
||||
{ "Content-Type" => content_type, "Content-MD5" => checksum, "x-ms-blob-content-disposition" => content_disposition, "x-ms-blob-type" => "BlockBlob" }
|
||||
end
|
||||
|
||||
private
|
||||
def private_url(key, expires_in:, filename:, disposition:, content_type:, **)
|
||||
signer.signed_uri(
|
||||
uri_for(key), false,
|
||||
service: "b",
|
||||
permissions: "r",
|
||||
expiry: format_expiry(expires_in),
|
||||
content_disposition: content_disposition_with(type: disposition, filename: filename),
|
||||
content_type: content_type
|
||||
).to_s
|
||||
end
|
||||
|
||||
def public_url(key, **)
|
||||
uri_for(key).to_s
|
||||
end
|
||||
|
||||
|
||||
def uri_for(key)
|
||||
client.generate_uri("#{container}/#{key}")
|
||||
end
|
||||
|
||||
def blob_for(key)
|
||||
client.get_blob_properties(container, key)
|
||||
rescue Azure::Core::Http::HTTPError
|
||||
false
|
||||
end
|
||||
|
||||
def format_expiry(expires_in)
|
||||
expires_in ? Time.now.utc.advance(seconds: expires_in).iso8601 : nil
|
||||
end
|
||||
|
||||
# Reads the object for the given key in chunks, yielding each to the block.
|
||||
def stream(key)
|
||||
blob = blob_for(key)
|
||||
|
||||
chunk_size = 5.megabytes
|
||||
offset = 0
|
||||
|
||||
raise ActiveStorage::FileNotFoundError unless blob.present?
|
||||
|
||||
while offset < blob.properties[:content_length]
|
||||
_, chunk = client.get_blob(container, key, start_range: offset, end_range: offset + chunk_size - 1)
|
||||
yield chunk.force_encoding(Encoding::BINARY)
|
||||
offset += chunk_size
|
||||
end
|
||||
end
|
||||
|
||||
def handle_errors
|
||||
yield
|
||||
rescue Azure::Core::Http::HTTPError => e
|
||||
case e.type
|
||||
when "BlobNotFound"
|
||||
raise ActiveStorage::FileNotFoundError
|
||||
when "Md5Mismatch"
|
||||
raise ActiveStorage::IntegrityError
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,41 +0,0 @@
|
||||
# Remember that Rails only eager loads everything in its production environment.
|
||||
# In the development and test environments, it only requires files as you reference constants.
|
||||
# You'll need to explicitly load app/bot
|
||||
|
||||
unless Rails.env.production?
|
||||
bot_files = Dir[Rails.root.join('app', 'bot', '**', '*.rb')]
|
||||
bot_reloader = ActiveSupport::FileUpdateChecker.new(bot_files) do
|
||||
bot_files.each { |file| require_dependency file }
|
||||
end
|
||||
|
||||
ActiveSupport::Reloader.to_prepare do
|
||||
bot_reloader.execute_if_updated
|
||||
end
|
||||
|
||||
bot_files.each { |file| require_dependency file }
|
||||
end
|
||||
|
||||
# ref: https://github.com/jgorset/facebook-messenger#make-a-configuration-provider
|
||||
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
||||
def valid_verify_token?(_verify_token)
|
||||
ENV['FB_VERIFY_TOKEN']
|
||||
end
|
||||
|
||||
def app_secret_for(_page_id)
|
||||
ENV['FB_APP_SECRET']
|
||||
end
|
||||
|
||||
def access_token_for(page_id)
|
||||
Channel::FacebookPage.where(page_id: page_id).last.page_access_token
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bot
|
||||
Chatwoot::Bot
|
||||
end
|
||||
end
|
||||
|
||||
Facebook::Messenger.configure do |config|
|
||||
config.provider = ChatwootFbProvider.new
|
||||
end
|
||||
49
config/initializers/facebook_messenger.rb
Normal file
49
config/initializers/facebook_messenger.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
# ref: https://github.com/jgorset/facebook-messenger#make-a-configuration-provider
|
||||
class ChatwootFbProvider < Facebook::Messenger::Configuration::Providers::Base
|
||||
def valid_verify_token?(_verify_token)
|
||||
ENV['FB_VERIFY_TOKEN']
|
||||
end
|
||||
|
||||
def app_secret_for(_page_id)
|
||||
ENV['FB_APP_SECRET']
|
||||
end
|
||||
|
||||
def access_token_for(page_id)
|
||||
Channel::FacebookPage.where(page_id: page_id).last.page_access_token
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bot
|
||||
Chatwoot::Bot
|
||||
end
|
||||
end
|
||||
|
||||
Rails.application.reloader.to_prepare do
|
||||
Facebook::Messenger.configure do |config|
|
||||
config.provider = ChatwootFbProvider.new
|
||||
end
|
||||
|
||||
Facebook::Messenger::Bot.on :message do |message|
|
||||
Rails.logger.info "MESSAGE_RECIEVED #{message}"
|
||||
response = ::Integrations::Facebook::MessageParser.new(message)
|
||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||
end
|
||||
|
||||
Facebook::Messenger::Bot.on :delivery do |delivery|
|
||||
# delivery.ids # => 'mid.1457764197618:41d102a3e1ae206a38'
|
||||
# delivery.sender # => { 'id' => '1008372609250235' }
|
||||
# delivery.recipient # => { 'id' => '2015573629214912' }
|
||||
# delivery.at # => 2016-04-22 21:30:36 +0200
|
||||
# delivery.seq # => 37
|
||||
updater = Integrations::Facebook::DeliveryStatus.new(delivery)
|
||||
updater.perform
|
||||
Rails.logger.info "Human was online at #{delivery.at}"
|
||||
end
|
||||
|
||||
Facebook::Messenger::Bot.on :message_echo do |message|
|
||||
Rails.logger.info "MESSAGE_ECHO #{message}"
|
||||
response = ::Integrations::Facebook::MessageParser.new(message)
|
||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||
end
|
||||
end
|
||||
@@ -1,28 +1,4 @@
|
||||
Raven.configure do |config|
|
||||
Sentry.init do |config|
|
||||
config.dsn = ENV['SENTRY_DSN']
|
||||
config.environments = %w[staging production]
|
||||
config.enabled_environments = %w[staging production]
|
||||
end
|
||||
|
||||
module QueryTrace
|
||||
def self.enable!
|
||||
::ActiveRecord::LogSubscriber.send(:include, self)
|
||||
end
|
||||
|
||||
def self.append_features(klass)
|
||||
super
|
||||
klass.class_eval do
|
||||
unless method_defined?(:log_info_without_trace)
|
||||
alias_method :log_info_without_trace, :sql
|
||||
alias_method :sql, :log_info_with_trace
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def log_info_with_trace(event)
|
||||
log_info_without_trace(event)
|
||||
trace_log = Rails.backtrace_cleaner.clean(caller).first
|
||||
logger.debug(" \\_ \e[33mCalled from:\e[0m #{trace_log}") if trace_log && event.payload[:name] != 'SCHEMA'
|
||||
end
|
||||
end
|
||||
|
||||
QueryTrace.enable! unless Rails.env.production?
|
||||
|
||||
Reference in New Issue
Block a user