[PLUGIN][ActivityPub][Util][Explorer] Simplify fetching Actor by URI

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-25 01:05:28 +00:00
parent 7c80277436
commit f5e92de62d
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
8 changed files with 110 additions and 138 deletions

View File

@ -42,23 +42,20 @@ use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
use App\Entity\Activity;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\BugFoundException;
use App\Util\Exception\NoSuchActorException;
use App\Util\Nickname;
use Component\Collection\Util\Controller\OrderedCollection;
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
use Component\FreeNetwork\Util\Discovery;
use Exception;
use InvalidArgumentException;
use const PHP_URL_HOST;
use Plugin\ActivityPub\Controller\Inbox;
use Plugin\ActivityPub\Controller\Outbox;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Entity\ActivitypubActor;
use Plugin\ActivityPub\Entity\ActivitypubObject;
use Plugin\ActivityPub\Util\Explorer;
use Plugin\ActivityPub\Util\HTTPSignature;
use Plugin\ActivityPub\Util\Model;
use Plugin\ActivityPub\Util\OrderedCollectionController;
@ -190,7 +187,7 @@ class ActivityPub extends Plugin
&& !\is_null($ap_other = DB::findOneBy(ActivitypubActor::class, ['actor_id' => $other->getId()], return_null: true))
) {
// Are they both in the same server?
$canAdmin = parse_url($ap_actor->getUri(), PHP_URL_HOST) === parse_url($ap_other->getUri(), PHP_URL_HOST);
$canAdmin = parse_url($ap_actor->getUri(), \PHP_URL_HOST) === parse_url($ap_other->getUri(), \PHP_URL_HOST);
return Event::stop;
}
@ -339,7 +336,7 @@ class ActivityPub extends Plugin
FreeNetworkActorProtocol::protocolSucceeded(
'activitypub',
$actor,
Discovery::normalize($actor->getNickname() . '@' . parse_url($inbox, PHP_URL_HOST)),
Discovery::normalize($actor->getNickname() . '@' . parse_url($inbox, \PHP_URL_HOST)),
);
}
}
@ -518,8 +515,8 @@ class ActivityPub extends Plugin
// Try Actor
try {
return self::getActorByUri($resource, try_online: false);
} catch (Exception) {
return Explorer::getOneFromUri($resource, try_online: false);
} catch (\Exception) {
// Ignore, this is brute forcing, it's okay not to find
}
@ -539,49 +536,4 @@ class ActivityPub extends Plugin
return Model::jsonToType($response->getContent());
}
}
/**
* Get an Actor from ActivityPub URI, if it doesn't exist, attempt to fetch it
* This should only be necessary internally.
*
* @throws NoSuchActorException
*
* @return Actor got from URI
*/
public static function getActorByUri(string $resource, bool $try_online = true): Actor
{
// Try local
if (Common::isValidHttpUrl($resource)) {
// This means $resource is a valid url
$resource_parts = parse_url($resource);
// TODO: Use URLMatcher
if ($resource_parts['host'] === 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 DB::findOneBy(LocalUser::class, ['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 known remote
$aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $resource], return_null: true);
if (!\is_null($aprofile)) {
return Actor::getById($aprofile->getActorId());
}
// Try remote
if ($try_online) {
$aprofile = ActivitypubActor::getByAddr($resource);
if ($aprofile instanceof ActivitypubActor) {
return Actor::getById($aprofile->getActorId());
}
}
throw new NoSuchActorException("From URI: {$resource}");
}
}

View File

@ -35,12 +35,11 @@ namespace Plugin\ActivityPub\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use App\Core\Event;
use App\Util\Common;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Actor;
use App\Util\Common;
use App\Util\Exception\ClientException;
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
use Component\FreeNetwork\Util\Discovery;
@ -52,6 +51,7 @@ use Plugin\ActivityPub\Util\Explorer;
use Plugin\ActivityPub\Util\HTTPSignature;
use Plugin\ActivityPub\Util\Model;
use Plugin\ActivityPub\Util\TypeResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* ActivityPub Inbox Handler
@ -73,7 +73,7 @@ class Inbox extends Controller
{
$error = function (string $m, ?Exception $e = null): TypeResponse {
Log::error('ActivityPub Error Answer: ' . ($json = json_encode(['error' => $m, 'exception' => var_export($e, true)])));
if (is_null($e)) {
if (\is_null($e)) {
return new TypeResponse($json, 400);
} else {
throw $e;
@ -102,8 +102,8 @@ class Inbox extends Controller
try {
$resource_parts = parse_url($type->get('actor'));
if ($resource_parts['host'] !== Common::config('site', 'server')) {
$ap_actor = DB::wrapInTransaction(fn() => ActivitypubActor::fromUri($type->get('actor')));
$actor = Actor::getById($ap_actor->getActorId());
$actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($type->get('actor')));
$ap_actor = DB::findOneBy(ActivitypubActor::class, ['actor_id' => $actor->getId()]);
} else {
throw new Exception('Only remote actors can use this endpoint.');
}
@ -140,7 +140,7 @@ class Inbox extends Controller
// If the signature fails verification the first time, update profile as it might have changed public key
if ($verified !== 1) {
try {
$res = Explorer::get_remote_user_activity($ap_actor->getUri());
$res = Explorer::getRemoteActorActivity($ap_actor->getUri());
if (\is_null($res)) {
return $error('Invalid remote actor (null response).');
}

View File

@ -33,6 +33,7 @@ declare(strict_types = 1);
namespace Plugin\ActivityPub\Entity;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Entity;
use function App\Core\I18n\_m;
use App\Core\Log;
@ -142,6 +143,11 @@ class ActivitypubActor extends Entity
// @codeCoverageIgnoreEnd
// }}} Autocode
public function getActor(): Actor
{
return Actor::getById($this->getActorId());
}
/**
* Look up, and if necessary create, an Activitypub_profile for the remote
* entity with the given WebFinger address.
@ -152,7 +158,7 @@ class ActivitypubActor extends Entity
*
* @throws Exception on error conditions
*/
public static function getByAddr(string $addr): self
public static function getByAddr(string $addr): Actor
{
// Normalize $addr, i.e. add 'acct:' if missing
$addr = Discovery::normalize($addr);
@ -166,9 +172,9 @@ class ActivitypubActor extends Entity
throw new Exception(_m('Not a valid WebFinger address (via cache).'));
}
try {
return self::fromUri($uri);
return DB::wrapInTransaction(fn () => Explorer::getOneFromUri($uri));
} catch (Exception $e) {
Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri), [$e]);
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
}
}
@ -190,7 +196,7 @@ class ActivitypubActor extends Entity
return self::fromXrd($addr, $xrd);
}
public static function fromXrd(string $addr, XML_XRD $xrd): self
public static function fromXrd(string $addr, XML_XRD $xrd): Actor
{
$hints = array_merge(
['webfinger' => $addr],
@ -201,9 +207,9 @@ class ActivitypubActor extends Entity
$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;
$actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($hints['activitypub']));
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $hints['activitypub']);
return $actor;
} catch (Exception $e) {
Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
throw $e;
@ -222,22 +228,6 @@ class ActivitypubActor extends Entity
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);
}
}
/**
* @param ActivitypubActor $ap_actor
*
@ -261,7 +251,7 @@ class ActivitypubActor 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' => ['uri'],
'primary key' => ['uri'],
'unique keys' => [
'activitypub_actor_id_ukey' => ['actor_id'],
],

View File

@ -35,8 +35,13 @@ namespace Plugin\ActivityPub\Util;
use App\Core\DB\DB;
use App\Core\HTTPClient;
use App\Core\Log;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Util\Common;
use App\Util\Exception\NoSuchActorException;
use App\Util\Nickname;
use Exception;
use InvalidArgumentException;
use const JSON_UNESCAPED_SLASHES;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActor;
@ -55,12 +60,12 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
*/
class Explorer
{
private array $discovered_activitypub_actor_profiles = [];
private array $discovered_actors = [];
/**
* Shortcut function to get a single profile from its URL.
*
* @param bool $grab_online whether to try online grabbing, defaults to true
* @param bool $try_online whether to try online grabbing, defaults to true
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
@ -68,15 +73,17 @@ class Explorer
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function get_profile_from_url(string $url, bool $grab_online = true): ActivitypubActor
public static function getOneFromUri(string $uri, bool $try_online = true): Actor
{
$discovery = new self();
// Get valid Actor object
$actor_profile = $discovery->lookup($url, $grab_online);
if (!empty($actor_profile)) {
return $actor_profile[0];
$actors = (new self())->lookup($uri, $try_online);
switch (\count($actors)) {
case 1:
return $actors[0];
case 0:
throw new NoSuchActorException('Invalid Actor.');
default:
throw new InvalidArgumentException('More than one actor found for this URI.');
}
throw new NoSuchActorException('Invalid Actor.');
}
/**
@ -84,8 +91,8 @@ class Explorer
* 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
* @param string $uri User's url
* @param bool $try_online whether to try online grabbing, defaults to true
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
@ -95,16 +102,16 @@ class Explorer
*
* @return array of Actor objects
*/
public function lookup(string $url, bool $grab_online = true): array
public function lookup(string $uri, bool $try_online = true): array
{
if (\in_array($url, ActivityPub::PUBLIC_TO)) {
if (\in_array($uri, ActivityPub::PUBLIC_TO)) {
return [];
}
Log::debug('ActivityPub Explorer: Started now looking for ' . $url);
$this->discovered_activitypub_actor_profiles = [];
Log::debug('ActivityPub Explorer: Started now looking for ' . $uri);
$this->discovered_actors = [];
return $this->_lookup($url, $grab_online);
return $this->_lookup($uri, $try_online);
}
/**
@ -112,8 +119,8 @@ class Explorer
* 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
* @param string $uri User's url
* @param bool $try_online whether to try online grabbing, defaults to true
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
@ -121,19 +128,19 @@ class Explorer
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return array of ActivityPub Actor objects
* @return array of Actor objects
*/
private function _lookup(string $url, bool $grab_online = true): array
private function _lookup(string $uri, bool $try_online = true): array
{
$grab_known = $this->grab_known_user($url);
$grab_known = $this->grabKnownActor($uri);
// 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))) {
if (!$grab_known && (!$try_online || !$this->grabRemoteActor($uri))) {
throw new NoSuchActorException('Actor not found.');
}
return $this->discovered_activitypub_actor_profiles;
return $this->discovered_actors;
}
/**
@ -147,21 +154,43 @@ class Explorer
*
* @return bool success state
*/
private function grab_known_user(string $uri): bool
private function grabKnownActor(string $uri): bool
{
Log::debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
// Try local
if (Common::isValidHttpUrl($uri)) {
// This means $uri is a valid url
$resource_parts = parse_url($uri);
// TODO: Use URLMatcher
if ($resource_parts['host'] === 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) {
$this->discovered_actors[] = DB::findOneBy(
LocalUser::class,
['nickname' => $matches[0][1]],
)->getActor();
return true;
} elseif (preg_match_all($reuri, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
$this->discovered_actors[] = Actor::getById((int) $matches[0][1]);
return true;
}
}
}
// Try standard ActivityPub route
// Is this a known filthy little mudblood?
$aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $uri], return_null: true);
if (!\is_null($aprofile)) {
Log::debug('ActivityPub Explorer: Found a known Aprofile for ' . $uri);
// We found something!
$this->discovered_activitypub_actor_profiles[] = $aprofile;
Log::debug('ActivityPub Explorer: Found a known ActivityPub Actor for ' . $uri);
$this->discovered_actors[] = $aprofile->getActor();
return true;
} else {
Log::debug('ActivityPub Explorer: Unable to find a known Aprofile for ' . $uri);
Log::debug('ActivityPub Explorer: Unable to find a known ActivityPub Actor for ' . $uri);
}
return false;
@ -171,7 +200,7 @@ class Explorer
* Get a remote user(s) profile(s) from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $url User's url
* @param string $uri User's url
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
@ -181,10 +210,10 @@ class Explorer
*
* @return bool success state
*/
private function grab_remote_user(string $url): bool
private function grabRemoteActor(string $uri): bool
{
Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $uri);
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
$res = json_decode($response->getContent(), true);
if ($response->getStatusCode() == 410) { // If it was deleted
return true; // Nothing to add.
@ -197,16 +226,16 @@ class Explorer
}
if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
$this->travel_collection($res['first']);
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $uri);
$this->travelCollection($res['first']);
return true;
} else {
try {
$this->discovered_activitypub_actor_profiles[] = Model\Actor::fromJson(json_encode($res));
$this->discovered_actors[] = DB::wrapInTransaction(fn() => Model\Actor::fromJson(json_encode($res)))->getActor();
return true;
} catch (Exception $e) {
Log::debug(
'ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $url
'ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $uri
. '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)
. ' and the following exception: ' . $e->getMessage(),
);
@ -226,23 +255,23 @@ class Explorer
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
private function travel_collection(string $url): bool
private function travelCollection(string $uri): bool
{
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
$response = HTTPClient::get($uri, ['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);
}
// Accumulate findings
foreach ($res['orderedItems'] as $actor_uri) {
$this->_lookup($actor_uri);
}
// Go through entire collection
if (!\is_null($res['next'])) {
$this->travel_collection($res['next']);
$this->travelCollection($res['next']);
}
return true;
@ -252,7 +281,7 @@ class Explorer
* 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
* @param string $uri User's url
*
* @throws ClientExceptionInterface
* @throws Exception
@ -263,9 +292,9 @@ class Explorer
* @return null|string 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): string|null
public static function getRemoteActorActivity(string $uri): string|null
{
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
// If it was deleted
if ($response->getStatusCode() == 410) {
return null;

View File

@ -46,6 +46,7 @@ use DateTimeInterface;
use InvalidArgumentException;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Util\Explorer;
use Plugin\ActivityPub\Util\Model;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
@ -84,7 +85,7 @@ class Activity extends Model
}
// Find Actor and Object
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
$actor = Explorer::getOneFromUri($type_activity->get('actor'));
$type_object = $type_activity->get('object');
if (\is_string($type_object)) { // Retrieve it
$type_object = ActivityPub::getObjectByUri($type_object, try_online: true);

View File

@ -139,7 +139,7 @@ class Note extends Model
}
if (\is_null($actor_id)) {
$actor = ActivityPub::getActorByUri($type_note->get('attributedTo'));
$actor = Explorer::getOneFromUri($type_note->get('attributedTo'));
$actor_id = $actor->getId();
}
$map = [
@ -190,8 +190,8 @@ class Note extends Model
continue;
}
try {
$actor = ActivityPub::getActorByUri($target);
$object_mentions_ids[$actor->getId()] = $target;
$actor = Explorer::getOneFromUri($target);
$attention_ids[$actor->getId()] = $target;
// If $to is a group and note is unlisted, set note's scope as Group
if ($actor->isGroup() && $map['scope'] === 'unlisted') {
$map['scope'] = VisibilityScope::GROUP;
@ -211,8 +211,8 @@ class Note extends Model
continue;
}
try {
$actor = ActivityPub::getActorByUri($target);
$object_mentions_ids[$actor->getId()] = $target;
$actor = Explorer::getOneFromUri($target);
$attention_ids[$actor->getId()] = $target;
} catch (Exception $e) {
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
}

View File

@ -161,7 +161,7 @@ class PinnedNotes extends Plugin
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
{
if ($type_name === 'Person') {
$actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->get('id'));
$actor = \Plugin\ActivityPub\Util\Explorer::getOneFromUri($type->get('id'));
$router_args = ['id' => $actor->getId()];
$router_type = Router::ABSOLUTE_URL;
$action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);

View File

@ -254,7 +254,7 @@ class WebMonetization extends Plugin
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
{
if ($type_name === 'Person') {
$actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->getId());
$actor = \Plugin\ActivityPub\Util\Explorer::getOneFromUri($type->getId());
$wallet = DB::findOneBy(Wallet::class, ['actor_id' => $actor->getId()], return_null: true);
if (!\is_null($address = $wallet?->getAddress())) {