forked from GNUsocial/gnu-social
[ActivityPub] Port Explorer
This commit is contained in:
parent
5189269e5b
commit
51c984849f
@ -8,7 +8,7 @@
|
||||
"ext-ctype": "*",
|
||||
"ext-curl": "*",
|
||||
"ext-iconv": "*",
|
||||
"ext-vips": "*",
|
||||
"ext-openssl": "*",
|
||||
"alchemy/zippy": "v0.5.x-dev",
|
||||
"composer/package-versions-deprecated": "1.11.99.3",
|
||||
"doctrine/annotations": "^1.0",
|
||||
@ -62,8 +62,7 @@
|
||||
"twig/extra-bundle": "^2.12|^3.0",
|
||||
"twig/markdown-extra": "^3.0",
|
||||
"twig/twig": "^2.12|^3.0",
|
||||
"wikimedia/composer-merge-plugin": "^2.0",
|
||||
"ext-openssl": "*"
|
||||
"wikimedia/composer-merge-plugin": "^2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"doctrine/doctrine-fixtures-bundle": "^3.4",
|
||||
|
530
composer.lock
generated
530
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -9,8 +9,12 @@ use App\Core\Modules\Plugin;
|
||||
use App\Core\Router\RouteLoader;
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\LocalUser;
|
||||
use App\Util\Exception\NoSuchActorException;
|
||||
use App\Util\Nickname;
|
||||
use Exception;
|
||||
use Plugin\ActivityPub\Controller\Inbox;
|
||||
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
||||
use Plugin\ActivityPub\Util\Response\ActorResponse;
|
||||
use Plugin\ActivityPub\Util\Response\NoteResponse;
|
||||
use Plugin\ActivityPub\Util\Response\TypeResponse;
|
||||
@ -19,6 +23,7 @@ use XML_XRD_Element_Link;
|
||||
|
||||
class ActivityPub extends Plugin
|
||||
{
|
||||
// ActivityStreams 2.0 Accept Headers
|
||||
public static array $accept_headers = [
|
||||
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
||||
'application/activity+json',
|
||||
@ -26,6 +31,16 @@ class ActivityPub extends Plugin
|
||||
'application/ld+json',
|
||||
];
|
||||
|
||||
// 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,
|
||||
];
|
||||
|
||||
public function version(): string
|
||||
{
|
||||
return '3.0.0';
|
||||
@ -43,23 +58,52 @@ class ActivityPub extends Plugin
|
||||
'activitypub_inbox',
|
||||
'/inbox.json',
|
||||
[Inbox::class, 'handle'],
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]]
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
||||
);
|
||||
$r->connect(
|
||||
'activitypub_actor_inbox',
|
||||
'/actor/{gsactor_id<\d+>}/inbox.json',
|
||||
[Inbox::class, 'handle'],
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]]
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
||||
);
|
||||
$r->connect(
|
||||
'activitypub_actor_outbox',
|
||||
'/actor/{gsactor_id<\d+>}/outbox.json',
|
||||
[Inbox::class, 'handle'],
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]]
|
||||
options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]],
|
||||
);
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@ -99,21 +143,19 @@ class ActivityPub extends Plugin
|
||||
/**
|
||||
* Add activity+json mimetype on WebFinger
|
||||
*
|
||||
* @param XML_XRD $xrd
|
||||
* @param Managed_DataObject $object
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onEndWebFingerProfileLinks(XML_XRD $xrd, Actor $object)
|
||||
public function onEndWebFingerProfileLinks(XML_XRD $xrd, Actor $object): bool
|
||||
{
|
||||
if ($object->isPerson()) {
|
||||
$link = new XML_XRD_Element_Link(
|
||||
'self',
|
||||
$object->getUri(Router::ABSOLUTE_URL),//Router::url('actor_view_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
|
||||
'application/activity+json'
|
||||
rel: 'self',
|
||||
href: $object->getUri(Router::ABSOLUTE_URL),//Router::url('actor_view_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
|
||||
type: 'application/activity+json',
|
||||
);
|
||||
$xrd->links[] = clone $link;
|
||||
}
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onFreeNetworkGenerateLocalActorUri(int $actor_id, ?array &$actor_uri): bool
|
||||
|
@ -42,7 +42,6 @@ class ActivitypubActivity extends Entity
|
||||
{
|
||||
// {{{ Autocode
|
||||
// @codeCoverageIgnoreStart
|
||||
private int $id;
|
||||
private string $activity_uri;
|
||||
private int $actor_id;
|
||||
private string $verb;
|
||||
@ -54,17 +53,6 @@ class ActivitypubActivity extends Entity
|
||||
private DateTimeInterface $created;
|
||||
private DateTimeInterface $modified;
|
||||
|
||||
public function setId(int $id): self
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getActivityUri(): string
|
||||
{
|
||||
return $this->activity_uri;
|
||||
@ -183,7 +171,6 @@ class ActivitypubActivity extends Entity
|
||||
return [
|
||||
'name' => 'activitypub_activity',
|
||||
'fields' => [
|
||||
'id' => ['type' => 'serial', 'not null' => true],
|
||||
'activity_uri' => ['type' => 'text', 'not null' => true, 'description' => 'Activity\'s URI'],
|
||||
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'],
|
||||
'verb' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'internal activity verb, influenced by activity pub verbs'],
|
||||
@ -195,7 +182,7 @@ class ActivitypubActivity extends Entity
|
||||
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
||||
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
|
||||
],
|
||||
'primary key' => ['id'],
|
||||
'primary key' => ['activity_uri'],
|
||||
'indexes' => [
|
||||
'activity_activity_uri_idx' => ['activity_uri'],
|
||||
'activity_object_uri_idx' => ['object_uri'],
|
||||
|
@ -32,8 +32,16 @@ declare(strict_types = 1);
|
||||
|
||||
namespace Plugin\ActivityPub\Entity;
|
||||
|
||||
use App\Core\Cache;
|
||||
use App\Core\Entity;
|
||||
use function App\Core\I18n\_m;
|
||||
use App\Core\Log;
|
||||
use App\Entity\Actor;
|
||||
use Component\FreeNetwork\Util\Discovery;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use Plugin\ActivityPub\Util\DiscoveryHints;
|
||||
use Plugin\ActivityPub\Util\Explorer;
|
||||
|
||||
/**
|
||||
* Table Definition for activitypub_actor
|
||||
@ -121,6 +129,97 @@ class ActivitypubActor extends Entity
|
||||
// @codeCoverageIgnoreEnd
|
||||
// }}} Autocode
|
||||
|
||||
/**
|
||||
* Look up, and if necessary create, an Activitypub_profile for the remote
|
||||
* entity with the given WebFinger address.
|
||||
* This should never return null -- you will either get an object or
|
||||
* an exception will be thrown.
|
||||
*
|
||||
* @param string $addr WebFinger address
|
||||
*
|
||||
* @throws Exception on error conditions
|
||||
*/
|
||||
public static function getByAddr(string $addr): self
|
||||
{
|
||||
// Normalize $addr, i.e. add 'acct:' if missing
|
||||
$addr = Discovery::normalize($addr);
|
||||
|
||||
// Try the cache
|
||||
$uri = Cache::get(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), fn () => false);
|
||||
|
||||
if ($uri !== false) {
|
||||
if (\is_null($uri)) {
|
||||
// TRANS: Exception.
|
||||
throw new Exception(_m('Not a valid WebFinger address (via cache).'));
|
||||
}
|
||||
try {
|
||||
return self::fromUri($uri);
|
||||
} catch (Exception $e) {
|
||||
Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
|
||||
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
|
||||
}
|
||||
}
|
||||
|
||||
// Now, try some discovery
|
||||
|
||||
$disco = new Discovery();
|
||||
|
||||
try {
|
||||
$xrd = $disco->lookup($addr);
|
||||
} catch (Exception $e) {
|
||||
// Save negative cache entry so we don't waste time looking it up again.
|
||||
// @todo FIXME: Distinguish temporary failures?
|
||||
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), null);
|
||||
// TRANS: Exception.
|
||||
throw new Exception(_m('Not a valid WebFinger address: ' . $e->getMessage()));
|
||||
}
|
||||
|
||||
$hints = array_merge(
|
||||
['webfinger' => $addr],
|
||||
DiscoveryHints::fromXRD($xrd),
|
||||
);
|
||||
|
||||
if (\array_key_exists('activitypub', $hints)) {
|
||||
$uri = $hints['activitypub'];
|
||||
try {
|
||||
LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
|
||||
$aprofile = self::fromUri($hints['activitypub']);
|
||||
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $aprofile->getUri());
|
||||
return $aprofile;
|
||||
} catch (Exception $e) {
|
||||
Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
|
||||
throw $e;
|
||||
// keep looking
|
||||
//
|
||||
// @todo FIXME: This means an error discovering from profile page
|
||||
// may give us a corrupt entry using the webfinger URI, which
|
||||
// will obscure the correct page-keyed profile later on.
|
||||
}
|
||||
}
|
||||
|
||||
// XXX: try hcard
|
||||
// XXX: try FOAF
|
||||
|
||||
// TRANS: Exception. %s is a WebFinger address.
|
||||
throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures a valid Activitypub_profile when provided with a valid URI.
|
||||
*
|
||||
* @param bool $grab_online whether to try online grabbing, defaults to true
|
||||
*
|
||||
* @throws Exception if it isn't possible to return an Activitypub_profile
|
||||
*/
|
||||
public static function fromUri(string $url, bool $grab_online = true): self
|
||||
{
|
||||
try {
|
||||
return Explorer::get_profile_from_url($url, $grab_online);
|
||||
} catch (Exception $e) {
|
||||
throw new Exception('No valid ActivityPub profile found for given URI.', previous: $e);
|
||||
}
|
||||
}
|
||||
|
||||
public static function schemaDef(): array
|
||||
{
|
||||
return [
|
||||
|
179
plugins/ActivityPub/Util/DiscoveryHints.php
Normal file
179
plugins/ActivityPub/Util/DiscoveryHints.php
Normal file
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Plugin\ActivityPub\Util;
|
||||
|
||||
// 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/>.
|
||||
use App\Core\Event;
|
||||
use App\Core\HTTPClient;
|
||||
use Component\FreeNetwork\Util\Discovery;
|
||||
use Component\FreeNetwork\Util\WebfingerResource\WebfingerResourceActor;
|
||||
use Mf2 as Mf2;
|
||||
use XML_XRD;
|
||||
|
||||
/**
|
||||
* ActivityPub implementation for GNU social
|
||||
*
|
||||
* @package GNUsocial
|
||||
*
|
||||
* @author Evan Prodromou
|
||||
* @author Brion Vibber
|
||||
* @author James Walker
|
||||
* @author Siebrand Mazeland
|
||||
* @author Mikael Nordfeldth
|
||||
* @author Diogo Cordeiro
|
||||
* @copyright 2010, 2019, 2021 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*
|
||||
* @see http://www.gnu.org/software/social/
|
||||
*/
|
||||
class DiscoveryHints
|
||||
{
|
||||
public static function fromXRD(XML_XRD $xrd)
|
||||
{
|
||||
$hints = [];
|
||||
|
||||
if (Event::handle('StartDiscoveryHintsFromXRD', [$xrd, &$hints])) {
|
||||
foreach ($xrd->links as $link) {
|
||||
if ($link->rel === 'self' && $link->type === 'application/activity+json') {
|
||||
$hints['activitypub'] = $link->href;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Event::handle('EndDiscoveryHintsFromXRD', [$xrd, &$hints]);
|
||||
}
|
||||
|
||||
return $hints;
|
||||
}
|
||||
}
|
||||
//class DiscoveryHints
|
||||
//{
|
||||
// public static function fromXRD(XML_XRD $xrd)
|
||||
// {
|
||||
// $hints = [];
|
||||
//
|
||||
// if (Event::handle('StartDiscoveryHintsFromXRD', [$xrd, &$hints])) {
|
||||
// foreach ($xrd->links as $link) {
|
||||
// switch ($link->rel) {
|
||||
// case WebfingerResourceActor::PROFILEPAGE:
|
||||
// $hints['profileurl'] = $link->href;
|
||||
// break;
|
||||
// case Discovery::UPDATESFROM:
|
||||
// if (empty($link->type) || $link->type == 'application/atom+xml') {
|
||||
// $hints['feedurl'] = $link->href;
|
||||
// }
|
||||
// break;
|
||||
// case Discovery::HCARD:
|
||||
// case Discovery::MF2_HCARD:
|
||||
// $hints['hcard'] = $link->href;
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// Event::handle('EndDiscoveryHintsFromXRD', [$xrd, &$hints]);
|
||||
// }
|
||||
//
|
||||
// return $hints;
|
||||
// }
|
||||
//
|
||||
// public static function fromHcardUrl($url)
|
||||
// {
|
||||
// $response = HTTPClient::get($url, ['headers' => ['Accept' => 'text/html,application/xhtml+xml']]);
|
||||
//
|
||||
// if (!HTTPClient::statusCodeIsOkay($response)) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// return self::hcardHints(
|
||||
// $response->getContent(),
|
||||
// HTTPClient::getEffectiveUrl($response)
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// public static function hcardHints($body, $url)
|
||||
// {
|
||||
// $hcard = self::hcard($body, $url);
|
||||
//
|
||||
// if (empty($hcard)) {
|
||||
// return [];
|
||||
// }
|
||||
//
|
||||
// $hints = [];
|
||||
//
|
||||
// // XXX: don't copy stuff into an array and then copy it again
|
||||
//
|
||||
// if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
|
||||
// $hints['nickname'] = $hcard['nickname'][0];
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
|
||||
// $hints['fullname'] = $hcard['name'][0];
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
|
||||
// $hints['avatar'] = $hcard['photo'][0];
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
|
||||
// $hints['bio'] = $hcard['note'][0];
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
|
||||
// $hints['location'] = $hcard['adr'][0]['value'];
|
||||
// }
|
||||
//
|
||||
// if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
|
||||
// $hints['homepage'] = $hcard['url'][0];
|
||||
// }
|
||||
//
|
||||
// return $hints;
|
||||
// }
|
||||
//
|
||||
// private static function hcard($body, $url)
|
||||
// {
|
||||
// $mf2 = new Mf2\Parser($body, $url);
|
||||
// $mf2 = $mf2->parse();
|
||||
//
|
||||
// if (empty($mf2['items'])) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// $hcards = [];
|
||||
//
|
||||
// foreach ($mf2['items'] as $item) {
|
||||
// if (!in_array('h-card', $item['type'])) {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// // We found a match, return it immediately
|
||||
// if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
|
||||
// return $item['properties'];
|
||||
// }
|
||||
//
|
||||
// // Let's keep all the hcards for later, to return one of them at least
|
||||
// $hcards[] = $item['properties'];
|
||||
// }
|
||||
//
|
||||
// // No match immediately for the url we expected, but there were h-cards found
|
||||
// if (count($hcards) > 0) {
|
||||
// return $hcards[0];
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//}
|
373
plugins/ActivityPub/Util/Explorer.php
Normal file
373
plugins/ActivityPub/Util/Explorer.php
Normal file
@ -0,0 +1,373 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
// 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\ActivityPub\Util;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\HTTPClient;
|
||||
use App\Core\Log;
|
||||
use App\Core\Security;
|
||||
use App\Entity\Actor;
|
||||
use App\Util\Exception\NoSuchActorException;
|
||||
use App\Util\Formatting;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Plugin\ActivityPub\ActivityPub;
|
||||
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
||||
use Plugin\ActivityPub\Entity\ActivitypubRsa;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
/**
|
||||
* ActivityPub implementation for GNU social
|
||||
*
|
||||
* @package GNUsocial
|
||||
*
|
||||
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*
|
||||
* @see http://www.gnu.org/software/social/
|
||||
*/
|
||||
|
||||
/**
|
||||
* ActivityPub's own Explorer
|
||||
*
|
||||
* Allows to discovery new remote actors
|
||||
*
|
||||
* @author Diogo Peralta Cordeiro (@diogo.site)
|
||||
*
|
||||
* @category Plugin
|
||||
* @package GNUsocial
|
||||
*
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*/
|
||||
class Explorer
|
||||
{
|
||||
private $discovered_actor_profiles = [];
|
||||
|
||||
/**
|
||||
* Shortcut function to get a single profile from its URL.
|
||||
*
|
||||
* @param bool $grab_online whether to try online grabbing, defaults to true
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws NoSuchActorException
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*
|
||||
* @return Actor
|
||||
*/
|
||||
public static function get_profile_from_url(string $url, bool $grab_online = true): ActivitypubActor
|
||||
{
|
||||
$discovery = new self();
|
||||
// Get valid Actor object
|
||||
$actor_profile = $discovery->lookup($url, $grab_online);
|
||||
if (!empty($actor_profile)) {
|
||||
return $actor_profile[0];
|
||||
}
|
||||
throw new NoSuchActorException('Invalid Actor.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get every profile from the given URL
|
||||
* This function cleans the $this->discovered_actor_profiles array
|
||||
* so that there is no erroneous data
|
||||
*
|
||||
* @param string $url User's url
|
||||
* @param bool $grab_online whether to try online grabbing, defaults to true
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws NoSuchActorException
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*
|
||||
* @return array of Profile objects
|
||||
*/
|
||||
public function lookup(string $url, bool $grab_online = true)
|
||||
{
|
||||
if (\in_array($url, ActivityPub::PUBLIC_TO)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
Log::debug('ActivityPub Explorer: Started now looking for ' . $url);
|
||||
$this->discovered_actor_profiles = [];
|
||||
|
||||
return $this->_lookup($url, $grab_online);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get every profile from the given URL
|
||||
* This is a recursive function that will accumulate the results on
|
||||
* $discovered_actor_profiles array
|
||||
*
|
||||
* @param string $url User's url
|
||||
* @param bool $grab_online whether to try online grabbing, defaults to true
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws NoSuchActorException
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*
|
||||
* @return array of Profile objects
|
||||
*/
|
||||
private function _lookup(string $url, bool $grab_online = true): array
|
||||
{
|
||||
$grab_known = $this->grab_known_user($url);
|
||||
|
||||
// First check if we already have it locally and, if so, return it.
|
||||
// If the known fetch fails and remote grab is required: store locally and return.
|
||||
if (!$grab_known && (!$grab_online || !$this->grab_remote_user($url))) {
|
||||
throw new NoSuchActorException('Actor not found.');
|
||||
}
|
||||
|
||||
return $this->discovered_actor_profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a known user profile from its URL and joins it on
|
||||
* $this->discovered_actor_profiles
|
||||
*
|
||||
* @param string $uri Actor's uri
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws NoSuchActorException
|
||||
*
|
||||
* @return bool success state
|
||||
*/
|
||||
private function grab_known_user(string $uri): bool
|
||||
{
|
||||
Log::debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
|
||||
|
||||
// Try standard ActivityPub route
|
||||
// Is this a known filthy little mudblood?
|
||||
$aprofile = self::get_aprofile_by_url($uri);
|
||||
if ($aprofile instanceof ActivitypubActor) {
|
||||
Log::debug('ActivityPub Explorer: Found a known Aprofile for ' . $uri);
|
||||
|
||||
// We found something!
|
||||
$this->discovered_actor_profiles[] = $aprofile;
|
||||
return true;
|
||||
} else {
|
||||
Log::debug('ActivityPub Explorer: Unable to find a known Aprofile for ' . $uri);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a remote user(s) profile(s) from its URL and joins it on
|
||||
* $this->discovered_actor_profiles
|
||||
*
|
||||
* @param string $url User's url
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws NoSuchActorException
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*
|
||||
* @return bool success state
|
||||
*/
|
||||
private function grab_remote_user(string $url): bool
|
||||
{
|
||||
Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
|
||||
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
|
||||
$res = json_decode($response->getContent(), true);
|
||||
if ($response->getStatusCode() == 410) { // If it was deleted
|
||||
return true; // Nothing to add.
|
||||
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
|
||||
return false; // Try to add at another time.
|
||||
}
|
||||
if (\is_null($res)) {
|
||||
Log::debug('ActivityPub Explorer: Invalid JSON returned from given Actor URL: ' . $response->getContent());
|
||||
return true; // Nothing to add.
|
||||
}
|
||||
|
||||
if (isset($res['type']) && $res['type'] === 'OrderedCollection' && isset($res['first'])) { // It's a potential collection of actors!!!
|
||||
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
|
||||
$this->travel_collection($res['first']);
|
||||
return true;
|
||||
} elseif (self::validate_remote_response($res)) {
|
||||
Log::debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
|
||||
$this->discovered_actor_profiles[] = $this->store_profile($res);
|
||||
return true;
|
||||
} else {
|
||||
Log::debug('ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $url . '. He returned the following: ' . json_encode($res, \JSON_UNESCAPED_SLASHES));
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save remote user profile in known instance
|
||||
*
|
||||
* @param array $res remote response
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws NoSuchActorException
|
||||
*
|
||||
* @return Actor remote Profile object
|
||||
*/
|
||||
private function store_profile(array $res): ActivitypubActor
|
||||
{
|
||||
// Actor
|
||||
$actor_map = [
|
||||
'nickname' => $res['preferredUsername'],
|
||||
'fullname' => $res['name'] ?? null,
|
||||
'created' => new DateTime($res['published'] ?? 'now'),
|
||||
'bio' => isset($res['summary']) ? mb_substr(Security::sanitize($res['summary']), 0, 1000) : null,
|
||||
'homepage' => $res['url'] ?? $res['id'],
|
||||
'modified' => new DateTime(),
|
||||
];
|
||||
|
||||
$actor = new Actor();
|
||||
foreach ($actor_map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$actor->{$set}($val);
|
||||
}
|
||||
|
||||
DB::persist($actor);
|
||||
|
||||
// ActivityPub Actor
|
||||
$aprofile = new ActivitypubActor();
|
||||
$aprofile->setInboxUri($res['inbox']);
|
||||
$aprofile->setInboxSharedUri($res['endpoints']['sharedInbox'] ?? $res['inbox']);
|
||||
$aprofile->setUri($res['id']);
|
||||
$aprofile->setActorId($actor->getId());
|
||||
$aprofile->setCreated(new DateTime());
|
||||
$aprofile->setModified(new DateTime());
|
||||
|
||||
DB::persist($aprofile);
|
||||
|
||||
// Public Key
|
||||
$apRSA = new ActivitypubRsa();
|
||||
$apRSA->setActorId($actor->getID());
|
||||
$apRSA->setPublicKey($res['publicKey']['publicKeyPem']);
|
||||
$apRSA->setCreated(new DateTime());
|
||||
$apRSA->setModified(new DateTime());
|
||||
|
||||
DB::persist($apRSA);
|
||||
|
||||
// Avatar
|
||||
//if (isset($res['icon']['url'])) {
|
||||
// try {
|
||||
// $this->update_avatar($profile, $res['icon']['url']);
|
||||
// } catch (Exception $e) {
|
||||
// // Let the exception go, it isn't a serious issue
|
||||
// Log::debug('ActivityPub Explorer: An error ocurred while grabbing remote avatar: ' . $e->getMessage());
|
||||
// }
|
||||
//}
|
||||
|
||||
return $aprofile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a remote response in order to determine whether this
|
||||
* response is a valid profile or not
|
||||
*
|
||||
* @param array $res remote response
|
||||
*
|
||||
* @return bool success state
|
||||
*/
|
||||
public static function validate_remote_response(array $res): bool
|
||||
{
|
||||
return !(!isset($res['id'], $res['preferredUsername'], $res['inbox'], $res['publicKey']['publicKeyPem']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a ActivityPub Profile from it's uri
|
||||
*
|
||||
* @param string $v URL
|
||||
*
|
||||
* @return ActivitypubActor|bool false if fails | Aprofile object if successful
|
||||
*/
|
||||
public static function get_aprofile_by_url(string $v): ActivitypubActor|bool
|
||||
{
|
||||
$aprofile = ActivitypubActor::getWithPK(['uri' => $v]);
|
||||
return \is_null($aprofile) ? false : ActivitypubActor::getWithPK(['uri' => $v]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the Explorer to transverse a collection of persons.
|
||||
*
|
||||
* @throws NoSuchActorException
|
||||
*/
|
||||
private function travel_collection(string $url): bool
|
||||
{
|
||||
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
|
||||
$res = json_decode($response->getContent(), true);
|
||||
|
||||
if (!isset($res['orderedItems'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($res['orderedItems'] as $profile) {
|
||||
if ($this->_lookup($profile) == false) {
|
||||
Log::debug('ActivityPub Explorer: Found an invalid actor for ' . $profile);
|
||||
}
|
||||
}
|
||||
// Go through entire collection
|
||||
if (!\is_null($res['next'])) {
|
||||
$this->travel_collection($res['next']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a remote user array from its URL (this function is only used for
|
||||
* profile updating and shall not be used for anything else)
|
||||
*
|
||||
* @param string $url User's url
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*
|
||||
* @return array|false If it is able to fetch, false if it's gone
|
||||
* // Exceptions when network issues or unsupported Activity format
|
||||
*/
|
||||
public static function get_remote_user_activity(string $url): bool|array
|
||||
{
|
||||
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
|
||||
// If it was deleted
|
||||
if ($response->getStatusCode() == 410) {
|
||||
return false;
|
||||
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
|
||||
throw new Exception('Non Ok Status Code for given Actor URL.');
|
||||
}
|
||||
$res = json_decode($response->getContent(), true);
|
||||
if (\is_null($res)) {
|
||||
Log::debug('ActivityPub Explorer: Invalid JSON returned from given Actor URL: ' . $response->getContent());
|
||||
throw new Exception('Given Actor URL didn\'t return a valid JSON.');
|
||||
}
|
||||
if (self::validate_remote_response($res)) {
|
||||
Log::debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);
|
||||
return $res;
|
||||
}
|
||||
throw new Exception('ActivityPub Explorer: Failed to get activity.');
|
||||
}
|
||||
}
|
@ -6,10 +6,12 @@ namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Event;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Note;
|
||||
use App\Util\Exception\ClientException;
|
||||
use App\Util\Formatting;
|
||||
use Component\FreeNetwork\Entity\FreenetworkActor;
|
||||
use DateTime;
|
||||
use Plugin\ActivityPub\ActivityPub;
|
||||
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
||||
|
||||
abstract class AS2ToEntity
|
||||
@ -35,40 +37,56 @@ abstract class AS2ToEntity
|
||||
*/
|
||||
public static function store(array $activity, ?string $source = null): array
|
||||
{
|
||||
$map = [
|
||||
'activity_uri' => $activity['id'],
|
||||
'actor_id' => FreenetworkActor::getOrCreateByRemoteUri(actor_uri: $activity['actor'])->getActorId(),
|
||||
'verb' => self::activity_stream_two_verb_to_gs_verb($activity['type']),
|
||||
'object_type' => self::activity_stream_two_object_type_to_gs_table($activity['object']['type']),
|
||||
'object_uri' => $activity['object']['id'],
|
||||
'is_local' => false,
|
||||
'created' => new DateTime($activity['published'] ?? 'now'),
|
||||
'modified' => new DateTime(),
|
||||
'source' => $source,
|
||||
];
|
||||
$act = ActivitypubActivity::getWithPK(['activity_uri' => $activity['id']]);
|
||||
if (\is_null($act)) {
|
||||
$actor = ActivityPub::getActorByUri($activity['actor']);
|
||||
$map = [
|
||||
'activity_uri' => $activity['id'],
|
||||
'actor_id' => $actor->getId(),
|
||||
'verb' => self::activity_stream_two_verb_to_gs_verb($activity['type']),
|
||||
'object_type' => self::activity_stream_two_object_type_to_gs_table($activity['object']['type']),
|
||||
'object_uri' => $activity['object']['id'],
|
||||
'is_local' => false,
|
||||
'created' => new DateTime($activity['published'] ?? 'now'),
|
||||
'modified' => new DateTime(),
|
||||
'source' => $source,
|
||||
];
|
||||
|
||||
$act = new ActivitypubActivity();
|
||||
foreach ($map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$act->{$set}($val);
|
||||
$act = new ActivitypubActivity();
|
||||
foreach ($map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$act->{$set}($val);
|
||||
}
|
||||
|
||||
$obj = null;
|
||||
switch ($activity['object']['type']) {
|
||||
case 'Note':
|
||||
$obj = AS2ToNote::translate($activity['object'], $source, $activity['actor'], $act);
|
||||
break;
|
||||
default:
|
||||
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
|
||||
throw new ClientException('Unsupported Object type.');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
DB::persist($obj);
|
||||
$act->setObjectId($obj->getId());
|
||||
DB::persist($act);
|
||||
} else {
|
||||
$actor = Actor::getById($act->getActorId());
|
||||
switch ($activity['object']['type']) {
|
||||
case 'Note':
|
||||
$obj = Note::getWithPK(['id' => $act->getObjectId()]);
|
||||
break;
|
||||
default:
|
||||
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
|
||||
throw new ClientException('Unsupported Object type.');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$obj = null;
|
||||
switch ($activity['object']['type']) {
|
||||
case 'Note':
|
||||
$obj = AS2ToNote::translate($activity['object'], $source);
|
||||
break;
|
||||
default:
|
||||
if (!Event::handle('ActivityPubObject', [$activity['object']['type'], $activity['object'], &$obj])) {
|
||||
throw new ClientException('Unsupported Object type.');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
DB::persist($obj);
|
||||
$act->setObjectId($obj->getId());
|
||||
DB::persist($act);
|
||||
|
||||
return [$act, $obj];
|
||||
return [$actor, $act, $obj];
|
||||
}
|
||||
}
|
||||
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Plugin\ActivityPub\Util\Model\AS2ToEntity;
|
||||
|
||||
use App\Core\Event;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Note;
|
||||
use App\Util\Formatting;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
||||
abstract class AS2ToGSActor
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Note
|
||||
*/
|
||||
public static function translate(array $args, ?string $source = null): Actor
|
||||
{
|
||||
$map = [
|
||||
'isLocal' => false,
|
||||
'created' => new DateTime($args['published'] ?? 'now'),
|
||||
'content' => $args['content'] ?? null,
|
||||
'content_type' => 'text/html',
|
||||
'rendered' => null,
|
||||
'modified' => new DateTime(),
|
||||
'source' => $source,
|
||||
];
|
||||
if ($map['content'] !== null) {
|
||||
Event::handle('RenderNoteContent', [
|
||||
$map['content'],
|
||||
$map['content_type'],
|
||||
&$map['rendered'],
|
||||
Actor::getById(1), // just for testing
|
||||
null, // reply to
|
||||
]);
|
||||
}
|
||||
|
||||
$obj = new Note();
|
||||
foreach ($map as $prop => $val) {
|
||||
$set = Formatting::snakeCaseToCamelCase("set_{$prop}");
|
||||
$obj->{$set}($val);
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
}
|
@ -8,19 +8,24 @@ use App\Core\Event;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Note;
|
||||
use App\Util\Formatting;
|
||||
use Component\FreeNetwork\Entity\FreenetworkActor;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Plugin\ActivityPub\ActivityPub;
|
||||
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
||||
|
||||
abstract class AS2ToNote
|
||||
{
|
||||
/**
|
||||
*@throws Exception
|
||||
*/
|
||||
public static function translate(array $object, ?string $source = null): Note
|
||||
public static function translate(array $object, ?string $source, ?string $actor_uri, ?ActivitypubActivity $act = null): Note
|
||||
{
|
||||
$actor_id = FreenetworkActor::getOrCreateByRemoteUri(actor_uri: $object['attributedTo'])->getActorId();
|
||||
$map = [
|
||||
if (isset($actor_uri) && $actor_uri === $object['attributedTo']) {
|
||||
$actor_id = $act->getActorId();
|
||||
} else {
|
||||
$actor_id = ActivityPub::getActorByUri($object['attributedTo'])->getId();
|
||||
}
|
||||
$map = [
|
||||
'is_local' => false,
|
||||
'created' => new DateTime($object['published'] ?? 'now'),
|
||||
'content' => $object['content'] ?? null,
|
||||
|
6
plugins/ActivityPub/composer.json
Normal file
6
plugins/ActivityPub/composer.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"require": {
|
||||
"masterminds/html5": "^2.7",
|
||||
"mf2/mf2": "^0.4.6"
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
{
|
||||
"require": {
|
||||
"ext-vips": "*",
|
||||
"jcupitt/vips": "1.0.8"
|
||||
}
|
||||
}
|
||||
|
@ -172,6 +172,9 @@
|
||||
"masterminds/html5": {
|
||||
"version": "2.7.4"
|
||||
},
|
||||
"mf2/mf2": {
|
||||
"version": "0.4.6"
|
||||
},
|
||||
"ml/iri": {
|
||||
"version": "1.1.4"
|
||||
},
|
||||
@ -578,6 +581,9 @@
|
||||
"symfony/options-resolver": {
|
||||
"version": "v5.2.4"
|
||||
},
|
||||
"symfony/password-hasher": {
|
||||
"version": "v5.3.8"
|
||||
},
|
||||
"symfony/phpunit-bridge": {
|
||||
"version": "5.1",
|
||||
"recipe": {
|
||||
|
Loading…
Reference in New Issue
Block a user