5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-20 00:36:23 +00:00

feat: crear webhooks en la social inbox #15109

This commit is contained in:
f 2024-02-16 16:01:17 -03:00
parent b792cb2d43
commit b24f49fe26
No known key found for this signature in database
5 changed files with 76 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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|

View file

@ -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'

View file

@ -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