feat: Add events for Notification read / read all (#3203)

This commit is contained in:
Ian Morland 2021-12-14 20:38:50 +00:00 committed by GitHub
parent 11fd012f70
commit 6136ce8d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 2 deletions

View File

@ -9,7 +9,10 @@
namespace Flarum\Notification\Command;
use Flarum\Notification\Event\ReadAll;
use Flarum\Notification\NotificationRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Carbon;
class ReadAllNotificationsHandler
{
@ -19,11 +22,18 @@ class ReadAllNotificationsHandler
protected $notifications;
/**
* @param NotificationRepository $notifications
* @var Dispatcher
*/
public function __construct(NotificationRepository $notifications)
protected $events;
/**
* @param NotificationRepository $notifications
* @param Dispatcher $events
*/
public function __construct(NotificationRepository $notifications, Dispatcher $events)
{
$this->notifications = $notifications;
$this->events = $events;
}
/**
@ -37,5 +47,7 @@ class ReadAllNotificationsHandler
$actor->assertRegistered();
$this->notifications->markAllAsRead($actor);
$this->events->dispatch(new ReadAll($actor, Carbon::now()));
}
}

View File

@ -10,10 +10,25 @@
namespace Flarum\Notification\Command;
use Carbon\Carbon;
use Flarum\Notification\Event\Read;
use Flarum\Notification\Notification;
use Illuminate\Contracts\Events\Dispatcher;
class ReadNotificationHandler
{
/**
* @var Dispatcher
*/
protected $events;
/**
* @param Dispatcher $events
*/
public function __construct(Dispatcher $events)
{
$this->events = $events;
}
/**
* @param ReadNotification $command
* @return \Flarum\Notification\Notification
@ -36,6 +51,8 @@ class ReadNotificationHandler
$notification->read_at = Carbon::now();
$this->events->dispatch(new Read($actor, $notification, Carbon::now()));
return $notification;
}
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Notification\Event;
use DateTime;
use Flarum\Notification\Notification;
use Flarum\User\User;
class Read
{
/**
* @var User
*/
public $actor;
/**
* @var Notification
*/
public $notification;
/**
* @var DateTime
*/
public $timestamp;
public function __construct(User $user, Notification $notification, DateTime $timestamp)
{
$this->user = $user;
$this->notification = $notification;
$this->timestamp = $timestamp;
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Notification\Event;
use DateTime;
use Flarum\User\User;
class ReadAll
{
/**
* @var User
*/
public $actor;
/**
* @var DateTime
*/
public $timestamp;
public function __construct(User $user, DateTime $timestamp)
{
$this->user = $user;
$this->timestamp = $timestamp;
}
}