Files
firefly-iii/app/Generator/Webhook/StandardMessageGenerator.php

236 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2021-08-10 19:31:55 +02:00
/*
2021-08-10 19:31:55 +02:00
* StandardMessageGenerator.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-08-10 19:31:55 +02:00
declare(strict_types=1);
namespace FireflyIII\Generator\Webhook;
2022-09-17 16:54:13 +02:00
use FireflyIII\Enums\WebhookResponse;
use FireflyIII\Enums\WebhookTrigger;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\Webhook;
use FireflyIII\Models\WebhookMessage;
use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\TransactionGroupTransformer;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class StandardMessageGenerator
*/
class StandardMessageGenerator implements MessageGeneratorInterface
{
private Collection $objects;
private int $trigger;
2021-03-21 09:15:40 +01:00
private User $user;
private int $version = 0;
private Collection $webhooks;
2023-01-05 19:05:23 +01:00
public function __construct()
{
$this->objects = new Collection();
$this->webhooks = new Collection();
}
public function generateMessages(): void
{
2023-10-29 06:33:43 +01:00
app('log')->debug(__METHOD__);
// get the webhooks:
2023-01-05 19:05:23 +01:00
if (0 === $this->webhooks->count()) {
$this->webhooks = $this->getWebhooks();
}
// do some debugging
2023-10-29 06:33:43 +01:00
app('log')->debug(
sprintf('StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).', $this->objects->count(), $this->webhooks->count())
);
$this->run();
}
2023-12-20 19:35:52 +01:00
public function getVersion(): int
{
return $this->version;
}
public function setObjects(Collection $objects): void
{
$this->objects = $objects;
}
public function setTrigger(int $trigger): void
{
$this->trigger = $trigger;
}
public function setUser(User $user): void
{
$this->user = $user;
}
public function setWebhooks(Collection $webhooks): void
{
$this->webhooks = $webhooks;
}
2023-06-21 12:34:58 +02:00
private function getWebhooks(): Collection
2023-05-29 13:56:55 +02:00
{
2023-06-21 12:34:58 +02:00
return $this->user->webhooks()->where('active', true)->where('trigger', $this->trigger)->get(['webhooks.*']);
2023-05-29 13:56:55 +02:00
}
2023-06-21 12:34:58 +02:00
private function run(): void
2023-05-29 13:56:55 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in StandardMessageGenerator::run');
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var Webhook $webhook */
foreach ($this->webhooks as $webhook) {
$this->runWebhook($webhook);
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Done with StandardMessageGenerator::run');
2023-05-29 13:56:55 +02:00
}
/**
2023-06-21 12:34:58 +02:00
* @throws FireflyException
* */
2023-06-21 12:34:58 +02:00
private function runWebhook(Webhook $webhook): void
2023-05-29 13:56:55 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in runWebhook(#%d)', $webhook->id));
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var Model $object */
foreach ($this->objects as $object) {
$this->generateMessage($webhook, $object);
}
}
/**
2022-03-29 15:10:05 +02:00
* @throws FireflyException
* */
private function generateMessage(Webhook $webhook, Model $model): void
{
$class = get_class($model);
2022-12-31 06:57:05 +01:00
// Line is ignored because all of Firefly III's Models have an id property.
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in generateMessage(#%d, %s#%d)', $webhook->id, $class, $model->id));
$uuid = Uuid::uuid4();
$basicMessage = [
'uuid' => $uuid->toString(),
'user_id' => 0,
2022-09-17 16:54:13 +02:00
'trigger' => WebhookTrigger::from($webhook->trigger)->name,
'response' => WebhookResponse::from($webhook->response)->name,
'url' => $webhook->url,
'version' => sprintf('v%d', $this->getVersion()),
'content' => [],
];
// depends on the model how user_id is set:
switch ($class) {
default:
2022-12-31 06:57:05 +01:00
// Line is ignored because all of Firefly III's Models have an id property.
2023-10-29 06:32:00 +01:00
app('log')->error(
2023-01-05 19:05:23 +01:00
sprintf('Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.', $webhook->id, $class, $model->id)
2023-10-29 06:22:57 +01:00
);
return;
2023-12-20 19:35:52 +01:00
case TransactionGroup::class:
2023-12-20 19:35:52 +01:00
// @var TransactionGroup $model
$basicMessage['user_id'] = $model->user->id;
2023-12-20 19:35:52 +01:00
break;
}
// then depends on the response what to put in the message:
switch ($webhook->response) {
default:
2023-10-29 06:32:00 +01:00
app('log')->error(
sprintf('The response code for webhook #%d is "%d" and the message generator cant handle it. Soft fail.', $webhook->id, $webhook->response)
);
return;
2023-12-20 19:35:52 +01:00
2022-09-17 16:54:13 +02:00
case WebhookResponse::NONE->value:
$basicMessage['content'] = [];
2023-12-20 19:35:52 +01:00
break;
2023-12-20 19:35:52 +01:00
2022-09-17 16:54:13 +02:00
case WebhookResponse::TRANSACTIONS->value:
2023-12-21 04:59:23 +01:00
/** @var TransactionGroup $model */
2022-10-30 14:24:10 +01:00
$transformer = new TransactionGroupTransformer();
2023-12-20 19:35:52 +01:00
try {
$basicMessage['content'] = $transformer->transformObject($model);
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(
sprintf('The transformer could not include the requested transaction group for webhook #%d: %s', $webhook->id, $e->getMessage())
);
2023-10-29 06:32:00 +01:00
app('log')->error($e->getTraceAsString());
return;
}
2023-12-20 19:35:52 +01:00
break;
2023-12-20 19:35:52 +01:00
2022-09-17 16:54:13 +02:00
case WebhookResponse::ACCOUNTS->value:
2023-12-21 04:59:23 +01:00
/** @var TransactionGroup $model */
$accounts = $this->collectAccounts($model);
foreach ($accounts as $account) {
2022-10-30 14:24:10 +01:00
$transformer = new AccountTransformer();
$transformer->setParameters(new ParameterBag());
$basicMessage['content'][] = $transformer->transform($account);
}
}
$this->storeMessage($webhook, $basicMessage);
}
2023-06-21 12:34:58 +02:00
private function collectAccounts(TransactionGroup $transactionGroup): Collection
{
2023-06-21 12:34:58 +02:00
$accounts = new Collection();
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var TransactionJournal $journal */
foreach ($transactionGroup->transactionJournals as $journal) {
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$accounts->push($transaction->account);
}
}
2023-06-21 12:34:58 +02:00
return $accounts->unique();
}
2022-03-29 15:10:05 +02:00
private function storeMessage(Webhook $webhook, array $message): void
{
2022-10-30 14:24:10 +01:00
$webhookMessage = new WebhookMessage();
$webhookMessage->webhook()->associate($webhook);
$webhookMessage->sent = false;
$webhookMessage->errored = false;
$webhookMessage->uuid = $message['uuid'];
$webhookMessage->message = $message;
$webhookMessage->save();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Stored new webhook message #%d', $webhookMessage->id));
}
2020-12-22 05:35:06 +01:00
}