This commit is contained in:
Sami Mazouz 2024-05-09 16:25:04 +01:00
parent d4d3c98dd3
commit 06fef7eec0
No known key found for this signature in database
5 changed files with 90 additions and 10 deletions

View File

@ -23,10 +23,13 @@ use Tobyz\JsonApiServer\Pagination\OffsetPagination;
*/ */
class NotificationResource extends AbstractDatabaseResource class NotificationResource extends AbstractDatabaseResource
{ {
protected bool $initialized = false;
public function __construct( public function __construct(
protected Dispatcher $bus, protected Dispatcher $bus,
protected NotificationRepository $notifications, protected NotificationRepository $notifications,
) { ) {
$this->initialized = true;
} }
public function type(): string public function type(): string
@ -63,21 +66,19 @@ class NotificationResource extends AbstractDatabaseResource
->before(function (Context $context) { ->before(function (Context $context) {
$context->getActor()->markNotificationsAsRead()->save(); $context->getActor()->markNotificationsAsRead()->save();
}) })
->defaultInclude([ ->defaultInclude(array_filter([
'fromUser', 'fromUser',
'subject', 'subject',
'subject.discussion' $this->initialized && count($this->subjectTypes()) > 1
]) ? 'subject.discussion'
: null,
]))
->paginate(), ->paginate(),
]; ];
} }
public function fields(): array public function fields(): array
{ {
$subjectTypes = $this->api->typesForModels(
(new Notification())->getSubjectModels()
);
return [ return [
Schema\Str::make('contentType') Schema\Str::make('contentType')
->property('type'), ->property('type'),
@ -87,7 +88,7 @@ class NotificationResource extends AbstractDatabaseResource
Schema\Boolean::make('isRead') Schema\Boolean::make('isRead')
->writable() ->writable()
->get(fn (Notification $notification) => (bool) $notification->read_at) ->get(fn (Notification $notification) => (bool) $notification->read_at)
->set(function (Notification $notification, Context $context) { ->set(function (Notification $notification, bool $value, Context $context) {
$this->bus->dispatch( $this->bus->dispatch(
new ReadNotification($notification->id, $context->getActor()) new ReadNotification($notification->id, $context->getActor())
); );
@ -99,8 +100,15 @@ class NotificationResource extends AbstractDatabaseResource
->type('users') ->type('users')
->includable(), ->includable(),
Schema\Relationship\ToOne::make('subject') Schema\Relationship\ToOne::make('subject')
->collection($subjectTypes) ->collection($this->subjectTypes())
->includable(), ->includable(),
]; ];
} }
protected function subjectTypes(): array
{
return $this->api->typesForModels(
(new Notification())->getSubjectModels()
);
}
} }

View File

@ -65,7 +65,7 @@ class Config implements ArrayAccess
public function safeModeExtensions(): ?array public function safeModeExtensions(): ?array
{ {
return $this->data['safe_mode_extensions']; return $this->data['safe_mode_extensions'] ?? null;
} }
private function requireKeys(mixed ...$keys): void private function requireKeys(mixed ...$keys): void

View File

@ -0,0 +1,67 @@
<?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\Tests\integration\api\notifications;
use Flarum\Discussion\Discussion;
use Flarum\Notification\Notification;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
class UpdateTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
User::class => [
$this->normalUser(),
],
Discussion::class => [
['id' => 1, 'title' => 'Foo', 'comment_count' => 1, 'user_id' => 2],
],
Post::class => [
['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => 'Foo'],
],
Notification::class => [
['id' => 1, 'user_id' => 2, 'from_user_id' => 1, 'type' => 'discussionRenamed', 'subject_id' => 1, 'read_at' => null],
]
]);
}
/**
* @test
*/
public function can_mark_all_as_read()
{
$response = $this->send(
$this->request('PATCH', '/api/notifications/1', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'type' => 'notifications',
'attributes' => [
'isRead' => true
],
],
],
])
);
$this->assertEquals(200, $response->getStatusCode(), (string) $response->getBody());
}
}

View File

@ -17,6 +17,7 @@ use Flarum\Api\Resource\UserResource;
use Flarum\Api\Schema; use Flarum\Api\Schema;
use Flarum\Discussion\Discussion; use Flarum\Discussion\Discussion;
use Flarum\Extend; use Flarum\Extend;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase; use Flarum\Testing\integration\TestCase;
use Flarum\User\User; use Flarum\User\User;

View File

@ -208,6 +208,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
'unique' => $instance->uniqueKeys ?? null, 'unique' => $instance->uniqueKeys ?? null,
]; ];
} else { } else {
if (class_exists($tableOrModelClass) && is_subclass_of($tableOrModelClass, Model::class)) {
$tableOrModelClass = (new $tableOrModelClass)->getTable();
}
$databaseContent[$tableOrModelClass] = [ $databaseContent[$tableOrModelClass] = [
'rows' => $_rows, 'rows' => $_rows,
'unique' => null, 'unique' => null,