diff --git a/plugins/ActivityPub/Util/Model/AS2ToEntity/AS2ToEntity.php b/plugins/ActivityPub/Util/Model/AS2ToEntity/AS2ToEntity.php index a40f2691fe..01531711b3 100644 --- a/plugins/ActivityPub/Util/Model/AS2ToEntity/AS2ToEntity.php +++ b/plugins/ActivityPub/Util/Model/AS2ToEntity/AS2ToEntity.php @@ -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'), }; diff --git a/plugins/ActivityPub/Util/Model/EntityToType/ActivityToType.php b/plugins/ActivityPub/Util/Model/EntityToType/ActivityToType.php new file mode 100644 index 0000000000..bc9f249c66 --- /dev/null +++ b/plugins/ActivityPub/Util/Model/EntityToType/ActivityToType.php @@ -0,0 +1,41 @@ + '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); + } +} diff --git a/plugins/ActivityPub/Util/Model/EntityToType/EntityToType.php b/plugins/ActivityPub/Util/Model/EntityToType/EntityToType.php index f2c6e0ef0d..df4f53cc8b 100644 --- a/plugins/ActivityPub/Util/Model/EntityToType/EntityToType.php +++ b/plugins/ActivityPub/Util/Model/EntityToType/EntityToType.php @@ -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 = [ diff --git a/plugins/ActivityPub/Util/Model/EntityToType/NoteToType.php b/plugins/ActivityPub/Util/Model/EntityToType/NoteToType.php index 0e2488a8da..c2553bf4cc 100644 --- a/plugins/ActivityPub/Util/Model/EntityToType/NoteToType.php +++ b/plugins/ActivityPub/Util/Model/EntityToType/NoteToType.php @@ -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()), diff --git a/src/Controller/Activity.php b/src/Controller/Activity.php new file mode 100644 index 0000000000..4d5c8d637b --- /dev/null +++ b/src/Controller/Activity.php @@ -0,0 +1,54 @@ +. + +// }}} + +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]); + } +} diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index 77beaf0cf6..401f9b0bfa 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -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()]); } /** diff --git a/src/Routes/Activity.php b/src/Routes/Activity.php new file mode 100644 index 0000000000..86dc5d230b --- /dev/null +++ b/src/Routes/Activity.php @@ -0,0 +1,47 @@ +. + +// }}} + +/** + * 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']); + } +}