Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/Jobs/SendMessageToGotifyJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Jobs;

use App\Notifications\Dto\GotifyMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

class SendMessageToGotifyJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;

public $backoff = 10;

/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 5;

public function __construct(
public GotifyMessage $message,
public string $url,
public string $token,
) {
$this->onQueue('high');
}

/**
* Execute the job.
*/
public function handle(): void
{
$url = rtrim($this->url, '/').'/message?token='.$this->token;
$response = Http::post($url, $this->message->toPayload());
if ($response->failed()) {
throw new \RuntimeException('Gotify notification failed with '.$response->status().' status code.'.$response->body());
}
}
}
195 changes: 195 additions & 0 deletions app/Livewire/Notifications/Gotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace App\Livewire\Notifications;

use App\Models\GotifyNotificationSettings;
use App\Models\Team;
use App\Notifications\Test;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;

class Gotify extends Component
{
use AuthorizesRequests;

protected $listeners = ['refresh' => '$refresh'];

#[Locked]
public Team $team;

#[Locked]
public GotifyNotificationSettings $settings;

#[Validate(['boolean'])]
public bool $gotifyEnabled = false;

#[Validate(['nullable', 'string'])]
public ?string $gotifyUrl = null;

#[Validate(['nullable', 'string'])]
public ?string $gotifyToken = null;

#[Validate(['boolean'])]
public bool $deploymentSuccessGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $deploymentFailureGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $statusChangeGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $backupSuccessGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $backupFailureGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $scheduledTaskSuccessGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $scheduledTaskFailureGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $dockerCleanupSuccessGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $dockerCleanupFailureGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $serverDiskUsageGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $serverReachableGotifyNotifications = false;

#[Validate(['boolean'])]
public bool $serverUnreachableGotifyNotifications = true;

#[Validate(['boolean'])]
public bool $serverPatchGotifyNotifications = false;

public function mount()
{
try {
$this->team = auth()->user()->currentTeam();
$this->settings = $this->team->gotifyNotificationSettings;
$this->authorize('view', $this->settings);
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->authorize('update', $this->settings);
$this->settings->gotify_enabled = $this->gotifyEnabled;
$this->settings->gotify_url = $this->gotifyUrl;
$this->settings->gotify_token = $this->gotifyToken;

$this->settings->deployment_success_gotify_notifications = $this->deploymentSuccessGotifyNotifications;
$this->settings->deployment_failure_gotify_notifications = $this->deploymentFailureGotifyNotifications;
$this->settings->status_change_gotify_notifications = $this->statusChangeGotifyNotifications;
$this->settings->backup_success_gotify_notifications = $this->backupSuccessGotifyNotifications;
$this->settings->backup_failure_gotify_notifications = $this->backupFailureGotifyNotifications;
$this->settings->scheduled_task_success_gotify_notifications = $this->scheduledTaskSuccessGotifyNotifications;
$this->settings->scheduled_task_failure_gotify_notifications = $this->scheduledTaskFailureGotifyNotifications;
$this->settings->docker_cleanup_success_gotify_notifications = $this->dockerCleanupSuccessGotifyNotifications;
$this->settings->docker_cleanup_failure_gotify_notifications = $this->dockerCleanupFailureGotifyNotifications;
$this->settings->server_disk_usage_gotify_notifications = $this->serverDiskUsageGotifyNotifications;
$this->settings->server_reachable_gotify_notifications = $this->serverReachableGotifyNotifications;
$this->settings->server_unreachable_gotify_notifications = $this->serverUnreachableGotifyNotifications;
$this->settings->server_patch_gotify_notifications = $this->serverPatchGotifyNotifications;

$this->settings->save();
refreshSession();
} else {
$this->gotifyEnabled = $this->settings->gotify_enabled;
$this->gotifyUrl = $this->settings->gotify_url;
$this->gotifyToken = $this->settings->gotify_token;

$this->deploymentSuccessGotifyNotifications = $this->settings->deployment_success_gotify_notifications;
$this->deploymentFailureGotifyNotifications = $this->settings->deployment_failure_gotify_notifications;
$this->statusChangeGotifyNotifications = $this->settings->status_change_gotify_notifications;
$this->backupSuccessGotifyNotifications = $this->settings->backup_success_gotify_notifications;
$this->backupFailureGotifyNotifications = $this->settings->backup_failure_gotify_notifications;
$this->scheduledTaskSuccessGotifyNotifications = $this->settings->scheduled_task_success_gotify_notifications;
$this->scheduledTaskFailureGotifyNotifications = $this->settings->scheduled_task_failure_gotify_notifications;
$this->dockerCleanupSuccessGotifyNotifications = $this->settings->docker_cleanup_success_gotify_notifications;
$this->dockerCleanupFailureGotifyNotifications = $this->settings->docker_cleanup_failure_gotify_notifications;
$this->serverDiskUsageGotifyNotifications = $this->settings->server_disk_usage_gotify_notifications;
$this->serverReachableGotifyNotifications = $this->settings->server_reachable_gotify_notifications;
$this->serverUnreachableGotifyNotifications = $this->settings->server_unreachable_gotify_notifications;
$this->serverPatchGotifyNotifications = $this->settings->server_patch_gotify_notifications;
}
}

public function instantSaveGotifyEnabled()
{
try {
$this->validate([
'gotifyUrl' => 'required',
'gotifyToken' => 'required',
], [
'gotifyUrl.required' => 'Gotify URL is required.',
'gotifyToken.required' => 'Gotify Token is required.',
]);
$this->saveModel();
} catch (\Throwable $e) {
$this->gotifyEnabled = false;

return handleError($e, $this);
} finally {
$this->dispatch('refresh');
}
}

public function instantSave()
{
try {
$this->syncData(true);
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->dispatch('refresh');
}
}

public function submit()
{
try {
$this->resetErrorBag();
$this->syncData(true);
$this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function saveModel()
{
$this->syncData(true);
refreshSession();
$this->dispatch('success', 'Settings saved.');
}

public function sendTestNotification()
{
try {
$this->authorize('sendTest', $this->settings);
$this->team->notify(new Test(channel: 'gotify'));
$this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function render()
{
return view('livewire.notifications.gotify');
}
}
63 changes: 63 additions & 0 deletions app/Models/GotifyNotificationSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class GotifyNotificationSettings extends Model
{
use Notifiable;

public $timestamps = false;

protected $fillable = [
'team_id',

'gotify_enabled',
'gotify_url',
'gotify_token',

'deployment_success_gotify_notifications',
'deployment_failure_gotify_notifications',
'status_change_gotify_notifications',
'backup_success_gotify_notifications',
'backup_failure_gotify_notifications',
'scheduled_task_success_gotify_notifications',
'scheduled_task_failure_gotify_notifications',
'docker_cleanup_gotify_notifications',
'server_disk_usage_gotify_notifications',
'server_reachable_gotify_notifications',
'server_unreachable_gotify_notifications',
'server_patch_gotify_notifications',
];

protected $casts = [
'gotify_enabled' => 'boolean',
'gotify_url' => 'encrypted',
'gotify_token' => 'encrypted',

'deployment_success_gotify_notifications' => 'boolean',
'deployment_failure_gotify_notifications' => 'boolean',
'status_change_gotify_notifications' => 'boolean',
'backup_success_gotify_notifications' => 'boolean',
'backup_failure_gotify_notifications' => 'boolean',
'scheduled_task_success_gotify_notifications' => 'boolean',
'scheduled_task_failure_gotify_notifications' => 'boolean',
'docker_cleanup_gotify_notifications' => 'boolean',
'server_disk_usage_gotify_notifications' => 'boolean',
'server_reachable_gotify_notifications' => 'boolean',
'server_unreachable_gotify_notifications' => 'boolean',
'server_patch_gotify_notifications' => 'boolean',
];

public function team()
{
return $this->belongsTo(Team::class);
}

public function isEnabled()
{
return $this->gotify_enabled;
}
}
20 changes: 18 additions & 2 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\ServerReachabilityChanged;
use App\Notifications\Channels\SendsDiscord;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsGotify;
use App\Notifications\Channels\SendsPushover;
use App\Notifications\Channels\SendsSlack;
use App\Traits\HasNotificationSettings;
Expand Down Expand Up @@ -36,7 +37,7 @@
]
)]

class Team extends Model implements SendsDiscord, SendsEmail, SendsPushover, SendsSlack
class Team extends Model implements SendsDiscord, SendsEmail, SendsGotify, SendsPushover, SendsSlack
{
use HasFactory, HasNotificationSettings, HasSafeStringAttribute, Notifiable;

Expand All @@ -56,6 +57,7 @@ protected static function booted()
$team->slackNotificationSettings()->create();
$team->telegramNotificationSettings()->create();
$team->pushoverNotificationSettings()->create();
$team->gotifyNotificationSettings()->create();
$team->webhookNotificationSettings()->create();
});

Expand Down Expand Up @@ -168,6 +170,14 @@ public function routeNotificationForPushover()
];
}

public function routeNotificationForGotify()
{
return [
'url' => data_get($this, 'gotify_url', null),
'token' => data_get($this, 'gotify_token', null),
];
}

public function getRecipients(): array
{
$recipients = $this->members()->pluck('email')->toArray();
Expand All @@ -191,7 +201,8 @@ public function isAnyNotificationEnabled()
$this->getNotificationSettings('discord')?->isEnabled() ||
$this->getNotificationSettings('slack')?->isEnabled() ||
$this->getNotificationSettings('telegram')?->isEnabled() ||
$this->getNotificationSettings('pushover')?->isEnabled();
$this->getNotificationSettings('pushover')?->isEnabled() ||
$this->getNotificationSettings('gotify')?->isEnabled();
}

public function subscriptionEnded()
Expand Down Expand Up @@ -316,6 +327,11 @@ public function pushoverNotificationSettings()
return $this->hasOne(PushoverNotificationSettings::class);
}

public function gotifyNotificationSettings()
{
return $this->hasOne(GotifyNotificationSettings::class);
}

public function webhookNotificationSettings()
{
return $this->hasOne(WebhookNotificationSettings::class);
Expand Down
Loading