2020-12-01 23:54:13 -06:00
|
|
|
<?php
|
2021-01-29 11:50:35 -06:00
|
|
|
/*
|
|
|
|
* WebhookMessage.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-01 23:54:13 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
2021-05-24 01:06:56 -05:00
|
|
|
|
2021-05-24 01:50:17 -05:00
|
|
|
use Eloquent;
|
2021-03-07 05:13:22 -06:00
|
|
|
use FireflyIII\User;
|
2021-05-24 01:50:17 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-12-01 23:54:13 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2020-12-03 23:20:44 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2021-05-24 01:50:17 -05:00
|
|
|
use Illuminate\Support\Carbon;
|
2021-03-07 05:13:22 -06:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2020-12-01 23:54:13 -06:00
|
|
|
|
|
|
|
/**
|
2020-12-03 23:20:44 -06:00
|
|
|
* FireflyIII\Models\WebhookMessage
|
2020-12-01 23:54:13 -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 string|null $deleted_at
|
|
|
|
* @property int $webhook_id
|
|
|
|
* @property bool $sent
|
|
|
|
* @property bool $errored
|
|
|
|
* @property int $attempts
|
|
|
|
* @property string $uuid
|
|
|
|
* @property array $message
|
|
|
|
* @property array|null $logs
|
|
|
|
* @property-read Webhook $webhook
|
|
|
|
* @method static Builder|WebhookMessage newModelQuery()
|
|
|
|
* @method static Builder|WebhookMessage newQuery()
|
|
|
|
* @method static Builder|WebhookMessage query()
|
|
|
|
* @method static Builder|WebhookMessage whereAttempts($value)
|
|
|
|
* @method static Builder|WebhookMessage whereCreatedAt($value)
|
|
|
|
* @method static Builder|WebhookMessage whereDeletedAt($value)
|
|
|
|
* @method static Builder|WebhookMessage whereErrored($value)
|
|
|
|
* @method static Builder|WebhookMessage whereId($value)
|
|
|
|
* @method static Builder|WebhookMessage whereLogs($value)
|
|
|
|
* @method static Builder|WebhookMessage whereMessage($value)
|
|
|
|
* @method static Builder|WebhookMessage whereSent($value)
|
|
|
|
* @method static Builder|WebhookMessage whereUpdatedAt($value)
|
|
|
|
* @method static Builder|WebhookMessage whereUuid($value)
|
|
|
|
* @method static Builder|WebhookMessage whereWebhookId($value)
|
|
|
|
* @mixin Eloquent
|
|
|
|
* @property-read Collection|WebhookAttempt[] $webhookAttempts
|
|
|
|
* @property-read int|null $webhook_attempts_count
|
2020-12-01 23:54:13 -06:00
|
|
|
*/
|
|
|
|
class WebhookMessage extends Model
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $casts
|
|
|
|
= [
|
|
|
|
'sent' => 'boolean',
|
|
|
|
'errored' => 'boolean',
|
|
|
|
'uuid' => 'string',
|
|
|
|
'message' => 'json',
|
2021-05-24 01:06:56 -05:00
|
|
|
'logs' => 'json',
|
2020-12-01 23:54:13 -06:00
|
|
|
];
|
|
|
|
|
2021-03-07 05:13:22 -06:00
|
|
|
/**
|
|
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return WebhookMessage
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public static function routeBinder(string $value): WebhookMessage
|
|
|
|
{
|
|
|
|
if (auth()->check()) {
|
|
|
|
$messageId = (int)$value;
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
/** @var WebhookMessage $message */
|
|
|
|
$message = self::find($messageId);
|
2021-05-24 01:06:56 -05:00
|
|
|
if (null !== $message && $message->webhook->user_id === $user->id) {
|
|
|
|
return $message;
|
2021-03-07 05:13:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:54:13 -06:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
|
|
|
public function webhook(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Webhook::class);
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:20:44 -06:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function webhookAttempts(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(WebhookAttempt::class);
|
|
|
|
}
|
2020-12-21 22:35:06 -06:00
|
|
|
}
|