2022-03-24 07:57:44 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Plugin\WebHooks;
|
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2022-03-24 07:57:44 +09:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\HTTPClient;
|
|
|
|
use App\Core\Log;
|
|
|
|
use App\Core\Modules\Plugin;
|
2022-03-28 00:21:19 +09:00
|
|
|
use App\Core\Queue;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2022-03-24 07:57:44 +09:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
2022-03-25 06:59:24 +09:00
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Util\Common;
|
2022-03-24 07:57:44 +09:00
|
|
|
use App\Util\Exception\ServerException;
|
2022-03-24 09:47:34 +09:00
|
|
|
use Exception;
|
2022-03-24 07:57:44 +09:00
|
|
|
use Functional as F;
|
|
|
|
use Plugin\WebHooks\Controller as C;
|
|
|
|
use Plugin\WebHooks\Entity as E;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class WebHooks extends Plugin
|
|
|
|
{
|
|
|
|
public const controller_route = 'webhook';
|
|
|
|
|
2022-03-28 00:43:59 +09:00
|
|
|
public function onAddRoute(Router $r)
|
2022-03-24 07:57:44 +09:00
|
|
|
{
|
|
|
|
$r->connect(self::controller_route, '/webhook-settings', C\WebHooks::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
|
|
|
|
{
|
|
|
|
if ($section === 'others') {
|
|
|
|
$tabs[] = [
|
|
|
|
'title' => 'Web Hooks',
|
|
|
|
'desc' => 'Add hooks that run when an internal event occurs, allowing your third party resource to react',
|
|
|
|
'id' => 'settings-webhooks',
|
|
|
|
'controller' => C\WebHooks::setup(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-03-25 06:59:24 +09:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:57:44 +09:00
|
|
|
public function onNewNotificationEnd(Actor $sender, Activity $activity, array $effective_targets, ?string $reason)
|
|
|
|
{
|
|
|
|
foreach ($effective_targets as $actor) {
|
2022-03-25 06:59:24 +09:00
|
|
|
$this->maybeEnqueue($actor, 'notifications', [$sender, $activity, $effective_targets, $reason]);
|
2022-03-24 07:57:44 +09:00
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-03-25 06:59:24 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:57:44 +09:00
|
|
|
/**
|
|
|
|
* @param array<Actor $sender, Activity $activity, array $effective_targets, ?string $reason> $args
|
|
|
|
*/
|
2022-03-25 06:59:24 +09:00
|
|
|
public function onQueueWebhook(string $type, string $hook_target, Actor $actor, array $args)
|
2022-03-24 07:57:44 +09:00
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'notifications':
|
|
|
|
[$sender, $activity, $targets, $reason] = $args;
|
|
|
|
$data = [
|
|
|
|
'type' => 'notification',
|
2022-03-24 09:47:34 +09:00
|
|
|
'activity' => '%activity%',
|
2022-03-24 07:57:44 +09:00
|
|
|
'actor' => ['id' => $sender->getId(), 'nickname' => $sender->getNickname()],
|
|
|
|
'targets' => F\map(array_values($targets), fn (Actor $actor) => ['id' => $actor->getId(), 'nickname' => $actor->getNickname()]),
|
|
|
|
'reason' => $reason,
|
|
|
|
];
|
2022-03-25 06:59:24 +09:00
|
|
|
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;
|
2022-03-24 07:57:44 +09:00
|
|
|
default:
|
|
|
|
throw new ServerException("Webhook notification handler for event {$type} not implemented");
|
|
|
|
}
|
2022-03-25 06:59:24 +09:00
|
|
|
|
|
|
|
// 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;
|
2022-03-24 07:57:44 +09:00
|
|
|
}
|
|
|
|
}
|