Skip to content

Commit

Permalink
Add moderator updates to the modlog (#799)
Browse files Browse the repository at this point in the history
Co-authored-by: debounced <[email protected]>
  • Loading branch information
BentiGorlich and debounced authored Jun 6, 2024
1 parent d38d2da commit 9389aa2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
30 changes: 30 additions & 0 deletions migrations/Version20240528172429.php
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 Version20240528172429 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add a field to the magazine log table for adding and removing a moderator';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE magazine_log ADD acting_user_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE magazine_log ADD CONSTRAINT FK_87D3D4C53EAD8611 FOREIGN KEY (acting_user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_87D3D4C53EAD8611 ON magazine_log (acting_user_id)');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE magazine_log DROP CONSTRAINT FK_87D3D4C53EAD8611');
$this->addSql('DROP INDEX IDX_87D3D4C53EAD8611');
$this->addSql('ALTER TABLE magazine_log DROP acting_user_id');
}
}
2 changes: 2 additions & 0 deletions src/Entity/MagazineLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
'post_comment_deleted' => MagazineLogPostCommentDeleted::class,
'post_comment_restored' => MagazineLogPostCommentRestored::class,
'ban' => MagazineLogBan::class,
'moderator_add' => MagazineLogModeratorAdd::class,
'moderator_remove' => MagazineLogModeratorRemove::class,
])]
abstract class MagazineLog
{
Expand Down
39 changes: 39 additions & 0 deletions src/Entity/MagazineLogModeratorAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Entity\Contracts\ContentInterface;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;

#[Entity]
class MagazineLogModeratorAdd extends MagazineLog
{
#[ManyToOne(targetEntity: User::class)]
#[JoinColumn(nullable: true, onDelete: 'CASCADE')]
public ?User $actingUser;

public function __construct(Magazine $magazine, User $addedMod, ?User $actingUser)
{
parent::__construct($magazine, $addedMod);
$this->actingUser = $actingUser;
}

public function getSubject(): ContentInterface|null
{
return null;
}

public function clearSubject(): MagazineLog
{
return $this;
}

public function getType(): string
{
return 'log_moderator_add';
}
}
39 changes: 39 additions & 0 deletions src/Entity/MagazineLogModeratorRemove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Entity\Contracts\ContentInterface;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;

#[Entity]
class MagazineLogModeratorRemove extends MagazineLog
{
#[ManyToOne(targetEntity: User::class)]
#[JoinColumn(nullable: true, onDelete: 'CASCADE')]
public ?User $actingUser;

public function __construct(Magazine $magazine, User $addedMod, ?User $actingUser)
{
parent::__construct($magazine, $addedMod);
$this->actingUser = $actingUser;
}

public function getSubject(): ContentInterface|null
{
return null;
}

public function clearSubject(): MagazineLog
{
return $this;
}

public function getType(): string
{
return 'log_moderator_remove';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace App\EventSubscriber\ActivityPub;

use App\Entity\Magazine;
use App\Entity\MagazineLogModeratorAdd;
use App\Entity\MagazineLogModeratorRemove;
use App\Event\Magazine\MagazineModeratorAddedEvent;
use App\Event\Magazine\MagazineModeratorRemovedEvent;
use App\Message\ActivityPub\Outbox\AddMessage;
use App\Message\ActivityPub\Outbox\RemoveMessage;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -21,6 +24,7 @@ public function __construct(
private readonly MessageBusInterface $bus,
private readonly CacheInterface $cache,
private readonly LoggerInterface $logger,
private readonly EntityManagerInterface $entityManager,
) {
}

Expand All @@ -30,6 +34,9 @@ public function onModeratorAdded(MagazineModeratorAddedEvent $event): void
if (!$event->magazine->apId or (null !== $event->addedBy and !$event->addedBy->apId)) {
$this->bus->dispatch(new AddMessage($event->addedBy->getId(), $event->magazine->getId(), $event->user->getId()));
}
$log = new MagazineLogModeratorAdd($event->magazine, $event->user, $event->addedBy);
$this->entityManager->persist($log);
$this->entityManager->flush();
$this->deleteCache($event->magazine);
}

Expand All @@ -39,6 +46,9 @@ public function onModeratorRemoved(MagazineModeratorRemovedEvent $event): void
if (!$event->magazine->apId or (null !== $event->removedBy and !$event->removedBy->apId)) {
$this->bus->dispatch(new RemoveMessage($event->removedBy->getId(), $event->magazine->getId(), $event->user->getId()));
}
$log = new MagazineLogModeratorRemove($event->magazine, $event->user, $event->removedBy);
$this->entityManager->persist($log);
$this->entityManager->flush();
$this->deleteCache($event->magazine);
}

Expand Down
8 changes: 8 additions & 0 deletions templates/modlog/_blocks.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@
{{ component('user_inline', {user: log.user, showAvatar: false}) }} {% if log.meta is same as 'ban' %}{{ 'he_banned'|trans|lower }}{% else %}{{ 'he_unbanned'|trans|lower }}{% endif %} {{ component('user_inline', {user: log.ban.user, showAvatar: false}) }}
{% if showMagazine %} {{ 'in'|trans|lower }} {{ component('magazine_inline', {magazine: log.ban.magazine, showIcon: false }) }}{% endif %}{% if log.ban.reason %} - {{ log.ban.reason }}{% endif %}
{% endblock %}

{% block log_moderator_add %}
{{ component('user_inline', {user: log.actingUser}) }} {{ 'magazine_log_mod_added'|trans }}: {{ component('user_inline', {user: log.user}) }}
{% endblock %}

{% block log_moderator_remove %}
{{ component('user_inline', {user: log.actingUser}) }} {{ 'magazine_log_mod_removed'|trans }}: {{ component('user_inline', {user: log.user}) }}
{% endblock %}
2 changes: 2 additions & 0 deletions translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,5 @@ related_entry: Related
restrict_magazine_creation: Restrict local magazine creation to admins and global mods
sso_show_first: Show SSO first on login and registration pages
continue_with: Continue with
magazine_log_mod_added: has added a moderator
magazine_log_mod_removed: has removed a moderator

0 comments on commit 9389aa2

Please sign in to comment.