From 57a07ef74f876638df4c9d69031881ad8e7d5987 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Mon, 21 Feb 2022 04:53:12 +0000 Subject: [PATCH] [COMPONENT][FreeNetwork] Add to Search the query expression --- components/FreeNetwork/FreeNetwork.php | 29 +++++++++++++++++++--- plugins/ActivityPub/ActivityPub.php | 31 ++++++++++++++++++++++-- plugins/ActivityPub/Util/Model/Actor.php | 4 +-- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/components/FreeNetwork/FreeNetwork.php b/components/FreeNetwork/FreeNetwork.php index 2798cffabc..ea561c0ad3 100644 --- a/components/FreeNetwork/FreeNetwork.php +++ b/components/FreeNetwork/FreeNetwork.php @@ -25,6 +25,8 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\GSFile; use App\Core\HTTPClient; +use App\Util\Formatting; +use Doctrine\Common\Collections\ExpressionBuilder; use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Modules\Component; @@ -76,6 +78,12 @@ class FreeNetwork extends Component public const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token'; public const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token'; public const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize'; + private static array $protocols = []; + + public function onInitializeComponent() + { + Event::handle('AddFreeNetworkProtocol', [&self::$protocols]); + } public function onAddRoute(RouteLoader $m): bool { @@ -489,10 +497,8 @@ class FreeNetwork extends Component public static function notify(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool { - $protocols = []; - Event::handle('AddFreeNetworkProtocol', [&$protocols]); $delivered = []; - foreach ($protocols as $protocol) { + foreach (self::$protocols as $protocol) { $protocol::freeNetworkDistribute($sender, $activity, $targets, $reason, $delivered); } $failed_targets = array_udiff($targets, $delivered, fn (Actor $a, Actor $b): int => $a->getId() <=> $b->getId()); @@ -510,6 +516,23 @@ class FreeNetwork extends Component return '!' . $nickname . '@' . parse_url($uri, \PHP_URL_HOST); } + /** + * Add fediverse: query expression + * // TODO: adding WebFinger would probably be nice + */ + public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool + { + if (Formatting::startsWith($term, ['fediverse:'])) { + foreach (self::$protocols as $protocol) { + // 10 is strlen of `fediverse:` + if ($protocol::freeNetworkGrabRemote(mb_substr($term, 10))) { + break; + } + } + } + return Event::next; + } + public function onPluginVersion(array &$versions): bool { $versions[] = [ diff --git a/plugins/ActivityPub/ActivityPub.php b/plugins/ActivityPub/ActivityPub.php index cb3080019c..1ddc3aa573 100644 --- a/plugins/ActivityPub/ActivityPub.php +++ b/plugins/ActivityPub/ActivityPub.php @@ -48,7 +48,6 @@ use App\Util\Common; use App\Util\Exception\BugFoundException; use App\Util\Exception\NoSuchActorException; use App\Util\Nickname; -use Codeception\Coverage\Subscriber\Local; use Component\Collection\Util\Controller\OrderedCollection; use Component\FreeNetwork\Entity\FreeNetworkActorProtocol; use Component\FreeNetwork\Util\Discovery; @@ -246,6 +245,34 @@ class ActivityPub extends Plugin return Event::next; } + /** + * The FreeNetwork component will call this function to pull ActivityPub objects by URI + * + * @param string $uri Query + * @return bool true if imported, false otherwise + */ + public static function freeNetworkGrabRemote(string $uri): bool + { + if (Common::isValidHttpUrl($uri)) { + try { + $object = self::getObjectByUri($uri); + if (!\is_null($object)) { + if ($object instanceof Type\AbstractObject) { + if (in_array($object->get('type'), array_keys(Model\Actor::$_as2_actor_type_to_gs_actor_type))) { + DB::wrapInTransaction(fn() => Model\Actor::fromJson($object)); + } else { + DB::wrapInTransaction(fn() => Model\Activity::fromJson($object)); + } + } + return true; + } + } catch (\Exception|\Throwable) { + // May be invalid input, we can safely ignore in this case + } + } + return false; + } + /** * The FreeNetwork component will call this function to distribute this instance's activities * @@ -440,7 +467,7 @@ class ActivityPub extends Plugin * @throws ServerExceptionInterface * @throws TransportExceptionInterface * - * @return null|mixed|Note got from URI + * @return null|mixed|Note|Actor got from URI */ public static function getObjectByUri(string $resource, bool $try_online = true) { diff --git a/plugins/ActivityPub/Util/Model/Actor.php b/plugins/ActivityPub/Util/Model/Actor.php index 80c20f0adf..fe77afbccf 100644 --- a/plugins/ActivityPub/Util/Model/Actor.php +++ b/plugins/ActivityPub/Util/Model/Actor.php @@ -62,12 +62,12 @@ use Plugin\ActivityPub\Util\Model; */ class Actor extends Model { - private static array $_gs_actor_type_to_as2_actor_type = [ + public static array $_gs_actor_type_to_as2_actor_type = [ GSActor::PERSON => 'Person', GSActor::GROUP => 'Group', GSActor::BOT => 'Application', ]; - private static array $_as2_actor_type_to_gs_actor_type = [ + public static array $_as2_actor_type_to_gs_actor_type = [ 'Person' => GSActor::PERSON, 'Group' => GSActor::GROUP, 'Organization' => GSActor::GROUP,