Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f09649
commit d614dad
Showing
13 changed files
with
529 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20240411103740 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->addSql('CREATE TABLE complex_rules (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, type VARCHAR(180) NOT NULL, rule CLOB NOT NULL, actions CLOB NOT NULL, run_configuration VARCHAR(180) DEFAULT \'not_aborted\' NOT NULL, enabled BOOLEAN DEFAULT 1 NOT NULL)'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->addSql('DROP TABLE complex_rules'); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace App\Automod\Enum; | ||
|
||
use App\Dto\Model\EnrichedInstanceData; | ||
use App\Dto\Model\LocalUser; | ||
use InvalidArgumentException; | ||
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\PrivateMessageReportView; | ||
use Rikudou\LemmyApi\Response\View\RegistrationApplicationView; | ||
|
||
enum ComplexRuleType: string | ||
{ | ||
case Post = 'post'; | ||
case Comment = 'comment'; | ||
case Person = 'person'; | ||
case CommentReport = 'comment_report'; | ||
case PostReport = 'post_report'; | ||
case PrivateMessageReport = 'private_message_report'; | ||
case RegistrationApplication = 'registration_application'; | ||
case LocalUser = 'local_user'; | ||
case Instance = 'instance'; | ||
|
||
public static function fromClass(string $class): self | ||
{ | ||
return match ($class) { | ||
PostView::class => self::Post, | ||
CommentView::class => self::Comment, | ||
Person::class => self::Person, | ||
CommentReportView::class => self::CommentReport, | ||
PostReportView::class => self::PostReport, | ||
PrivateMessageReportView::class => self::PrivateMessageReport, | ||
RegistrationApplicationView::class => self::RegistrationApplication, | ||
LocalUser::class => self::LocalUser, | ||
EnrichedInstanceData::class => self::Instance, | ||
default => throw new InvalidArgumentException("Unsupported class: {$class}"), | ||
}; | ||
} | ||
} |
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,100 @@ | ||
<?php | ||
|
||
namespace App\Automod\ModAction; | ||
|
||
use App\Automod\Enum\ComplexRuleType; | ||
use App\Context\Context; | ||
use App\Dto\Model\EnrichedInstanceData; | ||
use App\Dto\Model\LocalUser; | ||
use App\Enum\FurtherAction; | ||
use App\Enum\RunConfiguration; | ||
use App\Repository\ComplexRuleRepository; | ||
use App\Service\ExpressionLanguage; | ||
use LogicException; | ||
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\PrivateMessageReportView; | ||
use Rikudou\LemmyApi\Response\View\RegistrationApplicationView; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunction; | ||
use Symfony\Component\ExpressionLanguage\SyntaxError; | ||
|
||
/** | ||
* @implements ModAction<PostView|CommentView|Person|CommentReportView|PostReportView|PrivateMessageReportView|RegistrationApplicationView|LocalUser|EnrichedInstanceData> | ||
*/ | ||
final readonly class ComplexRuleAction implements ModAction | ||
{ | ||
public function __construct( | ||
private ComplexRuleRepository $ruleRepository, | ||
private ExpressionLanguage $expressionLanguage, | ||
private LemmyApi $api, | ||
) { | ||
} | ||
|
||
public function shouldRun(object $object): bool | ||
{ | ||
$type = ComplexRuleType::fromClass(get_class($object)); | ||
$rules = $this->ruleRepository->findBy([ | ||
'enabled' => true, | ||
'type' => $type, | ||
]); | ||
|
||
if (!count($rules)) { | ||
return false; | ||
} | ||
|
||
foreach ($rules as $rule) { | ||
$expression = $rule->getRule(); | ||
if ($this->expressionLanguage->evaluate($expression, ['object' => $object])) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function takeAction(object $object, Context $context = new Context()): FurtherAction | ||
{ | ||
$this->expressionLanguage->addFunction(new ExpressionFunction( | ||
'notify', | ||
fn () => throw new LogicException('Uncompilable function'), | ||
function (array $expressionContext, string $message) use ($context): bool { | ||
$context->addMessage($message); | ||
return true; | ||
} | ||
)); | ||
|
||
$type = ComplexRuleType::fromClass(get_class($object)); | ||
|
||
$canContinue = true; | ||
$rules = $this->ruleRepository->findBy(['enabled' => true, 'type' => $type]); | ||
foreach ($rules as $rule) { | ||
if (!$canContinue && $rule->getRunConfiguration() !== RunConfiguration::Always) { | ||
continue; | ||
} | ||
$expression = $rule->getActions(); | ||
try { | ||
$result = (bool) $this->expressionLanguage->evaluate($expression, [ | ||
'object' => $object, | ||
'lemmy' => $this->api, | ||
]); | ||
} catch (SyntaxError $e) { | ||
$context->addMessage("There's a syntax error in complex rule with id '{$rule->getId()}': {$e->getMessage()}"); | ||
continue; | ||
} | ||
if (!$result) { | ||
$canContinue = false; | ||
} | ||
} | ||
|
||
return $canContinue ? FurtherAction::CanContinue : FurtherAction::ShouldAbort; | ||
} | ||
|
||
public function getRunConfiguration(): RunConfiguration | ||
{ | ||
return RunConfiguration::Always; | ||
} | ||
} |
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,99 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use App\Automod\Enum\ComplexRuleType; | ||
use App\Enum\RunConfiguration; | ||
use App\Repository\ComplexRuleRepository; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Table(name: 'complex_rules')] | ||
#[ORM\Entity(repositoryClass: ComplexRuleRepository::class)] | ||
class ComplexRule | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column(length: 180, enumType: ComplexRuleType::class)] | ||
private ?ComplexRuleType $type = null; | ||
|
||
#[ORM\Column(type: Types::TEXT)] | ||
private ?string $rule = null; | ||
|
||
#[ORM\Column(type: Types::TEXT)] | ||
private ?string $actions = null; | ||
|
||
#[ORM\Column(length: 180, enumType: RunConfiguration::class, options: ['default' => RunConfiguration::WhenNotAborted->value])] | ||
private RunConfiguration $runConfiguration = RunConfiguration::WhenNotAborted; | ||
|
||
#[ORM\Column(options: ['default' => true])] | ||
private bool $enabled = true; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getType(): ?ComplexRuleType | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function setType(ComplexRuleType $type): static | ||
{ | ||
$this->type = $type; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRule(): ?string | ||
{ | ||
return $this->rule; | ||
} | ||
|
||
public function setRule(string $rule): static | ||
{ | ||
$this->rule = $rule; | ||
|
||
return $this; | ||
} | ||
|
||
public function getActions(): ?string | ||
{ | ||
return $this->actions; | ||
} | ||
|
||
public function setActions(string $actions): static | ||
{ | ||
$this->actions = $actions; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRunConfiguration(): RunConfiguration | ||
{ | ||
return $this->runConfiguration; | ||
} | ||
|
||
public function setRunConfiguration(RunConfiguration $runConfiguration): static | ||
{ | ||
$this->runConfiguration = $runConfiguration; | ||
|
||
return $this; | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function setEnabled(bool $enabled): static | ||
{ | ||
$this->enabled = $enabled; | ||
|
||
return $this; | ||
} | ||
} |
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
Oops, something went wrong.