-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add events for add/removing moderators
- add handling of adding/removing of moderators (needs testing) - add outgoing messages for adding/removing moderators - add `addedByUser` field to moderator table (update all calls of the moderator creation) - local followers/subscribers should immediately be added to the count now
- Loading branch information
1 parent
d109356
commit 1a388a9
Showing
27 changed files
with
542 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20231112133420 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'add column "added_by_user_id" to moderator table'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE moderator ADD added_by_user_id INT DEFAULT NULL'); | ||
$this->addSql('ALTER TABLE moderator ADD CONSTRAINT FK_6A30B268CA792C6B FOREIGN KEY (added_by_user_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
$this->addSql('CREATE INDEX IDX_6A30B268CA792C6B ON moderator (added_by_user_id)'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE moderator DROP CONSTRAINT FK_6A30B268CA792C6B'); | ||
$this->addSql('DROP INDEX IDX_6A30B268CA792C6B'); | ||
$this->addSql('ALTER TABLE moderator DROP added_by_user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Event\Magazine; | ||
|
||
use App\Entity\Magazine; | ||
use App\Entity\User; | ||
|
||
class MagazineModeratorAddedEvent | ||
{ | ||
public function __construct(public Magazine $magazine, public User $user, public ?User $addedBy) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Event\Magazine; | ||
|
||
use App\Entity\Magazine; | ||
use App\Entity\User; | ||
|
||
class MagazineModeratorRemovedEvent | ||
{ | ||
public function __construct(public Magazine $magazine, public User $user, public ?User $removedBy) | ||
{ | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/EventSubscriber/ActivityPub/MagazineModeratorAddedRemovedSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EventSubscriber\ActivityPub; | ||
|
||
use App\Event\Magazine\MagazineModeratorAddedEvent; | ||
use App\Event\Magazine\MagazineModeratorRemovedEvent; | ||
use App\Message\ActivityPub\Outbox\AddMessage; | ||
use App\Message\ActivityPub\Outbox\RemoveMessage; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
class MagazineModeratorAddedRemovedSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private readonly MessageBusInterface $bus, | ||
) { | ||
} | ||
|
||
public function onModeratorAdded(MagazineModeratorAddedEvent $event): void | ||
{ | ||
// if the magazine is local then we have authority over it, otherwise the addedBy user has to be defined | ||
if (!$event->magazine->apId or null !== $event->addedBy) { | ||
$this->bus->dispatch(new AddMessage($event->addedBy->getId(), $event->magazine->getId(), $event->user->getId())); | ||
} | ||
} | ||
|
||
public function onModeratorRemoved(MagazineModeratorRemovedEvent $event): void | ||
{ | ||
// if the magazine is local then we have authority over it, otherwise the removedBy user has to be defined | ||
if (!$event->magazine->apId or null !== $event->removedBy) { | ||
$this->bus->dispatch(new RemoveMessage($event->removedBy->getId(), $event->magazine->getId(), $event->user->getId())); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
MagazineModeratorAddedEvent::class => 'onModeratorAdded', | ||
MagazineModeratorRemovedEvent::class => 'onModeratorRemoved', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Factory\ActivityPub; | ||
|
||
use App\Entity\Contracts\ActivityPubActivityInterface; | ||
use App\Entity\Magazine; | ||
use App\Entity\User; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class AddRemoveFactory | ||
{ | ||
public function __construct( | ||
private readonly UrlGeneratorInterface $urlGenerator, | ||
) { | ||
} | ||
|
||
public function buildAdd(User $actor, User $added, Magazine $magazine): array | ||
{ | ||
return $this->build($actor, $added, $magazine, 'Add'); | ||
} | ||
|
||
public function buildRemove(User $actor, User $removed, Magazine $magazine): array | ||
{ | ||
return $this->build($actor, $removed, $magazine, 'Remove'); | ||
} | ||
|
||
#[ArrayShape([ | ||
'@context' => 'array', | ||
'id' => 'string', | ||
'actor' => 'string', | ||
'to' => 'array', | ||
'object' => 'string', | ||
'cc' => 'array', | ||
'type' => 'string', | ||
'target' => 'string', | ||
'audience' => 'string', | ||
])] | ||
private function build(User $actor, User $targetUser, Magazine $magazine, string $type): array | ||
{ | ||
$id = Uuid::v4()->toRfc4122(); | ||
|
||
return [ | ||
'@context' => [ActivityPubActivityInterface::CONTEXT_URL, ActivityPubActivityInterface::SECURITY_URL], | ||
'id' => $this->urlGenerator->generate('ap_object', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL), | ||
'actor' => $actor->apId ?? $this->urlGenerator->generate('ap_user', ['username' => $actor->username], UrlGeneratorInterface::ABSOLUTE_URL), | ||
'to' => [ActivityPubActivityInterface::PUBLIC_URL], | ||
'object' => $targetUser->apId ?? $this->urlGenerator->generate('ap_user', ['username' => $targetUser->username], UrlGeneratorInterface::ABSOLUTE_URL), | ||
'cc' => [$magazine->apId ?? $this->urlGenerator->generate('ap_magazine', ['name' => $magazine->name], UrlGeneratorInterface::ABSOLUTE_URL)], | ||
'type' => $type, | ||
'target' => $magazine->apAttributedToUrl ?? $this->urlGenerator->generate('ap_magazine_moderators', ['name' => $magazine->name], UrlGeneratorInterface::ABSOLUTE_URL), | ||
'audience' => $magazine->apId ?? $this->urlGenerator->generate('ap_magazine', ['name' => $magazine->name], UrlGeneratorInterface::ABSOLUTE_URL), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Message\ActivityPub\Inbox; | ||
|
||
use App\Message\Contracts\AsyncApMessageInterface; | ||
|
||
class AddMessage implements AsyncApMessageInterface | ||
{ | ||
public function __construct(public array $payload) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Message\ActivityPub\Inbox; | ||
|
||
use App\Message\Contracts\AsyncApMessageInterface; | ||
|
||
class RemoveMessage implements AsyncApMessageInterface | ||
{ | ||
public function __construct(public array $payload) | ||
{ | ||
} | ||
} |
Oops, something went wrong.