Skip to content

Commit

Permalink
Add Gotify priority environment variable (#159)
Browse files Browse the repository at this point in the history
Adds environment variable `VIGILANT_NOTIFICATION_GOTIFY_PRIORITY` to
control Gotify message priority.
  • Loading branch information
VerifiedJoseph authored Apr 19, 2024
1 parent a6900fe commit 4f86a67
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
9 changes: 5 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ To send push notifications, a notification service is required. Vigilant support

To use Gotify, a URL and application token must be given.

| Name | Description |
| ------------------------------------ | -------------------------- |
| `VIGILANT_NOTIFICATION_GOTIFY_URL` | URL used to access Gotify. |
| `VIGILANT_NOTIFICATION_GOTIFY_TOKEN` | Gotify application token. |
| Name | Description |
| --------------------------------------- | --------------------------------------- |
| `VIGILANT_NOTIFICATION_GOTIFY_URL` | URL used to access Gotify. |
| `VIGILANT_NOTIFICATION_GOTIFY_TOKEN` | Gotify application token. |
| `VIGILANT_NOTIFICATION_GOTIFY_PRIORITY` | Gotify message priority. Defaults to 4. |

### Ntfy

Expand Down
2 changes: 1 addition & 1 deletion docs/feeds.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ feeds:
| `url` | **Yes** | Feed URL |
| `interval` | **Yes** | Interval between feed checks in seconds. Minimum value is `300` (5 minutes). |
| `gotify_token` | No | Gotify application token. Overrides token set with an environment variable. |
| `gotify_priority` | No | Gotify message priority. |
| `gotify_priority` | No | Gotify message priority. Overrides default value and/or environment variable. |
| `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. Overrides default value and/or environment variable. |
18 changes: 18 additions & 0 deletions src/Config/Validate/Gotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public function url(): void
$this->config['notification_gotify_url'] = $this->getEnv('NOTIFICATION_GOTIFY_URL');
}

/**
* Validate `VIGILANT_NOTIFICATION_GOTIFY_PRIORITY`
*
* @throws ConfigException if priority is not a number
*/
public function priority(): void
{
if ($this->hasEnv('NOTIFICATION_GOTIFY_PRIORITY') === true) {
if (is_numeric($this->getEnv('NOTIFICATION_GOTIFY_PRIORITY')) === false) {
throw new ConfigException(
'Gotify priority value is not a number [VIGILANT_NOTIFICATION_GOTIFY_PRIORITY]'
);
}

$this->config['notification_gotify_priority'] = (int) $this->getEnv('NOTIFICATION_GOTIFY_PRIORITY');
}
}

/**
* Validate `VIGILANT_NOTIFICATION_GOTIFY_TOKEN`
*
Expand Down
9 changes: 5 additions & 4 deletions src/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ public function notificationService(array $supportedServices): void
$this->config['notification_service'] = $service;

if ($service === 'gotify') {
$ntfy = new Validate\Gotify($this->config);
$ntfy->url();
$ntfy->token();
$this->config = $ntfy->getConfig();
$gotify = new Validate\Gotify($this->config);
$gotify->url();
$gotify->priority();
$gotify->token();
$this->config = $gotify->getConfig();
}

if ($service === 'ntfy') {
Expand Down
28 changes: 28 additions & 0 deletions tests/Config/Validate/GotifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public function testNoUrl(): void
$validate->url();
}

/**
* Test priority
*/
public function testPriority(): void
{
putenv('VIGILANT_NOTIFICATION_GOTIFY_PRIORITY=1');

$validate = new Validate(self::$defaults);
$validate->priority();
$config = $validate->getConfig();

$this->assertEquals(1, $config['notification_gotify_priority']);
}

/**
* Test priority is not a number
*/
public function testPriorityNotNumber(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Gotify priority value is not a number');

putenv('VIGILANT_NOTIFICATION_GOTIFY_PRIORITY=hello');

$validate = new Validate(self::$defaults);
$validate->priority();
}

/**
* Test token
*/
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function resetEnvs(): void
// Gotify
putenv('VIGILANT_NOTIFICATION_GOTIFY_URL');
putenv('VIGILANT_NOTIFICATION_GOTIFY_TOKEN');
putenv('VIGILANT_NOTIFICATION_GOTIFY_PRIORITY');

// Ntfy
putenv('VIGILANT_NOTIFICATION_NTFY_URL');
Expand Down

0 comments on commit 4f86a67

Please sign in to comment.