Skip to content

Commit

Permalink
Fix notification function
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Apr 11, 2024
1 parent c762c0f commit e432b08
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/Automod/ModAction/ComplexRuleAction.php
Expand Up @@ -10,6 +10,7 @@
use App\Enum\RunConfiguration;
use App\Repository\ComplexRuleRepository;
use App\Service\Expression\ExpressionLanguage;
use App\Service\Expression\ExpressionLanguageNotifier;
use LogicException;
use Rikudou\LemmyApi\LemmyApi;
use Rikudou\LemmyApi\Response\Model\Person;
Expand All @@ -31,6 +32,7 @@ public function __construct(
private ComplexRuleRepository $ruleRepository,
private ExpressionLanguage $expressionLanguage,
private LemmyApi $api,
private ExpressionLanguageNotifier $notifier,
) {
}

Expand Down Expand Up @@ -58,15 +60,7 @@ public function shouldRun(object $object): bool

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;
}
));

$this->notifier->currentContext = $context;
$type = ComplexRuleType::fromClass(get_class($object));

$canContinue = true;
Expand Down
13 changes: 13 additions & 0 deletions src/Service/Expression/ExpressionLanguageFunctions.php
Expand Up @@ -12,6 +12,7 @@
public function __construct(
private MessageBusInterface $messageBus,
private ExpressionLanguage $expressionLanguage,
private ExpressionLanguageNotifier $notifier,
) {
}

Expand Down Expand Up @@ -48,6 +49,11 @@ public function getFunctions(): array
$this->uncompilableFunction(),
$this->catchErrorFunction(...),
),
new ExpressionFunction(
'notify',
$this->uncompilableFunction(),
$this->notifyFunction(...),
),
];
}

Expand Down Expand Up @@ -114,4 +120,11 @@ private function catchErrorFunction(array $context, string $expression, string $
return $this->expressionLanguage->evaluate($onErrorExpression, $context);
}
}

private function notifyFunction(array $context, string $message): bool
{
$this->notifier->currentContext?->addMessage($message);

return true;
}
}
10 changes: 10 additions & 0 deletions src/Service/Expression/ExpressionLanguageNotifier.php
@@ -0,0 +1,10 @@
<?php

namespace App\Service\Expression;

use App\Context\Context;

final class ExpressionLanguageNotifier
{
public ?Context $currentContext = null;
}

0 comments on commit e432b08

Please sign in to comment.