2021-08-25 04:29:26 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-25 04:29:26 +09:00
|
|
|
namespace Plugin\ActivityPub;
|
|
|
|
|
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Plugin;
|
|
|
|
use App\Core\Router\RouteLoader;
|
2021-10-05 01:00:58 +09:00
|
|
|
use App\Core\Router\Router;
|
2021-10-18 21:22:02 +09:00
|
|
|
use App\Entity\Actor;
|
2021-10-27 12:14:01 +09:00
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Util\Exception\NoSuchActorException;
|
|
|
|
use App\Util\Nickname;
|
2021-08-25 04:29:26 +09:00
|
|
|
use Exception;
|
|
|
|
use Plugin\ActivityPub\Controller\Inbox;
|
2021-10-27 12:14:01 +09:00
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
2021-10-05 01:00:58 +09:00
|
|
|
use Plugin\ActivityPub\Util\Response\ActorResponse;
|
|
|
|
use Plugin\ActivityPub\Util\Response\NoteResponse;
|
|
|
|
use Plugin\ActivityPub\Util\Response\TypeResponse;
|
2021-10-18 21:22:02 +09:00
|
|
|
use XML_XRD;
|
|
|
|
use XML_XRD_Element_Link;
|
2021-08-25 04:29:26 +09:00
|
|
|
|
|
|
|
class ActivityPub extends Plugin
|
|
|
|
{
|
2021-10-27 12:14:01 +09:00
|
|
|
// ActivityStreams 2.0 Accept Headers
|
2021-10-18 21:22:02 +09:00
|
|
|
public static array $accept_headers = [
|
|
|
|
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
|
|
|
'application/activity+json',
|
|
|
|
'application/json',
|
|
|
|
'application/ld+json',
|
|
|
|
];
|
|
|
|
|
2021-10-27 12:14:01 +09:00
|
|
|
// So that this isn't hardcoded everywhere
|
|
|
|
public const PUBLIC_TO = ['https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
'Public',
|
|
|
|
'as:Public',
|
|
|
|
];
|
|
|
|
public const HTTP_CLIENT_HEADERS = [
|
|
|
|
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
|
|
|
'User-Agent' => 'GNUsocialBot ' . GNUSOCIAL_VERSION . ' - ' . GNUSOCIAL_PROJECT_URL,
|
|
|
|
];
|
|
|
|
|
2021-08-25 04:29:26 +09:00
|
|
|
public function version(): string
|
|
|
|
{
|
|
|
|
return '3.0.0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This code executes when GNU social creates the page routing, and we hook
|
2021-10-05 01:00:58 +09:00
|
|
|
* on this event to add our Inbox and Outbox handler for ActivityPub.
|
2021-08-25 04:29:26 +09:00
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @param RouteLoader $r the router that was initialized
|
2021-08-25 04:29:26 +09:00
|
|
|
*/
|
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
2021-09-17 01:04:05 +09:00
|
|
|
$r->connect(
|
2021-10-20 07:46:01 +09:00
|
|
|
'activitypub_inbox',
|
|
|
|
'/inbox.json',
|
2021-09-17 01:04:05 +09:00
|
|
|
[Inbox::class, 'handle'],
|
2021-10-27 12:14:01 +09:00
|
|
|
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
2021-09-17 01:04:05 +09:00
|
|
|
);
|
|
|
|
$r->connect(
|
2021-10-20 07:46:01 +09:00
|
|
|
'activitypub_actor_inbox',
|
|
|
|
'/actor/{gsactor_id<\d+>}/inbox.json',
|
2021-09-17 01:04:05 +09:00
|
|
|
[Inbox::class, 'handle'],
|
2021-10-27 12:14:01 +09:00
|
|
|
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
2021-09-17 01:04:05 +09:00
|
|
|
);
|
2021-08-25 04:29:26 +09:00
|
|
|
$r->connect(
|
2021-10-20 07:46:01 +09:00
|
|
|
'activitypub_actor_outbox',
|
|
|
|
'/actor/{gsactor_id<\d+>}/outbox.json',
|
2021-08-25 04:29:26 +09:00
|
|
|
[Inbox::class, 'handle'],
|
2021-10-27 12:14:01 +09:00
|
|
|
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
2021-08-25 04:29:26 +09:00
|
|
|
);
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:14:01 +09:00
|
|
|
public static function getActorByUri(string $resource, ?bool $attempt_fetch = true): Actor
|
|
|
|
{
|
|
|
|
// Try local
|
|
|
|
if (filter_var($resource, \FILTER_VALIDATE_URL) !== false) {
|
|
|
|
// This means $resource is a valid url
|
|
|
|
$resource_parts = parse_url($resource);
|
|
|
|
// TODO: Use URLMatcher
|
|
|
|
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
|
|
|
|
$str = $resource_parts['path'];
|
|
|
|
// actor_view_nickname
|
|
|
|
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
|
|
|
|
// actor_view_id
|
|
|
|
$reuri = '/\/actor\/(\d+)\/?/m';
|
|
|
|
if (preg_match_all($renick, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
|
|
|
|
return LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor();
|
|
|
|
} elseif (preg_match_all($reuri, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
|
|
|
|
return Actor::getById((int) $matches[0][1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Try remote
|
|
|
|
$aprofile = ActivitypubActor::getByAddr($resource);
|
|
|
|
if ($aprofile instanceof ActivitypubActor) {
|
|
|
|
return Actor::getById($aprofile->getActorId());
|
|
|
|
} else {
|
|
|
|
throw new NoSuchActorException("From URI: {$resource}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 01:00:58 +09:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool
|
|
|
|
{
|
2021-10-10 17:26:18 +09:00
|
|
|
if (\count(array_intersect(self::$accept_headers, $accept_header)) === 0) {
|
2021-10-05 01:00:58 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
switch ($route) {
|
|
|
|
case 'actor_view_id':
|
|
|
|
case 'actor_view_nickname':
|
|
|
|
$response = ActorResponse::handle($vars['actor']);
|
|
|
|
return Event::stop;
|
|
|
|
case 'note_view':
|
|
|
|
$response = NoteResponse::handle($vars['note']);
|
|
|
|
return Event::stop;
|
|
|
|
/*case 'actor_favourites_id':
|
|
|
|
case 'actor_favourites_nickname':
|
|
|
|
$response = LikeResponse::handle($vars['actor']);
|
|
|
|
return Event::stop;
|
|
|
|
case 'actor_subscriptions_id':
|
|
|
|
case 'actor_subscriptions_nickname':
|
|
|
|
$response = FollowingResponse::handle($vars['actor']);
|
|
|
|
return Event::stop;
|
|
|
|
case 'actor_subscribers_id':
|
|
|
|
case 'actor_subscribers_nickname':
|
|
|
|
$response = FollowersResponse::handle($vars['actor']);
|
|
|
|
return Event::stop;*/
|
|
|
|
default:
|
|
|
|
if (Event::handle('ActivityStreamsTwoResponse', [$route, &$response])) {
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:22:02 +09:00
|
|
|
/**
|
|
|
|
* Add activity+json mimetype on WebFinger
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-10-27 12:14:01 +09:00
|
|
|
public function onEndWebFingerProfileLinks(XML_XRD $xrd, Actor $object): bool
|
2021-10-05 01:00:58 +09:00
|
|
|
{
|
2021-10-18 21:22:02 +09:00
|
|
|
if ($object->isPerson()) {
|
|
|
|
$link = new XML_XRD_Element_Link(
|
2021-10-27 12:14:01 +09:00
|
|
|
rel: 'self',
|
|
|
|
href: $object->getUri(Router::ABSOLUTE_URL),//Router::url('actor_view_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
|
|
|
|
type: 'application/activity+json',
|
2021-10-18 21:22:02 +09:00
|
|
|
);
|
|
|
|
$xrd->links[] = clone $link;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
2021-10-27 12:14:01 +09:00
|
|
|
return Event::next;
|
2021-10-18 21:22:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onFreeNetworkGenerateLocalActorUri(int $actor_id, ?array &$actor_uri): bool
|
|
|
|
{
|
|
|
|
$actor_uri['ActivityPub'] = Router::url('actor_view_id', ['id' => $actor_id], Router::ABSOLUTE_URL);
|
|
|
|
return Event::next;
|
2021-10-05 01:00:58 +09:00
|
|
|
}
|
2021-09-07 07:47:28 +09:00
|
|
|
}
|