From 5c1c0e9ecb6682498656c22c94ae516bd347c748 Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 19 Apr 2024 18:06:20 +0100 Subject: [PATCH 1/4] Add checking for `VIGILANT_NOTIFICATION_NTFY_PRIORITY` --- src/Config/Validate/Ntfy.php | 16 ++++++++++++++++ src/Config/Validator.php | 1 + 2 files changed, 17 insertions(+) diff --git a/src/Config/Validate/Ntfy.php b/src/Config/Validate/Ntfy.php index c14e0080..1a947332 100644 --- a/src/Config/Validate/Ntfy.php +++ b/src/Config/Validate/Ntfy.php @@ -21,6 +21,22 @@ public function url(): void $this->config['notification_ntfy_url'] = $this->getEnv('NOTIFICATION_NTFY_URL'); } + /** + * Validate `VIGILANT_NOTIFICATION_NTFY_PRIORITY` + * + * @throws ConfigException if priority is not a number + */ + public function priority(): void + { + if ($this->hasEnv('NOTIFICATION_NTFY_PRIORITY') === true) { + if (is_numeric($this->getEnv('NOTIFICATION_NTFY_PRIORITY')) === false) { + throw new ConfigException('Ntfy priority value is not a number [VIGILANT_NOTIFICATION_NTFY_TOPIC]'); + } + + $this->config['notification_ntfy_priority'] = $this->getEnv('NOTIFICATION_NTFY_TOPIC'); + } + } + /** * Validate `VIGILANT_NOTIFICATION_NTFY_TOPIC` * diff --git a/src/Config/Validator.php b/src/Config/Validator.php index ae8e33b3..45754d4c 100644 --- a/src/Config/Validator.php +++ b/src/Config/Validator.php @@ -127,6 +127,7 @@ public function notificationService(array $supportedServices): void $ntfy = new Validate\Ntfy($this->config); $ntfy->url(); $ntfy->topic(); + $ntfy->priority(); $ntfy->auth(); $this->config = $ntfy->getConfig(); } From ed2741cd0b78863a07dde5566a5c73dd6132dad9 Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 19 Apr 2024 18:10:39 +0100 Subject: [PATCH 2/4] Add tests --- src/Config/Validate/Ntfy.php | 2 +- tests/Config/Validate/NtfyTest.php | 28 ++++++++++++++++++++++++++++ tests/TestCase.php | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Config/Validate/Ntfy.php b/src/Config/Validate/Ntfy.php index 1a947332..4e8309bc 100644 --- a/src/Config/Validate/Ntfy.php +++ b/src/Config/Validate/Ntfy.php @@ -33,7 +33,7 @@ public function priority(): void throw new ConfigException('Ntfy priority value is not a number [VIGILANT_NOTIFICATION_NTFY_TOPIC]'); } - $this->config['notification_ntfy_priority'] = $this->getEnv('NOTIFICATION_NTFY_TOPIC'); + $this->config['notification_ntfy_priority'] = (int) $this->getEnv('NOTIFICATION_NTFY_PRIORITY'); } } diff --git a/tests/Config/Validate/NtfyTest.php b/tests/Config/Validate/NtfyTest.php index 5be19c75..924ad340 100644 --- a/tests/Config/Validate/NtfyTest.php +++ b/tests/Config/Validate/NtfyTest.php @@ -49,6 +49,34 @@ public function testNoUrl(): void $validate->url(); } + /** + * Test priority + */ + public function testPriority(): void + { + putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY=1'); + + $validate = new Validate(self::$defaults); + $validate->priority(); + $config = $validate->getConfig(); + + $this->assertEquals(1, $config['notification_ntfy_priority']); + } + + /** + * Test priority is not a number + */ + public function testPriorityNotNumber(): void + { + $this->expectException(ConfigException::class); + $this->expectExceptionMessage('Ntfy priority value is not a number'); + + putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY=hello'); + + $validate = new Validate(self::$defaults); + $validate->priority(); + } + /** * Test topic */ diff --git a/tests/TestCase.php b/tests/TestCase.php index a522cd6b..5161360f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -46,6 +46,7 @@ protected function resetEnvs(): void // Ntfy putenv('VIGILANT_NOTIFICATION_NTFY_URL'); + putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY'); putenv('VIGILANT_NOTIFICATION_NTFY_TOPIC'); putenv('VIGILANT_NOTIFICATION_NTFY_AUTH'); putenv('VIGILANT_NOTIFICATION_NTFY_TOKEN'); From 8dbd56d4db53ebdcfd5de068998d00f4876f8838 Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 19 Apr 2024 18:14:57 +0100 Subject: [PATCH 3/4] Update configuration.md --- docs/configuration.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0cedc085..b1f9a9f3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -41,10 +41,11 @@ To use Gotify, a URL and application token must be given. To use Ntfy, a URL and topic must be given, all other environment variable are optional. -| Name | Description | -| ------------------------------------ | ---------------------- | -| `VIGILANT_NOTIFICATION_NTFY_URL` | URL used to access Ntfy. | -| `VIGILANT_NOTIFICATION_NTFY_TOPIC` | Ntfy topic. | +| Name | Description | +| ------------------------------------- | --------------------------------------------------------------------------------------- | +| `VIGILANT_NOTIFICATION_NTFY_URL` | URL used to access Ntfy. | +| `VIGILANT_NOTIFICATION_NTFY_TOPIC` | Ntfy topic. | +| `VIGILANT_NOTIFICATION_NTFY_PRIORITY` | Ntfy [message priority](https://docs.ntfy.sh/publish/#message-priority). Defaults to 3. | Vigilant can be used a with ntfy server that has password or [access token](https://docs.ntfy.sh/config/#access-tokens) authentication enabled. From 48a3532c4f7ac9831e7aa1b280b1b2f3e289a2dd Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 19 Apr 2024 18:18:27 +0100 Subject: [PATCH 4/4] Update feeds.md --- docs/feeds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/feeds.md b/docs/feeds.md index f0fe4789..0a002f63 100644 --- a/docs/feeds.md +++ b/docs/feeds.md @@ -29,4 +29,4 @@ feeds: | `gotify_priority` | No | Gotify message priority. | | `ntfy_topic` | No | Ntfy topic. Overrides topic set with an environment variable. | | `ntfy_token` | No | Ntfy access token. Overrides token set with an environment variable. | -| `ntfy_priority` | No | Ntfy message priority. | +| `ntfy_priority` | No | Ntfy message priority. Overrides default value and/or environment variable. |