Files
firefly-iii/app/Api/V1/Controllers/Webhook/ShowController.php

164 lines
6.3 KiB
PHP
Raw Normal View History

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;
2023-12-29 08:19:04 +01:00
use Illuminate\Support\Facades\Log;
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;
2023-12-29 08:42:03 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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
{
2023-12-02 12:56:48 +01:00
public const string 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
*
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2020-11-29 18:35:49 +01:00
*/
public function index(): JsonResponse
{
if (false === config('firefly.allow_webhooks')) {
2023-12-29 12:06:23 +01:00
Log::channel('audit')->info('User tries to view all webhooks, but webhooks are DISABLED.');
2023-12-29 08:42:03 +01:00
throw new NotFoundHttpException('Webhooks are not enabled.');
}
2023-12-29 08:19:04 +01:00
Log::channel('audit')->info('User views all webhooks.');
$manager = $this->getManager();
$collection = $this->repository->all();
$pageSize = $this->parameters->get('limit');
$count = $collection->count();
$webhooks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
2021-03-07 12:13:22 +01:00
// make paginator:
$paginator = new LengthAwarePaginator($webhooks, $count, $pageSize, $this->parameters->get('page'));
2023-12-20 19:35:52 +01: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);
$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.
*/
public function show(Webhook $webhook): JsonResponse
{
if (false === config('firefly.allow_webhooks')) {
2023-12-29 12:06:23 +01:00
Log::channel('audit')->info(sprintf('User tries to view webhook #%d, but webhooks are DISABLED.', $webhook->id));
2023-12-29 08:42:03 +01:00
throw new NotFoundHttpException('Webhooks are not enabled.');
}
2023-12-29 08:19:04 +01:00
Log::channel('audit')->info(sprintf('User views webhook #%d.', $webhook->id));
$manager = $this->getManager();
2021-03-07 12:13:22 +01:00
/** @var WebhookTransformer $transformer */
$transformer = app(WebhookTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($webhook, $transformer, self::RESOURCE_KEY);
2021-03-07 12:13:22 +01:00
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.
*/
public function triggerTransaction(Webhook $webhook, TransactionGroup $group): JsonResponse
{
if (false === config('firefly.allow_webhooks')) {
2023-12-29 12:06:23 +01:00
Log::channel('audit')->info(sprintf('User tries to trigger webhook #%d on transaction group #%d, but webhooks are DISABLED.', $webhook->id, $group->id));
2023-12-29 08:42:03 +01:00
throw new NotFoundHttpException('Webhooks are not enabled.');
}
2023-07-01 12:18:07 +02:00
app('log')->debug(sprintf('Now in triggerTransaction(%d, %d)', $webhook->id, $group->id));
2023-12-29 08:19:04 +01:00
Log::channel('audit')->info(sprintf('User triggers webhook #%d on transaction group #%d.', $webhook->id, $group->id));
2023-12-29 08:25:01 +01:00
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
}