[ActivityPub] Add ActivityToType

Minor bug fixes
This commit is contained in:
Diogo Peralta Cordeiro 2021-11-30 00:46:29 +00:00
parent 4ddd00a091
commit df3fbbc9e7
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
7 changed files with 151 additions and 7 deletions

View File

@ -25,9 +25,9 @@ abstract class AS2ToEntity
};
}
public static function activity_stream_two_object_type_to_gs_table($verb)
public static function activity_stream_two_object_type_to_gs_table($object)
{
return match ($verb) {
return match ($object) {
'Note' => 'note',
default => throw new ClientException('Invalid verb'),
};

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Plugin\ActivityPub\Util\Model\EntityToType;
use App\Core\Router\Router;
use App\Entity\Activity;
use App\Util\Exception\ClientException;
use DateTimeInterface;
use Exception;
use Plugin\ActivityPub\Util\Type;
class ActivityToType
{
public static function gs_verb_to_activity_stream_two_verb($verb)
{
return match ($verb) {
'create' => 'Create',
default => throw new ClientException('Invalid verb'),
};
}
/**
* @throws Exception
*/
public static function translate(Activity $activity): Type\Core\Activity
{
$attr = [
'type' => self::gs_verb_to_activity_stream_two_verb($activity->getVerb()),
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('activity_view', ['id' => $activity->getId()], Router::ABSOLUTE_URL),
'published' => $activity->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $activity->getActor()->getUri(Router::ABSOLUTE_URL),
//'to' => $to,
//'cc' => $cc,
'object' => $activity->getObject()->getUrl(),
];
return Type::create($attr);
}
}

View File

@ -11,12 +11,15 @@ use Plugin\ActivityPub\Util\Type;
abstract class EntityToType
{
/**
* @return Type
* @throws Exception
*/
public static function translate(Entity $entity): Type
public static function translate(Entity $entity): mixed
{
switch ($entity::class) {
case 'Note':
case 'App\Entity\Activity':
return ActivityToType::translate($entity);
case 'App\Entity\Note':
return NoteToType::translate($entity);
default:
$map = [

View File

@ -17,12 +17,11 @@ class NoteToType
*/
public static function translate(Note $note): Type\Extended\Object\Note
{
$attributedTo = null;
$attr = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL),
'published' => $note->getCreated()->format(DateTimeInterface::RFC3339),
'attributedTo' => $attributedTo,
'attributedTo' => $note->getActor()->getUri(Router::ABSOLUTE_URL),
//'to' => $to,
//'cc' => $cc,
'content' => json_encode($note->getRendered()),

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Util\Exception\ClientException;
use Symfony\Component\HttpFoundation\Request;
class Activity extends Controller
{
/**
* Generic function that handles getting a representation for a note
*/
private function activity(int $id, callable $handle)
{
$activity = DB::findOneBy('activity', ['id' => $id]);
if (empty($activity)) {
throw new ClientException(_m('No such activity.'), 404);
} else {
return $handle($activity);
}
}
/**
* The page where the note and it's info is shown
*/
public function ActivityShow(Request $request, int $id)
{
return $this->activity($id, fn ($activity) => ['_template' => '/cards/activity/view.html.twig', 'activity' => $activity]);
}
}

View File

@ -153,7 +153,7 @@ class Activity extends Entity
public function getObject(): mixed
{
return DB::findBy($this->getObjectType(), ['id' => $this->getObjectId()]);
return DB::findOneBy($this->getObjectType(), ['id' => $this->getObjectId()]);
}
/**

47
src/Routes/Activity.php Normal file
View File

@ -0,0 +1,47 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Define social's Activity routes
*
* @package GNUsocial
* @category Router
*
* @author Diogo 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
*/
namespace App\Routes;
use App\Controller as C;
use App\Core\Router\RouteLoader;
abstract class Activity
{
public const LOAD_ORDER = 11;
public static function load(RouteLoader $r): void
{
$r->connect('activity_view', '/activity/{id<\d+>}', [C\Activity::class, 'ActivityShow']);
}
}