Start cleaning up notifications.

This commit is contained in:
James Cole 2024-12-14 06:57:57 +01:00
parent b976239580
commit fd2c1615cf
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 50 additions and 24 deletions

View File

@ -25,20 +25,23 @@ declare(strict_types=1);
namespace FireflyIII\Notifications;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
use NotificationChannels\Pushover\PushoverChannel;
use Wijourdil\NtfyNotificationChannel\Channels\NtfyChannel;
class ReturnsAvailableChannels
{
public static function returnChannels(string $type): array
public static function returnChannels(string $type, ?User $user = null): array
{
$channels = ['mail'];
if ('owner' === $type) {
return self::returnOwnerChannels();
}
if('user' === $type && null !== $user) {
return self::returnUserChannels($user);
}
return $channels;
@ -75,6 +78,38 @@ class ReturnsAvailableChannels
Log::debug(sprintf('Final channel set in ReturnsAvailableChannels: %s ', implode(', ', $channels)));
return $channels;
}
private static function returnUserChannels(User $user): array {
$channels = ['mail'];
$slackUrl = app('preferences')->getEncryptedForUser($user, 'slack_webhook_url', '')->data;
if (UrlValidator::isValidWebhookURL($slackUrl)) {
$channels[] = 'slack';
}
// validate presence of of Ntfy settings.
if ('' !== (string) app('preferences')->getEncryptedForUser($user,'ntfy_topic', '')->data) {
Log::debug('Enabled ntfy.');
$channels[] = NtfyChannel::class;
}
if ('' === (string) app('preferences')->getEncryptedForUser($user,'ntfy_topic', '')->data) {
Log::warning('No topic name for Ntfy, channel is disabled.');
}
// pushover
$pushoverAppToken = (string) app('preferences')->getEncryptedForUser($user, 'pushover_app_token', '')->data;
$pushoverUserToken = (string) app('preferences')->getEncryptedForUser($user, 'pushover_user_token', '')->data;
if ('' === $pushoverAppToken || '' === $pushoverUserToken) {
Log::warning('[b] No Pushover token, channel is disabled.');
}
if ('' !== $pushoverAppToken && '' !== $pushoverUserToken) {
Log::debug('Enabled pushover.');
$channels[] = PushoverChannel::class;
}
Log::debug(sprintf('Final channel set in ReturnsAvailableChannels (user): %s ', implode(', ', $channels)));
// only the owner can get notifications over
return $channels;
}

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Security;
use FireflyIII\Notifications\ReturnsAvailableChannels;
use FireflyIII\Support\Notifications\UrlValidator;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
@ -48,13 +49,13 @@ class DisabledMFANotification extends Notification
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @param User $notifiable
*
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function toArray($notifiable)
public function toArray(User $notifiable)
{
return [
];
@ -63,15 +64,15 @@ class DisabledMFANotification extends Notification
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @param User $notifiable
*
* @return MailMessage
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function toMail($notifiable)
public function toMail(User $notifiable)
{
$subject = (string)trans('email.disabled_mfa_subject');
$subject = (string) trans('email.disabled_mfa_subject');
return (new MailMessage())->markdown('emails.security.disabled-mfa', ['user' => $this->user])->subject($subject);
}
@ -79,15 +80,15 @@ class DisabledMFANotification extends Notification
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @param User $notifiable
*
* @return SlackMessage
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function toSlack($notifiable)
public function toSlack(User $notifiable)
{
$message = (string)trans('email.disabled_mfa_slack', ['email' => $this->user->email]);
$message = (string) trans('email.disabled_mfa_slack', ['email' => $this->user->email]);
return (new SlackMessage())->content($message);
}
@ -95,24 +96,14 @@ class DisabledMFANotification extends Notification
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @param User $notifiable
*
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function via($notifiable)
public function via(User $notifiable)
{
/** @var null|User $user */
$user = auth()->user();
$slackUrl = null === $user ? '' : app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data;
if (is_array($slackUrl)) {
$slackUrl = '';
}
if (UrlValidator::isValidWebhookURL((string)$slackUrl)) {
return ['mail', 'slack'];
}
return ['mail'];
return ReturnsAvailableChannels::returnChannels('user', $notifiable);
}
}