Skip to content

Commit

Permalink
Better notification handling of mod actions
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Feb 17, 2024
1 parent 599f8e3 commit b084e0b
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 68 deletions.
10 changes: 4 additions & 6 deletions src/Automod/ModAction/BanUser/AbstractBanUserModAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Automod\ModAction\BanUser;

use App\Automod\ModAction\AbstractModAction;
use App\Context\Context;
use App\Entity\BannedUsername;
use App\Entity\InstanceBanRegex;
use App\Enum\FurtherAction;
Expand Down Expand Up @@ -30,11 +31,6 @@
private MessageBusInterface $messageBus;
private BannedUsernameRepository $usernameRepository;

public function getDescription(): ?string
{
return 'user has been banned';
}

public function shouldRun(object $object): bool
{
$author = $this->getAuthor($object);
Expand All @@ -61,7 +57,7 @@ public function shouldRun(object $object): bool
return false;
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
$creator = $this->getAuthor($object);

Expand All @@ -83,6 +79,7 @@ public function takeAction(object $object, array $previousActions = []): Further
continue;
}

$context->addMessage("banned for matching regex `{$rule->getRegex()}`");

if ($object instanceof PostView) {
$this->messageBus->dispatch(new RemovePostMessage($object->post->id), [
Expand All @@ -100,6 +97,7 @@ public function takeAction(object $object, array $previousActions = []): Further
}

if ($banned = $this->findMatchingUsernameRule($creator->name)) {
$context->addMessage("banned for username matching regex `{$banned->getUsername()}`");
$this->messageBus->dispatch(new BanUserMessage(user: $creator, reason: $banned->getReason(), removePosts: $banned->shouldRemoveAll(), removeComments: $banned->shouldRemoveAll()), [
new DispatchAfterCurrentBusStamp(),
]);
Expand Down
10 changes: 3 additions & 7 deletions src/Automod/ModAction/BanUser/BanUserForEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Automod\ModAction\BanUser;

use App\Automod\ModAction\AbstractModAction;
use App\Context\Context;
use App\Dto\Model\LocalUser;
use App\Entity\BannedEmail;
use App\Enum\FurtherAction;
Expand Down Expand Up @@ -48,7 +49,7 @@ private function findMatchingEmailRegex(string $email): ?BannedEmail
}

#[Override]
public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
if ($object->admin) {
return FurtherAction::ShouldAbort;
Expand All @@ -60,13 +61,8 @@ public function takeAction(object $object, array $previousActions = []): Further
$this->messageBus->dispatch(new BanUserMessage(user: $this->api->user()->get($object->personId), reason: $rule->getReason(), removePosts: true, removeComments: true), [
new DispatchAfterCurrentBusStamp(),
]);
$context->addMessage("banned for their email matching regex `{$rule->getRegex()}`");

return FurtherAction::ShouldAbort;
}

#[Override]
public function getDescription(): ?string
{
return 'user has been banned for their email';
}
}
5 changes: 2 additions & 3 deletions src/Automod/ModAction/ModAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Automod\ModAction;

use App\Context\Context;
use App\Dto\Model\LocalUser;
use App\Enum\FurtherAction;
use App\Enum\RunConfiguration;
Expand All @@ -24,11 +25,9 @@ public function shouldRun(object $object): bool;

/**
* @param TObject $object
* @param array<ModAction> $previousActions
*/
public function takeAction(object $object, array $previousActions = []): FurtherAction;
public function takeAction(object $object, Context $context = new Context()): FurtherAction;

public function getRunConfiguration(): RunConfiguration;

public function getDescription(): ?string;
}
53 changes: 31 additions & 22 deletions src/Automod/ModAction/Notification/NotifyOfActionTaken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

use App\Automod\Enum\AutomodPriority;
use App\Automod\ModAction\ModAction;
use App\Context\Context;
use App\Dto\Model\LocalUser;
use App\Enum\FurtherAction;
use App\Enum\RunConfiguration;
use App\Service\InstanceLinkConverter;
use App\Service\Notification\NotificationSender;
use Rikudou\LemmyApi\LemmyApi;
use Rikudou\LemmyApi\Response\Model\Person;
use Rikudou\LemmyApi\Response\View\CommentReportView;
use Rikudou\LemmyApi\Response\View\CommentView;
use Rikudou\LemmyApi\Response\View\PostReportView;
use Rikudou\LemmyApi\Response\View\PostView;
use Rikudou\LemmyApi\Response\View\RegistrationApplicationView;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
* @implements ModAction<CommentView|PostView|Person>
* @implements ModAction<CommentView|PostView|Person|RegistrationApplicationView>
*/
#[AsTaggedItem(priority: AutomodPriority::Notification->value)]
final readonly class NotifyOfActionTaken implements ModAction
Expand All @@ -25,48 +31,56 @@ public function __construct(
private string $instance,
private InstanceLinkConverter $linkConverter,
private NotificationSender $notificationSender,
private LemmyApi $api,
) {
}

public function shouldRun(object $object): bool
{
return ($object instanceof CommentView || $object instanceof PostView || $object instanceof Person)
return ($object instanceof CommentView || $object instanceof PostView || $object instanceof Person || $object instanceof RegistrationApplicationView)
&& $this->notificationSender->hasEnabledChannels()
;
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
if (!count($previousActions)) {
if (!count($context->getMessages())) {
return FurtherAction::CanContinue;
}
$username = "{$object->creator->name}@" . parse_url($object->creator->actorId, PHP_URL_HOST);
$target = null;
$username = null;
if ($object instanceof PostView) {
$target = $this->linkConverter->convertPostLink($object->post);
$username = "{$object->creator->name}@" . parse_url($object->creator->actorId, PHP_URL_HOST);
} elseif ($object instanceof CommentView) {
$target = $this->linkConverter->convertCommentLink($object->comment);
$username = "{$object->creator->name}@" . parse_url($object->creator->actorId, PHP_URL_HOST);
} elseif ($object instanceof Person) {
$target = $this->linkConverter->convertPersonLink($object);
$username = "{$object->name}@" . parse_url($object->actorId, PHP_URL_HOST);
} elseif ($object instanceof RegistrationApplicationView) {
$target = $this->linkConverter->convertPersonLink($object->creator);
$username = "{$object->creator->name}@" . parse_url($object->creator->actorId, PHP_URL_HOST);
} elseif ($object instanceof CommentReportView) {
$target = $this->linkConverter->convertCommentLink($object->comment);
$username = "{$object->commentCreator->name}@" . parse_url($object->commentCreator->actorId, PHP_URL_HOST);
} elseif ($object instanceof PostReportView) {
$target = $this->linkConverter->convertPostLink($object->post);
$username = "{$object->postCreator->name}@" . parse_url($object->postCreator->actorId, PHP_URL_HOST);
} elseif ($object instanceof LocalUser) {
$person = $this->api->user()->get($object->personId);
$target = $this->linkConverter->convertPersonLink($person);
$username = "{$person->name}@" . parse_url($person->actorId, PHP_URL_HOST);
}

if ($target === null) {
return FurtherAction::CanContinue;
}

$actionNames = array_map(
fn (ModAction $action) => $action->getDescription(),
array_filter($previousActions, fn (ModAction $action) => $action->getDescription() !== null),
);

if (!count($actionNames)) {
if ($target === null || $username === null) {
return FurtherAction::CanContinue;
}

$message = "Actions have been taken against [{$username}](https://{$this->instance}/u/{$username}) for {$target}:\n\n";

foreach ($actionNames as $actionName) {
$message .= " - {$actionName}\n";
foreach ($context->getMessages() as $contextMessage) {
$message .= " - {$contextMessage}\n";
}

$this->notificationSender->sendNotificationAsync($message);
Expand All @@ -77,9 +91,4 @@ public function getRunConfiguration(): RunConfiguration
{
return RunConfiguration::Always;
}

public function getDescription(): ?string
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Automod\ModAction\Notification;

use App\Automod\ModAction\ModAction;
use App\Context\Context;
use App\Enum\FurtherAction;
use App\Enum\RunConfiguration;
use App\Service\Notification\NotificationSender;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function shouldRun(object $object): bool
return false;
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
if ($object instanceof CommentView) {
$this->notificationSender->sendNotificationAsync("User's first comment: https://{$this->instance}/comment/{$object->comment->id}");
Expand All @@ -68,9 +69,4 @@ public function getRunConfiguration(): RunConfiguration
{
return RunConfiguration::Always;
}

public function getDescription(): ?string
{
return null;
}
}
8 changes: 2 additions & 6 deletions src/Automod/ModAction/Notification/NotifyOnNewLocalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Automod\Enum\AutomodPriority;
use App\Automod\ModAction\ModAction;
use App\Context\Context;
use App\Enum\FurtherAction;
use App\Enum\RunConfiguration;
use App\Service\Notification\NotificationSender;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function shouldRun(object $object): bool
return $this->enabled && $object instanceof Person && $object->local && $this->notificationSender->hasEnabledChannels();
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
$this->notificationSender->sendNotificationAsync(
"New user has been added: [{$object->name}](https://{$this->instance}/u/{$object->name}@{$this->instance})",
Expand All @@ -44,9 +45,4 @@ public function getRunConfiguration(): RunConfiguration
{
return RunConfiguration::Always;
}

public function getDescription(): ?string
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Automod\ModAction\RegistrationApplication;

use App\Automod\ModAction\AbstractModAction;
use App\Context\Context;
use App\Enum\FurtherAction;
use App\Repository\AutoApprovalRegexRepository;
use Rikudou\LemmyApi\Response\View\RegistrationApplicationView;
Expand Down Expand Up @@ -40,15 +41,12 @@ public function shouldRun(object $object): bool
return $found;
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
$this->api->admin()->approveRegistrationApplication($object->registrationApplication);

return FurtherAction::ShouldAbort;
}
$context->addMessage('Registration application has been automatically approved.');

public function getDescription(): ?string
{
return 'user has been automatically approved';
return FurtherAction::ShouldAbort;
}
}
10 changes: 4 additions & 6 deletions src/Automod/ModAction/Report/AbstractReportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Automod\ModAction\Report;

use App\Automod\ModAction\AbstractModAction;
use App\Context\Context;
use App\Entity\ReportRegex;
use App\Enum\FurtherAction;
use App\Repository\ReportRegexRepository;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function shouldRun(object $object): bool
return false;
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
foreach ($this->getTextsToCheck($object) as $text) {
if ($text === null) {
Expand All @@ -64,18 +65,15 @@ public function takeAction(object $object, array $previousActions = []): Further
continue;
}

$context->addMessage("content has been reported for matching regex `{$rule->getRegex()}`");

$this->report($object, $rule->getMessage());
break;
}

return FurtherAction::CanContinue;
}

public function getDescription(): ?string
{
return 'user has been reported';
}

private function findMatchingRule(?string $content): ?ReportRegex
{
if ($content === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace App\Automod\ModAction\UserReport;

use App\Automod\ModAction\AbstractModAction;
use App\Context\Context;
use App\Entity\TrustedUser;
use App\Enum\FurtherAction;
use App\Repository\TrustedUserRepository;
use App\Service\InstanceLinkConverter;
use Doctrine\ORM\EntityManagerInterface;
use LogicException;
use Rikudou\LemmyApi\Response\View\CommentReportView;
use Rikudou\LemmyApi\Response\View\PostReportView;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
* @extends AbstractModAction<CommentReportView|PostReportView>
Expand All @@ -19,6 +22,9 @@
public function __construct(
private TrustedUserRepository $trustedUserRepository,
private EntityManagerInterface $entityManager,
private InstanceLinkConverter $linkConverter,
#[Autowire('%app.lemmy.instance%')]
private string $instance,
) {
}

Expand Down Expand Up @@ -48,22 +54,25 @@ public function shouldRun(object $object): bool
return in_array($object->creator->id, $trustedIds, true);
}

public function takeAction(object $object, array $previousActions = []): FurtherAction
public function takeAction(object $object, Context $context = new Context()): FurtherAction
{
$reporter = sprintf(
'[%1$s@%2$s](https://%3$s/u/%1$s@%2$s',
$object->creator->name,
parse_url($object->creator->actorId, PHP_URL_HOST),
$this->instance,
);
if ($object instanceof CommentReportView) {
$context->addMessage("reported comment ({$this->linkConverter->convertCommentLink($object->comment)}) has been automatically resolved because it was reported by a trusted user ({$reporter})");
$this->api->moderator()->removeComment($object->comment, $object->commentReport->reason);
$this->api->moderator()->resolveCommentReport($object->commentReport);
}
if ($object instanceof PostReportView) {
$context->addMessage("reported post ({$this->linkConverter->convertPostLink($object->post)}) has been automatically resolved because it was reported by a trusted user ({$reporter})");
$this->api->moderator()->removePost($object->post, $object->postReport->reason);
$this->api->moderator()->resolvePostReport($object->postReport);
}

return FurtherAction::CanContinue;
}

public function getDescription(): ?string
{
return 'the content has been deleted';
}
}
Loading

0 comments on commit b084e0b

Please sign in to comment.