From 48b42c539c6f6ace1e4fa6958c9a6ad79be5725a Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 24 Mar 2022 00:47:34 +0000 Subject: [PATCH] [PLUGINS][WebHooks] Use ActivityPub to serialize the activity, so the object is included --- plugins/WebHooks/WebHooks.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/WebHooks/WebHooks.php b/plugins/WebHooks/WebHooks.php index 36c62d1ae8..ca13aa061d 100644 --- a/plugins/WebHooks/WebHooks.php +++ b/plugins/WebHooks/WebHooks.php @@ -31,6 +31,7 @@ use App\Core\Router\RouteLoader; use App\Entity\Activity; use App\Entity\Actor; use App\Util\Exception\ServerException; +use Exception; use Functional as F; use Plugin\WebHooks\Controller as C; use Plugin\WebHooks\Entity as E; @@ -79,13 +80,19 @@ class WebHooks extends Plugin [$sender, $activity, $targets, $reason] = $args; $data = [ 'type' => 'notification', + 'activity' => '%activity%', 'actor' => ['id' => $sender->getId(), 'nickname' => $sender->getNickname()], - 'activity' => ['id' => $activity->getId(), 'object_type' => $activity->getObjectType(), 'object_id' => $activity->getObjectId(), 'verb' => $activity->getVerb()], 'targets' => F\map(array_values($targets), fn (Actor $actor) => ['id' => $actor->getId(), 'nickname' => $actor->getNickname()]), 'reason' => $reason, ]; - Log::debug("WebHook: POST {$target} on behalf of actor {$actor->getId()} ({$actor->getNickname()})", [$data, ['json' => json_encode($data)]]); - HTTPClient::post($target, ['json' => json_encode($data)]); + // 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 { + HTTPClient::post($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; default: throw new ServerException("Webhook notification handler for event {$type} not implemented");