2020-11-29 18:35:49 +01:00
|
|
|
<?php
|
2021-01-29 18:50:35 +01:00
|
|
|
/*
|
2021-03-07 12:13:22 +01:00
|
|
|
* ShowController.php
|
2021-01-29 18:50:35 +01:00
|
|
|
* 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-03-28 11:39:26 +02:00
|
|
|
declare(strict_types=1);
|
2020-11-29 18:35:49 +01:00
|
|
|
|
2021-03-28 11:39:26 +02:00
|
|
|
namespace FireflyIII\Api\V1\Controllers\Webhook;
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
use FireflyIII\Api\V1\Controllers\Controller;
|
2023-01-05 19:05:23 +01:00
|
|
|
use FireflyIII\Events\RequestedSendWebhookMessages;
|
2021-09-18 10:20:19 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2023-01-05 19:05:23 +01:00
|
|
|
use FireflyIII\Generator\Webhook\MessageGeneratorInterface;
|
|
|
|
use FireflyIII\Models\TransactionGroup;
|
2021-03-07 12:13:22 +01:00
|
|
|
use FireflyIII\Models\Webhook;
|
2020-11-29 18:35:49 +01:00
|
|
|
use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
|
|
|
|
use FireflyIII\Transformers\WebhookTransformer;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2023-01-05 19:05:23 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2020-11-29 18:35:49 +01:00
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use League\Fractal\Resource\Collection as FractalCollection;
|
2021-03-07 12:13:22 +01:00
|
|
|
use League\Fractal\Resource\Item;
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-07 12:13:22 +01:00
|
|
|
* Class ShowController
|
2020-11-29 18:35:49 +01:00
|
|
|
*/
|
2021-03-07 12:13:22 +01:00
|
|
|
class ShowController extends Controller
|
2020-11-29 18:35:49 +01:00
|
|
|
{
|
2021-03-07 12:13:22 +01:00
|
|
|
public const RESOURCE_KEY = 'webhooks';
|
2020-11-29 18:35:49 +01:00
|
|
|
private WebhookRepositoryInterface $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
$this->repository = app(WebhookRepositoryInterface::class);
|
2021-04-06 08:51:27 +02:00
|
|
|
$this->repository->setUser(auth()->user());
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-19 17:22:16 +02:00
|
|
|
* This endpoint is documented at:
|
2023-02-12 06:53:36 +01:00
|
|
|
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/webhooks/listWebhook
|
2021-09-19 17:22:16 +02:00
|
|
|
*
|
2021-03-07 12:13:22 +01:00
|
|
|
* Display a listing of the webhooks of the user.
|
2020-11-29 18:35:49 +01:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2021-09-18 10:20:19 +02:00
|
|
|
* @throws FireflyException
|
2020-11-29 18:35:49 +01:00
|
|
|
*/
|
|
|
|
public function index(): JsonResponse
|
|
|
|
{
|
2022-03-29 14:56:27 +02:00
|
|
|
$manager = $this->getManager();
|
2021-03-07 12:13:22 +01:00
|
|
|
$collection = $this->repository->all();
|
2023-10-05 18:52:01 +02:00
|
|
|
$pageSize = $this->parameters->get('limit');
|
2022-03-29 14:56:27 +02:00
|
|
|
$count = $collection->count();
|
|
|
|
$webhooks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
2021-03-07 12:13:22 +01:00
|
|
|
|
|
|
|
// make paginator:
|
2020-11-29 18:35:49 +01:00
|
|
|
$paginator = new LengthAwarePaginator($webhooks, $count, $pageSize, $this->parameters->get('page'));
|
2023-06-21 12:34:58 +02:00
|
|
|
$paginator->setPath(route('api.v1.webhooks.index') . $this->buildParams());
|
2020-11-29 18:35:49 +01:00
|
|
|
|
|
|
|
/** @var WebhookTransformer $transformer */
|
|
|
|
$transformer = app(WebhookTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
2021-03-07 12:13:22 +01:00
|
|
|
$resource = new FractalCollection($webhooks, $transformer, self::RESOURCE_KEY);
|
2020-11-29 18:35:49 +01:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
|
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:13:22 +01:00
|
|
|
/**
|
2021-09-19 17:22:16 +02:00
|
|
|
* This endpoint is documented at:
|
2023-02-12 06:53:36 +01:00
|
|
|
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/webhooks/getWebhook
|
2021-09-19 17:22:16 +02:00
|
|
|
*
|
2021-03-07 12:13:22 +01:00
|
|
|
* Show single instance.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param Webhook $webhook
|
2021-03-07 12:13:22 +01:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function show(Webhook $webhook): JsonResponse
|
|
|
|
{
|
|
|
|
$manager = $this->getManager();
|
|
|
|
|
|
|
|
/** @var WebhookTransformer $transformer */
|
|
|
|
$transformer = app(WebhookTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
$resource = new Item($webhook, $transformer, self::RESOURCE_KEY);
|
|
|
|
|
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
|
|
|
}
|
2023-01-05 19:05:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This endpoint is documented at:
|
2023-02-12 06:53:36 +01:00
|
|
|
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/webhooks/triggerWebhookTransaction
|
2023-01-05 19:05:23 +01:00
|
|
|
*
|
|
|
|
* This method recycles part of the code of the StoredGroupEventHandler.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param Webhook $webhook
|
|
|
|
* @param TransactionGroup $group
|
2023-05-29 13:56:55 +02:00
|
|
|
*
|
2023-01-05 19:05:23 +01:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function triggerTransaction(Webhook $webhook, TransactionGroup $group): JsonResponse
|
|
|
|
{
|
2023-07-01 12:18:07 +02:00
|
|
|
app('log')->debug(sprintf('Now in triggerTransaction(%d, %d)', $webhook->id, $group->id));
|
2023-01-05 19:05:23 +01:00
|
|
|
/** @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:
|
2023-06-29 11:34:34 +02:00
|
|
|
app('log')->debug('send event RequestedSendWebhookMessages');
|
2023-01-05 19:05:23 +01:00
|
|
|
event(new RequestedSendWebhookMessages());
|
2023-05-29 13:56:55 +02:00
|
|
|
|
2023-01-05 19:05:23 +01:00
|
|
|
return response()->json([], 204);
|
|
|
|
}
|
2021-03-28 11:39:26 +02:00
|
|
|
}
|