Expand test notification framework.

This commit is contained in:
James Cole
2024-12-08 16:28:22 +01:00
parent c06fb8daf6
commit 2f7a1c941e
15 changed files with 509 additions and 70 deletions

View File

@@ -67,23 +67,4 @@ class HomeController extends Controller
return view('admin.index', compact('title', 'mainTitleIcon', 'email'));
}
/**
* Send a test message to the admin.
*
* @return Redirector|RedirectResponse
*/
public function testMessage()
{
die('disabled.');
Log::channel('audit')->info('User sends test message.');
/** @var User $user */
$user = auth()->user();
app('log')->debug('Now in testMessage() controller.');
event(new AdminRequestedTestMessage($user));
session()->flash('info', (string)trans('firefly.send_test_triggered'));
return redirect(route('admin.index'));
}
}

View File

@@ -23,9 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Admin;
use FireflyIII\Events\Test\TestNotificationChannel;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\NotificationRequest;
use FireflyIII\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class NotificationController extends Controller
@@ -38,30 +41,69 @@ class NotificationController extends Controller
$subTitle = (string) trans('firefly.title_owner_notifications');
$subTitleIcon = 'envelope-o';
$slackUrl = app('fireflyconfig')->get('slack_webhook_url', '')->data;
$discordUrl = app('fireflyconfig')->get('discord_webhook_url', '')->data;
$discordUrl = app('fireflyconfig')->get('discord_webhook_url', '')->data;
$channels = config('notifications.channels');
// admin notification settings:
$notifications = [];
foreach (config('notifications.notifications.owner') as $key => $info) {
if($info['enabled']) {
if ($info['enabled']) {
$notifications[$key] = app('fireflyconfig')->get(sprintf('notification_%s', $key), true)->data;
}
}
return view('admin.notifications.index', compact('title', 'subTitle', 'mainTitleIcon', 'subTitleIcon', 'channels', 'slackUrl','discordUrl','notifications'));
return view('admin.notifications.index', compact('title', 'subTitle', 'mainTitleIcon', 'subTitleIcon', 'channels', 'slackUrl', 'discordUrl', 'notifications'));
}
public function postIndex(NotificationRequest $request): RedirectResponse {
public function postIndex(NotificationRequest $request): RedirectResponse
{
$all = $request->getAll();
var_dump($request->getAll());
exit;
// app('fireflyconfig')->set(sprintf('notification_%s', $key), $value);;
foreach (config('notifications.notifications.owner') as $key => $info) {
if (array_key_exists($key, $all)) {
app('fireflyconfig')->set(sprintf('notification_%s', $key), $all[$key]);
}
}
if ('' === $all['slack_url']) {
app('fireflyconfig')->delete('slack_webhook_url');
}
if ('' === $all['discord_url']) {
app('fireflyconfig')->delete('discord_webhook_url');
}
if ('' !== $all['slack_url']) {
app('fireflyconfig')->set('slack_webhook_url', $all['slack_url']);
}
if ('' !== $all['discord_url']) {
app('fireflyconfig')->set('discord_webhook_url', $all['discord_url']);
}
session()->flash('success', (string)trans('firefly.notification_settings_saved'));
session()->flash('success', (string) trans('firefly.notification_settings_saved'));
return redirect(route('admin.index'));
return redirect(route('admin.notification.index'));
}
public function testNotification(Request $request): RedirectResponse
{
$all = $request->all();
$channel = $all['test_submit'] ?? '';
switch ($channel) {
default:
session()->flash('error', (string) trans('firefly.notification_test_failed', ['channel' => $channel]));
break;
case 'email':
case 'discord':
case 'slack':
/** @var User $user */
$user = auth()->user();
app('log')->debug(sprintf('Now in testNotification("%s") controller.', $channel));
event(new TestNotificationChannel($channel, $user));
session()->flash('success', (string) trans('firefly.notification_test_executed', ['channel' => $channel]));
}
return redirect(route('admin.notification.index'));
}
}

View File

@@ -23,6 +23,8 @@ declare(strict_types=1);
namespace FireflyIII\Http\Requests;
use FireflyIII\Rules\Admin\IsValidDiscordUrl;
use FireflyIII\Rules\Admin\IsValidSlackUrl;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
@@ -42,18 +44,9 @@ class NotificationRequest extends FormRequest
}
$return[$key] = $value;
}
$return['discord_url'] = $this->convertString('discordUrl');
$return['slack_url'] = $this->convertString('slackUrl');
$return['discord_url'] = $this->convertString('discord_url');
$return['slack_url'] = $this->convertString('slack_url');
return $return;
// if (UrlValidator::isValidWebhookURL($url)) {
// app('fireflyconfig')->set('slack_webhook_url', $url);
// }
// }
//
//
// var_dump($this->all());
// exit;
// return [];
}
/**
@@ -61,10 +54,14 @@ class NotificationRequest extends FormRequest
*/
public function rules(): array
{
// fixed
return [
//'password' => 'required',
$rules = [
'discord_url' => ['nullable', 'url', 'min:1', new IsValidDiscordUrl()],
'slack_url' => ['nullable', 'url', 'min:1', new IsValidSlackUrl()],
];
foreach (config('notifications.notifications.owner') as $key => $info) {
$rules[sprintf('notification_%s', $key)] = 'in:0,1';
}
return $rules;
}
}