1
0
mirror of https://github.com/flarum/core.git synced 2025-07-17 14:51:19 +02:00

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 fbfc80f979
commit 77264be5e3
4 changed files with 102 additions and 2 deletions

View File

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

View File

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