Make sure webhook messages can be versionised later.

This commit is contained in:
James Cole
2020-12-04 20:19:52 +01:00
parent 7ee9b51b3f
commit 48d1d5c90b
10 changed files with 413 additions and 99 deletions

View File

@@ -0,0 +1,57 @@
<?php
/*
* MessageGeneratorInterface.php
* Copyright (c) 2020 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/>.
*/
namespace FireflyIII\Generator\Webhook;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
* Interface MessageGeneratorInterface
*/
interface MessageGeneratorInterface
{
/**
* @return int
*/
public function getVersion(): int;
/**
*
*/
public function generateMessages(): void;
/**
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param Collection $transactionGroups
*/
public function setTransactionGroups(Collection $transactionGroups): void;
/**
* @param int $trigger
*/
public function setTrigger(int $trigger): void;
}

View File

@@ -22,7 +22,6 @@
namespace FireflyIII\Generator\Webhook;
use FireflyIII\Events\RequestedSendWebhookMessages;
use FireflyIII\Events\StoredWebhookMessage;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
@@ -38,10 +37,11 @@ use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class WebhookMessageGenerator
* Class StandardMessageGenerator
*/
class WebhookMessageGenerator
class StandardMessageGenerator implements MessageGeneratorInterface
{
private int $version = 1;
private User $user;
private Collection $transactionGroups;
private int $trigger;
@@ -89,6 +89,9 @@ class WebhookMessageGenerator
return $this->user->webhooks()->where('active', 1)->where('trigger', $this->trigger)->get(['webhooks.*']);
}
/**
* Will also trigger a send.
*/
private function run(): void
{
/** @var Webhook $webhook */
@@ -100,8 +103,6 @@ class WebhookMessageGenerator
/**
* @param Webhook $webhook
*
* @throws FireflyException
*/
private function runWebhook(Webhook $webhook): void
{
@@ -126,19 +127,28 @@ class WebhookMessageGenerator
'trigger' => config('firefly.webhooks.triggers')[$webhook->trigger],
'url' => $webhook->url,
'uuid' => $uuid->toString(),
'version' => 0,
'version' => sprintf('v%d',$this->getVersion()),
'response' => config('firefly.webhooks.responses')[$webhook->response],
'content' => [],
];
switch ($webhook->response) {
default:
throw new FireflyException(sprintf('Cannot handle this webhook response (%d)', $webhook->response));
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;
case Webhook::RESPONSE_NONE:
$message['content'] = [];
break;
case Webhook::RESPONSE_TRANSACTIONS:
$transformer = new TransactionGroupTransformer;
$message['content'] = $transformer->transformObject($transactionGroup);
$transformer = new TransactionGroupTransformer;
try {
$message['content'] = $transformer->transformObject($transactionGroup);
} catch (FireflyException $e) {
$message['content'] = ['error' => 'Internal error prevented Firefly III from including data', 'message' => $e->getMessage()];
}
break;
case Webhook::RESPONSE_ACCOUNTS:
$accounts = $this->collectAccounts($transactionGroup);
@@ -173,6 +183,8 @@ class WebhookMessageGenerator
/**
* @param Webhook $webhook
* @param array $message
*
* @return WebhookMessage
*/
private function storeMessage(Webhook $webhook, array $message): WebhookMessage
{
@@ -188,4 +200,11 @@ class WebhookMessageGenerator
}
/**
* @inheritDoc
*/
public function getVersion(): int
{
return $this->version;
}
}