From b24f49fe2611791eb377a56d4f7e9cdbeafafbd4 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 16 Feb 2024 16:01:17 -0300 Subject: [PATCH] feat: crear webhooks en la social inbox #15109 --- .../api/v1/social_inbox_controller.rb | 22 +++++++++++ app/models/deploy_social_distributed_press.rb | 39 +++++++++++++++++++ app/models/social_inbox.rb | 6 +++ config/application.rb | 3 ++ config/routes.rb | 6 +++ 5 files changed, 76 insertions(+) create mode 100644 app/controllers/api/v1/social_inbox_controller.rb diff --git a/app/controllers/api/v1/social_inbox_controller.rb b/app/controllers/api/v1/social_inbox_controller.rb new file mode 100644 index 00000000..3881b6bc --- /dev/null +++ b/app/controllers/api/v1/social_inbox_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Api + module V1 + # Recibe webhooks de la Social Inbox + class SocialInboxController < BaseController + include WebhookConcern + + def moderationqueued + head :accepted + end + + def onapproved + head :accepted + end + + def onrejected + head :accepted + end + end + end +end diff --git a/app/models/deploy_social_distributed_press.rb b/app/models/deploy_social_distributed_press.rb index db555ab7..f5c38a22 100644 --- a/app/models/deploy_social_distributed_press.rb +++ b/app/models/deploy_social_distributed_press.rb @@ -7,6 +7,8 @@ class DeploySocialDistributedPress < Deploy # Solo luego de publicar remotamente DEPENDENCIES = %i[deploy_distributed_press deploy_rsync deploy_full_rsync] + after_save :create_hooks! + # EnvĂ­a las notificaciones def deploy(output: false) with_tempfile(site.private_key_pem) do |file| @@ -52,4 +54,41 @@ class DeploySocialDistributedPress < Deploy def flags_for_build(**args) "--key #{Shellwords.escape args[:private_key].path}" end + + private + + # Obtiene el hostname de la API de Sutty + # + # @return [String] + def api_hostname + Rails.application.routes.default_url_options[:host].sub('panel', 'api') + end + + # Crea los hooks en la Social Inbox para que nos avise de actividades + # nuevas + # + # @return [nil] + def create_hooks! + hook_client = site.social_inbox.hook + + hook_client.class::EVENTS.each do |event| + event_url = :"v1_site_webhooks_social_inbox_#{event}_url" + + webhook = DistributedPress::V1::Social::Schemas::Webhook.new.call({ + method: 'POST', + url: Rails.application.routes.url_helpers.public_send(event_url, site_id: site.name, host: api_hostname), + headers: { + 'X-Social-Inbox': rol.token + } + }) + + raise ArgumentError, webhook.errors.messages if webhook.failure? + + response = hook_client.put(event: event, hook: webhook) + + raise ArgumentError, response.parsed_body unless response.ok? + rescue ArgumentError => e + ExceptionNotifier.notify_exception(e, data: { site_id: site.name, usuarie_id: rol.usuarie_id }) + end + end end diff --git a/app/models/social_inbox.rb b/app/models/social_inbox.rb index 47c3457c..8aa5b504 100644 --- a/app/models/social_inbox.rb +++ b/app/models/social_inbox.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'distributed_press/v1/social/client' +require 'distributed_press/v1/social/hook' # Gestiona la Social Inbox de un sitio class SocialInbox @@ -34,6 +35,11 @@ class SocialInbox ) end + # @return [DistributedPress::V1::Social::Hook] + def hook + @hook ||= DistributedPress::V1::Social::Hook.new(client: client, actor: actor) + end + # @return [String] def public_key_url @public_key_url ||= URI("https://#{hostname}").tap do |uri| diff --git a/config/application.rb b/config/application.rb index 529e341a..73a7a884 100644 --- a/config/application.rb +++ b/config/application.rb @@ -2,6 +2,9 @@ require_relative 'boot' +require 'redis-client' +require 'hiredis-client' +require 'brs' require 'rails' # Pick the frameworks you want: require 'active_model/railtie' diff --git a/config/routes.rb b/config/routes.rb index 8186b64e..f6f081f7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,12 @@ Rails.application.routes.draw do namespace :webhooks do post :pull, to: 'webhooks#pull' + + namespace :social_inbox do + post :moderationqueued, to: 'social_inbox#moderationqueued' + post :onapproved, to: 'social_inbox#onapproved' + post :onrejected, to: 'social_inbox#onrejected' + end end end end