Skip to content

Commit

Permalink
fix: Improved pusher notification
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 3, 2024
1 parent 4664d75 commit ec80ce7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/Jobs/BackupTasks/SendPushoverNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class SendPushoverNotificationJob implements ShouldQueue
* @param BackupTask $backupTask The backup task associated with the notification
* @param BackupTaskLog $backupTaskLog The log entry for the backup task
* @param string $notificationStreamValue The value of the notification stream
* @param string $additionalNotificationStreamValue An additional token needed for Pushover
*/
public function __construct(
public BackupTask $backupTask,
public BackupTaskLog $backupTaskLog,
public string $notificationStreamValue
public string $notificationStreamValue,
public string $additionalNotificationStreamValue,
) {
//
}
Expand All @@ -47,6 +49,6 @@ public function __construct(
*/
public function handle(): void
{
$this->backupTask->sendPushoverNotification($this->backupTaskLog, $this->notificationStreamValue);
$this->backupTask->sendPushoverNotification($this->backupTaskLog, $this->notificationStreamValue, $this->additionalNotificationStreamValue);
}
}
10 changes: 7 additions & 3 deletions app/Models/BackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function getBackupTasksData(): array
->get();

$dates = Collection::make($startDate->daysUntil($endDate)->toArray())
->map(fn (CarbonInterface $date): string => $date->format('Y-m-d'));
->map(fn (CarbonInterface $carbon): string => $carbon->format('Y-m-d'));

$fileBackups = $databaseBackups = array_fill_keys($dates->toArray(), 0);

Expand Down Expand Up @@ -706,12 +706,15 @@ public function dispatchNotification(NotificationStream $notificationStream, Bac
{
$streamValue = (string) $notificationStream->getAttribute('value');

/* This is one of the additional fields, out of two, to supply additional data for the external service to operate. */
$additionalStreamValueOne = (string) $notificationStream->getAttribute('additional_field_one');

match ($notificationStream->getAttribute('type')) {
NotificationStream::TYPE_EMAIL => $this->sendEmailNotification($backupTaskLog, $streamValue),
NotificationStream::TYPE_DISCORD => SendDiscordNotificationJob::dispatch($this, $backupTaskLog, $streamValue)->onQueue($queue),
NotificationStream::TYPE_SLACK => SendSlackNotificationJob::dispatch($this, $backupTaskLog, $streamValue)->onQueue($queue),
NotificationStream::TYPE_TEAMS => SendTeamsNotificationJob::dispatch($this, $backupTaskLog, $streamValue)->onQueue($queue),
NotificationStream::TYPE_PUSHOVER => SendPushoverNotificationJob::dispatch($this, $backupTaskLog, $streamValue)->onQueue($queue),
NotificationStream::TYPE_PUSHOVER => SendPushoverNotificationJob::dispatch($this, $backupTaskLog, $streamValue, $additionalStreamValueOne)->onQueue($queue),
default => throw new InvalidArgumentException("Unsupported notification type: {$notificationStream->getAttribute('type')}"),
};
}
Expand Down Expand Up @@ -914,7 +917,7 @@ public function sendTeamsWebhookNotification(BackupTaskLog $backupTaskLog, strin
* @param BackupTaskLog $backupTaskLog The log entry for the backup task
* @param string $pushoverToken The Pushover API token
*/
public function sendPushoverNotification(BackupTaskLog $backupTaskLog, string $pushoverToken): void
public function sendPushoverNotification(BackupTaskLog $backupTaskLog, string $pushoverToken, string $userToken): void
{
$isSuccessful = $backupTaskLog->getAttribute('successful_at') !== null;
$status = $isSuccessful ? 'success' : 'failure';
Expand All @@ -925,6 +928,7 @@ public function sendPushoverNotification(BackupTaskLog $backupTaskLog, string $p

$payload = [
'token' => $pushoverToken,
'user' => $userToken,
'title' => "{$this->label} Backup Task: " . ucfirst($status),
'message' => $message . " Details:\n" .
'Backup Type: ' . ucfirst($this->type) . "\n" .
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Models/BackupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,13 @@
$task = BackupTask::factory()->create();
$log = BackupTaskLog::factory()->create(['backup_task_id' => $task->id]);
$pushoverToken = 'abc123';
$userToken = 'def456';

Http::fake([
'https://api.pushover.net/1/messages.json' => Http::response('', 200),
]);

$task->sendPushoverNotification($log, $pushoverToken);
$task->sendPushoverNotification($log, $pushoverToken, $userToken);

Http::assertSent(fn ($request): bool => $request->url() === 'https://api.pushover.net/1/messages.json');
});
Expand All @@ -925,12 +926,13 @@
$task = BackupTask::factory()->create();
$log = BackupTaskLog::factory()->create(['backup_task_id' => $task->id]);
$pushoverToken = 'abc123';
$userToken = 'def456';

Http::fake([
'https://api.pushover.net/1/messages.json' => Http::response('Error', 500),
]);

expect(fn () => $task->sendPushoverNotification($log, $pushoverToken))
expect(fn () => $task->sendPushoverNotification($log, $pushoverToken, $userToken))
->toThrow(RuntimeException::class, 'Pushover notification failed: Error');
});

Expand Down

0 comments on commit ec80ce7

Please sign in to comment.