Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions app/Jobs/SendMessageToMatrixJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Jobs;

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

class SendMessageToMatrixJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
private MatrixMessage $message,
private string $homeserverUrl,
private string $roomId,
private string $accessToken
) {
$this->onQueue('high');
}

public function handle(): void
{
$transactionId = 'coolify_' . time() . '_' . rand(1000, 9999);
$url = rtrim($this->homeserverUrl, '/') . "/_matrix/client/r0/rooms/{$this->roomId}/send/m.room.message/{$transactionId}";

$body = [
'msgtype' => 'm.text',
'body' => "{$this->message->title}\n\n{$this->message->description}",
'format' => 'org.matrix.custom.html',
'formatted_body' => "<h3 style=\"color: {$this->message->color}\">{$this->message->title}</h3><p>{$this->message->description}</p>",
];

Http::withHeaders([
'Authorization' => 'Bearer ' . $this->accessToken,
'Content-Type' => 'application/json',
])->put($url, $body);
}
}
207 changes: 207 additions & 0 deletions app/Livewire/Notifications/Matrix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

namespace App\Livewire\Notifications;

use App\Models\MatrixNotificationSettings;
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 Matrix extends Component
{
use AuthorizesRequests;

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

#[Locked]
public Team $team;

#[Locked]
public MatrixNotificationSettings $settings;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

public function mount()
{
try {
$this->team = auth()->user()->currentTeam();
$this->settings = $this->team->matrixNotificationSettings;
$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->matrix_enabled = $this->matrixEnabled;
$this->settings->matrix_homeserver_url = $this->matrixHomeserverUrl;
$this->settings->matrix_room_id = $this->matrixRoomId;
$this->settings->matrix_access_token = $this->matrixAccessToken;
$this->settings->matrix_friendly_name = $this->matrixFriendlyName;

$this->settings->deployment_success_matrix_notifications = $this->deploymentSuccessMatrixNotifications;
$this->settings->deployment_failure_matrix_notifications = $this->deploymentFailureMatrixNotifications;
$this->settings->status_change_matrix_notifications = $this->statusChangeMatrixNotifications;
$this->settings->backup_success_matrix_notifications = $this->backupSuccessMatrixNotifications;
$this->settings->backup_failure_matrix_notifications = $this->backupFailureMatrixNotifications;
$this->settings->scheduled_task_success_matrix_notifications = $this->scheduledTaskSuccessMatrixNotifications;
$this->settings->scheduled_task_failure_matrix_notifications = $this->scheduledTaskFailureMatrixNotifications;
$this->settings->docker_cleanup_success_matrix_notifications = $this->dockerCleanupSuccessMatrixNotifications;
$this->settings->docker_cleanup_failure_matrix_notifications = $this->dockerCleanupFailureMatrixNotifications;
$this->settings->server_disk_usage_matrix_notifications = $this->serverDiskUsageMatrixNotifications;
$this->settings->server_reachable_matrix_notifications = $this->serverReachableMatrixNotifications;
$this->settings->server_unreachable_matrix_notifications = $this->serverUnreachableMatrixNotifications;
$this->settings->server_patch_matrix_notifications = $this->serverPatchMatrixNotifications;

$this->settings->save();
refreshSession();
} else {
$this->matrixEnabled = $this->settings->matrix_enabled;
$this->matrixHomeserverUrl = $this->settings->matrix_homeserver_url;
$this->matrixRoomId = $this->settings->matrix_room_id;
$this->matrixAccessToken = $this->settings->matrix_access_token;
$this->matrixFriendlyName = $this->settings->matrix_friendly_name;

$this->deploymentSuccessMatrixNotifications = $this->settings->deployment_success_matrix_notifications;
$this->deploymentFailureMatrixNotifications = $this->settings->deployment_failure_matrix_notifications;
$this->statusChangeMatrixNotifications = $this->settings->status_change_matrix_notifications;
$this->backupSuccessMatrixNotifications = $this->settings->backup_success_matrix_notifications;
$this->backupFailureMatrixNotifications = $this->settings->backup_failure_matrix_notifications;
$this->scheduledTaskSuccessMatrixNotifications = $this->settings->scheduled_task_success_matrix_notifications;
$this->scheduledTaskFailureMatrixNotifications = $this->settings->scheduled_task_failure_matrix_notifications;
$this->dockerCleanupSuccessMatrixNotifications = $this->settings->docker_cleanup_success_matrix_notifications;
$this->dockerCleanupFailureMatrixNotifications = $this->settings->docker_cleanup_failure_matrix_notifications;
$this->serverDiskUsageMatrixNotifications = $this->settings->server_disk_usage_matrix_notifications;
$this->serverReachableMatrixNotifications = $this->settings->server_reachable_matrix_notifications;
$this->serverUnreachableMatrixNotifications = $this->settings->server_unreachable_matrix_notifications;
$this->serverPatchMatrixNotifications = $this->settings->server_patch_matrix_notifications;
}
}

public function instantSaveMatrixEnabled()
{
try {
$this->validate([
'matrixHomeserverUrl' => 'required',
'matrixRoomId' => 'required',
'matrixAccessToken' => 'required',
], [
'matrixHomeserverUrl.required' => 'Matrix Homeserver URL is required.',
'matrixRoomId.required' => 'Matrix Room ID is required.',
'matrixAccessToken.required' => 'Matrix Access Token is required.',
]);
$this->saveModel();
} catch (\Throwable $e) {
$this->matrixEnabled = 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: 'matrix'));
$this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

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

namespace App\Models;

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

class MatrixNotificationSettings extends Model
{
use Notifiable;

public $timestamps = false;

protected $fillable = [
'team_id',

'matrix_enabled',
'matrix_homeserver_url',
'matrix_room_id',
'matrix_access_token',
'matrix_friendly_name',

'deployment_success_matrix_notifications',
'deployment_failure_matrix_notifications',
'status_change_matrix_notifications',
'backup_success_matrix_notifications',
'backup_failure_matrix_notifications',
'scheduled_task_success_matrix_notifications',
'scheduled_task_failure_matrix_notifications',
'docker_cleanup_success_matrix_notifications',
'docker_cleanup_failure_matrix_notifications',
'server_disk_usage_matrix_notifications',
'server_reachable_matrix_notifications',
'server_unreachable_matrix_notifications',
'server_patch_matrix_notifications',
];

protected $casts = [
'matrix_enabled' => 'boolean',
'matrix_homeserver_url' => 'encrypted',
'matrix_room_id' => 'encrypted',
'matrix_access_token' => 'encrypted',

'deployment_success_matrix_notifications' => 'boolean',
'deployment_failure_matrix_notifications' => 'boolean',
'status_change_matrix_notifications' => 'boolean',
'backup_success_matrix_notifications' => 'boolean',
'backup_failure_matrix_notifications' => 'boolean',
'scheduled_task_success_matrix_notifications' => 'boolean',
'scheduled_task_failure_matrix_notifications' => 'boolean',
'docker_cleanup_success_matrix_notifications' => 'boolean',
'docker_cleanup_failure_matrix_notifications' => 'boolean',
'server_disk_usage_matrix_notifications' => 'boolean',
'server_reachable_matrix_notifications' => 'boolean',
'server_unreachable_matrix_notifications' => 'boolean',
'server_patch_matrix_notifications' => 'boolean',
];

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

public function isEnabled()
{
return $this->matrix_enabled;
}
}
17 changes: 15 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\SendsMatrix;
use App\Notifications\Channels\SendsPushover;
use App\Notifications\Channels\SendsSlack;
use App\Traits\HasNotificationSettings;
Expand Down Expand Up @@ -35,7 +36,7 @@
]
)]

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

Expand All @@ -53,6 +54,7 @@ protected static function booted()
$team->slackNotificationSettings()->create();
$team->telegramNotificationSettings()->create();
$team->pushoverNotificationSettings()->create();
$team->matrixNotificationSettings()->create();
});

static::saving(function ($team) {
Expand Down Expand Up @@ -187,7 +189,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('matrix')?->isEnabled();
}

public function subscriptionEnded()
Expand Down Expand Up @@ -306,4 +309,14 @@ public function pushoverNotificationSettings()
{
return $this->hasOne(PushoverNotificationSettings::class);
}

public function matrixNotificationSettings()
{
return $this->hasOne(MatrixNotificationSettings::class);
}

public function routeNotificationForMatrix()
{
return null;
}
}
Loading