Add notifications.

This commit is contained in:
James Cole 2022-09-24 11:41:07 +02:00
parent 665b78ebf5
commit be1dff49fa
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
9 changed files with 152 additions and 16 deletions

View File

@ -0,0 +1,41 @@
<?php
/*
* NewVersionAvailable.php
* Copyright (c) 2022 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/>.
*/
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;
}
}

View File

@ -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));
}
}
}
}

View File

@ -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));
}

View File

@ -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 [
//
];
}
}

View File

@ -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',

View File

@ -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;
}
}

View File

@ -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' => [

View File

@ -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.',

View File

@ -0,0 +1,6 @@
@component('mail::message')
{{ $message }}
[Firefly III @ GitHub](https://github.com/firefly-iii/firefly-iii/releases)
@endcomponent