mirror of
https://github.com/flarum/core.git
synced 2025-10-19 02:36:08 +02:00
Rework notifications architecture
- The recipient(s) are the concern of the notifier/sender, not the notification itself - Allow “retraction” of notifications (e.g. if a discussion is stickied, but then it is unstickied) - Misc. cleanup
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Notifications\Senders\RetractableSender;
|
||||
use Flarum\Core\Models\Notification as NotificationModel;
|
||||
use Flarum\Core\Models\User;
|
||||
use Illuminate\Container\Container;
|
||||
@@ -18,12 +19,28 @@ class Notifier
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function send(Notification $notification)
|
||||
public function send(Notification $notification, array $users)
|
||||
{
|
||||
foreach ($this->methods as $method => $sender) {
|
||||
$sender = $this->container->make($sender);
|
||||
if ($notification->getRecipient()->shouldNotify($notification::getType(), $method) && $sender::compatibleWith($notification)) {
|
||||
$sender->send($notification);
|
||||
|
||||
if ($sender::compatibleWith($notification)) {
|
||||
foreach ($users as $user) {
|
||||
if ($user->shouldNotify($notification::getType(), $method)) {
|
||||
$sender->send($notification, $user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function retract(Notification $notification)
|
||||
{
|
||||
foreach ($this->methods as $method => $sender) {
|
||||
$sender = $this->container->make($sender);
|
||||
|
||||
if ($sender instanceof RetractableSender && $sender::compatibleWith($notification)) {
|
||||
$sender->retract($notification);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,22 +2,31 @@
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\Notification as NotificationModel;
|
||||
use Flarum\Core\Models\User;
|
||||
use ReflectionClass;
|
||||
|
||||
class NotificationAlerter implements NotificationSender
|
||||
class NotificationAlerter implements NotificationSender, RetractableSender
|
||||
{
|
||||
public function send(Notification $notification)
|
||||
public function send(Notification $notification, User $user)
|
||||
{
|
||||
$model = NotificationModel::alert(
|
||||
$notification->getRecipient()->id,
|
||||
$user->id,
|
||||
$notification::getType(),
|
||||
$notification->getSender()->id,
|
||||
$notification->getSubject()->id,
|
||||
$notification->getAlertData()
|
||||
);
|
||||
|
||||
$model->save();
|
||||
}
|
||||
|
||||
public function retract(Notification $notification)
|
||||
{
|
||||
$models = NotificationModel::where('type', $notification::getType())
|
||||
->where('subject_id', $notification->getSubject()->id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
public static function compatibleWith($className)
|
||||
{
|
||||
return (new ReflectionClass($className))->implementsInterface('Flarum\Core\Notifications\Types\AlertableNotification');
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Models\Forum;
|
||||
use Illuminate\Mail\Mailer;
|
||||
use ReflectionClass;
|
||||
@@ -13,10 +14,9 @@ class NotificationEmailer implements NotificationSender
|
||||
$this->forum = $forum;
|
||||
}
|
||||
|
||||
public function send(Notification $notification)
|
||||
public function send(Notification $notification, User $user)
|
||||
{
|
||||
$this->mailer->send($notification->getEmailView(), ['notification' => $notification], function ($message) use ($notification) {
|
||||
$recipient = $notification->getRecipient();
|
||||
$this->mailer->send($notification->getEmailView(), ['notification' => $notification], function ($message) use ($notification, $recipient) {
|
||||
$message->to($recipient->email, $recipient->username)
|
||||
->subject('['.$this->forum->title.'] '.$notification->getEmailSubject());
|
||||
});
|
||||
|
@@ -1,10 +1,11 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
interface NotificationSender
|
||||
{
|
||||
public function send(Notification $notification);
|
||||
public function send(Notification $notification, User $user);
|
||||
|
||||
public static function compatibleWith($class);
|
||||
}
|
||||
|
8
src/Core/Notifications/Senders/RetractableSender.php
Normal file
8
src/Core/Notifications/Senders/RetractableSender.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
|
||||
interface RetractableSender
|
||||
{
|
||||
public function retract(Notification $notification);
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Types;
|
||||
|
||||
interface AlertableNotification
|
||||
{
|
||||
public function getAlertData();
|
||||
|
||||
public static function getType();
|
||||
}
|
@@ -1,33 +1,37 @@
|
||||
<?php namespace Flarum\Core\Notifications\Types;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Models\DiscussionRenamedPost;
|
||||
|
||||
class DiscussionRenamedNotification extends Notification implements AlertableNotification
|
||||
{
|
||||
public $post;
|
||||
protected $discussion;
|
||||
|
||||
public $oldTitle;
|
||||
protected $sender;
|
||||
|
||||
public function __construct(User $recipient, User $sender, DiscussionRenamedPost $post, $oldTitle)
|
||||
protected $post;
|
||||
|
||||
public function __construct(Discussion $discussion, User $sender, DiscussionRenamedPost $post = null)
|
||||
{
|
||||
$this->discussion = $discussion;
|
||||
$this->sender = $sender;
|
||||
$this->post = $post;
|
||||
$this->oldTitle = $oldTitle;
|
||||
|
||||
parent::__construct($recipient, $sender);
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post->discussion;
|
||||
return $this->discussion;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->sender;
|
||||
}
|
||||
|
||||
public function getAlertData()
|
||||
{
|
||||
return [
|
||||
'number' => $this->post->number,
|
||||
'oldTitle' => $this->oldTitle
|
||||
];
|
||||
return ['postNumber' => $this->post->number];
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
|
@@ -1,36 +1,6 @@
|
||||
<?php namespace Flarum\Core\Notifications\Types;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
abstract class Notification
|
||||
{
|
||||
protected $recipient;
|
||||
|
||||
protected $sender;
|
||||
|
||||
public function __construct(User $recipient, User $sender)
|
||||
{
|
||||
$this->recipient = $recipient;
|
||||
$this->sender = $sender;
|
||||
}
|
||||
|
||||
public function getRecipient()
|
||||
{
|
||||
return $this->recipient;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->sender;
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
abstract public static function getType();
|
||||
}
|
||||
|
Reference in New Issue
Block a user