mirror of
https://github.com/flarum/core.git
synced 2025-10-18 10:16:09 +02:00
Implement notifications
This commit is contained in:
50
src/Api/Actions/Notifications/IndexAction.php
Normal file
50
src/Api/Actions/Notifications/IndexAction.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php namespace Flarum\Api\Actions\Notifications;
|
||||
|
||||
use Flarum\Core\Repositories\NotificationRepositoryInterface;
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\NotificationSerializer;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Flarum\Core\Search\Discussions\UserSearcher $searcher
|
||||
*/
|
||||
public function __construct(Actor $actor, NotificationRepositoryInterface $notifications)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->notifications = $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a user's notifications feed.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$start = $params->start();
|
||||
$count = $params->count(10, 50);
|
||||
|
||||
if (! $this->actor->isAuthenticated()) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
$notifications = $this->notifications->findByUser($user->id, $count, $start);
|
||||
|
||||
$user->markNotificationsAsRead()->save();
|
||||
|
||||
// Finally, we can set up the notification serializer and use it to create
|
||||
// a collection of notification results.
|
||||
$serializer = new NotificationSerializer(['sender', 'subject', 'subject.discussion']);
|
||||
$document = $this->document()->setData($serializer->collection($notifications));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
34
src/Api/Actions/Notifications/UpdateAction.php
Normal file
34
src/Api/Actions/Notifications/UpdateAction.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php namespace Flarum\Api\Actions\Notifications;
|
||||
|
||||
use Flarum\Core\Commands\ReadNotificationCommand;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\NotificationSerializer;
|
||||
|
||||
class UpdateAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Edit a discussion. Allows renaming the discussion, and updating its read
|
||||
* state with regards to the current user.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$notificationId = $params->get('id');
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
// if ($params->get('notifications.isRead')) {
|
||||
$command = new ReadNotificationCommand($notificationId, $user);
|
||||
$notification = $this->dispatch($command, $params);
|
||||
// }
|
||||
|
||||
// Presumably, the discussion was updated successfully. (One of the command
|
||||
// handlers would have thrown an exception if not.) We set this
|
||||
// discussion as our document's primary element.
|
||||
$serializer = new NotificationSerializer;
|
||||
$document = $this->document()->setData($serializer->resource($notification));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user