[PLUGINS][WebHooks] Use ActivityPub to serialize the activity, so the object is included

This commit is contained in:
Hugo Sales 2022-03-24 00:47:34 +00:00
parent d41a67a9f9
commit 48b42c539c
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -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");