2020-12-02 12:30:09 -06:00
|
|
|
<?php
|
2021-01-29 11:50:35 -06:00
|
|
|
/*
|
|
|
|
* WebhookEventHandler.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/>.
|
|
|
|
*/
|
|
|
|
|
2020-12-21 22:35:06 -06:00
|
|
|
declare(strict_types=1);
|
2020-12-02 12:30:09 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Handlers\Events;
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2020-12-05 00:01:26 -06:00
|
|
|
use FireflyIII\Jobs\SendWebhookMessage;
|
2020-12-02 12:30:09 -06:00
|
|
|
use FireflyIII\Models\WebhookMessage;
|
2023-04-01 00:04:42 -05:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-12-02 12:30:09 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WebhookEventHandler
|
|
|
|
*/
|
|
|
|
class WebhookEventHandler
|
|
|
|
{
|
|
|
|
/**
|
2020-12-04 13:19:52 -06:00
|
|
|
* Will try to send at most 3 messages so the flow doesn't get broken too much.
|
2020-12-02 12:30:09 -06:00
|
|
|
*/
|
|
|
|
public function sendWebhookMessages(): void
|
|
|
|
{
|
2023-01-05 12:05:23 -06:00
|
|
|
Log::debug(sprintf('Now in %s', __METHOD__));
|
2021-01-15 14:01:53 -06:00
|
|
|
// kick off the job!
|
2023-01-15 08:47:25 -06:00
|
|
|
$messages = WebhookMessage::where('webhook_messages.sent', false)
|
2022-12-29 12:41:57 -06:00
|
|
|
->get(['webhook_messages.*'])
|
|
|
|
->filter(
|
|
|
|
function (WebhookMessage $message) {
|
|
|
|
return $message->webhookAttempts()->count() <= 2;
|
|
|
|
}
|
|
|
|
)->splice(0, 5);
|
2020-12-04 13:19:52 -06:00
|
|
|
Log::debug(sprintf('Found %d webhook message(s) ready to be send.', $messages->count()));
|
2020-12-05 00:01:26 -06:00
|
|
|
foreach ($messages as $message) {
|
2023-01-05 12:05:23 -06:00
|
|
|
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));
|
|
|
|
}
|
2020-12-05 00:01:26 -06:00
|
|
|
}
|
2020-12-02 12:30:09 -06:00
|
|
|
}
|
2020-12-21 22:35:06 -06:00
|
|
|
}
|