Skip to content

Commit

Permalink
Fix/outgoing deletes (#795)
Browse files Browse the repository at this point in the history
Co-authored-by: debounced <[email protected]>
  • Loading branch information
BentiGorlich and debounced authored May 31, 2024
1 parent f220062 commit 85a9083
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 250 deletions.
25 changes: 25 additions & 0 deletions migrations/Version20240529115400.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

class Version20240529115400 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove admin as the owner of remote magazines';
}

public function up(Schema $schema): void
{
$this->addSql('DELETE FROM moderator mod WHERE mod.is_owner = true AND EXISTS (SELECT * FROM magazine m WHERE mod.magazine_id = m.id AND m.ap_id IS NOT NULL);');
}

public function down(Schema $schema): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __invoke(Magazine $magazine, Request $request): JsonResponse
}

#[ArrayShape([
'@context' => 'string',
'@context' => 'array',
'type' => 'string',
'id' => 'string',
'totalItems' => 'int',
Expand All @@ -50,7 +50,7 @@ private function getCollectionItems(Magazine $magazine): array
}

return [
'@context' => ActivityPubActivityInterface::CONTEXT_URL,
'@context' => [ActivityPubActivityInterface::CONTEXT_URL],
'type' => 'OrderedCollection',
'id' => $this->urlGenerator->generate('ap_magazine_moderators', ['name' => $magazine->name], UrlGeneratorInterface::ABSOLUTE_URL),
'totalItems' => \sizeof($items),
Expand Down
11 changes: 5 additions & 6 deletions src/Entity/Magazine.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Magazine implements VisibilityInterface, ActivityPubActorInterface, ApiRes
public function __construct(
string $name,
string $title,
User $user,
?User $user,
?string $description,
?string $rules,
bool $isAdult,
Expand All @@ -131,7 +131,9 @@ public function __construct(
$this->badges = new ArrayCollection();
$this->logs = new ArrayCollection();

$this->addModerator(new Moderator($this, $user, null, true, true));
if (null !== $user) {
$this->addModerator(new Moderator($this, $user, null, true, true));
}

$this->createdAtTraitConstruct();
}
Expand Down Expand Up @@ -233,10 +235,7 @@ public function getOwner(): ?User

public function getModeratorCount(): int
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('isOwner', false));

return $this->moderators->matching($criteria)->count();
return $this->moderators->count();
}

public function addEntry(Entry $entry): self
Expand Down
41 changes: 0 additions & 41 deletions src/EventSubscriber/Entry/EntryBeforeDeleteSubscriber.php

This file was deleted.

26 changes: 17 additions & 9 deletions src/EventSubscriber/Entry/EntryDeleteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace App\EventSubscriber\Entry;

use App\Entity\Entry;
use App\Entity\User;
use App\Event\Entry\EntryBeforeDeletedEvent;
use App\Event\Entry\EntryBeforePurgeEvent;
use App\Event\Entry\EntryDeletedEvent;
use App\Message\ActivityPub\Outbox\DeleteMessage;
Expand All @@ -28,6 +31,7 @@ public static function getSubscribedEvents(): array
return [
EntryDeletedEvent::class => 'onEntryDeleted',
EntryBeforePurgeEvent::class => 'onEntryBeforePurge',
EntryBeforeDeletedEvent::class => 'onEntryBeforeDelete',
];
}

Expand All @@ -41,17 +45,21 @@ public function onEntryBeforePurge(EntryBeforePurgeEvent $event): void
$event->entry->magazine->entryCount = $this->entryRepository->countEntriesByMagazine(
$event->entry->magazine
) - 1;
$this->onEntryBeforeDeleteImpl($event->user, $event->entry);
}

$this->bus->dispatch(new EntryDeletedNotificationMessage($event->entry->getId()));
public function onEntryBeforeDelete(EntryBeforeDeletedEvent $event): void
{
$this->onEntryBeforeDeleteImpl($event->user, $event->entry);
}

public function onEntryBeforeDeleteImpl(?User $user, Entry $entry): void
{
$this->bus->dispatch(new EntryDeletedNotificationMessage($entry->getId()));

if (!$event->entry->apId) {
$this->bus->dispatch(
new DeleteMessage(
$this->deleteWrapper->build($event->entry, Uuid::v4()->toRfc4122()),
$event->entry->user->getId(),
$event->entry->magazine->getId()
)
);
if (!$entry->apId || !$entry->magazine->apId || (null !== $user && $entry->magazine->userIsModerator($user))) {
$payload = $this->deleteWrapper->adjustDeletePayload($user, $entry, Uuid::v4()->toRfc4122());
$this->bus->dispatch(new DeleteMessage($payload, $entry->user->getId(), $entry->magazine->getId()));
}
}
}

This file was deleted.

31 changes: 20 additions & 11 deletions src/EventSubscriber/EntryComment/EntryCommentDeleteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace App\EventSubscriber\EntryComment;

use App\Entity\EntryComment;
use App\Entity\User;
use App\Event\EntryComment\EntryCommentBeforeDeletedEvent;
use App\Event\EntryComment\EntryCommentBeforePurgeEvent;
use App\Event\EntryComment\EntryCommentDeletedEvent;
use App\Message\ActivityPub\Outbox\DeleteMessage;
Expand All @@ -19,7 +22,7 @@ class EntryCommentDeleteSubscriber implements EventSubscriberInterface
public function __construct(
private readonly CacheInterface $cache,
private readonly MessageBusInterface $bus,
private readonly DeleteWrapper $deleteWrapper
private readonly DeleteWrapper $deleteWrapper,
) {
}

Expand All @@ -28,6 +31,7 @@ public static function getSubscribedEvents(): array
return [
EntryCommentDeletedEvent::class => 'onEntryCommentDeleted',
EntryCommentBeforePurgeEvent::class => 'onEntryCommentBeforePurge',
EntryCommentBeforeDeletedEvent::class => 'onEntryCommentBeforeDelete',
];
}

Expand All @@ -40,18 +44,23 @@ public function onEntryCommentDeleted(EntryCommentDeletedEvent $event): void

public function onEntryCommentBeforePurge(EntryCommentBeforePurgeEvent $event): void
{
$this->cache->invalidateTags(['entry_comment_'.$event->comment->root?->getId() ?? $event->comment->getId()]);
$this->onEntryCommentBeforeDeleteImpl($event->user, $event->comment);
}

$this->bus->dispatch(new EntryCommentDeletedNotificationMessage($event->comment->getId()));
public function onEntryCommentBeforeDelete(EntryCommentBeforeDeletedEvent $event): void
{
$this->onEntryCommentBeforeDeleteImpl($event->user, $event->comment);
}

public function onEntryCommentBeforeDeleteImpl(?User $user, EntryComment $comment): void
{
$this->cache->invalidateTags(['entry_comment_'.$comment->root?->getId() ?? $comment->getId()]);

$this->bus->dispatch(new EntryCommentDeletedNotificationMessage($comment->getId()));

if (!$event->comment->apId) {
$this->bus->dispatch(
new DeleteMessage(
$this->deleteWrapper->build($event->comment, Uuid::v4()->toRfc4122()),
$event->comment->user->getId(),
$event->comment->magazine->getId()
)
);
if (!$comment->apId || !$comment->magazine->apId || (null !== $user && $comment->magazine->userIsModerator($user))) {
$payload = $this->deleteWrapper->adjustDeletePayload($user, $comment, Uuid::v4()->toRfc4122());
$this->bus->dispatch(new DeleteMessage($payload, $comment->user->getId(), $comment->magazine->getId()));
}
}
}
41 changes: 0 additions & 41 deletions src/EventSubscriber/Post/PostBeforeDeleteSubscriber.php

This file was deleted.

26 changes: 17 additions & 9 deletions src/EventSubscriber/Post/PostDeleteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace App\EventSubscriber\Post;

use App\Entity\Post;
use App\Entity\User;
use App\Event\Post\PostBeforeDeletedEvent;
use App\Event\Post\PostBeforePurgeEvent;
use App\Event\Post\PostDeletedEvent;
use App\Message\ActivityPub\Outbox\DeleteMessage;
Expand All @@ -28,6 +31,7 @@ public static function getSubscribedEvents(): array
return [
PostDeletedEvent::class => 'onPostDeleted',
PostBeforePurgeEvent::class => 'onPostBeforePurge',
PostBeforeDeletedEvent::class => 'onPostBeforeDelete',
];
}

Expand All @@ -39,17 +43,21 @@ public function onPostDeleted(PostDeletedEvent $event)
public function onPostBeforePurge(PostBeforePurgeEvent $event): void
{
$event->post->magazine->postCount = $this->postRepository->countPostsByMagazine($event->post->magazine) - 1;
$this->onPostBeforeDeleteImpl($event->user, $event->post);
}

$this->bus->dispatch(new PostDeletedNotificationMessage($event->post->getId()));
public function onPostBeforeDelete(PostBeforeDeletedEvent $event): void
{
$this->onPostBeforeDeleteImpl($event->user, $event->post);
}

public function onPostBeforeDeleteImpl(?User $user, Post $post): void
{
$this->bus->dispatch(new PostDeletedNotificationMessage($post->getId()));

if (!$event->post->apId) {
$this->bus->dispatch(
new DeleteMessage(
$this->deleteWrapper->build($event->post, Uuid::v4()->toRfc4122()),
$event->post->user->getId(),
$event->post->magazine->getId()
)
);
if (!$post->apId || !$post->magazine->apId || (null !== $user && $post->magazine->userIsModerator($user))) {
$payload = $this->deleteWrapper->adjustDeletePayload($user, $post, Uuid::v4()->toRfc4122());
$this->bus->dispatch(new DeleteMessage($payload, $post->user->getId(), $post->magazine->getId()));
}
}
}
Loading

0 comments on commit 85a9083

Please sign in to comment.