mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-04 04:24:26 +00:00
Clear up webhooks
This commit is contained in:
@@ -80,6 +80,7 @@ class DestroyController extends Controller
|
||||
*/
|
||||
public function destroy(TransactionGroup $transactionGroup): JsonResponse
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
// grab asset account(s) from group:
|
||||
$accounts = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
|
||||
@@ -24,12 +24,18 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Api\V1\Controllers\Webhook;
|
||||
|
||||
use FireflyIII\Api\V1\Controllers\Controller;
|
||||
use FireflyIII\Enums\WebhookTrigger;
|
||||
use FireflyIII\Events\RequestedSendWebhookMessages;
|
||||
use FireflyIII\Events\StoredTransactionGroup;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Generator\Webhook\MessageGeneratorInterface;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\Webhook;
|
||||
use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
|
||||
use FireflyIII\Transformers\WebhookTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
@@ -111,4 +117,35 @@ class ShowController extends Controller
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* This endpoint is documented at:
|
||||
* https://api-docs.firefly-iii.org/#/webhooks/triggerWebhookTransaction
|
||||
*
|
||||
* This method recycles part of the code of the StoredGroupEventHandler.
|
||||
*
|
||||
* @param Webhook $webhook
|
||||
* @param TransactionGroup $group
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function triggerTransaction(Webhook $webhook, TransactionGroup $group): JsonResponse
|
||||
{
|
||||
/** @var MessageGeneratorInterface $engine */
|
||||
$engine = app(MessageGeneratorInterface::class);
|
||||
$engine->setUser(auth()->user());
|
||||
|
||||
// tell the generator which trigger it should look for
|
||||
$engine->setTrigger($webhook->trigger);
|
||||
// tell the generator which objects to process
|
||||
$engine->setObjects(new Collection([$group]));
|
||||
// set the webhook to trigger
|
||||
$engine->setWebhooks(new Collection([$webhook]));
|
||||
// tell the generator to generate the messages
|
||||
$engine->generateMessages();
|
||||
|
||||
// trigger event to send them:
|
||||
event(new RequestedSendWebhookMessages());
|
||||
return response()->json([], 204);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,6 @@ class SubmitController extends Controller
|
||||
* This endpoint is documented at:
|
||||
* https://api-docs.firefly-iii.org/#/webhooks/submitWebook
|
||||
*
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Webhook $webhook
|
||||
*
|
||||
* @return JsonResponse
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Events;
|
||||
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class DestroyedTransactionGroup.
|
||||
@@ -45,6 +46,7 @@ class DestroyedTransactionGroup extends Event
|
||||
*/
|
||||
public function __construct(TransactionGroup $transactionGroup)
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
$this->transactionGroup = $transactionGroup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,12 @@ interface MessageGeneratorInterface
|
||||
*/
|
||||
public function setObjects(Collection $objects): void;
|
||||
|
||||
/**
|
||||
* @param Collection $webhooks
|
||||
* @return void
|
||||
*/
|
||||
public function setWebhooks(Collection $webhooks): void;
|
||||
|
||||
/**
|
||||
* @param int $trigger
|
||||
*/
|
||||
|
||||
@@ -53,6 +53,15 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
private int $version = 0;
|
||||
private Collection $webhooks;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->objects = new Collection();
|
||||
$this->webhooks = new Collection();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -60,7 +69,9 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
// get the webhooks:
|
||||
$this->webhooks = $this->getWebhooks();
|
||||
if (0 === $this->webhooks->count()) {
|
||||
$this->webhooks = $this->getWebhooks();
|
||||
}
|
||||
|
||||
// do some debugging
|
||||
Log::debug(
|
||||
@@ -129,7 +140,9 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
switch ($class) {
|
||||
default:
|
||||
// Line is ignored because all of Firefly III's Models have an id property.
|
||||
Log::error(sprintf('Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.', $webhook->id, $class, $model->id)); // @phpstan-ignore-line
|
||||
Log::error(
|
||||
sprintf('Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.', $webhook->id, $class, $model->id)
|
||||
); // @phpstan-ignore-line
|
||||
|
||||
return;
|
||||
case TransactionGroup::class:
|
||||
@@ -240,4 +253,12 @@ class StandardMessageGenerator implements MessageGeneratorInterface
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setWebhooks(Collection $webhooks): void
|
||||
{
|
||||
$this->webhooks = $webhooks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,9 @@ class WebhookEventHandler
|
||||
*/
|
||||
public function sendWebhookMessages(): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
// kick off the job!
|
||||
$messages = WebhookMessage::where('webhook_messages.sent', 0)
|
||||
//->where('webhook_messages.errored', 0)
|
||||
$messages = WebhookMessage::where('webhook_messages.sent',false)
|
||||
->get(['webhook_messages.*'])
|
||||
->filter(
|
||||
function (WebhookMessage $message) {
|
||||
@@ -48,7 +48,13 @@ class WebhookEventHandler
|
||||
)->splice(0, 5);
|
||||
Log::debug(sprintf('Found %d webhook message(s) ready to be send.', $messages->count()));
|
||||
foreach ($messages as $message) {
|
||||
SendWebhookMessage::dispatch($message)->afterResponse();
|
||||
if (false === $message->sent) {
|
||||
Log::debug(sprintf('Send message #%d', $message->id));
|
||||
SendWebhookMessage::dispatch($message)->afterResponse();
|
||||
}
|
||||
if (false !== $message->sent) {
|
||||
Log::debug(sprintf('Skip message #%d', $message->id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class AcceptHeaders
|
||||
if ('GET' === $method && !$request->accepts(['application/json', 'application/vdn.api+json'])) {
|
||||
throw new BadHttpHeaderException('Your request must accept either application/json or application/vdn.api+json.');
|
||||
}
|
||||
$allowed = ['application/x-www-form-urlencoded', 'application/json'];
|
||||
$allowed = ['application/x-www-form-urlencoded', 'application/json',''];
|
||||
$submitted = (string)$request->header('Content-Type');
|
||||
if (('POST' === $method || 'PUT' === $method) && !in_array($submitted, $allowed, true)) {
|
||||
$error = new BadHttpHeaderException(sprintf('Content-Type cannot be "%s"', $submitted));
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Models;
|
||||
use Eloquent;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -117,4 +118,15 @@ class WebhookMessage extends Model
|
||||
{
|
||||
return $this->hasMany(WebhookAttempt::class);
|
||||
}
|
||||
/**
|
||||
* Get the amount
|
||||
*
|
||||
* @return Attribute
|
||||
*/
|
||||
protected function sent(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value) => (bool)$value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
|
||||
*/
|
||||
public function destroy(TransactionGroup $group): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
$service = new TransactionGroupDestroyService();
|
||||
$service->destroy($group);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ class JournalDestroyService
|
||||
*/
|
||||
public function destroy(TransactionJournal $journal): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($journal->transactions()->get() as $transaction) {
|
||||
Log::debug(sprintf('Will now delete transaction #%d', $transaction->id));
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace FireflyIII\Services\Internal\Destroy;
|
||||
|
||||
use FireflyIII\Events\DestroyedTransactionGroup;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class TransactionGroupDestroyService
|
||||
@@ -38,6 +39,7 @@ class TransactionGroupDestroyService
|
||||
*/
|
||||
public function destroy(TransactionGroup $transactionGroup): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
foreach ($transactionGroup->transactionJournals as $journal) {
|
||||
|
||||
@@ -56,7 +56,8 @@ class StandardWebhookSender implements WebhookSenderInterface
|
||||
// have the signature generator generate a signature. If it fails, the error thrown will
|
||||
// end up in send() to be caught.
|
||||
$signatureGenerator = app(SignatureGeneratorInterface::class);
|
||||
|
||||
$this->message->sent = true;
|
||||
$this->message->save();
|
||||
try {
|
||||
$signature = $signatureGenerator->generate($this->message);
|
||||
} catch (FireflyException $e) {
|
||||
@@ -108,7 +109,6 @@ class StandardWebhookSender implements WebhookSenderInterface
|
||||
$client = new Client();
|
||||
try {
|
||||
$res = $client->request('POST', $this->message->webhook->url, $options);
|
||||
$this->message->sent = true;
|
||||
} catch (RequestException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
@@ -127,6 +127,7 @@ class StandardWebhookSender implements WebhookSenderInterface
|
||||
|
||||
return;
|
||||
}
|
||||
$this->message->sent = true;
|
||||
$this->message->save();
|
||||
|
||||
Log::debug(sprintf('Webhook message #%d was sent. Status code %d', $this->message->id, $res->getStatusCode()));
|
||||
|
||||
Reference in New Issue
Block a user