Files
firefly-iii/app/Models/WebhookMessage.php

98 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2021-01-29 18:50:35 +01: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-22 05:35:06 +01:00
declare(strict_types=1);
namespace FireflyIII\Models;
2021-05-24 08:06:56 +02:00
2023-12-10 06:51:59 +01:00
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
2021-03-07 12:13:22 +01:00
use FireflyIII\User;
2023-01-05 19:05:23 +01:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2020-12-04 06:20:44 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-03-07 12:13:22 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2023-12-10 06:51:59 +01:00
class WebhookMessage extends Model
{
2023-11-05 19:41:37 +01:00
use ReturnsIntegerIdTrait;
protected $casts
= [
'sent' => 'boolean',
'errored' => 'boolean',
'uuid' => 'string',
'message' => 'json',
2021-05-24 08:06:56 +02:00
'logs' => 'json',
];
2021-03-07 12:13:22 +01:00
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @throws NotFoundHttpException
*/
2023-11-04 14:18:49 +01:00
public static function routeBinder(string $value): self
2021-03-07 12:13:22 +01:00
{
if (auth()->check()) {
2024-12-22 08:43:12 +01:00
$messageId = (int) $value;
2023-12-20 19:35:52 +01:00
2021-03-07 12:13:22 +01:00
/** @var User $user */
$user = auth()->user();
2023-12-20 19:35:52 +01:00
/** @var null|WebhookMessage $message */
$message = self::find($messageId);
2021-05-24 08:06:56 +02:00
if (null !== $message && $message->webhook->user_id === $user->id) {
return $message;
2021-03-07 12:13:22 +01:00
}
}
2023-12-20 19:35:52 +01:00
2022-10-30 14:24:28 +01:00
throw new NotFoundHttpException();
2021-03-07 12:13:22 +01:00
}
public function webhook(): BelongsTo
{
return $this->belongsTo(Webhook::class);
}
2020-12-04 06:20:44 +01:00
public function webhookAttempts(): HasMany
{
return $this->hasMany(WebhookAttempt::class);
}
2023-02-22 18:14:14 +01:00
2023-01-05 19:05:23 +01:00
/**
* Get the amount
*/
protected function sent(): Attribute
{
return Attribute::make(
2024-12-22 08:43:12 +01:00
get: static fn ($value) => (bool) $value,
2023-01-05 19:05:23 +01:00
);
}
2023-11-05 19:41:37 +01:00
protected function webhookId(): Attribute
{
return Attribute::make(
2024-12-22 08:43:12 +01:00
get: static fn ($value) => (int) $value,
2023-11-05 19:41:37 +01:00
);
}
2020-12-22 05:35:06 +01:00
}