firefly-iii/app/Models/Webhook.php

144 lines
5.0 KiB
PHP
Raw Normal View History

2020-11-29 11:35:49 -06:00
<?php
2021-01-29 11:50:35 -06:00
/*
* Webhook.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-11-29 11:35:49 -06:00
namespace FireflyIII\Models;
2021-08-28 08:47:09 -05:00
2021-05-24 01:50:17 -05:00
use Eloquent;
2020-11-29 11:35:49 -06:00
use FireflyIII\User;
2021-05-24 01:50:17 -05:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
2020-11-29 11:35:49 -06:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2020-11-29 11:35:49 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
2021-05-24 01:50:17 -05:00
use Illuminate\Support\Carbon;
2020-11-29 11:35:49 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
2020-12-03 23:20:44 -06:00
* FireflyIII\Models\Webhook
2020-11-29 11:35:49 -06:00
*
2021-08-28 08:47:09 -05:00
* @property int $id
2021-05-24 01:50:17 -05:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property int $user_id
* @property bool $active
* @property int $trigger
* @property int $response
* @property int $delivery
* @property string $url
* @property-read User $user
* @property-read Collection|WebhookMessage[] $webhookMessages
* @property-read int|null $webhook_messages_count
* @method static Builder|Webhook newModelQuery()
* @method static Builder|Webhook newQuery()
2020-12-03 23:20:44 -06:00
* @method static \Illuminate\Database\Query\Builder|Webhook onlyTrashed()
2021-05-24 01:50:17 -05:00
* @method static Builder|Webhook query()
* @method static Builder|Webhook whereActive($value)
* @method static Builder|Webhook whereCreatedAt($value)
* @method static Builder|Webhook whereDeletedAt($value)
* @method static Builder|Webhook whereDelivery($value)
* @method static Builder|Webhook whereId($value)
* @method static Builder|Webhook whereResponse($value)
* @method static Builder|Webhook whereTrigger($value)
* @method static Builder|Webhook whereUpdatedAt($value)
* @method static Builder|Webhook whereUrl($value)
* @method static Builder|Webhook whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|Webhook withTrashed()
* @method static \Illuminate\Database\Query\Builder|Webhook withoutTrashed()
2021-05-24 01:50:17 -05:00
* @mixin Eloquent
2021-08-28 08:47:09 -05:00
* @property string $title
* @property string $secret
2021-05-24 01:50:17 -05:00
* @method static Builder|Webhook whereSecret($value)
* @method static Builder|Webhook whereTitle($value)
* @property int|null $user_group_id
2021-09-18 03:08:10 -05:00
* @method static Builder|Webhook whereUserGroupId($value)
2020-11-29 11:35:49 -06:00
*/
class Webhook extends Model
{
use SoftDeletes;
2020-11-29 11:35:49 -06:00
// dont forget to update the config in firefly.php
// triggers
2022-03-29 07:59:58 -05:00
public const DELIVERY_JSON = 300;
public const RESPONSE_ACCOUNTS = 210;
public const RESPONSE_NONE = 220;
2020-11-29 11:35:49 -06:00
// actions
public const RESPONSE_TRANSACTIONS = 200;
2022-03-29 07:59:58 -05:00
public const TRIGGER_DESTROY_TRANSACTION = 120;
public const TRIGGER_STORE_TRANSACTION = 100;
2020-11-29 11:35:49 -06:00
// delivery
2022-03-29 07:59:58 -05:00
public const TRIGGER_UPDATE_TRANSACTION = 110;
2020-11-29 11:35:49 -06:00
protected $casts
= [
2020-11-29 11:35:49 -06:00
'active' => 'boolean',
'trigger' => 'integer',
'response' => 'integer',
'delivery' => 'integer',
];
2021-08-28 08:47:09 -05:00
protected $fillable = ['active', 'trigger', 'response', 'delivery', 'user_id', 'url', 'title', 'secret'];
2020-11-29 11:35:49 -06:00
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
* @return Webhook
* @throws NotFoundHttpException
2020-11-29 11:35:49 -06:00
*/
public static function routeBinder(string $value): Webhook
{
if (auth()->check()) {
2022-03-29 07:59:58 -05:00
$webhookId = (int) $value;
2020-11-29 11:35:49 -06:00
/** @var User $user */
$user = auth()->user();
/** @var Webhook $webhook */
2021-03-07 05:13:22 -06:00
$webhook = $user->webhooks()->find($webhookId);
2020-11-29 11:35:49 -06:00
if (null !== $webhook) {
return $webhook;
}
}
throw new NotFoundHttpException;
}
/**
* @codeCoverageIgnore
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @codeCoverageIgnore
* @return HasMany
*/
public function webhookMessages(): HasMany
{
return $this->hasMany(WebhookMessage::class);
}
2020-12-21 22:35:06 -06:00
}