Saeed Vaziry e997d0deea
WIP notifications and other refactors (#88)
* WIP notifications and other refactors
- refactor notification channels
- send notifications on events related to the servers and sites
- delete server log files on server deletion
- add telegram notification channel
- add new icons
- cache configs and icons on installation and updates
- new navbar for dark mode and settings

* discord channel

* build assets

* pint
2024-01-07 09:54:08 +01:00

74 lines
1.7 KiB
PHP

<?php
namespace App\NotificationChannels;
use App\Contracts\Notification;
use Illuminate\Support\Facades\Http;
class Discord extends AbstractNotificationChannel
{
public function channel(): string
{
return 'discord';
}
public function createRules(array $input): array
{
return [
'webhook_url' => 'required|url',
];
}
public function createData(array $input): array
{
return [
'webhook_url' => $input['webhook_url'] ?? '',
];
}
public function data(): array
{
return [
'webhook_url' => $this->notificationChannel->data['webhook_url'] ?? '',
];
}
public function connect(): bool
{
$connect = $this->checkConnection(
__('Congratulations! 🎉'),
__("You've connected your Discord to :app", ['app' => config('app.name')])."\n".
__('Manage your notification channels')."\n".
route('notification-channels')
);
if (! $connect) {
$this->notificationChannel->delete();
return false;
}
$this->notificationChannel->connected = true;
$this->notificationChannel->save();
return true;
}
private function checkConnection(string $subject, string $text): bool
{
$connect = Http::post($this->data()['webhook_url'], [
'content' => '*'.$subject.'*'."\n".$text,
]);
return $connect->ok();
}
public function send(object $notifiable, Notification $notification): void
{
$data = $this->notificationChannel->data;
Http::post($data['webhook_url'], [
'content' => $notification->toSlack($notifiable),
]);
}
}