Skip to content

Firefly III: a personal finances manager

Add notifications.

James Colebe1dff49fa05
diff --git a/app/Events/NewVersionAvailable.php b/app/Events/NewVersionAvailable.php
new file mode 100644
index 0000000000..cd02644e4a
--- /dev/null
+++ b/app/Events/NewVersionAvailable.php
@@ -0,0 +1,41 @@
+<?php
+/*
+ * NewVersionAvailable.php
+ * Copyright (c) 2022 [email protected]
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Events;
+
+use Illuminate\Queue\SerializesModels;
+
+class NewVersionAvailable extends Event
+{
+ use SerializesModels;
+
+ public string $message;
+
+ /**
+ * Create a new event instance. This event is triggered when a new version is available.
+ *
+ * @param string $message
+ */
+ public function __construct(string $message)
+ {
+ $this->message = $message;
+ }
+}
diff --git a/app/Handlers/Events/AdminEventHandler.php b/app/Handlers/Events/AdminEventHandler.php
index ffa5094deb..4abc4605c9 100644
--- a/app/Handlers/Events/AdminEventHandler.php
+++ b/app/Handlers/Events/AdminEventHandler.php
@@ -22,12 +22,12 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
-use Exception;
use FireflyIII\Events\AdminRequestedTestMessage;
-use FireflyIII\Exceptions\FireflyException;
-use FireflyIII\Mail\AdminTestMail;
+use FireflyIII\Events\NewVersionAvailable;
use FireflyIII\Notifications\Admin\TestNotification;
+use FireflyIII\Notifications\Admin\VersionCheckResult;
use FireflyIII\Repositories\User\UserRepositoryInterface;
+use FireflyIII\Support\Facades\FireflyConfig;
use Illuminate\Support\Facades\Notification;
use Log;
use Mail;
@@ -43,7 +43,7 @@ class AdminEventHandler
*
* @param AdminRequestedTestMessage $event
*
- * @return bool
+ * @return void
*/
public function sendTestMessage(AdminRequestedTestMessage $event): void
{
@@ -56,4 +56,27 @@ class AdminEventHandler
Notification::send($event->user, new TestNotification($event->user->email));
}
+
+ /**
+ * Send new version message to admin.
+ *
+ * @param NewVersionAvailable $event
+ * @return void
+ */
+ public function sendNewVersion(NewVersionAvailable $event): void
+ {
+ $sendMail = FireflyConfig::get('notification_new_version', true)->data;
+ if (false === $sendMail) {
+ return;
+ }
+
+ /** @var UserRepositoryInterface $repository */
+ $repository = app(UserRepositoryInterface::class);
+ $all = $repository->all();
+ foreach ($all as $user) {
+ if ($repository->hasRole($user, 'owner')) {
+ Notification::send($user, new VersionCheckResult($event->message));
+ }
+ }
+ }
}
diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php
index 1ffe47fabd..8588427697 100644
--- a/app/Handlers/Events/UserEventHandler.php
+++ b/app/Handlers/Events/UserEventHandler.php
@@ -165,7 +165,6 @@ class UserEventHandler
*
* @param Login $event
*
- * @return bool
* @throws FireflyException
*/
public function demoUserBackToEnglish(Login $event): void
@@ -272,13 +271,8 @@ class UserEventHandler
* This method will send the user a registration mail, welcoming him or her to Firefly III.
* This message is only sent when the configuration of Firefly III says so.
*
- * TODO this is an admin setting not a variable. Fix that first.
- *
* @param RegisteredUser $event
*
- * @return bool
- * @throws FireflyException
- * @deprecated
*/
public function sendRegistrationMail(RegisteredUser $event): void
{
@@ -290,7 +284,6 @@ class UserEventHandler
/**
* @param RegisteredUser $event
- * @return void
*/
public function sendAdminRegistrationNotification(RegisteredUser $event): void
{
@@ -357,9 +350,10 @@ class UserEventHandler
];
}
$preference = array_values($preference);
+ $send = app('preferences')->getForUser($user, 'notification_user_login', true)->data;
app('preferences')->setForUser($user, 'login_ip_history', $preference);
- if (false === $inArray && true === config('firefly.warn_new_ip')) {
+ if (false === $inArray && true === $send) {
event(new DetectedNewIPAddress($user, $ip));
}
diff --git a/app/Notifications/Admin/VersionCheckResult.php b/app/Notifications/Admin/VersionCheckResult.php
index dda7443427..edf1998d09 100644
--- a/app/Notifications/Admin/VersionCheckResult.php
+++ b/app/Notifications/Admin/VersionCheckResult.php
@@ -22,7 +22,67 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin;
-class VersionCheckResult
+use FireflyIII\Models\Bill;
+use Illuminate\Bus\Queueable;
+use Illuminate\Notifications\Messages\MailMessage;
+use Illuminate\Notifications\Notification;
+
+/**
+ * Class VersionCheckResult
+ *
+ */
+class VersionCheckResult extends Notification
{
+ use Queueable;
+
+ private string $message;
+
+ /**
+ * Create a new notification instance.
+ *
+ * @return void
+ */
+ public function __construct(string $message)
+ {
+ $this->message = $message;
+ }
+ /**
+ * Get the notification's delivery channels.
+ *
+ * @param mixed $notifiable
+ * @return array
+ */
+ public function via($notifiable)
+ {
+ return ['mail'];
+ }
+
+ /**
+ * Get the mail representation of the notification.
+ *
+ * @param mixed $notifiable
+ * @return \Illuminate\Notifications\Messages\MailMessage
+ */
+ public function toMail($notifiable)
+ {
+
+ return (new MailMessage)
+ ->markdown('emails.new-version', ['message' => $this->message])
+ ->subject((string)trans('email.new_version_email_subject'));
+ }
+
+ /**
+ * Get the array representation of the notification.
+ *
+ * @param mixed $notifiable
+ * @return array
+ */
+ public function toArray($notifiable)
+ {
+ return [
+ //
+ ];
+ }
}
+
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 8fa3c3635d..99738ab1ce 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -27,6 +27,7 @@ use FireflyIII\Events\ActuallyLoggedIn;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Events\DestroyedTransactionGroup;
use FireflyIII\Events\DetectedNewIPAddress;
+use FireflyIII\Events\NewVersionAvailable;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
use FireflyIII\Events\RequestedReportOnJournals;
@@ -109,6 +110,9 @@ class EventServiceProvider extends ServiceProvider
AdminRequestedTestMessage::class => [
'FireflyIII\Handlers\Events\AdminEventHandler@sendTestMessage',
],
+ NewVersionAvailable::class => [
+ 'FireflyIII\Handlers\Events\AdminEventHandler@sendNewVersion',
+ ],
// is a Transaction Journal related event.
StoredTransactionGroup::class => [
'FireflyIII\Handlers\Events\StoredGroupEventHandler@processRules',
diff --git a/app/Services/FireflyIIIOrg/Update/UpdateRequest.php b/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
index 6a25fa44ee..285541c19e 100644
--- a/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
+++ b/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\FireflyIIIOrg\Update;
use Carbon\Carbon;
+use FireflyIII\Events\NewVersionAvailable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use JsonException;
@@ -168,6 +169,7 @@ class UpdateRequest implements UpdateRequestInterface
return $return;
}
+
// a newer version is available!
/** @var Carbon $released */
$released = $information['date'];
@@ -189,7 +191,7 @@ class UpdateRequest implements UpdateRequestInterface
return $return;
}
- // its been around for a while:
+ // it's been around for a while:
$return['level'] = 'success';
$return['message'] = (string) trans(
'firefly.update_new_version_alert',
@@ -215,6 +217,9 @@ class UpdateRequest implements UpdateRequestInterface
}
Log::debug('New release is here!', $return);
+ // send event, this may result in a notification.
+ event(new NewVersionAvailable($return['message']));
+
return $return;
}
}
diff --git a/config/firefly.php b/config/firefly.php
index 7e4e886d68..da5c219273 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -146,7 +146,7 @@ return [
// notifications
'available_notifications' => ['bill_reminder', 'new_access_token', 'transaction_creation', 'user_login'],
- 'admin_notifications' => ['admin_new_reg', 'user_new_reg'],
+ 'admin_notifications' => ['admin_new_reg', 'user_new_reg', 'new_version'],
// enabled languages
'languages' => [
diff --git a/resources/lang/en_US/email.php b/resources/lang/en_US/email.php
index b33fb9c907..cadb14c1a5 100644
--- a/resources/lang/en_US/email.php
+++ b/resources/lang/en_US/email.php
@@ -61,6 +61,9 @@ return [
'registered_pw_reset_link' => 'Password reset:',
'registered_doc_link' => 'Documentation:',
+ // new version
+ 'new_version_email_subject' => 'A new Firefly III version is available',
+
// email change
'email_change_subject' => 'Your Firefly III email address has changed',
'email_change_body_to_new' => 'You or somebody with access to your Firefly III account has changed your email address. If you did not expect this message, please ignore and delete it.',
diff --git a/resources/views/emails/new-version.blade.php b/resources/views/emails/new-version.blade.php
new file mode 100644
index 0000000000..c153311857
--- /dev/null
+++ b/resources/views/emails/new-version.blade.php
@@ -0,0 +1,6 @@
+@component('mail::message')
+{{ $message }}
+
+[Firefly III @ GitHub](https://github.com/firefly-iii/firefly-iii/releases)
+
+@endcomponent