diff --git a/app/models/bot/alegre.rb b/app/models/bot/alegre.rb index e8d78182c..362fb444f 100644 --- a/app/models/bot/alegre.rb +++ b/app/models/bot/alegre.rb @@ -127,6 +127,11 @@ def get_language_from_transcription end end + def self.valid_request?(request) + request.query_parameters['token'] == CheckConfig.get('alegre_token') + end + + def self.default_model CheckConfig.get('alegre_default_model') || Bot::Alegre::ELASTICSEARCH_MODEL end @@ -474,7 +479,7 @@ def self.request_api(method, path, params = {}, query_or_body = 'body', retries ActiveRecord::Base.clear_active_connections! ActiveRecord::Base.connection.close end - uri = URI(CheckConfig.get('alegre_host') + path) + uri = URI(CheckConfig.get('alegre_host') + path+ '?token=' + CheckConfig.get('alegre_token')) klass = 'Net::HTTP::' + method.capitalize request = klass.constantize.new(uri.path, 'Content-Type' => 'application/json') if query_or_body == 'query' @@ -491,7 +496,12 @@ def self.request_api(method, path, params = {}, query_or_body = 'body', retries response_body = response.body Rails.logger.info("[Alegre Bot] Alegre response: #{response_body.inspect}") ActiveRecord::Base.connection.reconnect! if RequestStore.store[:pause_database_connection] - JSON.parse(response_body) + parsed_response = JSON.parse(response_body) + if parsed_response.dig("queue") == 'audio__Model' && parsed_response.dig("body", "callback_url") != nil + redis = Redis.new(REDIS_CONFIG) + redis_response = redis.blpop(parsed_response.dig("body", "id"), timeout=120) + return JSON.parse(redis_response[1]) + end rescue StandardError => e if retries > 0 sleep 1 diff --git a/app/models/concerns/alegre_similarity.rb b/app/models/concerns/alegre_similarity.rb index d33140547..bc5d6faf6 100644 --- a/app/models/concerns/alegre_similarity.rb +++ b/app/models/concerns/alegre_similarity.rb @@ -19,7 +19,7 @@ def get_similar_items(pm) type = Bot::Alegre.get_pm_type(pm) Rails.logger.info "[Alegre Bot] [ProjectMedia ##{pm.id}] [Similarity 2/5] Type is #{type.blank? ? "blank" : type}" unless type.blank? - if !self.should_get_similar_items_of_type?('master', pm.team_id) || !self.should_get_similar_items_of_type?(type, pm.team_id) + if !Bot::Alegre.should_get_similar_items_of_type?('master', pm.team_id) || !Bot::Alegre.should_get_similar_items_of_type?(type, pm.team_id) Rails.logger.info "[Alegre Bot] [ProjectMedia ##{pm.id}] [Similarity 3/5] ProjectMedia cannot be checked for similar items" return {} else diff --git a/app/models/concerns/alegre_webhooks.rb b/app/models/concerns/alegre_webhooks.rb index 1f6f8d65a..a0e20156f 100644 --- a/app/models/concerns/alegre_webhooks.rb +++ b/app/models/concerns/alegre_webhooks.rb @@ -1,6 +1,7 @@ +class AlegreCallbackError < StandardError +end module AlegreWebhooks extend ActiveSupport::Concern - module ClassMethods def webhook(request) #at this point, we know that the data is stored and ready for querying, so we can safely query based on package of: @@ -9,8 +10,14 @@ def webhook(request) # "model_type": model_type, # "data": data, # }) - pm = ProjectMedia.find(request.params["data"]["requested"]["body"]["context"]["project_media_id"]) - relate_project_media_to_similar_items(pm) + begin + doc_id = request.params["data"]["requested"]["body"]["id"] + redis = Redis.new(REDIS_CONFIG) + redis.lpush(doc_id, request.params.to_json) + redis.expire(doc_id, 1.day.to_i) + rescue StandardError => e + CheckSentry.notify(AlegreCallbackError.new(e.message), { alegre_response: body }) + end end end end