[COMPONENT][FreeNetwork] Add to Search the query expression

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-21 04:53:12 +00:00
parent c380cbd846
commit 57a07ef74f
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 57 additions and 7 deletions

View File

@ -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[] = [

View File

@ -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)
{

View File

@ -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,