| 
									
										
										
										
											2020-12-02 19:30:09 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-01-29 18:50:35 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * WebhookEventHandler.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 19:30:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Handlers\Events; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 07:01:26 +01:00
										 |  |  | use FireflyIII\Jobs\SendWebhookMessage; | 
					
						
							| 
									
										
										
										
											2020-12-02 19:30:09 +01:00
										 |  |  | use FireflyIII\Models\WebhookMessage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class WebhookEventHandler | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class WebhookEventHandler | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-12-04 20:19:52 +01:00
										 |  |  |      * Will try to send at most 3 messages so the flow doesn't get broken too much. | 
					
						
							| 
									
										
										
										
											2020-12-02 19:30:09 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function sendWebhookMessages(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2021-01-15 21:01:53 +01:00
										 |  |  |         // kick off the job!
 | 
					
						
							| 
									
										
										
										
											2023-01-15 15:47:25 +01:00
										 |  |  |         $messages = WebhookMessage::where('webhook_messages.sent', false) | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                                   ->get(['webhook_messages.*']) | 
					
						
							|  |  |  |                                   ->filter( | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |                                       static function (WebhookMessage $message) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                                           return $message->webhookAttempts()->count() <= 2; | 
					
						
							|  |  |  |                                       } | 
					
						
							|  |  |  |                                   )->splice(0, 5); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Found %d webhook message(s) ready to be send.', $messages->count())); | 
					
						
							| 
									
										
										
										
											2020-12-05 07:01:26 +01:00
										 |  |  |         foreach ($messages as $message) { | 
					
						
							| 
									
										
										
										
											2023-01-05 19:05:23 +01:00
										 |  |  |             if (false === $message->sent) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Send message #%d', $message->id)); | 
					
						
							| 
									
										
										
										
											2023-01-05 19:05:23 +01:00
										 |  |  |                 SendWebhookMessage::dispatch($message)->afterResponse(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (false !== $message->sent) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Skip message #%d', $message->id)); | 
					
						
							| 
									
										
										
										
											2023-01-05 19:05:23 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-05 07:01:26 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-02 19:30:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-22 05:35:06 +01:00
										 |  |  | } |