[PLUGIN][ActivityPub] Fix typo in getObjectByUri

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-24 01:58:41 +00:00
parent 7407028891
commit e3efd25b43
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 97 additions and 138 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @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
@ -48,6 +49,8 @@ use App\Util\Nickname;
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\Entity\ActivitypubActivity;
use Plugin\ActivityPub\Entity\ActivitypubActor;
@ -59,6 +62,7 @@ use Plugin\ActivityPub\Util\Response\NoteResponse;
use Plugin\ActivityPub\Util\TypeResponse;
use Plugin\ActivityPub\Util\Validator\contentLangModelValidator;
use Plugin\ActivityPub\Util\Validator\manuallyApprovesFollowersModelValidator;
use const PREG_SET_ORDER;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -66,11 +70,6 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use XML_XRD;
use XML_XRD_Element_Link;
use function count;
use function is_null;
use const PHP_URL_HOST;
use const PREG_SET_ORDER;
use InvalidArgumentException;
/**
* Adds ActivityPub support to GNU social when enabled
@ -135,11 +134,6 @@ class ActivityPub extends Plugin
/**
* Fill Actor->getUrl() calls with correct URL coming from ActivityPub
*
* @param Actor $actor
* @param int $type
* @param string|null $url
* @return bool
*/
public function onStartGetActorUri(Actor $actor, int $type, ?string &$url): bool
{
@ -147,7 +141,7 @@ class ActivityPub extends Plugin
// Is remote?
!$actor->getIsLocal()
// Is in ActivityPub?
&& !is_null($ap_actor = ActivitypubActor::getByPK(['actor_id' => $actor->getId()]))
&& !\is_null($ap_actor = ActivitypubActor::getByPK(['actor_id' => $actor->getId()]))
// We can only provide a full URL (anything else wouldn't make sense)
&& $type === Router::ABSOLUTE_URL
) {
@ -165,7 +159,7 @@ class ActivityPub extends Plugin
*/
public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool
{
if (count(array_intersect(self::$accept_headers, $accept_header)) === 0) {
if (\count(array_intersect(self::$accept_headers, $accept_header)) === 0) {
return Event::next;
}
switch ($route) {
@ -186,10 +180,6 @@ class ActivityPub extends Plugin
/**
* Add ActivityStreams 2 Extensions
*
* @param string $type_name
* @param array $validators
* @return bool
*/
public function onActivityPubValidateActivityStreamsTwoData(string $type_name, array &$validators): bool
{
@ -208,9 +198,6 @@ class ActivityPub extends Plugin
/**
* Let FreeNetwork Component know we exist and which class to use to call the freeNetworkDistribute method
*
* @param array $protocols
* @return bool
*/
public function onAddFreeNetworkProtocol(array &$protocols): bool
{
@ -221,12 +208,6 @@ class ActivityPub extends Plugin
/**
* The FreeNetwork component will call this function to distribute this instance's activities
*
* @param Actor $sender
* @param Activity $activity
* @param array $targets
* @param string|null $reason
* @param array $delivered
* @return bool
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
@ -237,7 +218,7 @@ class ActivityPub extends Plugin
$to_addr = [];
foreach ($targets as $actor) {
if (FreeNetworkActorProtocol::canIActor('activitypub', $actor->getId())) {
if (is_null($ap_target = ActivitypubActor::getByPK(['actor_id' => $actor->getId()]))) {
if (\is_null($ap_target = ActivitypubActor::getByPK(['actor_id' => $actor->getId()]))) {
continue;
}
$to_addr[$ap_target->getInboxSharedUri() ?? $ap_target->getInboxUri()][] = $actor;
@ -285,11 +266,6 @@ class ActivityPub extends Plugin
/**
* Internal tool to sign and send activities out
*
* @param Actor $sender
* @param string $json_activity
* @param string $inbox
* @param string $method
* @return ResponseInterface
* @throws Exception
*/
private static function postman(Actor $sender, string $json_activity, string $inbox, string $method = 'post'): ResponseInterface
@ -308,10 +284,6 @@ class ActivityPub extends Plugin
/**
* Add activity+json mimetype to WebFinger
*
* @param XML_XRD $xrd
* @param Actor $object
* @return bool
*/
public function onEndWebFingerProfileLinks(XML_XRD $xrd, Actor $object): bool
{
@ -328,10 +300,6 @@ class ActivityPub extends Plugin
/**
* When FreeNetwork component asks us to help with identifying Actors from XRDs
*
* @param XML_XRD $xrd
* @param Actor|null $actor
* @return bool
*/
public function onFreeNetworkFoundXrd(XML_XRD $xrd, ?Actor &$actor = null): bool
{
@ -341,7 +309,7 @@ class ActivityPub extends Plugin
$addr = Discovery::normalize($alias);
}
}
if (is_null($addr)) {
if (\is_null($addr)) {
return Event::next;
} else {
if (!FreeNetworkActorProtocol::canIAddr('activitypub', $addr)) {
@ -363,10 +331,6 @@ class ActivityPub extends Plugin
/**
* When FreeNetwork component asks us to help with identifying Actors from URIs
*
* @param string $target
* @param Actor|null $actor
* @return bool
*/
public function onFreeNetworkFindMentions(string $target, ?Actor &$actor = null): bool
{
@ -386,7 +350,6 @@ class ActivityPub extends Plugin
}
/**
* @param mixed $object
* @return string got from URI
*/
public static function getUriByObject(mixed $object): string
@ -417,13 +380,12 @@ class ActivityPub extends Plugin
* Get a Note from ActivityPub URI, if it doesn't exist, attempt to fetch it
* This should only be necessary internally.
*
* @param string $resource
* @param bool $try_online
* @return null|Note|mixed got from URI
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return null|mixed|Note got from URI
*/
public static function getObjectByUri(string $resource, bool $try_online = true)
{
@ -445,7 +407,7 @@ class ActivityPub extends Plugin
$resource_parts = parse_url($resource);
// TODO: Use URLMatcher
if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
$local_note = DB::find('note', ['url' => $resource]);
$local_note = DB::findOneBy('note', ['url' => $resource]);
if ($local_note instanceof Note) {
return $local_note;
}
@ -454,14 +416,14 @@ class ActivityPub extends Plugin
// Try remote
if (!$try_online) {
return null;
return;
}
$response = HTTPClient::get($resource, ['headers' => ActivityPub::HTTP_CLIENT_HEADERS]);
$response = HTTPClient::get($resource, ['headers' => self::HTTP_CLIENT_HEADERS]);
// If it was deleted
if ($response->getStatusCode() == 410) {
//$obj = Type::create('Tombstone', ['id' => $resource]);
return null;
return;
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
throw new Exception('Non Ok Status Code for given Object id.');
} else {
@ -473,9 +435,9 @@ class ActivityPub extends Plugin
* Get an Actor from ActivityPub URI, if it doesn't exist, attempt to fetch it
* This should only be necessary internally.
*
* @param string $resource
* @return Actor got from URI
* @throws NoSuchActorException
*
* @return Actor got from URI
*/
public static function getActorByUri(string $resource): Actor
{

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @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
@ -33,6 +34,7 @@ namespace Plugin\ActivityPub\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Actor;
@ -40,15 +42,13 @@ use App\Util\Exception\ClientException;
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
use Component\FreeNetwork\Util\Discovery;
use Exception;
use const PHP_URL_HOST;
use Plugin\ActivityPub\Entity\ActivitypubActor;
use Plugin\ActivityPub\Entity\ActivitypubRsa;
use Plugin\ActivityPub\Util\Explorer;
use Plugin\ActivityPub\Util\HTTPSignature;
use Plugin\ActivityPub\Util\Model;
use Plugin\ActivityPub\Util\TypeResponse;
use function App\Core\I18n\_m;
use function is_null;
use const PHP_URL_HOST;
/**
* ActivityPub Inbox Handler
@ -69,7 +69,7 @@ class Inbox extends Controller
};
$path = Router::url('activitypub_inbox', type: Router::ABSOLUTE_PATH);
if (!is_null($gsactor_id)) {
if (!\is_null($gsactor_id)) {
try {
$user = DB::findOneBy('local_user', ['id' => $gsactor_id]);
$path = Router::url('activitypub_actor_inbox', ['gsactor_id' => $user->getId()], type: Router::ABSOLUTE_PATH);
@ -121,8 +121,8 @@ class Inbox extends Controller
$signatureData = HTTPSignature::parseSignatureHeader($headers['signature']);
Log::debug('ActivityPub Inbox: HTTP Signature Data: ' . print_r($signatureData, true));
if (isset($signatureData['error'])) {
Log::debug('ActivityPub Inbox: HTTP Signature: ' . json_encode($signatureData, JSON_PRETTY_PRINT));
return $error(json_encode($signatureData, JSON_PRETTY_PRINT));
Log::debug('ActivityPub Inbox: HTTP Signature: ' . json_encode($signatureData, \JSON_PRETTY_PRINT));
return $error(json_encode($signatureData, \JSON_PRETTY_PRINT));
}
[$verified, /*$headers*/] = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body);
@ -131,7 +131,7 @@ class Inbox extends Controller
if ($verified !== 1) {
try {
$res = Explorer::get_remote_user_activity($ap_actor->getUri());
if (is_null($res)) {
if (\is_null($res)) {
return $error('Invalid remote actor (null response).');
}
} catch (Exception $e) {
@ -153,7 +153,7 @@ class Inbox extends Controller
}
// HTTP signature checked out, make sure the "actor" of the activity matches that of the signature
Log::debug('ActivityPub Inbox: HTTP Signature: Authorized request. Will now start the inbox handler.');
Log::debug('ActivityPub Inbox: HTTP Signature: Authorised request. Will now start the inbox handler.');
// TODO: Check if Actor has authority over payload
@ -162,7 +162,7 @@ class Inbox extends Controller
FreeNetworkActorProtocol::protocolSucceeded(
'activitypub',
$ap_actor->getActorId(),
Discovery::normalize($actor->getNickname() . '@' . parse_url($ap_actor->getInboxUri(), PHP_URL_HOST))
Discovery::normalize($actor->getNickname() . '@' . parse_url($ap_actor->getInboxUri(), PHP_URL_HOST)),
);
DB::flush();
dd($ap_act, $act = $ap_act->getActivity(), $act->getActor(), $act->getObject());

View File

@ -25,6 +25,7 @@ namespace Plugin\Favourite;
use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
@ -34,13 +35,10 @@ use App\Entity\Feed;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException;
use App\Util\Nickname;
use DateTime;
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
class Favourite extends NoteHandlerPlugin
{
@ -48,7 +46,7 @@ class Favourite extends NoteHandlerPlugin
{
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
$note_already_favoured = DB::find('favourite', $opts);
if (is_null($note_already_favoured)) {
if (\is_null($note_already_favoured)) {
DB::persist(FavouriteEntity::create($opts));
$act = Activity::create([
'actor_id' => $actor_id,
@ -67,7 +65,7 @@ class Favourite extends NoteHandlerPlugin
public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{
$note_already_favoured = DB::find('favourite', ['note_id' => $note_id, 'actor_id' => $actor_id]);
if (!is_null($note_already_favoured)) {
if (!\is_null($note_already_favoured)) {
DB::remove($note_already_favoured);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
$act = Activity::create([
@ -88,14 +86,11 @@ class Favourite extends NoteHandlerPlugin
* HTML rendering event that adds the favourite form as a note
* action, if a user is logged in
*
* @param Request $request
* @param Note $note
* @param array $actions
* @return bool Event hook
*/
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
{
if (is_null($user = Common::user())) {
if (\is_null($user = Common::user())) {
return Event::next;
}
@ -112,7 +107,7 @@ class Favourite extends NoteHandlerPlugin
$query_string = $request->getQueryString();
// Concatenating get parameter to redirect the user to where he came from
$favourite_action_url .= !is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
$favourite_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
$extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
$favourite_action = [
@ -174,7 +169,7 @@ class Favourite extends NoteHandlerPlugin
private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{
if (!in_array($type_activity->get('type'), ['Like', 'Undo'])) {
if (!\in_array($type_activity->get('type'), ['Like', 'Undo'])) {
return Event::next;
}
if ($type_activity->get('type') === 'Like') { // Favourite
@ -214,14 +209,16 @@ class Favourite extends NoteHandlerPlugin
} else {
$act = self::unfavourNote($note_id, $actor->getId(), source: 'ActivityPub');
}
if (!\is_null($act)) {
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new \DateTime($type_activity->get('published') ?? 'now'),
'modified' => new \DateTime(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
}
return Event::stop;
}