mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-02-12 06:42:19 +00:00
Clean up event handlers and other code.
This commit is contained in:
@@ -27,13 +27,13 @@ namespace FireflyIII\Generator\Webhook;
|
||||
use FireflyIII\Enums\WebhookResponse;
|
||||
use FireflyIII\Enums\WebhookTrigger;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\WebhookMessageFactory;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\Webhook;
|
||||
use FireflyIII\Models\WebhookMessage;
|
||||
use FireflyIII\Models\WebhookResponse as WebhookResponseModel;
|
||||
use FireflyIII\Models\WebhookTrigger as WebhookTriggerModel;
|
||||
use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
|
||||
@@ -55,11 +55,11 @@ use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
*/
|
||||
class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
{
|
||||
private Collection $objects;
|
||||
private Collection $objects;
|
||||
private WebhookTrigger $trigger;
|
||||
private User $user;
|
||||
private int $version = 0;
|
||||
private Collection $webhooks;
|
||||
private User $user;
|
||||
private int $version = 0;
|
||||
private Collection $webhooks;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -77,10 +77,10 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
|
||||
// do some debugging
|
||||
Log::debug(sprintf(
|
||||
'StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).',
|
||||
$this->objects->count(),
|
||||
$this->webhooks->count()
|
||||
));
|
||||
'StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).',
|
||||
$this->objects->count(),
|
||||
$this->webhooks->count()
|
||||
));
|
||||
$this->run();
|
||||
}
|
||||
|
||||
@@ -92,8 +92,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
->leftJoin('webhook_triggers', 'webhook_webhook_trigger.webhook_trigger_id', 'webhook_triggers.id')
|
||||
->where('active', true)
|
||||
->whereIn('webhook_triggers.title', [$this->trigger->name, WebhookTrigger::ANY->name])
|
||||
->get(['webhooks.*'])
|
||||
;
|
||||
->get(['webhooks.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,15 +127,15 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
*/
|
||||
private function generateMessage(Webhook $webhook, Model $model): void
|
||||
{
|
||||
$class = $model::class;
|
||||
$class = $model::class;
|
||||
// Line is ignored because all of Firefly III's Models have an id property.
|
||||
Log::debug(sprintf('Now in generateMessage(#%d, %s#%d)', $webhook->id, $class, $model->id));
|
||||
$uuid = Uuid::uuid4();
|
||||
$uuid = Uuid::uuid4();
|
||||
|
||||
/** @var WebhookResponseModel $response */
|
||||
$response = $webhook->webhookResponses()->first();
|
||||
$response = $webhook->webhookResponses()->first();
|
||||
$this->getTriggerTitles($webhook->webhookTriggers()->get());
|
||||
$basicMessage = [
|
||||
$basicMessage = [
|
||||
'uuid' => $uuid->toString(),
|
||||
'user_id' => 0,
|
||||
'user_group_id' => 0,
|
||||
@@ -181,17 +180,17 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
switch ($responseTitle) {
|
||||
default:
|
||||
Log::error(sprintf(
|
||||
'The response code for webhook #%d is "%s" and the message generator cant handle it. Soft fail.',
|
||||
$webhook->id,
|
||||
$webhook->response
|
||||
));
|
||||
'The response code for webhook #%d is "%s" and the message generator cant handle it. Soft fail.',
|
||||
$webhook->id,
|
||||
$webhook->response
|
||||
));
|
||||
|
||||
return;
|
||||
|
||||
case WebhookResponse::BUDGET->name:
|
||||
$basicMessage['content'] = [];
|
||||
if ($model instanceof Budget) {
|
||||
$enrichment = new BudgetEnrichment();
|
||||
$enrichment = new BudgetEnrichment();
|
||||
$enrichment->setUser($model->user);
|
||||
|
||||
/** @var Budget $model */
|
||||
@@ -200,17 +199,17 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
$basicMessage['content'] = $transformer->transform($model);
|
||||
}
|
||||
if ($model instanceof BudgetLimit) {
|
||||
$user = $model->budget->user;
|
||||
$enrichment = new BudgetLimitEnrichment();
|
||||
$user = $model->budget->user;
|
||||
$enrichment = new BudgetLimitEnrichment();
|
||||
$enrichment->setUser($user);
|
||||
|
||||
$parameters = new ParameterBag();
|
||||
$parameters = new ParameterBag();
|
||||
$parameters->set('start', $model->start_date);
|
||||
$parameters->set('end', $model->end_date);
|
||||
|
||||
/** @var BudgetLimit $model */
|
||||
$model = $enrichment->enrichSingle($model);
|
||||
$transformer = new BudgetLimitTransformer();
|
||||
$model = $enrichment->enrichSingle($model);
|
||||
$transformer = new BudgetLimitTransformer();
|
||||
$transformer->setParameters($parameters);
|
||||
$basicMessage['content'] = $transformer->transform($model);
|
||||
}
|
||||
@@ -224,16 +223,16 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
|
||||
case WebhookResponse::TRANSACTIONS->name:
|
||||
/** @var TransactionGroup $model */
|
||||
$transformer = new TransactionGroupTransformer();
|
||||
$transformer = new TransactionGroupTransformer();
|
||||
|
||||
try {
|
||||
$basicMessage['content'] = $transformer->transformObject($model);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error(sprintf(
|
||||
'The transformer could not include the requested transaction group for webhook #%d: %s',
|
||||
$webhook->id,
|
||||
$e->getMessage()
|
||||
));
|
||||
'The transformer could not include the requested transaction group for webhook #%d: %s',
|
||||
$webhook->id,
|
||||
$e->getMessage()
|
||||
));
|
||||
Log::error($e->getTraceAsString());
|
||||
|
||||
return;
|
||||
@@ -243,18 +242,19 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
|
||||
case WebhookResponse::ACCOUNTS->name:
|
||||
/** @var TransactionGroup $model */
|
||||
$accounts = $this->collectAccounts($model);
|
||||
$enrichment = new AccountEnrichment();
|
||||
$accounts = $this->collectAccounts($model);
|
||||
$enrichment = new AccountEnrichment();
|
||||
$enrichment->setDate(null);
|
||||
$enrichment->setUser($model->user);
|
||||
$accounts = $enrichment->enrich($accounts);
|
||||
$accounts = $enrichment->enrich($accounts);
|
||||
foreach ($accounts as $account) {
|
||||
$transformer = new AccountTransformer();
|
||||
$transformer = new AccountTransformer();
|
||||
$transformer->setParameters(new ParameterBag());
|
||||
$basicMessage['content'][] = $transformer->transform($account);
|
||||
}
|
||||
}
|
||||
$this->storeMessage($webhook, $basicMessage);
|
||||
$factory = new WebhookMessageFactory();
|
||||
$factory->create($webhook, $basicMessage);
|
||||
}
|
||||
|
||||
public function getVersion(): int
|
||||
@@ -277,18 +277,6 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
return $accounts->unique();
|
||||
}
|
||||
|
||||
private function storeMessage(Webhook $webhook, array $message): void
|
||||
{
|
||||
$webhookMessage = new WebhookMessage();
|
||||
$webhookMessage->webhook()->associate($webhook);
|
||||
$webhookMessage->sent = false;
|
||||
$webhookMessage->errored = false;
|
||||
$webhookMessage->uuid = $message['uuid'];
|
||||
$webhookMessage->message = $message;
|
||||
$webhookMessage->save();
|
||||
Log::debug(sprintf('Stored new webhook message #%d', $webhookMessage->id));
|
||||
}
|
||||
|
||||
public function setObjects(Collection $objects): void
|
||||
{
|
||||
$this->objects = $objects;
|
||||
|
||||
Reference in New Issue
Block a user