[PLUGIN][ActivityPub][Util][Explorer] Simplify fetching Actor by URI
This commit is contained in:
parent
7c80277436
commit
f5e92de62d
@ -42,23 +42,20 @@ use App\Core\Router\RouteLoader;
|
|||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
use App\Entity\Activity;
|
use App\Entity\Activity;
|
||||||
use App\Entity\Actor;
|
use App\Entity\Actor;
|
||||||
use App\Entity\LocalUser;
|
|
||||||
use App\Entity\Note;
|
use App\Entity\Note;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
use App\Util\Exception\BugFoundException;
|
use App\Util\Exception\BugFoundException;
|
||||||
use App\Util\Exception\NoSuchActorException;
|
|
||||||
use App\Util\Nickname;
|
|
||||||
use Component\Collection\Util\Controller\OrderedCollection;
|
use Component\Collection\Util\Controller\OrderedCollection;
|
||||||
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
|
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
|
||||||
use Component\FreeNetwork\Util\Discovery;
|
use Component\FreeNetwork\Util\Discovery;
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use const PHP_URL_HOST;
|
|
||||||
use Plugin\ActivityPub\Controller\Inbox;
|
use Plugin\ActivityPub\Controller\Inbox;
|
||||||
use Plugin\ActivityPub\Controller\Outbox;
|
use Plugin\ActivityPub\Controller\Outbox;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubObject;
|
use Plugin\ActivityPub\Entity\ActivitypubObject;
|
||||||
|
use Plugin\ActivityPub\Util\Explorer;
|
||||||
use Plugin\ActivityPub\Util\HTTPSignature;
|
use Plugin\ActivityPub\Util\HTTPSignature;
|
||||||
use Plugin\ActivityPub\Util\Model;
|
use Plugin\ActivityPub\Util\Model;
|
||||||
use Plugin\ActivityPub\Util\OrderedCollectionController;
|
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))
|
&& !\is_null($ap_other = DB::findOneBy(ActivitypubActor::class, ['actor_id' => $other->getId()], return_null: true))
|
||||||
) {
|
) {
|
||||||
// Are they both in the same server?
|
// 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;
|
return Event::stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,7 +336,7 @@ class ActivityPub extends Plugin
|
|||||||
FreeNetworkActorProtocol::protocolSucceeded(
|
FreeNetworkActorProtocol::protocolSucceeded(
|
||||||
'activitypub',
|
'activitypub',
|
||||||
$actor,
|
$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 Actor
|
||||||
try {
|
try {
|
||||||
return self::getActorByUri($resource, try_online: false);
|
return Explorer::getOneFromUri($resource, try_online: false);
|
||||||
} catch (Exception) {
|
} catch (\Exception) {
|
||||||
// Ignore, this is brute forcing, it's okay not to find
|
// Ignore, this is brute forcing, it's okay not to find
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,49 +536,4 @@ class ActivityPub extends Plugin
|
|||||||
return Model::jsonToType($response->getContent());
|
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}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -35,12 +35,11 @@ namespace Plugin\ActivityPub\Controller;
|
|||||||
use App\Core\Controller;
|
use App\Core\Controller;
|
||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Core\Event;
|
use App\Core\Event;
|
||||||
use App\Util\Common;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use function App\Core\I18n\_m;
|
use function App\Core\I18n\_m;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
use App\Entity\Actor;
|
use App\Entity\Actor;
|
||||||
|
use App\Util\Common;
|
||||||
use App\Util\Exception\ClientException;
|
use App\Util\Exception\ClientException;
|
||||||
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
|
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
|
||||||
use Component\FreeNetwork\Util\Discovery;
|
use Component\FreeNetwork\Util\Discovery;
|
||||||
@ -52,6 +51,7 @@ use Plugin\ActivityPub\Util\Explorer;
|
|||||||
use Plugin\ActivityPub\Util\HTTPSignature;
|
use Plugin\ActivityPub\Util\HTTPSignature;
|
||||||
use Plugin\ActivityPub\Util\Model;
|
use Plugin\ActivityPub\Util\Model;
|
||||||
use Plugin\ActivityPub\Util\TypeResponse;
|
use Plugin\ActivityPub\Util\TypeResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ActivityPub Inbox Handler
|
* ActivityPub Inbox Handler
|
||||||
@ -73,7 +73,7 @@ class Inbox extends Controller
|
|||||||
{
|
{
|
||||||
$error = function (string $m, ?Exception $e = null): TypeResponse {
|
$error = function (string $m, ?Exception $e = null): TypeResponse {
|
||||||
Log::error('ActivityPub Error Answer: ' . ($json = json_encode(['error' => $m, 'exception' => var_export($e, true)])));
|
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);
|
return new TypeResponse($json, 400);
|
||||||
} else {
|
} else {
|
||||||
throw $e;
|
throw $e;
|
||||||
@ -102,8 +102,8 @@ class Inbox extends Controller
|
|||||||
try {
|
try {
|
||||||
$resource_parts = parse_url($type->get('actor'));
|
$resource_parts = parse_url($type->get('actor'));
|
||||||
if ($resource_parts['host'] !== Common::config('site', 'server')) {
|
if ($resource_parts['host'] !== Common::config('site', 'server')) {
|
||||||
$ap_actor = DB::wrapInTransaction(fn() => ActivitypubActor::fromUri($type->get('actor')));
|
$actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($type->get('actor')));
|
||||||
$actor = Actor::getById($ap_actor->getActorId());
|
$ap_actor = DB::findOneBy(ActivitypubActor::class, ['actor_id' => $actor->getId()]);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('Only remote actors can use this endpoint.');
|
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 the signature fails verification the first time, update profile as it might have changed public key
|
||||||
if ($verified !== 1) {
|
if ($verified !== 1) {
|
||||||
try {
|
try {
|
||||||
$res = Explorer::get_remote_user_activity($ap_actor->getUri());
|
$res = Explorer::getRemoteActorActivity($ap_actor->getUri());
|
||||||
if (\is_null($res)) {
|
if (\is_null($res)) {
|
||||||
return $error('Invalid remote actor (null response).');
|
return $error('Invalid remote actor (null response).');
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,7 @@ declare(strict_types = 1);
|
|||||||
namespace Plugin\ActivityPub\Entity;
|
namespace Plugin\ActivityPub\Entity;
|
||||||
|
|
||||||
use App\Core\Cache;
|
use App\Core\Cache;
|
||||||
|
use App\Core\DB\DB;
|
||||||
use App\Core\Entity;
|
use App\Core\Entity;
|
||||||
use function App\Core\I18n\_m;
|
use function App\Core\I18n\_m;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
@ -142,6 +143,11 @@ class ActivitypubActor extends Entity
|
|||||||
// @codeCoverageIgnoreEnd
|
// @codeCoverageIgnoreEnd
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
public function getActor(): Actor
|
||||||
|
{
|
||||||
|
return Actor::getById($this->getActorId());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Look up, and if necessary create, an Activitypub_profile for the remote
|
* Look up, and if necessary create, an Activitypub_profile for the remote
|
||||||
* entity with the given WebFinger address.
|
* entity with the given WebFinger address.
|
||||||
@ -152,7 +158,7 @@ class ActivitypubActor extends Entity
|
|||||||
*
|
*
|
||||||
* @throws Exception on error conditions
|
* @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
|
// Normalize $addr, i.e. add 'acct:' if missing
|
||||||
$addr = Discovery::normalize($addr);
|
$addr = Discovery::normalize($addr);
|
||||||
@ -166,9 +172,9 @@ class ActivitypubActor extends Entity
|
|||||||
throw new Exception(_m('Not a valid WebFinger address (via cache).'));
|
throw new Exception(_m('Not a valid WebFinger address (via cache).'));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return self::fromUri($uri);
|
return DB::wrapInTransaction(fn () => Explorer::getOneFromUri($uri));
|
||||||
} catch (Exception $e) {
|
} 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);
|
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,7 +196,7 @@ class ActivitypubActor extends Entity
|
|||||||
return self::fromXrd($addr, $xrd);
|
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(
|
$hints = array_merge(
|
||||||
['webfinger' => $addr],
|
['webfinger' => $addr],
|
||||||
@ -201,9 +207,9 @@ class ActivitypubActor extends Entity
|
|||||||
$uri = $hints['activitypub'];
|
$uri = $hints['activitypub'];
|
||||||
try {
|
try {
|
||||||
LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
|
LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
|
||||||
$aprofile = self::fromUri($hints['activitypub']);
|
$actor = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($hints['activitypub']));
|
||||||
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $aprofile->getUri());
|
Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $hints['activitypub']);
|
||||||
return $aprofile;
|
return $actor;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
|
Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
|
||||||
throw $e;
|
throw $e;
|
||||||
@ -222,22 +228,6 @@ class ActivitypubActor extends Entity
|
|||||||
throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
|
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
|
* @param ActivitypubActor $ap_actor
|
||||||
*
|
*
|
||||||
|
@ -35,8 +35,13 @@ namespace Plugin\ActivityPub\Util;
|
|||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Core\HTTPClient;
|
use App\Core\HTTPClient;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
|
use App\Entity\Actor;
|
||||||
|
use App\Entity\LocalUser;
|
||||||
|
use App\Util\Common;
|
||||||
use App\Util\Exception\NoSuchActorException;
|
use App\Util\Exception\NoSuchActorException;
|
||||||
|
use App\Util\Nickname;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
use const JSON_UNESCAPED_SLASHES;
|
use const JSON_UNESCAPED_SLASHES;
|
||||||
use Plugin\ActivityPub\ActivityPub;
|
use Plugin\ActivityPub\ActivityPub;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
||||||
@ -55,12 +60,12 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|||||||
*/
|
*/
|
||||||
class Explorer
|
class Explorer
|
||||||
{
|
{
|
||||||
private array $discovered_activitypub_actor_profiles = [];
|
private array $discovered_actors = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortcut function to get a single profile from its URL.
|
* 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 ClientExceptionInterface
|
||||||
* @throws NoSuchActorException
|
* @throws NoSuchActorException
|
||||||
@ -68,15 +73,17 @@ class Explorer
|
|||||||
* @throws ServerExceptionInterface
|
* @throws ServerExceptionInterface
|
||||||
* @throws TransportExceptionInterface
|
* @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();
|
$actors = (new self())->lookup($uri, $try_online);
|
||||||
// Get valid Actor object
|
switch (\count($actors)) {
|
||||||
$actor_profile = $discovery->lookup($url, $grab_online);
|
case 1:
|
||||||
if (!empty($actor_profile)) {
|
return $actors[0];
|
||||||
return $actor_profile[0];
|
case 0:
|
||||||
}
|
|
||||||
throw new NoSuchActorException('Invalid Actor.');
|
throw new NoSuchActorException('Invalid Actor.');
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('More than one actor found for this URI.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,8 +91,8 @@ class Explorer
|
|||||||
* This function cleans the $this->discovered_actor_profiles array
|
* This function cleans the $this->discovered_actor_profiles array
|
||||||
* so that there is no erroneous data
|
* so that there is no erroneous data
|
||||||
*
|
*
|
||||||
* @param string $url User's url
|
* @param string $uri User's 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 ClientExceptionInterface
|
||||||
* @throws NoSuchActorException
|
* @throws NoSuchActorException
|
||||||
@ -95,16 +102,16 @@ class Explorer
|
|||||||
*
|
*
|
||||||
* @return array of Actor objects
|
* @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 [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::debug('ActivityPub Explorer: Started now looking for ' . $url);
|
Log::debug('ActivityPub Explorer: Started now looking for ' . $uri);
|
||||||
$this->discovered_activitypub_actor_profiles = [];
|
$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
|
* This is a recursive function that will accumulate the results on
|
||||||
* $discovered_actor_profiles array
|
* $discovered_actor_profiles array
|
||||||
*
|
*
|
||||||
* @param string $url User's url
|
* @param string $uri User's 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 ClientExceptionInterface
|
||||||
* @throws NoSuchActorException
|
* @throws NoSuchActorException
|
||||||
@ -121,19 +128,19 @@ class Explorer
|
|||||||
* @throws ServerExceptionInterface
|
* @throws ServerExceptionInterface
|
||||||
* @throws TransportExceptionInterface
|
* @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.
|
// 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 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.');
|
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
|
* @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.');
|
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
|
// Try standard ActivityPub route
|
||||||
// Is this a known filthy little mudblood?
|
// Is this a known filthy little mudblood?
|
||||||
$aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $uri], return_null: true);
|
$aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $uri], return_null: true);
|
||||||
if (!\is_null($aprofile)) {
|
if (!\is_null($aprofile)) {
|
||||||
Log::debug('ActivityPub Explorer: Found a known Aprofile for ' . $uri);
|
Log::debug('ActivityPub Explorer: Found a known ActivityPub Actor for ' . $uri);
|
||||||
|
$this->discovered_actors[] = $aprofile->getActor();
|
||||||
// We found something!
|
|
||||||
$this->discovered_activitypub_actor_profiles[] = $aprofile;
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} 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;
|
return false;
|
||||||
@ -171,7 +200,7 @@ class Explorer
|
|||||||
* Get a remote user(s) profile(s) from its URL and joins it on
|
* Get a remote user(s) profile(s) from its URL and joins it on
|
||||||
* $this->discovered_actor_profiles
|
* $this->discovered_actor_profiles
|
||||||
*
|
*
|
||||||
* @param string $url User's url
|
* @param string $uri User's url
|
||||||
*
|
*
|
||||||
* @throws ClientExceptionInterface
|
* @throws ClientExceptionInterface
|
||||||
* @throws NoSuchActorException
|
* @throws NoSuchActorException
|
||||||
@ -181,10 +210,10 @@ class Explorer
|
|||||||
*
|
*
|
||||||
* @return bool success state
|
* @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);
|
Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $uri);
|
||||||
$response = HTTPClient::get($url, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
|
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
|
||||||
$res = json_decode($response->getContent(), true);
|
$res = json_decode($response->getContent(), true);
|
||||||
if ($response->getStatusCode() == 410) { // If it was deleted
|
if ($response->getStatusCode() == 410) { // If it was deleted
|
||||||
return true; // Nothing to add.
|
return true; // Nothing to add.
|
||||||
@ -197,16 +226,16 @@ class Explorer
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
|
if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
|
||||||
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $url);
|
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $uri);
|
||||||
$this->travel_collection($res['first']);
|
$this->travelCollection($res['first']);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
try {
|
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;
|
return true;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug(
|
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)
|
. '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)
|
||||||
. ' and the following exception: ' . $e->getMessage(),
|
. ' and the following exception: ' . $e->getMessage(),
|
||||||
);
|
);
|
||||||
@ -226,23 +255,23 @@ class Explorer
|
|||||||
* @throws ServerExceptionInterface
|
* @throws ServerExceptionInterface
|
||||||
* @throws TransportExceptionInterface
|
* @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);
|
$res = json_decode($response->getContent(), true);
|
||||||
|
|
||||||
if (!isset($res['orderedItems'])) {
|
if (!isset($res['orderedItems'])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($res['orderedItems'] as $profile) {
|
// Accumulate findings
|
||||||
if ($this->_lookup($profile) == false) {
|
foreach ($res['orderedItems'] as $actor_uri) {
|
||||||
Log::debug('ActivityPub Explorer: Found an invalid actor for ' . $profile);
|
$this->_lookup($actor_uri);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through entire collection
|
// Go through entire collection
|
||||||
if (!\is_null($res['next'])) {
|
if (!\is_null($res['next'])) {
|
||||||
$this->travel_collection($res['next']);
|
$this->travelCollection($res['next']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -252,7 +281,7 @@ class Explorer
|
|||||||
* Get a remote user array from its URL (this function is only used for
|
* Get a remote user array from its URL (this function is only used for
|
||||||
* profile updating and shall not be used for anything else)
|
* 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 ClientExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@ -263,9 +292,9 @@ class Explorer
|
|||||||
* @return null|string If it is able to fetch, false if it's gone
|
* @return null|string If it is able to fetch, false if it's gone
|
||||||
* // Exceptions when network issues or unsupported Activity format
|
* // 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 it was deleted
|
||||||
if ($response->getStatusCode() == 410) {
|
if ($response->getStatusCode() == 410) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -46,6 +46,7 @@ use DateTimeInterface;
|
|||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Plugin\ActivityPub\ActivityPub;
|
use Plugin\ActivityPub\ActivityPub;
|
||||||
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
||||||
|
use Plugin\ActivityPub\Util\Explorer;
|
||||||
use Plugin\ActivityPub\Util\Model;
|
use Plugin\ActivityPub\Util\Model;
|
||||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||||
@ -84,7 +85,7 @@ class Activity extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find Actor and Object
|
// Find Actor and Object
|
||||||
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
|
$actor = Explorer::getOneFromUri($type_activity->get('actor'));
|
||||||
$type_object = $type_activity->get('object');
|
$type_object = $type_activity->get('object');
|
||||||
if (\is_string($type_object)) { // Retrieve it
|
if (\is_string($type_object)) { // Retrieve it
|
||||||
$type_object = ActivityPub::getObjectByUri($type_object, try_online: true);
|
$type_object = ActivityPub::getObjectByUri($type_object, try_online: true);
|
||||||
|
@ -139,7 +139,7 @@ class Note extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (\is_null($actor_id)) {
|
if (\is_null($actor_id)) {
|
||||||
$actor = ActivityPub::getActorByUri($type_note->get('attributedTo'));
|
$actor = Explorer::getOneFromUri($type_note->get('attributedTo'));
|
||||||
$actor_id = $actor->getId();
|
$actor_id = $actor->getId();
|
||||||
}
|
}
|
||||||
$map = [
|
$map = [
|
||||||
@ -190,8 +190,8 @@ class Note extends Model
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$actor = ActivityPub::getActorByUri($target);
|
$actor = Explorer::getOneFromUri($target);
|
||||||
$object_mentions_ids[$actor->getId()] = $target;
|
$attention_ids[$actor->getId()] = $target;
|
||||||
// If $to is a group and note is unlisted, set note's scope as Group
|
// If $to is a group and note is unlisted, set note's scope as Group
|
||||||
if ($actor->isGroup() && $map['scope'] === 'unlisted') {
|
if ($actor->isGroup() && $map['scope'] === 'unlisted') {
|
||||||
$map['scope'] = VisibilityScope::GROUP;
|
$map['scope'] = VisibilityScope::GROUP;
|
||||||
@ -211,8 +211,8 @@ class Note extends Model
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$actor = ActivityPub::getActorByUri($target);
|
$actor = Explorer::getOneFromUri($target);
|
||||||
$object_mentions_ids[$actor->getId()] = $target;
|
$attention_ids[$actor->getId()] = $target;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
|
Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]);
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ class PinnedNotes extends Plugin
|
|||||||
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
|
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
|
||||||
{
|
{
|
||||||
if ($type_name === 'Person') {
|
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_args = ['id' => $actor->getId()];
|
||||||
$router_type = Router::ABSOLUTE_URL;
|
$router_type = Router::ABSOLUTE_URL;
|
||||||
$action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);
|
$action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);
|
||||||
|
@ -254,7 +254,7 @@ class WebMonetization extends Plugin
|
|||||||
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
|
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
|
||||||
{
|
{
|
||||||
if ($type_name === 'Person') {
|
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);
|
$wallet = DB::findOneBy(Wallet::class, ['actor_id' => $actor->getId()], return_null: true);
|
||||||
|
|
||||||
if (!\is_null($address = $wallet?->getAddress())) {
|
if (!\is_null($address = $wallet?->getAddress())) {
|
||||||
|
Loading…
Reference in New Issue
Block a user