Notifications: Aligned how user language is used

- This ensures content notifications are not translated to receiver
  language.
- This adds actual plaintext support for content notifications (Was
  previously just HTML as text view).
- Shares same base class across all mail notifications.
- Also cleaned up existing notification classes.

Future cleanup requested via #4501
This commit is contained in:
Dan Brown 2023-09-02 15:11:42 +01:00
parent f91049a3f2
commit 3847a76134
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
14 changed files with 114 additions and 117 deletions

View File

@ -3,13 +3,14 @@
namespace BookStack\Activity\Notifications\MessageParts; namespace BookStack\Activity\Notifications\MessageParts;
use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\Htmlable;
use Stringable;
/** /**
* A line of text with linked text included, intended for use * A line of text with linked text included, intended for use
* in MailMessages. The line should have a ':link' placeholder for * in MailMessages. The line should have a ':link' placeholder for
* where the link should be inserted within the line. * where the link should be inserted within the line.
*/ */
class LinkedMailMessageLine implements Htmlable class LinkedMailMessageLine implements Htmlable, Stringable
{ {
public function __construct( public function __construct(
protected string $url, protected string $url,
@ -23,4 +24,10 @@ class LinkedMailMessageLine implements Htmlable
$link = '<a href="' . e($this->url) . '">' . e($this->linkText) . '</a>'; $link = '<a href="' . e($this->url) . '">' . e($this->linkText) . '</a>';
return str_replace(':link', $link, e($this->line)); return str_replace(':link', $link, e($this->line));
} }
public function __toString(): string
{
$link = "{$this->linkText} ({$this->url})";
return str_replace(':link', $link, $this->line);
}
} }

View File

@ -3,12 +3,13 @@
namespace BookStack\Activity\Notifications\MessageParts; namespace BookStack\Activity\Notifications\MessageParts;
use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\Htmlable;
use Stringable;
/** /**
* A bullet point list of content, where the keys of the given list array * A bullet point list of content, where the keys of the given list array
* are bolded header elements, and the values follow. * are bolded header elements, and the values follow.
*/ */
class ListMessageLine implements Htmlable class ListMessageLine implements Htmlable, Stringable
{ {
public function __construct( public function __construct(
protected array $list protected array $list
@ -23,4 +24,13 @@ class ListMessageLine implements Htmlable
} }
return implode("<br>\n", $list); return implode("<br>\n", $list);
} }
public function __toString(): string
{
$list = [];
foreach ($this->list as $header => $content) {
$list[] = $header . ' ' . $content;
}
return implode("\n", $list);
}
} }

View File

@ -4,12 +4,11 @@ namespace BookStack\Activity\Notifications\Messages;
use BookStack\Activity\Models\Loggable; use BookStack\Activity\Models\Loggable;
use BookStack\Activity\Notifications\MessageParts\LinkedMailMessageLine; use BookStack\Activity\Notifications\MessageParts\LinkedMailMessageLine;
use BookStack\Notifications\MailNotification;
use BookStack\Users\Models\User; use BookStack\Users\Models\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
abstract class BaseActivityNotification extends Notification abstract class BaseActivityNotification extends MailNotification
{ {
use Queueable; use Queueable;
@ -19,22 +18,6 @@ abstract class BaseActivityNotification extends Notification
) { ) {
} }
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
abstract public function toMail(mixed $notifiable): MailMessage;
/** /**
* Get the array representation of the notification. * Get the array representation of the notification.
* *
@ -52,12 +35,12 @@ abstract class BaseActivityNotification extends Notification
/** /**
* Build the common reason footer line used in mail messages. * Build the common reason footer line used in mail messages.
*/ */
protected function buildReasonFooterLine(): LinkedMailMessageLine protected function buildReasonFooterLine(string $language): LinkedMailMessageLine
{ {
return new LinkedMailMessageLine( return new LinkedMailMessageLine(
url('/preferences/notifications'), url('/preferences/notifications'),
trans('notifications.footer_reason'), trans('notifications.footer_reason', [], $language),
trans('notifications.footer_reason_link'), trans('notifications.footer_reason_link', [], $language),
); );
} }
} }

View File

@ -5,26 +5,29 @@ namespace BookStack\Activity\Notifications\Messages;
use BookStack\Activity\Models\Comment; use BookStack\Activity\Models\Comment;
use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
class CommentCreationNotification extends BaseActivityNotification class CommentCreationNotification extends BaseActivityNotification
{ {
public function toMail(mixed $notifiable): MailMessage public function toMail(User $notifiable): MailMessage
{ {
/** @var Comment $comment */ /** @var Comment $comment */
$comment = $this->detail; $comment = $this->detail;
/** @var Page $page */ /** @var Page $page */
$page = $comment->entity; $page = $comment->entity;
return (new MailMessage()) $language = $notifiable->getLanguage();
->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()]))
->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')])) return $this->newMailMessage($language)
->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')], $language))
->line(new ListMessageLine([ ->line(new ListMessageLine([
trans('notifications.detail_page_name') => $page->name, trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_commenter') => $this->user->name, trans('notifications.detail_commenter', [], $language) => $this->user->name,
trans('notifications.detail_comment') => strip_tags($comment->html), trans('notifications.detail_comment', [], $language) => strip_tags($comment->html),
])) ]))
->action(trans('notifications.action_view_comment'), $page->getUrl('#comment' . $comment->local_id)) ->action(trans('notifications.action_view_comment', [], $language), $page->getUrl('#comment' . $comment->local_id))
->line($this->buildReasonFooterLine()); ->line($this->buildReasonFooterLine($language));
} }
} }

View File

@ -4,23 +4,26 @@ namespace BookStack\Activity\Notifications\Messages;
use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
class PageCreationNotification extends BaseActivityNotification class PageCreationNotification extends BaseActivityNotification
{ {
public function toMail(mixed $notifiable): MailMessage public function toMail(User $notifiable): MailMessage
{ {
/** @var Page $page */ /** @var Page $page */
$page = $this->detail; $page = $this->detail;
return (new MailMessage()) $language = $notifiable->getLanguage();
->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()]))
->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')])) return $this->newMailMessage($language)
->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')], $language))
->line(new ListMessageLine([ ->line(new ListMessageLine([
trans('notifications.detail_page_name') => $page->name, trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_created_by') => $this->user->name, trans('notifications.detail_created_by', [], $language) => $this->user->name,
])) ]))
->action(trans('notifications.action_view_page'), $page->getUrl()) ->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
->line($this->buildReasonFooterLine()); ->line($this->buildReasonFooterLine($language));
} }
} }

View File

@ -4,24 +4,27 @@ namespace BookStack\Activity\Notifications\Messages;
use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Activity\Notifications\MessageParts\ListMessageLine;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
class PageUpdateNotification extends BaseActivityNotification class PageUpdateNotification extends BaseActivityNotification
{ {
public function toMail(mixed $notifiable): MailMessage public function toMail(User $notifiable): MailMessage
{ {
/** @var Page $page */ /** @var Page $page */
$page = $this->detail; $page = $this->detail;
return (new MailMessage()) $language = $notifiable->getLanguage();
->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()]))
->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')])) return $this->newMailMessage($language)
->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()], $language))
->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')], $language))
->line(new ListMessageLine([ ->line(new ListMessageLine([
trans('notifications.detail_page_name') => $page->name, trans('notifications.detail_page_name', [], $language) => $page->name,
trans('notifications.detail_updated_by') => $this->user->name, trans('notifications.detail_updated_by', [], $language) => $this->user->name,
])) ]))
->line(trans('notifications.updated_page_debounce')) ->line(trans('notifications.updated_page_debounce', [], $language))
->action(trans('notifications.action_view_page'), $page->getUrl()) ->action(trans('notifications.action_view_page', [], $language), $page->getUrl())
->line($this->buildReasonFooterLine()); ->line($this->buildReasonFooterLine($language));
} }
} }

View File

@ -2,28 +2,17 @@
namespace BookStack\Notifications; namespace BookStack\Notifications;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
class ConfirmEmail extends MailNotification class ConfirmEmail extends MailNotification
{ {
public $token; public function __construct(
public string $token
/** ) {
* Create a new notification instance.
*
* @param string $token
*/
public function __construct($token)
{
$this->token = $token;
} }
/** public function toMail(User $notifiable): MailMessage
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{ {
$appName = ['appName' => setting('app-name')]; $appName = ['appName' => setting('app-name')];

View File

@ -2,15 +2,21 @@
namespace BookStack\Notifications; namespace BookStack\Notifications;
use BookStack\Users\Models\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
class MailNotification extends Notification implements ShouldQueue abstract class MailNotification extends Notification implements ShouldQueue
{ {
use Queueable; use Queueable;
/**
* Get the mail representation of the notification.
*/
abstract public function toMail(User $notifiable): MailMessage;
/** /**
* Get the notification's channels. * Get the notification's channels.
* *
@ -25,14 +31,14 @@ class MailNotification extends Notification implements ShouldQueue
/** /**
* Create a new mail message. * Create a new mail message.
*
* @return MailMessage
*/ */
protected function newMailMessage() protected function newMailMessage(string $language = ''): MailMessage
{ {
$data = ['language' => $language ?: null];
return (new MailMessage())->view([ return (new MailMessage())->view([
'html' => 'vendor.notifications.email', 'html' => 'vendor.notifications.email',
'text' => 'vendor.notifications.email-plain', 'text' => 'vendor.notifications.email-plain',
]); ], $data);
} }
} }

View File

@ -2,31 +2,17 @@
namespace BookStack\Notifications; namespace BookStack\Notifications;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPassword extends MailNotification class ResetPassword extends MailNotification
{ {
/** public function __construct(
* The password reset token. public string $token
* ) {
* @var string
*/
public $token;
/**
* Create a notification instance.
*
* @param string $token
*/
public function __construct($token)
{
$this->token = $token;
} }
/** public function toMail(User $notifiable): MailMessage
* Build the mail representation of the notification.
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail()
{ {
return $this->newMailMessage() return $this->newMailMessage()
->subject(trans('auth.email_reset_subject', ['appName' => setting('app-name')])) ->subject(trans('auth.email_reset_subject', ['appName' => setting('app-name')]))

View File

@ -2,16 +2,12 @@
namespace BookStack\Notifications; namespace BookStack\Notifications;
use BookStack\Users\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
class TestEmail extends MailNotification class TestEmail extends MailNotification
{ {
/** public function toMail(User $notifiable): MailMessage
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{ {
return $this->newMailMessage() return $this->newMailMessage()
->subject(trans('settings.maint_send_test_email_mail_subject')) ->subject(trans('settings.maint_send_test_email_mail_subject'))

View File

@ -7,25 +7,17 @@ use Illuminate\Notifications\Messages\MailMessage;
class UserInvite extends MailNotification class UserInvite extends MailNotification
{ {
public $token; public function __construct(
public string $token
/** ) {
* Create a new notification instance.
*/
public function __construct(string $token)
{
$this->token = $token;
} }
/**
* Get the mail representation of the notification.
*/
public function toMail(User $notifiable): MailMessage public function toMail(User $notifiable): MailMessage
{ {
$appName = ['appName' => setting('app-name')]; $appName = ['appName' => setting('app-name')];
$language = setting()->getUser($notifiable, 'language'); $language = $notifiable->getLanguage();
return $this->newMailMessage() return $this->newMailMessage($language)
->subject(trans('auth.user_invite_email_subject', $appName, $language)) ->subject(trans('auth.user_invite_email_subject', $appName, $language))
->greeting(trans('auth.user_invite_email_greeting', $appName, $language)) ->greeting(trans('auth.user_invite_email_greeting', $appName, $language))
->line(trans('auth.user_invite_email_text', [], $language)) ->line(trans('auth.user_invite_email_text', [], $language))

View File

@ -2,6 +2,7 @@
namespace BookStack\Translation; namespace BookStack\Translation;
use BookStack\Users\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class LanguageManager class LanguageManager
@ -80,6 +81,15 @@ class LanguageManager
return setting()->getUser($user, 'language', $default); return setting()->getUser($user, 'language', $default);
} }
/**
* Get the language for the given user.
*/
public function getLanguageForUser(User $user): string
{
$default = config('app.locale');
return setting()->getUser($user, 'language', $default);
}
/** /**
* Check if the given BookStack language value is a right-to-left language. * Check if the given BookStack language value is a right-to-left language.
*/ */

View File

@ -11,6 +11,7 @@ use BookStack\App\Model;
use BookStack\App\Sluggable; use BookStack\App\Sluggable;
use BookStack\Entities\Tools\SlugGenerator; use BookStack\Entities\Tools\SlugGenerator;
use BookStack\Notifications\ResetPassword; use BookStack\Notifications\ResetPassword;
use BookStack\Translation\LanguageManager;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
@ -338,6 +339,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
return ''; return '';
} }
/**
* Get the system language for this user.
*/
public function getLanguage(): string
{
return app()->make(LanguageManager::class)->getLanguageForUser($this);
}
/** /**
* Send the password reset notification. * Send the password reset notification.
* *

View File

@ -159,7 +159,7 @@ $style = [
<tr> <tr>
<td style="{{ $fontFamily }}"> <td style="{{ $fontFamily }}">
<p style="{{ $style['paragraph-sub'] }}"> <p style="{{ $style['paragraph-sub'] }}">
{{ trans('common.email_action_help', ['actionText' => $actionText]) }} {{ trans('common.email_action_help', ['actionText' => $actionText], $language) }}
</p> </p>
<p style="{{ $style['paragraph-sub'] }}"> <p style="{{ $style['paragraph-sub'] }}">
@ -187,7 +187,7 @@ $style = [
<p style="{{ $style['paragraph-sub'] }}"> <p style="{{ $style['paragraph-sub'] }}">
&copy; {{ date('Y') }} &copy; {{ date('Y') }}
<a style="{{ $style['anchor'] }}" href="{{ url('/') }}" target="_blank">{{ setting('app-name') }}</a>. <a style="{{ $style['anchor'] }}" href="{{ url('/') }}" target="_blank">{{ setting('app-name') }}</a>.
{{ trans('common.email_rights') }} {{ trans('common.email_rights', [], $language) }}
</p> </p>
</td> </td>
</tr> </tr>