mirror of
https://github.com/flarum/core.git
synced 2025-10-18 18:26:07 +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);
|
||||
}
|
||||
}
|
48
src/Api/Serializers/NotificationSerializer.php
Normal file
48
src/Api/Serializers/NotificationSerializer.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
class NotificationSerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The resource type.
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'notifications';
|
||||
|
||||
/**
|
||||
* Serialize attributes of an notification model for JSON output.
|
||||
*
|
||||
* @param Notification $notification The notification model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes($notification)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => (int) $notification->id,
|
||||
'contentType' => $notification->type,
|
||||
'content' => $notification->data,
|
||||
'time' => $notification->time->toRFC3339String(),
|
||||
'isRead' => (bool) $notification->is_read,
|
||||
'unreadCount' => $notification->unread_count
|
||||
];
|
||||
|
||||
return $this->extendAttributes($notification, $attributes);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
public function sender()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
public function subject()
|
||||
{
|
||||
return $this->hasOne([
|
||||
'Flarum\Core\Models\Discussion' => 'Flarum\Api\Serializers\DiscussionSerializer',
|
||||
'Flarum\Core\Models\CommentPost' => 'Flarum\Api\Serializers\PostSerializer'
|
||||
]);
|
||||
}
|
||||
}
|
@@ -11,6 +11,13 @@ $action = function ($class) {
|
||||
|
||||
Route::group(['prefix' => 'api', 'middleware' => 'Flarum\Api\Middleware\LoginWithHeader'], function () use ($action) {
|
||||
|
||||
// Get forum information
|
||||
Route::get('forum', [
|
||||
'as' => 'flarum.api.forum.show',
|
||||
'uses' => $action('Flarum\Api\Actions\Forum\ShowAction')
|
||||
]);
|
||||
|
||||
// Retrieve authentication token
|
||||
Route::post('token', [
|
||||
'as' => 'flarum.api.token',
|
||||
'uses' => $action('Flarum\Api\Actions\TokenAction')
|
||||
@@ -70,6 +77,12 @@ Route::group(['prefix' => 'api', 'middleware' => 'Flarum\Api\Middleware\LoginWit
|
||||
'uses' => $action('Flarum\Api\Actions\Notifications\IndexAction')
|
||||
]);
|
||||
|
||||
// Mark a single notification as read
|
||||
Route::put('notifications/{id}', [
|
||||
'as' => 'flarum.api.notifications.update',
|
||||
'uses' => $action('Flarum\Api\Actions\Notifications\UpdateAction')
|
||||
]);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Discussions
|
||||
|
Reference in New Issue
Block a user