[TOOLS] Run CS-fixer on all files

This commit is contained in:
2021-12-26 09:48:16 +00:00
committed by Diogo Peralta Cordeiro
parent 5e42723624
commit ec28f23025
66 changed files with 494 additions and 579 deletions

View File

@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
@@ -24,6 +24,7 @@ declare(strict_types=1);
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
@@ -63,29 +64,26 @@ class Activity extends Model
* Create an Entity from an ActivityStreams 2.0 JSON string
* This will persist new GSActivities, GSObjects, and APActivity
*
* @param string|AbstractObject $json
* @param array $options
* @return ActivitypubActivity
* @throws NoSuchActorException
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function fromJson(string|AbstractObject $json, array $options = []): ActivitypubActivity
{
$type_activity = is_string($json) ? self::jsonToType($json) : $json;
$type_activity = \is_string($json) ? self::jsonToType($json) : $json;
// Ditch known activities
$ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]);
if (!is_null($ap_act)) {
if (!\is_null($ap_act)) {
return $ap_act;
}
// Find Actor and Object
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
$actor = ActivityPub::getActorByUri($type_activity->get('actor'));
$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);
} else { // Encapsulated, if we have it locally, prefer it
$type_object = ActivityPub::getObjectByUri($type_object->get('id'), try_online: false) ?? $type_object;
@@ -118,20 +116,20 @@ class Activity extends Model
}
// Store Activity
$act = GSActivity::create([
'actor_id' => $actor->getId(),
'verb' => 'create',
'actor_id' => $actor->getId(),
'verb' => 'create',
'object_type' => 'note',
'object_id' => $note->getId(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'source' => 'ActivityPub',
'object_id' => $note->getId(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'source' => 'ActivityPub',
]);
DB::persist($act);
// Store ActivityPub Activity
$ap_act = ActivitypubActivity::create([
'activity_id' => $act->getId(),
'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);
}
@@ -141,9 +139,6 @@ class Activity extends Model
/**
* Get a JSON
*
* @param mixed $object
* @param int|null $options
* @return string
* @throws ClientException
*/
public static function toJson(mixed $object, ?int $options = null): string
@@ -151,24 +146,24 @@ class Activity extends Model
if ($object::class !== 'App\Entity\Activity') {
throw new InvalidArgumentException('First argument type is Activity');
}
$gs_verb_to_activity_stream_two_verb = null;
if (Event::handle('GSVerbToActivityStreamsTwoActivityType', [($verb = $object->getVerb()), &$gs_verb_to_activity_stream_two_verb]) === Event::next) {
$gs_verb_to_activity_stream_two_verb = match ($verb) {
'create' => 'Create',
'undo' => 'Undo',
default => throw new ClientException('Invalid verb'),
};
}
$gs_verb_to_activity_stream_two_verb = null;
if (Event::handle('GSVerbToActivityStreamsTwoActivityType', [($verb = $object->getVerb()), &$gs_verb_to_activity_stream_two_verb]) === Event::next) {
$gs_verb_to_activity_stream_two_verb = match ($verb) {
'create' => 'Create',
'undo' => 'Undo',
default => throw new ClientException('Invalid verb'),
};
}
$attr = [
'type' => $gs_verb_to_activity_stream_two_verb,
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('activity_view', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'type' => $gs_verb_to_activity_stream_two_verb,
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('activity_view', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
'cc' => ['https://www.w3.org/ns/activitystreams#Public'],
'actor' => $object->getActor()->getUri(Router::ABSOLUTE_URL),
'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
'cc' => ['https://www.w3.org/ns/activitystreams#Public'],
];
$attr['object'] = ($attr['type'] === 'Create') ? self::jsonToType(Model::toJson($object->getObject())) : ActivityPub::getUriByObject($object->getObject());

View File

@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
@@ -24,6 +24,7 @@ declare(strict_types=1);
*
* @package GNUsocial
* @category ActivityPub
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
@@ -60,29 +61,25 @@ use Plugin\ActivityPub\Util\Model;
*/
class Actor extends Model
{
/**
* Create an Entity from an ActivityStreams 2.0 JSON string
* This will persist a new GSActor, ActivityPubRSA, and ActivityPubActor
*
* @param string|AbstractObject $json
* @param array $options
* @return ActivitypubActor
* @throws Exception
*/
public static function fromJson(string|AbstractObject $json, array $options = []): ActivitypubActor
{
$person = is_string($json) ? self::jsonToType($json) : $json;
$person = \is_string($json) ? self::jsonToType($json) : $json;
// Actor
$actor_map = [
'nickname' => $person->get('preferredUsername'),
'fullname' => !empty($person->get('name')) ? $person->get('name') : null,
'created' => new DateTime($person->get('published') ?? 'now'),
'bio' => $person->get('summary'),
'created' => new DateTime($person->get('published') ?? 'now'),
'bio' => $person->get('summary'),
'is_local' => false,
'type' => GSActor::PERSON,
'roles' => UserRoles::USER,
'type' => GSActor::PERSON,
'roles' => UserRoles::USER,
'modified' => new DateTime(),
];
@@ -99,11 +96,11 @@ class Actor extends Model
// ActivityPub Actor
$ap_actor = ActivitypubActor::create([
'inbox_uri' => $person->get('inbox'),
'inbox_uri' => $person->get('inbox'),
'inbox_shared_uri' => ($person->has('endpoints') && isset($person->get('endpoints')['sharedInbox'])) ? $person->get('endpoints')['sharedInbox'] : null,
'uri' => $person->get('id'),
'actor_id' => $actor->getId(),
'url' => $person->get('url') ?? null,
'uri' => $person->get('id'),
'actor_id' => $actor->getId(),
'url' => $person->get('url') ?? null,
], $options['objects']['ActivitypubActor'] ?? null);
if (!isset($options['objects']['ActivitypubActor'])) {
@@ -112,7 +109,7 @@ class Actor extends Model
// Public Key
$apRSA = ActivitypubRsa::create([
'actor_id' => $actor->getID(),
'actor_id' => $actor->getID(),
'public_key' => ($person->has('publicKey') && isset($person->get('publicKey')['publicKeyPem'])) ? $person->get('publicKey')['publicKeyPem'] : null,
], $options['objects']['ActivitypubRsa'] ?? null);
@@ -125,8 +122,8 @@ class Actor extends Model
try {
// Retrieve media
$get_response = HTTPClient::get($person->get('icon')->get('url'));
$media = $get_response->getContent();
$mimetype = $get_response->getHeaders()['content-type'][0] ?? null;
$media = $get_response->getContent();
$mimetype = $get_response->getHeaders()['content-type'][0] ?? null;
unset($get_response);
// Only handle if it is an image
@@ -168,9 +165,8 @@ class Actor extends Model
/**
* Get a JSON
*
* @param mixed $object
* @param int|null $options PHP JSON options
* @return string
* @param null|int $options PHP JSON options
*
* @throws ServerException
*/
public static function toJson(mixed $object, ?int $options = null): string
@@ -178,41 +174,41 @@ class Actor extends Model
if ($object::class !== 'App\Entity\Actor') {
throw new InvalidArgumentException('First argument type is Actor');
}
$rsa = ActivitypubRsa::getByActor($object);
$rsa = ActivitypubRsa::getByActor($object);
$public_key = $rsa->getPublicKey();
$uri = null;
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Person',
'id' => $object->getUri(Router::ABSOLUTE_URL),
'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
$uri = null;
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Person',
'id' => $object->getUri(Router::ABSOLUTE_URL),
'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL),
'following' => Router::url('actor_subscriptions_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'followers' => Router::url('actor_subscribers_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'liked' => Router::url('favourites_view_by_actor_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
'liked' => Router::url('favourites_view_by_actor_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
//'streams' =>
'preferredUsername' => $object->getNickname(),
'publicKey' => [
'id' => $uri . "#public-key",
'owner' => $uri,
'publicKeyPem' => $public_key
'publicKey' => [
'id' => $uri . '#public-key',
'owner' => $uri,
'publicKeyPem' => $public_key,
],
'name' => $object->getFullname(),
'location' => $object->getLocation(),
'name' => $object->getFullname(),
'location' => $object->getLocation(),
'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'summary' => $object->getBio(),
'summary' => $object->getBio(),
//'tag' => $object->getSelfTags(),
'updated' => $object->getModified()->format(DateTimeInterface::RFC3339),
'url' => $object->getUrl(Router::ABSOLUTE_URL),
'url' => $object->getUrl(Router::ABSOLUTE_URL),
];
// Avatar
try {
$avatar = Avatar::getAvatar($object->getId());
$avatar = Avatar::getAvatar($object->getId());
$attr['icon'] = $attr['image'] = [
'type' => 'Image',
'type' => 'Image',
'mediaType' => $avatar->getAttachment()->getMimetype(),
'url' => $avatar->getUrl(type: Router::ABSOLUTE_URL),
'url' => $avatar->getUrl(type: Router::ABSOLUTE_URL),
];
} catch (Exception) {
// No icon for this actor
@@ -222,4 +218,4 @@ class Actor extends Model
Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]);
return $type->toJson($options);
}
}
}

View File

@@ -39,11 +39,10 @@ use App\Core\DB\DB;
use App\Core\Event;
use App\Core\GSFile;
use App\Core\HTTPClient;
use App\Core\VisibilityScope;
use Component\Language\Entity\Language;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Core\VisibilityScope;
use App\Entity\Note as GSNote;
use App\Entity\NoteTag;
use App\Util\Common;
@@ -56,11 +55,13 @@ use App\Util\TemporaryFile;
use Component\Attachment\Entity\ActorToAttachment;
use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation;
use Component\Language\Entity\Language;
use Component\Tag\Tag;
use DateTime;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use const PHP_URL_HOST;
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubObject;
use Plugin\ActivityPub\Util\Model;
@@ -68,10 +69,6 @@ use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use function array_key_exists;
use function is_null;
use function is_string;
use const PHP_URL_HOST;
/**
* This class handles translation between JSON and GSNotes
@@ -98,7 +95,7 @@ class Note extends Model
{
$handleInReplyTo = function (AbstractObject|string $type_note): ?int {
try {
$parent_note = is_null($type_note->get('inReplyTo')) ? null : ActivityPub::getObjectByUri($type_note->get('inReplyTo'), try_online: true);
$parent_note = \is_null($type_note->get('inReplyTo')) ? null : ActivityPub::getObjectByUri($type_note->get('inReplyTo'), try_online: true);
if ($parent_note instanceof GSNote) {
return $parent_note->getId();
} elseif ($parent_note instanceof Type\AbstractObject && $parent_note->get('type') === 'Note') {
@@ -116,13 +113,13 @@ class Note extends Model
};
$source = $options['source'] ?? 'ActivityPub';
$type_note = is_string($json) ? self::jsonToType($json) : $json;
$type_note = \is_string($json) ? self::jsonToType($json) : $json;
$actor = null;
$actor_id = null;
if ($json instanceof AbstractObject
&& array_key_exists('test_authority', $options)
&& \array_key_exists('test_authority', $options)
&& $options['test_authority']
&& array_key_exists('actor_uri', $options)
&& \array_key_exists('actor_uri', $options)
) {
$actor_uri = $options['actor_uri'];
if ($actor_uri !== $type_note->get('attributedTo')) {
@@ -135,7 +132,7 @@ class Note extends Model
}
}
if (is_null($actor_id)) {
if (\is_null($actor_id)) {
$actor = ActivityPub::getActorByUri($type_note->get('attributedTo'));
$actor_id = $actor->getId();
}
@@ -164,14 +161,14 @@ class Note extends Model
]);
}
if (!is_null($map['language_id'])) {
if (!\is_null($map['language_id'])) {
$map['language_id'] = Language::getByLocale($map['language_id'])->getId();
} else {
$map['language_id'] = null;
}
// Scope
if (in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('to'))) {
if (\in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('to'))) {
// Public: Visible for all, shown in public feeds
$map['scope'] = VisibilityScope::PUBLIC;
} elseif (\in_array('https://www.w3.org/ns/activitystreams#Public', $type_note->get('cc'))) {
@@ -258,7 +255,7 @@ class Note extends Model
case 'Hashtag':
$match = ltrim($ap_tag->get('name'), '#');
$tag = Tag::ensureValid($match);
$canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale());
$canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, \is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale());
DB::persist(NoteTag::create([
'tag' => $tag,
'canonical' => $canonical_tag,
@@ -323,7 +320,7 @@ class Note extends Model
'content' => $object->getRendered(),
'attachment' => [],
'tag' => [],
'inReplyTo' => is_null($object->getReplyTo()) ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())),
'inReplyTo' => \is_null($object->getReplyTo()) ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())),
'inConversation' => $object->getConversationUri(),
'directMessage' => $object->getScope() === VisibilityScope::MESSAGE,
];