mirror of
https://github.com/flarum/core.git
synced 2025-10-18 18:26:07 +02:00
Simplify and improve notifications API.
It turns out that the idea of “sending” a notification is flawed. (What happens if the notification subject is deleted shortly after? The notified user would end up with a dud notification which would be confusing. What about if a post is edited to mention an extra user? If you sent out notifications, the users who’ve already been mentioned would get a duplicate notification.) Instead, I’ve introduced the idea of notification “syncing”. Whenever a change is made to a piece of data (e.g. a post is created, edited, or deleted), you make a common notification and “sync” it to a set of users. The users who’ve received this notification before won’t get it again. It will be sent out to new users, and hidden from users who’ve received it before but are no longer recipients (e.g. users who’ve been “unmentioned” in a post). To keep track of this, we use the existing notifications database table, with an added `is_deleted` column. The syncing/diffing is handled all behind the scenes; the API is extremely simple (see Core\Notifications\DiscussionRenamedNotification + Core\Events\Handlers\DiscussionRenamedNotifier)
This commit is contained in:
38
src/Core/Notifications/DiscussionRenamedNotification.php
Normal file
38
src/Core/Notifications/DiscussionRenamedNotification.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Core\Models\DiscussionRenamedPost;
|
||||
|
||||
class DiscussionRenamedNotification extends NotificationAbstract
|
||||
{
|
||||
protected $post;
|
||||
|
||||
public function __construct(DiscussionRenamedPost $post)
|
||||
{
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post->discussion;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->post->user;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return ['postNumber' => (int) $this->post->number];
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return 'discussionRenamed';
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Models\Discussion';
|
||||
}
|
||||
}
|
54
src/Core/Notifications/NotificationAbstract.php
Normal file
54
src/Core/Notifications/NotificationAbstract.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
abstract class NotificationAbstract implements NotificationInterface
|
||||
{
|
||||
/**
|
||||
* Get the user that sent the notification.
|
||||
*
|
||||
* @return \Flarum\Core\Models\User|null
|
||||
*/
|
||||
public function getSender()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data to be stored in the notification.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the view to construct a notification email with.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailView()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subject line for a notification email.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailSubject()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the notification is able to be sent as an email.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isEmailable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
60
src/Core/Notifications/NotificationInterface.php
Normal file
60
src/Core/Notifications/NotificationInterface.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
interface NotificationInterface
|
||||
{
|
||||
/**
|
||||
* Get the model that is the subject of this activity.
|
||||
*
|
||||
* @return \Flarum\Core\Models\Model
|
||||
*/
|
||||
public function getSubject();
|
||||
|
||||
/**
|
||||
* Get the user that sent the notification.
|
||||
*
|
||||
* @return \Flarum\Core\Models\User|null
|
||||
*/
|
||||
public function getSender();
|
||||
|
||||
/**
|
||||
* Get the data to be stored in the notification.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData();
|
||||
|
||||
/**
|
||||
* Get the name of the view to construct a notification email with.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailView();
|
||||
|
||||
/**
|
||||
* Get the subject line for a notification email.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailSubject();
|
||||
|
||||
/**
|
||||
* Get the serialized type of this activity.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getType();
|
||||
|
||||
/**
|
||||
* Get the name of the model class for the subject of this activity.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSubjectModel();
|
||||
|
||||
/**
|
||||
* Whether or not the notification is able to be sent as an email.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isEmailable();
|
||||
}
|
@@ -1,12 +1,10 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Models\Forum;
|
||||
use Illuminate\Mail\Mailer;
|
||||
use ReflectionClass;
|
||||
|
||||
class NotificationEmailer implements NotificationSender
|
||||
class NotificationMailer
|
||||
{
|
||||
public function __construct(Mailer $mailer, Forum $forum)
|
||||
{
|
||||
@@ -14,7 +12,7 @@ class NotificationEmailer implements NotificationSender
|
||||
$this->forum = $forum;
|
||||
}
|
||||
|
||||
public function send(Notification $notification, User $user)
|
||||
public function send(NotificationInterface $notification, User $user)
|
||||
{
|
||||
$this->mailer->send(
|
||||
$notification->getEmailView(),
|
||||
@@ -25,9 +23,4 @@ class NotificationEmailer implements NotificationSender
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static function compatibleWith($class)
|
||||
{
|
||||
return (new ReflectionClass($class))->implementsInterface('Flarum\Core\Notifications\Types\EmailableNotification');
|
||||
}
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Notifications\Notifier;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Extend\NotificationType;
|
||||
|
||||
class NotificationServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(Dispatcher $events, Notifier $notifier)
|
||||
{
|
||||
$events->subscribe('Flarum\Core\Handlers\Events\DiscussionRenamedNotifier');
|
||||
|
||||
$notifier->registerMethod('alert', 'Flarum\Core\Notifications\Senders\NotificationAlerter');
|
||||
$notifier->registerMethod('email', 'Flarum\Core\Notifications\Senders\NotificationEmailer');
|
||||
|
||||
$this->extend(
|
||||
(new NotificationType('Flarum\Core\Notifications\Types\DiscussionRenamedNotification'))->enableByDefault('alert')
|
||||
);
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Repositories\NotificationRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentNotificationRepository'
|
||||
);
|
||||
|
||||
$this->app->singleton('Flarum\Core\Notifications\Notifier');
|
||||
$this->app->alias('Flarum\Core\Notifications\Notifier', 'flarum.notifier');
|
||||
}
|
||||
}
|
92
src/Core/Notifications/NotificationSyncer.php
Normal file
92
src/Core/Notifications/NotificationSyncer.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php namespace Flarum\Core\Notifications;
|
||||
|
||||
use Flarum\Core\Repositories\NotificationRepositoryInterface;
|
||||
use Flarum\Core\Models\Notification;
|
||||
use Carbon\Carbon;
|
||||
use Closure;
|
||||
|
||||
class NotificationSyncer
|
||||
{
|
||||
protected $onePerUser = false;
|
||||
|
||||
protected $sentTo = [];
|
||||
|
||||
protected $notifications;
|
||||
|
||||
protected $mailer;
|
||||
|
||||
public function __construct(NotificationRepositoryInterface $notifications, NotificationMailer $mailer)
|
||||
{
|
||||
$this->notifications = $notifications;
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync a notification so that it is visible to the specified users, and not
|
||||
* visible to anyone else. If it is being made visible for the first time,
|
||||
* attempt to send the user an email.
|
||||
*
|
||||
* @param \Flarum\Core\Notifications\NotificationInterface $notification
|
||||
* @param \Flarum\Core\Models\User[] $users
|
||||
* @return void
|
||||
*/
|
||||
public function sync(NotificationInterface $notification, array $users)
|
||||
{
|
||||
$attributes = [
|
||||
'type' => $notification::getType(),
|
||||
'sender_id' => $notification->getSender()->id,
|
||||
'subject_id' => $notification->getSubject()->id,
|
||||
'data' => ($data = $notification->getData()) ? json_encode($data) : null
|
||||
];
|
||||
|
||||
$toDelete = Notification::where($attributes)->get();
|
||||
$toUndelete = [];
|
||||
$newRecipients = [];
|
||||
|
||||
foreach ($users as $user) {
|
||||
$existing = $toDelete->where('user_id', $user->id)->first();
|
||||
|
||||
if (($k = $toDelete->search($existing)) !== false) {
|
||||
$toUndelete[] = $existing->id;
|
||||
$toDelete->pull($k);
|
||||
} elseif (! $this->onePerUser || ! in_array($user->id, $this->sentTo)) {
|
||||
$newRecipients[] = $user;
|
||||
$this->sentTo[] = $user->id;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($toDelete)) {
|
||||
Notification::whereIn('id', $toDelete->lists('id'))->update(['is_deleted' => true]);
|
||||
}
|
||||
|
||||
if (count($toUndelete)) {
|
||||
Notification::whereIn('id', $toUndelete)->update(['is_deleted' => false]);
|
||||
}
|
||||
|
||||
if (count($newRecipients)) {
|
||||
$now = Carbon::now('utc')->toDateTimeString();
|
||||
|
||||
Notification::insert(
|
||||
array_map(function ($user) use ($attributes, $notification, $now) {
|
||||
return $attributes + ['user_id' => $user->id, 'time' => $now];
|
||||
}, $newRecipients)
|
||||
);
|
||||
|
||||
foreach ($newRecipients as $user) {
|
||||
if ($user->shouldEmail($notification::getType())) {
|
||||
$this->mailer->send($notification, $user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function onePerUser(Closure $callback)
|
||||
{
|
||||
$this->sentTo = [];
|
||||
$this->onePerUser = true;
|
||||
|
||||
$callback();
|
||||
|
||||
$this->onePerUser = false;
|
||||
}
|
||||
}
|
@@ -1,86 +0,0 @@
|
||||
<?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;
|
||||
use Closure;
|
||||
|
||||
class Notifier
|
||||
{
|
||||
protected $methods = [];
|
||||
|
||||
protected $types = [];
|
||||
|
||||
protected $onePerUser = false;
|
||||
|
||||
protected $sentTo = [];
|
||||
|
||||
protected $container;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function send(Notification $notification, array $users)
|
||||
{
|
||||
foreach ($users as $user) {
|
||||
if ($this->onePerUser && in_array($user->id, $this->sentTo)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->methods as $method => $sender) {
|
||||
$sender = $this->container->make($sender);
|
||||
|
||||
if ($sender::compatibleWith($notification) &&
|
||||
$user->shouldNotify($notification::getType(), $method)) {
|
||||
$sender->send($notification, $user);
|
||||
$this->sentTo[] = $user->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function registerMethod($name, $class)
|
||||
{
|
||||
$this->methods[$name] = $class;
|
||||
}
|
||||
|
||||
public function registerType($class)
|
||||
{
|
||||
$this->types[] = $class;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return $this->methods;
|
||||
}
|
||||
|
||||
public function getTypes()
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
public function onePerUser(Closure $callback)
|
||||
{
|
||||
$this->sentTo = [];
|
||||
$this->onePerUser = true;
|
||||
|
||||
$callback();
|
||||
|
||||
$this->onePerUser = false;
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\Notification as NotificationModel;
|
||||
use Flarum\Core\Models\User;
|
||||
use ReflectionClass;
|
||||
|
||||
class NotificationAlerter implements NotificationSender, RetractableSender
|
||||
{
|
||||
public function send(Notification $notification, User $user)
|
||||
{
|
||||
$model = NotificationModel::alert(
|
||||
$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,11 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
interface NotificationSender
|
||||
{
|
||||
public function send(Notification $notification, User $user);
|
||||
|
||||
public static function compatibleWith($class);
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Senders;
|
||||
|
||||
use Flarum\Core\Notifications\Types\Notification;
|
||||
|
||||
interface RetractableSender
|
||||
{
|
||||
public function retract(Notification $notification);
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
<?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
|
||||
{
|
||||
protected $discussion;
|
||||
|
||||
protected $sender;
|
||||
|
||||
protected $post;
|
||||
|
||||
public function __construct(Discussion $discussion, User $sender, DiscussionRenamedPost $post = null)
|
||||
{
|
||||
$this->discussion = $discussion;
|
||||
$this->sender = $sender;
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->discussion;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->sender;
|
||||
}
|
||||
|
||||
public function getAlertData()
|
||||
{
|
||||
return ['postNumber' => $this->post->number];
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return 'discussionRenamed';
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Models\Discussion';
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Types;
|
||||
|
||||
interface EmailableNotification
|
||||
{
|
||||
public function getEmailView();
|
||||
|
||||
public function getEmailSubject();
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
<?php namespace Flarum\Core\Notifications\Types;
|
||||
|
||||
abstract class Notification
|
||||
{
|
||||
/**
|
||||
* Returns the serialized type of this notification.
|
||||
*
|
||||
* This method should be overwritten by subclasses.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getType()
|
||||
{
|
||||
return 'notification';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user