Files
firefly-iii/app/Http/Controllers/Admin/NotificationController.php

140 lines
5.6 KiB
PHP
Raw Normal View History

2024-12-07 08:05:51 +01:00
<?php
/*
* NotificationController.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Admin;
2024-12-08 16:28:22 +01:00
use FireflyIII\Events\Test\TestNotificationChannel;
2024-12-07 08:05:51 +01:00
use FireflyIII\Http\Controllers\Controller;
2024-12-07 09:41:09 +01:00
use FireflyIII\Http\Requests\NotificationRequest;
2024-12-11 07:23:46 +01:00
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
2024-12-08 16:28:22 +01:00
use FireflyIII\User;
2024-12-07 09:41:09 +01:00
use Illuminate\Http\RedirectResponse;
2024-12-08 16:28:22 +01:00
use Illuminate\Http\Request;
2024-12-07 08:05:51 +01:00
use Illuminate\Support\Facades\Log;
class NotificationController extends Controller
{
public function index()
{
Log::channel('audit')->info('User visits notifications index.');
$title = (string) trans('firefly.administration');
$mainTitleIcon = 'fa-hand-spock-o';
$subTitle = (string) trans('firefly.title_owner_notifications');
$subTitleIcon = 'envelope-o';
2024-12-09 06:27:37 +01:00
2024-12-11 07:23:46 +01:00
// notification settings:
$slackUrl = app('fireflyconfig')->getEncrypted('slack_webhook_url', '')->data;
$pushoverAppToken = app('fireflyconfig')->getEncrypted('pushover_app_token', '')->data;
$pushoverUserToken = app('fireflyconfig')->getEncrypted('pushover_user_token', '')->data;
$ntfyServer = app('fireflyconfig')->getEncrypted('ntfy_server', 'https://ntfy.sh')->data;
$ntfyTopic = app('fireflyconfig')->getEncrypted('ntfy_topic', '')->data;
$ntfyAuth = app('fireflyconfig')->get('ntfy_auth', false)->data;
$ntfyUser = app('fireflyconfig')->getEncrypted('ntfy_user', '')->data;
$ntfyPass = app('fireflyconfig')->getEncrypted('ntfy_pass', '')->data;
$channels = config('notifications.channels');
$forcedAvailability = [];
2024-12-07 09:41:09 +01:00
// admin notification settings:
$notifications = [];
foreach (config('notifications.notifications.owner') as $key => $info) {
2024-12-08 16:28:22 +01:00
if ($info['enabled']) {
2024-12-07 09:41:09 +01:00
$notifications[$key] = app('fireflyconfig')->get(sprintf('notification_%s', $key), true)->data;
}
}
2024-12-09 06:27:37 +01:00
// loop all channels
foreach ($channels as $channel => $info) {
$forcedAvailability[$channel] = true;
}
// validate presence of of Ntfy settings.
2024-12-11 07:23:46 +01:00
if ('' === $ntfyTopic) {
2024-12-09 06:27:37 +01:00
Log::warning('No topic name for Ntfy, channel is disabled.');
$forcedAvailability['ntfy'] = false;
}
2024-12-07 09:41:09 +01:00
2024-12-09 07:34:01 +01:00
// validate pushover
2024-12-11 07:23:46 +01:00
if ('' === $pushoverAppToken || '' === $pushoverUserToken) {
Log::warning('[a] No Pushover token, channel is disabled.');
2024-12-09 07:34:01 +01:00
$forcedAvailability['pushover'] = false;
}
2024-12-11 07:23:46 +01:00
return view('admin.notifications.index',
compact(
'title', 'subTitle', 'forcedAvailability', 'mainTitleIcon', 'subTitleIcon', 'channels',
'slackUrl', 'notifications',
'pushoverAppToken', 'pushoverUserToken',
'ntfyServer', 'ntfyTopic', 'ntfyAuth', 'ntfyUser', 'ntfyPass'
));
2024-12-07 09:41:09 +01:00
}
2024-12-08 16:28:22 +01:00
public function postIndex(NotificationRequest $request): RedirectResponse
{
$all = $request->getAll();
2024-12-07 09:41:09 +01:00
2024-12-08 16:28:22 +01:00
foreach (config('notifications.notifications.owner') as $key => $info) {
if (array_key_exists($key, $all)) {
app('fireflyconfig')->set(sprintf('notification_%s', $key), $all[$key]);
}
}
2024-12-11 07:23:46 +01:00
$variables = ['slack_webhook_url', 'pushover_app_token', 'pushover_user_token', 'ntfy_server', 'ntfy_topic', 'ntfy_user', 'ntfy_pass'];
foreach ($variables as $variable) {
if ('' === $all[$variable]) {
app('fireflyconfig')->delete($variable);
}
if ('' !== $all[$variable]) {
app('fireflyconfig')->setEncrypted($variable, $all[$variable]);
}
2024-12-08 16:28:22 +01:00
}
2024-12-11 07:23:46 +01:00
app('fireflyconfig')->set('ntfy_auth', $all['ntfy_auth'] ?? false);
2024-12-07 09:41:09 +01:00
2024-12-08 16:28:22 +01:00
session()->flash('success', (string) trans('firefly.notification_settings_saved'));
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 'slack':
2024-12-09 07:34:01 +01:00
case 'pushover':
2024-12-09 06:27:37 +01:00
case 'ntfy':
2024-12-11 07:23:46 +01:00
$owner = new OwnerNotifiable();
2024-12-08 16:28:22 +01:00
app('log')->debug(sprintf('Now in testNotification("%s") controller.', $channel));
2024-12-11 07:23:46 +01:00
event(new TestNotificationChannel($channel, $owner));
2024-12-08 16:28:22 +01:00
session()->flash('success', (string) trans('firefly.notification_test_executed', ['channel' => $channel]));
}
2024-12-07 09:41:09 +01:00
2024-12-08 16:28:22 +01:00
return redirect(route('admin.notification.index'));
2024-12-07 08:05:51 +01:00
}
}