-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: add Matrix notification support #6717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keithah
wants to merge
6
commits into
coollabsio:v4.x
Choose a base branch
from
keithah:add-matrix-notifications
base: v4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d32619
feat: add Matrix notification support
keithah 8347bba
docs: add comprehensive Matrix notification testing guide
keithah 35552e6
fix: address CodeRabbit review feedback
keithah 728c39c
enhance: address additional CodeRabbit suggestions
keithah 3099dd1
docs: fix Matrix testing documentation issues
keithah a2942e5
clean: remove git filter-branch backup files
keithah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
keithah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
keithah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } 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'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
keithah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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', | ||
| ]; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public function team() | ||
| { | ||
| return $this->belongsTo(Team::class); | ||
| } | ||
|
|
||
| public function isEnabled() | ||
| { | ||
| return $this->matrix_enabled; | ||
keithah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.