| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  | <?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); | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Models; | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  | use Eloquent; | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2023-01-05 19:05:23 +01:00
										 |  |  | use Illuminate\Database\Eloquent\Casts\Attribute; | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Collection; | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  | 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-05-24 08:50:17 +02:00
										 |  |  | use Illuminate\Support\Carbon; | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-12-04 06:20:44 +01:00
										 |  |  |  * FireflyIII\Models\WebhookMessage | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |  * @property int $id | 
					
						
							|  |  |  |  * @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 | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |  * @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 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |  * @property-read int|null $webhook_attempts_count | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class WebhookMessage extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $casts | 
					
						
							|  |  |  |         = [ | 
					
						
							|  |  |  |             'sent'    => 'boolean', | 
					
						
							|  |  |  |             'errored' => 'boolean', | 
					
						
							|  |  |  |             'uuid'    => 'string', | 
					
						
							|  |  |  |             'message' => 'json', | 
					
						
							| 
									
										
										
										
											2021-05-24 08:06:56 +02:00
										 |  |  |             'logs'    => 'json', | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Route binder. Converts the key in the URL to the specified object (or throw 404). | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  string  $value | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return WebhookMessage | 
					
						
							|  |  |  |      * @throws NotFoundHttpException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function routeBinder(string $value): WebhookMessage | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (auth()->check()) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $messageId = (int)$value; | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  |             /** @var User $user */ | 
					
						
							|  |  |  |             $user = auth()->user(); | 
					
						
							|  |  |  |             /** @var 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
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |         throw new NotFoundHttpException(); | 
					
						
							| 
									
										
										
										
											2021-03-07 12:13:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 06:54:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * @return BelongsTo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function webhook(): BelongsTo | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo(Webhook::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 06:20:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * @return HasMany | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function webhookAttempts(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(WebhookAttempt::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-05 19:05:23 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the amount | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Attribute | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function sent(): Attribute | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Attribute::make( | 
					
						
							|  |  |  |             get: fn ($value) => (bool)$value, | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-22 05:35:06 +01:00
										 |  |  | } |