Skip to content

Commit

Permalink
Merge branch 'main' into new/federate-edit
Browse files Browse the repository at this point in the history
  • Loading branch information
BentiGorlich authored Jul 26, 2024
2 parents 410233b + 1fbbef2 commit ae48d1e
Show file tree
Hide file tree
Showing 62 changed files with 783 additions and 92 deletions.
9 changes: 9 additions & 0 deletions src/Exception/EntityNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Exception;

class EntityNotFoundException extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/ActivityPubInboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface ActivityPubInboxInterface
interface ActivityPubInboxInterface extends MessageInterface
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/ActivityPubInboxReceiveInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface ActivityPubInboxReceiveInterface
interface ActivityPubInboxReceiveInterface extends MessageInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface ActivityPubOutboxDeliverInterface
interface ActivityPubOutboxDeliverInterface extends MessageInterface
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/ActivityPubOutboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface ActivityPubOutboxInterface
interface ActivityPubOutboxInterface extends MessageInterface
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/ActivityPubResolveInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface ActivityPubResolveInterface
interface ActivityPubResolveInterface extends MessageInterface
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/AsyncMessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface AsyncMessageInterface
interface AsyncMessageInterface extends MessageInterface
{
}
9 changes: 9 additions & 0 deletions src/Message/Contracts/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Message\Contracts;

interface MessageInterface
{
}
2 changes: 1 addition & 1 deletion src/Message/Contracts/SendConfirmationEmailInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace App\Message\Contracts;

interface SendConfirmationEmailInterface
interface SendConfirmationEmailInterface extends MessageInterface
{
}
27 changes: 19 additions & 8 deletions src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use App\Message\ActivityPub\Inbox\LikeMessage;
use App\Message\ActivityPub\Inbox\RemoveMessage;
use App\Message\ActivityPub\Inbox\UpdateMessage;
use App\Message\Contracts\MessageInterface;
use App\MessageHandler\MbinMessageHandler;
use App\Service\ActivityPub\ApHttpClient;
use App\Service\ActivityPub\SignatureValidator;
use App\Service\ActivityPubManager;
Expand All @@ -29,21 +31,30 @@
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
readonly class ActivityHandler
class ActivityHandler extends MbinMessageHandler
{
public function __construct(
private EntityManagerInterface $entityManager,
private SignatureValidator $signatureValidator,
private SettingsManager $settingsManager,
private MessageBusInterface $bus,
private ActivityPubManager $manager,
private ApHttpClient $apHttpClient,
private LoggerInterface $logger
private readonly EntityManagerInterface $entityManager,
private readonly SignatureValidator $signatureValidator,
private readonly SettingsManager $settingsManager,
private readonly MessageBusInterface $bus,
private readonly ActivityPubManager $manager,
private readonly ApHttpClient $apHttpClient,
private readonly LoggerInterface $logger
) {
parent::__construct($this->entityManager);
}

public function __invoke(ActivityMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof ActivityMessage)) {
throw new \LogicException();
}
$payload = @json_decode($message->payload, true);

if ($message->request && $message->headers) {
Expand Down
15 changes: 14 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/AddHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Entity\User;
use App\Message\ActivityPub\Inbox\AddMessage;
use App\Message\ActivityPub\Inbox\CreateMessage;
use App\Message\Contracts\MessageInterface;
use App\MessageHandler\MbinMessageHandler;
use App\Repository\ApActivityRepository;
use App\Repository\EntryRepository;
use App\Repository\MagazineRepository;
Expand All @@ -18,14 +20,16 @@
use App\Service\EntryManager;
use App\Service\MagazineManager;
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
class AddHandler
class AddHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly ActivityPubManager $activityPubManager,
private readonly ApHttpClient $apHttpClient,
private readonly ApActivityRepository $apActivityRepository,
Expand All @@ -37,10 +41,19 @@ public function __construct(
private readonly EntryManager $entryManager,
private readonly SettingsManager $settingsManager,
) {
parent::__construct($this->entityManager);
}

public function __invoke(AddMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof AddMessage)) {
throw new \LogicException();
}
$payload = $message->payload;
$actor = $this->activityPubManager->findUserActorOrCreateOrThrow($payload['actor']);
$targetMag = $this->magazineRepository->getMagazineFromModeratorsUrl($payload['target']);
Expand Down
13 changes: 12 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\EventSubscriber\VoteHandleSubscriber;
use App\Message\ActivityPub\Inbox\AnnounceMessage;
use App\Message\ActivityPub\Inbox\ChainActivityMessage;
use App\Message\Contracts\MessageInterface;
use App\MessageHandler\MbinMessageHandler;
use App\Service\ActivityPubManager;
use App\Service\VoteManager;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -16,7 +18,7 @@
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
class AnnounceHandler
class AnnounceHandler extends MbinMessageHandler
{
public function __construct(
private readonly ActivityPubManager $activityPubManager,
Expand All @@ -26,10 +28,19 @@ public function __construct(
private readonly VoteHandleSubscriber $voteHandleSubscriber,
private readonly LoggerInterface $logger,
) {
parent::__construct($this->entityManager);
}

public function __invoke(AnnounceMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof AnnounceMessage)) {
throw new \LogicException();
}
$chainDispatchCallback = function (array $object, ?string $adjustedUrl) use ($message) {
if ($adjustedUrl) {
$this->logger->info('got an adjusted url: {url}, using that instead of {old}', ['url' => $adjustedUrl, 'old' => $message->payload['object']['id'] ?? $message->payload['object']]);
Expand Down
22 changes: 20 additions & 2 deletions src/MessageHandler/ActivityPub/Inbox/ChainActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,50 @@
use App\Entity\EntryComment;
use App\Entity\Post;
use App\Entity\PostComment;
use App\Exception\EntityNotFoundException;
use App\Exception\TagBannedException;
use App\Exception\UserBannedException;
use App\Message\ActivityPub\Inbox\AnnounceMessage;
use App\Message\ActivityPub\Inbox\ChainActivityMessage;
use App\Message\ActivityPub\Inbox\DislikeMessage;
use App\Message\ActivityPub\Inbox\LikeMessage;
use App\Message\Contracts\MessageInterface;
use App\MessageHandler\MbinMessageHandler;
use App\Repository\ApActivityRepository;
use App\Service\ActivityPub\ApHttpClient;
use App\Service\ActivityPub\Note;
use App\Service\ActivityPub\Page;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
class ChainActivityHandler
class ChainActivityHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
private readonly ApHttpClient $client,
private readonly MessageBusInterface $bus,
private readonly ApActivityRepository $repository,
private readonly Note $note,
private readonly Page $page
) {
parent::__construct($this->entityManager);
}

public function __invoke(ChainActivityMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof ChainActivityMessage)) {
throw new \LogicException();
}
$this->logger->debug('Got chain activity message: {m}', ['m' => $message]);
if (!$message->chain || 0 === \sizeof($message->chain)) {
return;
Expand Down Expand Up @@ -70,6 +85,9 @@ public function __invoke(ChainActivityMessage $message): void
}
}

/**
* @throws \Exception if there was an unexpected exception
*/
private function retrieveObject(string $apUrl): Entry|EntryComment|Post|PostComment|null
{
try {
Expand Down Expand Up @@ -118,7 +136,7 @@ private function retrieveObject(string $apUrl): Entry|EntryComment|Post|PostComm
$this->logger->error('the user is banned, url: {url}', ['url' => $apUrl]);
} catch (TagBannedException) {
$this->logger->error('one of the used tags is banned, url: {url}', ['url' => $apUrl]);
} catch (\Exception $e) {
} catch (EntityNotFoundException $e) {
$this->logger->error('There was an exception while getting {url}: {ex} - {m}. {o}', ['url' => $apUrl, 'ex' => \get_class($e), 'm' => $e->getMessage(), 'o' => $e]);
}

Expand Down
15 changes: 14 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@
use App\Message\ActivityPub\Inbox\ChainActivityMessage;
use App\Message\ActivityPub\Inbox\CreateMessage;
use App\Message\ActivityPub\Outbox\AnnounceMessage;
use App\Message\Contracts\MessageInterface;
use App\MessageHandler\MbinMessageHandler;
use App\Repository\ApActivityRepository;
use App\Service\ActivityPub\Note;
use App\Service\ActivityPub\Page;
use App\Service\ActivityPubManager;
use App\Service\MessageManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
class CreateHandler
class CreateHandler extends MbinMessageHandler
{
private array $object;
private bool $stickyIt;

public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly Note $note,
private readonly Page $page,
private readonly MessageBusInterface $bus,
Expand All @@ -35,13 +39,22 @@ public function __construct(
private readonly ActivityPubManager $activityPubManager,
private readonly ApActivityRepository $repository
) {
parent::__construct($this->entityManager);
}

/**
* @throws \Exception
*/
public function __invoke(CreateMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof CreateMessage)) {
throw new \LogicException();
}
$this->object = $message->payload;
$this->stickyIt = $message->stickyIt;
$this->logger->debug('Got a CreateMessage of type {t}, {m}', ['t' => $message->payload['type'], 'm' => $message->payload]);
Expand Down
13 changes: 12 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/DeleteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use App\Entity\PostComment;
use App\Entity\User;
use App\Message\ActivityPub\Inbox\DeleteMessage;
use App\Message\Contracts\MessageInterface;
use App\Message\DeleteUserMessage;
use App\MessageHandler\MbinMessageHandler;
use App\Repository\ApActivityRepository;
use App\Repository\UserRepository;
use App\Service\ActivityPubManager;
Expand All @@ -23,7 +25,7 @@
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
class DeleteHandler
class DeleteHandler extends MbinMessageHandler
{
public function __construct(
private readonly MessageBusInterface $bus,
Expand All @@ -36,10 +38,19 @@ public function __construct(
private readonly PostManager $postManager,
private readonly PostCommentManager $postCommentManager
) {
parent::__construct($this->entityManager);
}

public function __invoke(DeleteMessage $message): void
{
$this->workWrapper($message);
}

public function doWork(MessageInterface $message): void
{
if (!($message instanceof DeleteMessage)) {
throw new \LogicException();
}
$actor = $this->activityPubManager->findActorOrCreate($message->payload['actor']);

$id = \is_array($message->payload['object']) ? $message->payload['object']['id'] : $message->payload['object'];
Expand Down
Loading

0 comments on commit ae48d1e

Please sign in to comment.