Update for new notifications API

This commit is contained in:
Toby Zerner 2015-05-20 12:30:57 +09:30
parent 736824d45c
commit 72a676ddfd
3 changed files with 21 additions and 40 deletions

View File

@ -36,7 +36,7 @@ class CategoriesServiceProvider extends ServiceProvider
new DiscussionGambit('Flarum\Categories\CategoryGambit'),
(new NotificationType('Flarum\Categories\DiscussionMovedNotification'))->enableByDefault('alert'),
(new NotificationType('Flarum\Categories\DiscussionMovedNotification', 'Flarum\Api\Serializers\DiscussionBasicSerializer'))->enableByDefault('alert'),
new Relationship('Flarum\Core\Models\Discussion', 'belongsTo', 'category', 'Flarum\Categories\Category'),

View File

@ -1,39 +1,29 @@
<?php namespace Flarum\Categories;
use Flarum\Core\Models\User;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Models\Notification as NotificationModel;
use Flarum\Core\Notifications\Types\Notification;
use Flarum\Core\Notifications\Types\AlertableNotification;
use Flarum\Core\Notifications\NotificationAbstract;
class DiscussionMovedNotification extends Notification implements AlertableNotification
class DiscussionMovedNotification extends NotificationAbstract
{
protected $discussion;
protected $sender;
protected $post;
public function __construct(Discussion $discussion, User $sender, DiscussionMovedPost $post = null)
public function __construct(DiscussionMovedPost $post)
{
$this->discussion = $discussion;
$this->sender = $sender;
$this->post = $post;
}
public function getSubject()
{
return $this->discussion;
return $this->post->discussion;
}
public function getSender()
{
return $this->sender;
return $this->post->user;
}
public function getAlertData()
public function getData()
{
return ['postNumber' => $this->post->number];
return ['postNumber' => (int) $this->post->number];
}
public static function getType()

View File

@ -3,16 +3,16 @@
use Flarum\Categories\DiscussionMovedPost;
use Flarum\Categories\DiscussionMovedNotification;
use Flarum\Categories\Events\DiscussionWasMoved;
use Flarum\Core\Notifications\Notifier;
use Flarum\Core\Notifications\NotificationSyncer;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionMovedNotifier
{
protected $notifier;
protected $notifications;
public function __construct(Notifier $notifier)
public function __construct(NotificationSyncer $notifications)
{
$this->notifier = $notifier;
$this->notifications = $notifications;
}
/**
@ -27,28 +27,19 @@ class DiscussionMovedNotifier
public function whenDiscussionWasMoved(DiscussionWasMoved $event)
{
$post = $this->createPost($event);
$post = $event->discussion->addPost($post);
if ($event->discussion->start_user_id !== $event->user->id) {
$notification = new DiscussionMovedNotification($event->discussion, $post->user, $post);
if ($post->exists) {
$this->notifier->send($notification, [$post->discussion->startUser]);
} else {
$this->notifier->retract($notification);
}
}
}
protected function createPost(DiscussionWasMoved $event)
{
return DiscussionMovedPost::reply(
$post = DiscussionMovedPost::reply(
$event->discussion->id,
$event->user->id,
$event->oldCategoryId,
$event->discussion->category_id
);
$post = $event->discussion->addPost($post);
if ($event->discussion->start_user_id !== $event->user->id) {
$notification = new DiscussionMovedNotification($post);
$this->notifications->sync($notification, $post->exists ? [$event->discussion->startUser] : []);
}
}
}