diff --git a/app/Notifications/User/BillReminder.php b/app/Notifications/User/BillReminder.php index 35b3f53047..8c41751137 100644 --- a/app/Notifications/User/BillReminder.php +++ b/app/Notifications/User/BillReminder.php @@ -26,11 +26,14 @@ namespace FireflyIII\Notifications\User; use FireflyIII\Models\Bill; use FireflyIII\Notifications\ReturnsAvailableChannels; +use FireflyIII\Notifications\ReturnsSettings; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\PushoverMessage; +use Ntfy\Message; /** * Class BillReminder @@ -66,14 +69,9 @@ class BillReminder extends Notification */ public function toMail(User $notifiable) { - $subject = (string) trans(sprintf('email.bill_warning_subject_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); - if (0 === $this->diff) { - $subject = (string) trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); - } - return (new MailMessage()) ->markdown('emails.bill-warning', ['field' => $this->field, 'diff' => $this->diff, 'bill' => $this->bill]) - ->subject($subject); + ->subject($this->getSubject()); } /** @@ -81,10 +79,6 @@ class BillReminder extends Notification */ public function toSlack(User $notifiable) { - $message = (string) trans(sprintf('email.bill_warning_subject_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); - if (0 === $this->diff) { - $message = (string) trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); - } $bill = $this->bill; $url = route('bills.show', [$bill->id]); @@ -93,7 +87,36 @@ class BillReminder extends Notification ->attachment(static function ($attachment) use ($bill, $url): void { $attachment->title((string) trans('firefly.visit_bill', ['name' => $bill->name]), $url); }) - ->content($message); + ->content($this->getSubject()); + } + + public function toNtfy(User $notifiable): Message + { + $settings = ReturnsSettings::getSettings('ntfy', 'user', $notifiable); + $message = new Message(); + $message->topic($settings['ntfy_topic']); + $message->title($this->getSubject()); + $message->body((string) trans('email.bill_warning_please_action')); + + return $message; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function toPushover(User $notifiable): PushoverMessage + { + return PushoverMessage::create((string) trans('email.bill_warning_please_action')) + ->title($this->getSubject()); + } + + private function getSubject(): string + { + $message = (string) trans(sprintf('email.bill_warning_subject_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); + if (0 === $this->diff) { + $message = (string) trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]); + } + return $message; } /** diff --git a/app/Notifications/User/NewAccessToken.php b/app/Notifications/User/NewAccessToken.php index 16175a22ae..65269d02a5 100644 --- a/app/Notifications/User/NewAccessToken.php +++ b/app/Notifications/User/NewAccessToken.php @@ -25,11 +25,14 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; use FireflyIII\Notifications\ReturnsAvailableChannels; +use FireflyIII\Notifications\ReturnsSettings; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\PushoverMessage; +use Ntfy\Message; /** * Class NewAccessToken @@ -66,6 +69,26 @@ class NewAccessToken extends Notification return new SlackMessage()->content((string) trans('email.access_token_created_body')); } + public function toNtfy(User $notifiable): Message + { + $settings = ReturnsSettings::getSettings('ntfy', 'user', $notifiable); + $message = new Message(); + $message->topic($settings['ntfy_topic']); + $message->title((string) trans('email.access_token_created_subject')); + $message->body((string) trans('email.access_token_created_body')); + + return $message; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function toPushover(User $notifiable): PushoverMessage + { + return PushoverMessage::create((string) trans('email.access_token_created_body')) + ->title((string) trans('email.access_token_created_subject')); + } + /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ diff --git a/app/Notifications/User/RuleActionFailed.php b/app/Notifications/User/RuleActionFailed.php index ea36540bb4..60f3016e90 100644 --- a/app/Notifications/User/RuleActionFailed.php +++ b/app/Notifications/User/RuleActionFailed.php @@ -25,10 +25,13 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; use FireflyIII\Notifications\ReturnsAvailableChannels; +use FireflyIII\Notifications\ReturnsSettings; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\PushoverMessage; +use Ntfy\Message; /** * Class RuleActionFailed @@ -81,12 +84,33 @@ class RuleActionFailed extends Notification } + public function toNtfy(User $notifiable): Message + { + $settings = ReturnsSettings::getSettings('ntfy', 'user', $notifiable); + $message = new Message(); + $message->topic($settings['ntfy_topic']); + $message->body($this->message); + + return $message; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function toPushover(User $notifiable): PushoverMessage + { + return PushoverMessage::create($this->message); + } + /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function via(User $notifiable) { - // todo disable mail channel - return ReturnsAvailableChannels::returnChannels('user', $notifiable); + $channels = ReturnsAvailableChannels::returnChannels('user', $notifiable); + if (($key = array_search('mail', $channels)) !== false) { + unset($channels[$key]); + } + return $channels; } } diff --git a/app/Notifications/User/TransactionCreation.php b/app/Notifications/User/TransactionCreation.php index 4262543c7e..9d1ac6e198 100644 --- a/app/Notifications/User/TransactionCreation.php +++ b/app/Notifications/User/TransactionCreation.php @@ -71,7 +71,6 @@ class TransactionCreation extends Notification */ public function via(User $notifiable) { - // todo only over email? - return ReturnsAvailableChannels::returnChannels('user', $notifiable); + return ['mail']; } } diff --git a/app/Notifications/User/UserLogin.php b/app/Notifications/User/UserLogin.php index c0a06bbad2..bfa803fe30 100644 --- a/app/Notifications/User/UserLogin.php +++ b/app/Notifications/User/UserLogin.php @@ -26,11 +26,14 @@ namespace FireflyIII\Notifications\User; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Notifications\ReturnsAvailableChannels; +use FireflyIII\Notifications\ReturnsSettings; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\PushoverMessage; +use Ntfy\Message; /** * Class UserLogin @@ -60,41 +63,38 @@ class UserLogin extends Notification public function toMail(User $notifiable) { $time = now(config('app.timezone'))->isoFormat((string) trans('config.date_time_js')); - $host = ''; - - try { - $hostName = app('steam')->getHostName($this->ip); - } catch (FireflyException $e) { - app('log')->error($e->getMessage()); - $hostName = $this->ip; - } - if ($hostName !== $this->ip) { - $host = $hostName; - } return (new MailMessage()) - ->markdown('emails.new-ip', ['time' => $time, 'ipAddress' => $this->ip, 'host' => $host]) + ->markdown('emails.new-ip', ['time' => $time, 'ipAddress' => $this->ip, 'host' => $this->getHost()]) ->subject((string) trans('email.login_from_new_ip')); } + public function toNtfy(User $notifiable): Message + { + $settings = ReturnsSettings::getSettings('ntfy', 'user', $notifiable); + $message = new Message(); + $message->topic($settings['ntfy_topic']); + $message->title((string) trans('email.login_from_new_ip')); + $message->body((string) trans('email.slack_login_from_new_ip', ['host' => $this->getHost(), 'ip' => $this->ip])); + + return $message; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function toPushover(User $notifiable): PushoverMessage + { + return PushoverMessage::create((string) trans('email.slack_login_from_new_ip', ['host' => $this->getHost(), 'ip' => $this->ip])) + ->title((string) trans('email.login_from_new_ip')); + } + /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function toSlack(User $notifiable) { - $host = ''; - - try { - $hostName = app('steam')->getHostName($this->ip); - } catch (FireflyException $e) { - app('log')->error($e->getMessage()); - $hostName = $this->ip; - } - if ($hostName !== $this->ip) { - $host = $hostName; - } - - return new SlackMessage()->content((string) trans('email.slack_login_from_new_ip', ['host' => $host, 'ip' => $this->ip])); + return new SlackMessage()->content((string) trans('email.slack_login_from_new_ip', ['host' => $this->getHost(), 'ip' => $this->ip])); } /** @@ -104,4 +104,19 @@ class UserLogin extends Notification { return ReturnsAvailableChannels::returnChannels('user', $notifiable); } + + private function getHost(): string { + $host = ''; + + try { + $hostName = app('steam')->getHostName($this->ip); + } catch (FireflyException $e) { + app('log')->error($e->getMessage()); + $hostName = $this->ip; + } + if ($hostName !== $this->ip) { + $host = $hostName; + } + return $host; + } } diff --git a/app/Notifications/User/UserNewPassword.php b/app/Notifications/User/UserNewPassword.php index e7988763b7..3b2cc285d0 100644 --- a/app/Notifications/User/UserNewPassword.php +++ b/app/Notifications/User/UserNewPassword.php @@ -25,10 +25,14 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; use FireflyIII\Notifications\ReturnsAvailableChannels; +use FireflyIII\Notifications\ReturnsSettings; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use NotificationChannels\Pushover\PushoverMessage; +use Ntfy\Message; /** * Class UserNewPassword @@ -67,6 +71,29 @@ class UserNewPassword extends Notification /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ + public function toSlack(User $notifiable) + { + return new SlackMessage()->content((string) trans('email.reset_pw_message')); + } + + public function toNtfy(User $notifiable): Message + { + $settings = ReturnsSettings::getSettings('ntfy', 'user', $notifiable); + $message = new Message(); + $message->topic($settings['ntfy_topic']); + $message->body((string) trans('email.reset_pw_message')); + + return $message; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function toPushover(User $notifiable): PushoverMessage + { + return PushoverMessage::create((string) trans('email.reset_pw_message')); + } + public function via(User $notifiable) { return ReturnsAvailableChannels::returnChannels('user', $notifiable); diff --git a/app/Notifications/User/UserRegistration.php b/app/Notifications/User/UserRegistration.php index a973b4a7fc..d839797b13 100644 --- a/app/Notifications/User/UserRegistration.php +++ b/app/Notifications/User/UserRegistration.php @@ -24,7 +24,6 @@ declare(strict_types=1); namespace FireflyIII\Notifications\User; -use FireflyIII\Notifications\ReturnsAvailableChannels; use FireflyIII\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; @@ -64,6 +63,7 @@ class UserRegistration extends Notification */ public function via(User $notifiable) { - return ReturnsAvailableChannels::returnChannels('user', $notifiable); + // other settings will not be available at this point anyway. + return ['mail']; } } diff --git a/resources/lang/en_US/email.php b/resources/lang/en_US/email.php index 408e75132a..9f07ac0a38 100644 --- a/resources/lang/en_US/email.php +++ b/resources/lang/en_US/email.php @@ -109,6 +109,7 @@ return [ // reset password 'reset_pw_subject' => 'Your password reset request', + 'reset_pw_message' => 'You have received password reset instructions in your email. If this was you, please follow the instructions.', 'reset_pw_instructions' => 'Somebody tried to reset your password. If it was you, please follow the link below to do so.', 'reset_pw_warning' => '**PLEASE** verify that the link actually goes to the Firefly III you expect it to go!',