From ca5520edbf7b4a394845fd8c6d206682fe24db47 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 24 Mar 2022 21:59:24 +0000 Subject: [PATCH] [PLUGIN][WebHooks] Add hook for subscriptions --- components/Subscription/Subscription.php | 3 +- plugins/WebHooks/Controller/WebHooks.php | 2 +- plugins/WebHooks/WebHooks.php | 55 +++++++++++++++++------- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/components/Subscription/Subscription.php b/components/Subscription/Subscription.php index 50314ea0d7..2b9b594d0d 100644 --- a/components/Subscription/Subscription.php +++ b/components/Subscription/Subscription.php @@ -112,8 +112,9 @@ class Subscription extends Component \is_int($subject) ? $subject : Actor::getById($subscriber_id), $activity, [$subscribed_id], - _m('{subject} subscribed to {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $activity->getObjectId()]), + $reason = _m('{subject} subscribed to {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $activity->getObjectId()]), ]); + Event::handle('NewSubscriptionEnd', [$subject, $activity, $object, $reason]); } return $activity; } diff --git a/plugins/WebHooks/Controller/WebHooks.php b/plugins/WebHooks/Controller/WebHooks.php index abcda1dad4..2a64697342 100644 --- a/plugins/WebHooks/Controller/WebHooks.php +++ b/plugins/WebHooks/Controller/WebHooks.php @@ -45,7 +45,7 @@ class WebHooks extends Controller $hooks = F\reindex(DB::findBy(E\WebHook::class, ['actor_id' => $user->getId()]), fn (E\WebHook $wh) => $wh->getEvent()); $form = Form::create([ ['notifications', TextType::class, ['label' => _m('Trigger this hook when I recieve a notification'), 'data' => ($hooks['notifications'] ?? null)?->getTarget()]], - ['follow', TextType::class, ['label' => _m('Trigger this hook when someone starts following me'), 'data' => ($hooks['follow'] ?? null)?->getTarget()]], + ['subscriptions', TextType::class, ['label' => _m('Trigger this hook when someone subscribes to me'), 'data' => ($hooks['subscriptions'] ?? null)?->getTarget()]], ['save_webhooks', SubmitType::class, ['label' => _m('Submit')]], ], form_options: ['action' => Router::url(P\WebHooks::controller_route)]); diff --git a/plugins/WebHooks/WebHooks.php b/plugins/WebHooks/WebHooks.php index e5bd175ef3..b22ae6d47a 100644 --- a/plugins/WebHooks/WebHooks.php +++ b/plugins/WebHooks/WebHooks.php @@ -30,6 +30,8 @@ use App\Core\Queue\Queue; use App\Core\Router\RouteLoader; use App\Entity\Activity; use App\Entity\Actor; +use App\Entity\LocalUser; +use App\Util\Common; use App\Util\Exception\ServerException; use Exception; use Functional as F; @@ -59,21 +61,32 @@ class WebHooks extends Plugin return Event::next; } + private function maybeEnqueue(Actor $actor, string $event, array $args): void + { + $hook_target = DB::findOneBy(E\WebHook::class, ['actor_id' => $actor->getId(), 'event' => $event], return_null: true)?->getTarget(); + if (!\is_null($hook_target)) { + Queue::enqueue([$event, $hook_target, $actor, $args], queue: 'webhook'); + } + } + public function onNewNotificationEnd(Actor $sender, Activity $activity, array $effective_targets, ?string $reason) { foreach ($effective_targets as $actor) { - $target = DB::findOneBy(E\WebHook::class, ['actor_id' => $actor->getId(), 'event' => 'notifications'], return_null: true)?->getTarget(); - if (!\is_null($target)) { - Queue::enqueue(['notifications', $target, $actor, [$sender, $activity, $effective_targets, $reason]], queue: 'webhook'); - } + $this->maybeEnqueue($actor, 'notifications', [$sender, $activity, $effective_targets, $reason]); } return Event::next; } + public function onNewSubscriptionEnd(LocalUser|Actor $subscriber, Activity $activity, Actor $hook_target, ?string $reason) + { + $this->maybeEnqueue($hook_target, 'subscriptions', [$subscriber, $activity, $hook_target, $reason]); + return Event::next; + } + /** * @param array $args */ - public function onQueueWebhook(string $type, string $target, Actor $actor, array $args) + public function onQueueWebhook(string $type, string $hook_target, Actor $actor, array $args) { switch ($type) { case 'notifications': @@ -85,18 +98,30 @@ class WebHooks extends Plugin 'targets' => F\map(array_values($targets), fn (Actor $actor) => ['id' => $actor->getId(), 'nickname' => $actor->getNickname()]), 'reason' => $reason, ]; - // toJson(Activity) is already JSON (hopefully that's obvious :') ), so replace it after converting the rest to JSON - $json = str_replace('"activity":"%activity%"', '"activity":' . \Plugin\ActivityPub\Util\Model\Activity::toJson($activity), json_encode($data)); - Log::debug("WebHooks: POST {$target} on behalf of actor {$actor->getId()} ({$actor->getNickname()})", [$data, ['json' => $json]]); - try { - $method = Common::config('plugin_webhooks', 'method'); - HTTPClient::{$method}($target, ['body' => $json, 'headers' => ['content-type' => 'application/json', 'user-agent' => 'GNU social']]); - } catch (Exception $e) { - Log::debug("WebHooks: Failed POST {$target} on behalf of actor {$actor->getId()} ({$actor->getNickname()})", [$e]); - } - return Event::stop; + break; + case 'subscriptions': + [$subscriber, $activity, $target, $reason] = $args; + $data = [ + 'type' => 'subscription', + 'activity' => '%activity%', + 'actor' => ['id' => $subscriber->getId(), 'nickname' => $subscriber->getNickname()], + 'targets' => [['id' => $target->getId(), 'nickname' => $target->getNickname()]], + 'reason' => $reason, + ]; + break; default: throw new ServerException("Webhook notification handler for event {$type} not implemented"); } + + // toJson(Activity) is already JSON (hopefully that's obvious :') ), so replace it after converting the rest to JSON + $json = str_replace('"activity":"%activity%"', '"activity":' . \Plugin\ActivityPub\Util\Model\Activity::toJson($activity), json_encode($data)); + Log::debug("WebHooks: POST {$hook_target} on behalf of actor {$actor->getId()} ({$actor->getNickname()})", [$data, ['json' => $json]]); + try { + $method = Common::config('plugin_webhooks', 'method'); + HTTPClient::{$method}($hook_target, ['body' => $json, 'headers' => ['content-type' => 'application/json', 'user-agent' => 'GNU social']]); + } catch (Exception $e) { + Log::debug("WebHooks: Failed POST {$hook_target} on behalf of actor {$actor->getId()} ({$actor->getNickname()})", [$e]); + } + return Event::stop; } }