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

65 lines
1.8 KiB
PHP

<?php
namespace App\Actions\NotificationChannels;
use App\Models\NotificationChannel;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class AddChannel
{
/**
* @throws ValidationException
*/
public function add(User $user, array $input): void
{
$this->validate($input);
$channel = new NotificationChannel([
'user_id' => $user->id,
'provider' => $input['provider'],
'label' => $input['label'],
]);
$this->validateType($channel, $input);
$channel->data = $channel->provider()->createData($input);
$channel->save();
if (! $channel->provider()->connect()) {
$channel->delete();
if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) {
throw ValidationException::withMessages([
'email' => __('Could not connect! Make sure you configured `.env` file correctly.'),
]);
}
throw ValidationException::withMessages([
'provider' => __('Could not connect'),
]);
}
$channel->connected = true;
$channel->save();
}
/**
* @throws ValidationException
*/
protected function validate(array $input): void
{
Validator::make($input, [
'provider' => 'required|in:'.implode(',', config('core.notification_channels_providers')),
'label' => 'required',
])->validate();
}
/**
* @throws ValidationException
*/
protected function validateType(NotificationChannel $channel, array $input): void
{
Validator::make($input, $channel->provider()->createRules($input))
->validate();
}
}