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:
Toby Zerner 2015-05-20 12:24:01 +09:30
parent fe8763c3d0
commit 8edb684ea9
25 changed files with 334 additions and 313 deletions

View File

@ -8,7 +8,7 @@ export default class DiscussionRenamedNotification extends Notification {
return super.view({
href: app.route.discussion(notification.subject(), notification.content().postNumber),
icon: 'pencil',
content: [username(notification.sender()), ' renamed']
content: [username(notification.sender()), ' changed the title']
});
}
}

View File

@ -14,7 +14,7 @@ class CreateNotificationsTable extends Migration
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('sender_id')->unsigned()->nullable();
@ -24,6 +24,7 @@ class CreateNotificationsTable extends Migration
$table->binary('data')->nullable();
$table->dateTime('time');
$table->boolean('is_read')->default(0);
$table->boolean('is_deleted')->default(0);
});
}

View File

@ -73,6 +73,6 @@ class IndexAction extends SerializeCollectionAction
$user->markNotificationsAsRead()->save();
return $this->notifications->findByUser($user->id, $request->limit, $request->offset);
return $this->notifications->findByUser($user, $request->limit, $request->offset);
}
}

View File

@ -64,9 +64,8 @@ abstract class BaseSerializer extends SerializerAbstract
}
}
if (is_array($serializer)) {
$class = get_class(is_object($data) ? $data : $model->$relation()->getRelated());
$serializer = $serializer[$class];
if ($serializer instanceof Closure) {
$serializer = $serializer($model, $data);
}
$serializer = new $serializer($this->actor, $links);
return $many ? $serializer->collection($data) : $serializer->resource($data);

View File

@ -4,10 +4,21 @@ class NotificationSerializer extends BaseSerializer
{
/**
* The resource type.
*
* @var string
*/
protected $type = 'notifications';
/**
* A map of notification types (key) to the serializer that should be used
* to output the notification's subject (value).
*
* @var array
*/
public static $subjects = [
'discussionRenamed' => 'Flarum\Api\Serializers\DiscussionBasicSerializer'
];
/**
* Serialize attributes of an notification model for JSON output.
*
@ -40,9 +51,8 @@ class NotificationSerializer extends BaseSerializer
public function subject()
{
return $this->hasOne([
'Flarum\Core\Models\Discussion' => 'Flarum\Api\Serializers\DiscussionSerializer',
'Flarum\Core\Models\CommentPost' => 'Flarum\Api\Serializers\PostSerializer'
]);
return $this->hasOne(function ($notification) {
return static::$subjects[$notification->type];
});
}
}

View File

@ -16,6 +16,7 @@ use League\Flysystem\Adapter\Local;
use Flarum\Core\Events\RegisterDiscussionGambits;
use Flarum\Core\Events\RegisterUserGambits;
use Flarum\Extend\Permission;
use Flarum\Extend\NotificationType;
class CoreServiceProvider extends ServiceProvider
{
@ -43,6 +44,12 @@ class CoreServiceProvider extends ServiceProvider
'Flarum\Core\Handlers\Commands'
);
});
$events->subscribe('Flarum\Core\Handlers\Events\DiscussionRenamedNotifier');
$this->extend(
(new NotificationType('Flarum\Core\Notifications\DiscussionRenamedNotification', 'Flarum\Api\Serializers\DiscussionBasicSerializer'))
->enableByDefault('alert'),
);
}
/**
@ -52,8 +59,6 @@ class CoreServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->register('Flarum\Core\Notifications\NotificationServiceProvider');
// Register a singleton entity that represents this forum. This entity
// will be used to check for global forum permissions (like viewing the
// forum, registering, and starting discussions.)
@ -89,6 +94,11 @@ class CoreServiceProvider extends ServiceProvider
$this->app->when('Flarum\Core\Handlers\Commands\DeleteAvatarCommandHandler')
->needs('League\Flysystem\FilesystemInterface')
->give($avatarFilesystem);
$this->app->bind(
'Flarum\Core\Repositories\NotificationRepositoryInterface',
'Flarum\Core\Repositories\EloquentNotificationRepository'
);
}
public function registerGambits()

View File

@ -4,7 +4,7 @@ use Flarum\Core\Events\PostWillBeSaved;
use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository;
use Flarum\Core\Models\CommentPost;
use Flarum\Core\Support\DispatchesEvents;
use Flarum\Core\Notifications\Notifier;
use Flarum\Core\Notifications\NotificationSyncer;
class PostReplyCommandHandler
{
@ -12,12 +12,12 @@ class PostReplyCommandHandler
protected $discussions;
protected $notifier;
protected $notifications;
public function __construct(DiscussionRepository $discussions, Notifier $notifier)
public function __construct(DiscussionRepository $discussions, NotificationSyncer $notifications)
{
$this->discussions = $discussions;
$this->notifier = $notifier;
$this->notifications = $notifications;
}
public function handle($command)
@ -46,7 +46,7 @@ class PostReplyCommandHandler
$post->save();
$this->notifier->onePerUser(function () use ($post) {
$this->notifications->onePerUser(function () use ($post) {
$this->dispatchEventsFor($post);
});

View File

@ -2,15 +2,17 @@
use Flarum\Core\Events\DiscussionWasRenamed;
use Flarum\Core\Models\DiscussionRenamedPost;
use Flarum\Core\Notifications\Types\DiscussionRenamedNotification;
use Flarum\Core\Notifications\Notifier;
use Flarum\Core\Notifications\DiscussionRenamedNotification;
use Flarum\Core\Notifications\NotificationSyncer;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionRenamedNotifier
{
public function __construct(Notifier $notifier)
protected $notifications;
public function __construct(NotificationSyncer $notifications)
{
$this->notifier = $notifier;
$this->notifications = $notifications;
}
/**
@ -25,28 +27,19 @@ class DiscussionRenamedNotifier
public function whenDiscussionWasRenamed(DiscussionWasRenamed $event)
{
$post = $this->createPost($event);
$post = $event->discussion->addPost($post);
if ($event->discussion->start_user_id !== $event->user->id) {
$notification = new DiscussionRenamedNotification($event->discussion, $post->user, $post);
if ($post->exists) {
$this->notifier->send($notification, [$post->discussion->startUser]);
} else {
$this->notifier->retract($notification);
}
}
}
protected function createPost(DiscussionWasRenamed $event)
{
return DiscussionRenamedPost::reply(
$post = DiscussionRenamedPost::reply(
$event->discussion->id,
$event->user->id,
$event->oldTitle,
$event->discussion->title
);
$post = $event->discussion->addPost($post);
if ($event->discussion->start_user_id !== $event->user->id) {
$notification = new DiscussionRenamedNotification($post);
$this->notifications->sync($notification, $post->exists ? [$event->discussion->startUser] : []);
}
}
}

View File

@ -327,7 +327,13 @@ class User extends Model
public function getUnreadNotificationsCount()
{
return $this->notifications()->where('time', '>', $this->notification_read_time ?: 0)->where('is_read', 0)->count(\DB::raw('DISTINCT type, subject_id'));
$types = array_keys(Notification::getTypes());
return $this->notifications()
->whereIn('type', array_filter($types, [$this, 'shouldAlert']))
->where('time', '>', $this->notification_read_time ?: 0)
->where('is_read', 0)
->count(\DB::raw('DISTINCT type, subject_id'));
}
public function getPreferencesAttribute($value)
@ -354,14 +360,19 @@ class User extends Model
];
}
public static function notificationPreferenceKey($type, $sender)
public static function notificationPreferenceKey($type, $method)
{
return 'notify_'.$type.'_'.$sender;
return 'notify_'.$type.'_'.$method;
}
public function shouldNotify($type, $method)
public function shouldAlert($type)
{
return $this->preference(static::notificationPreferenceKey($type, $method));
return $this->preference(static::notificationPreferenceKey($type, 'alert'));
}
public function shouldEmail($type)
{
return $this->preference(static::notificationPreferenceKey($type, 'email'));
}
public function preference($key, $default = null)

View 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';
}
}

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

View 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();
}

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

@ -1,8 +0,0 @@
<?php namespace Flarum\Core\Notifications\Senders;
use Flarum\Core\Notifications\Types\Notification;
interface RetractableSender
{
public function retract(Notification $notification);
}

View File

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

View File

@ -1,8 +0,0 @@
<?php namespace Flarum\Core\Notifications\Types;
interface EmailableNotification
{
public function getEmailView();
public function getEmailSubject();
}

View File

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

View File

@ -1,15 +1,17 @@
<?php namespace Flarum\Core\Repositories;
use Flarum\Core\Models\Notification;
use Flarum\Core\Models\User;
use DB;
class EloquentNotificationRepository implements NotificationRepositoryInterface
{
public function findByUser($userId, $limit = null, $offset = 0)
public function findByUser(User $user, $limit = null, $offset = 0)
{
$primaries = Notification::select(DB::raw('MAX(id) AS id'), DB::raw('SUM(is_read = 0) AS unread_count'))
->where('user_id', $userId)
->whereIn('type', array_keys(Notification::getTypes()))
->where('user_id', $user->id)
->whereIn('type', array_filter(array_keys(Notification::getTypes()), [$user, 'shouldAlert']))
->where('is_deleted', false)
->groupBy('type', 'subject_id')
->orderBy('time', 'desc')
->skip($offset)

View File

@ -1,6 +1,8 @@
<?php namespace Flarum\Core\Repositories;
use Flarum\Core\Models\User;
interface NotificationRepositoryInterface
{
public function findByUser($userId, $count = null, $start = 0);
public function findByUser(User $user, $count = null, $start = 0);
}

View File

@ -3,16 +3,20 @@
use Illuminate\Foundation\Application;
use Flarum\Core\Models\Notification;
use Flarum\Core\Models\User;
use Flarum\Api\Serializers\NotificationSerializer;
class NotificationType implements ExtenderInterface
{
protected $class;
protected $serializer;
protected $enabled = [];
public function __construct($class)
public function __construct($class, $serializer)
{
$this->class = $class;
$this->serializer = $serializer;
}
public function enableByDefault($method)
@ -24,17 +28,16 @@ class NotificationType implements ExtenderInterface
public function extend(Application $app)
{
$notifier = $app['flarum.notifier'];
$class = $this->class;
$notifier->registerType($class);
Notification::registerType($class);
foreach ($notifier->getMethods() as $method => $sender) {
if ($sender::compatibleWith($class)) {
User::registerPreference(User::notificationPreferenceKey($class::getType(), $method), 'boolval', in_array($method, $this->enabled));
}
User::registerPreference(User::notificationPreferenceKey($class::getType(), 'alert'), 'boolval', in_array('alert', $this->enabled));
if ($class::isEmailable()) {
User::registerPreference(User::notificationPreferenceKey($class::getType(), 'email'), 'boolval', in_array('email', $this->enabled));
}
NotificationSerializer::$subjects[$class::getType()] = $this->serializer;
}
}