feat: include current user as forum relation (#3668)

* feat: include current user as forum relation

* Update framework/core/src/Api/Controller/ShowForumController.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update framework/core/src/Api/Controller/ShowForumController.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update framework/core/src/Api/Serializer/ForumSerializer.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update framework/core/tests/integration/api/forum/ShowTest.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update framework/core/tests/integration/api/forum/ShowTest.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* No need to call the user endpoint as we already have the data loaded

* chore: return types

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
IanM 2022-11-25 11:00:19 +00:00 committed by GitHub
parent 6f4f964ce8
commit 67c0d75ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 40 deletions

View File

@ -25,15 +25,18 @@ class ShowForumController extends AbstractShowController
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public $include = ['groups']; public $include = ['groups', 'actor'];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function data(ServerRequestInterface $request, Document $document) protected function data(ServerRequestInterface $request, Document $document)
{ {
$actor = RequestUtil::getActor($request);
return [ return [
'groups' => Group::whereVisibleTo(RequestUtil::getActor($request))->get() 'groups' => Group::whereVisibleTo($actor)->get(),
'actor' => $actor->isGuest() ? null : $actor
]; ];
} }
} }

View File

@ -15,6 +15,7 @@ use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud; use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory; use Illuminate\Contracts\Filesystem\Factory;
use Tobscure\JsonApi\Relationship;
class ForumSerializer extends AbstractSerializer class ForumSerializer extends AbstractSerializer
{ {
@ -68,7 +69,7 @@ class ForumSerializer extends AbstractSerializer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDefaultAttributes($model) protected function getDefaultAttributes($model): array
{ {
$attributes = [ $attributes = [
'title' => $this->settings->get('forum_title'), 'title' => $this->settings->get('forum_title'),
@ -104,9 +105,9 @@ class ForumSerializer extends AbstractSerializer
} }
/** /**
* @return \Tobscure\JsonApi\Relationship * @return Relationship
*/ */
protected function groups($model) protected function groups($model): Relationship
{ {
return $this->hasMany($model, GroupSerializer::class); return $this->hasMany($model, GroupSerializer::class);
} }
@ -114,7 +115,7 @@ class ForumSerializer extends AbstractSerializer
/** /**
* @return null|string * @return null|string
*/ */
protected function getLogoUrl() protected function getLogoUrl(): ?string
{ {
$logoPath = $this->settings->get('logo_path'); $logoPath = $this->settings->get('logo_path');
@ -124,7 +125,7 @@ class ForumSerializer extends AbstractSerializer
/** /**
* @return null|string * @return null|string
*/ */
protected function getFaviconUrl() protected function getFaviconUrl(): ?string
{ {
$faviconPath = $this->settings->get('favicon_path'); $faviconPath = $this->settings->get('favicon_path');
@ -135,4 +136,12 @@ class ForumSerializer extends AbstractSerializer
{ {
return $this->assetsFilesystem->url($assetPath); return $this->assetsFilesystem->url($assetPath);
} }
/**
* @return Relationship|null
*/
protected function actor($model): ?Relationship
{
return $this->hasOne($model, CurrentUserSerializer::class);
}
} }

View File

@ -9,12 +9,9 @@
namespace Flarum\Frontend\Content; namespace Flarum\Frontend\Content;
use Flarum\Api\Client;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Flarum\Http\RequestUtil; use Flarum\Http\RequestUtil;
use Flarum\Locale\LocaleManager; use Flarum\Locale\LocaleManager;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
class CorePayload class CorePayload
@ -24,19 +21,12 @@ class CorePayload
*/ */
private $locales; private $locales;
/**
* @var Client
*/
private $api;
/** /**
* @param LocaleManager $locales * @param LocaleManager $locales
* @param Client $api
*/ */
public function __construct(LocaleManager $locales, Client $api) public function __construct(LocaleManager $locales)
{ {
$this->locales = $locales; $this->locales = $locales;
$this->api = $api;
} }
public function __invoke(Document $document, Request $request) public function __invoke(Document $document, Request $request)
@ -51,17 +41,10 @@ class CorePayload
{ {
$data = $this->getDataFromApiDocument($document->getForumApiDocument()); $data = $this->getDataFromApiDocument($document->getForumApiDocument());
$actor = RequestUtil::getActor($request);
if ($actor->exists) {
$user = $this->getUserApiDocument($request, $actor);
$data = array_merge($data, $this->getDataFromApiDocument($user));
}
return [ return [
'resources' => $data, 'resources' => $data,
'session' => [ 'session' => [
'userId' => $actor->id, 'userId' => RequestUtil::getActor($request)->id,
'csrfToken' => $request->getAttribute('session')->token() 'csrfToken' => $request->getAttribute('session')->token()
], ],
'locales' => $this->locales->getLocales(), 'locales' => $this->locales->getLocales(),
@ -79,18 +62,4 @@ class CorePayload
return $data; return $data;
} }
private function getUserApiDocument(Request $request, User $actor): array
{
$id = $actor->id;
return $this->getResponseBody(
$this->api->withParentRequest($request)->get("/users/$id")
);
}
private function getResponseBody(ResponseInterface $response)
{
return json_decode($response->getBody(), true);
}
} }

View File

@ -31,6 +31,22 @@ class ShowTest extends TestCase
]); ]);
} }
/**
* @test
*/
public function guest_user_does_not_see_actor_relationship()
{
$response = $this->send(
$this->request('GET', '/api')
);
$this->assertEquals(200, $response->getStatusCode());
$json = json_decode($response->getBody()->getContents(), true);
$this->assertArrayNotHasKey('actor', Arr::get($json, 'data.relationships'));
}
/** /**
* @test * @test
*/ */
@ -51,6 +67,8 @@ class ShowTest extends TestCase
$this->assertEquals('http://localhost/api', Arr::get($json, 'data.attributes.apiUrl')); $this->assertEquals('http://localhost/api', Arr::get($json, 'data.attributes.apiUrl'));
$this->assertArrayNotHasKey('adminUrl', Arr::get($json, 'data.attributes')); $this->assertArrayNotHasKey('adminUrl', Arr::get($json, 'data.attributes'));
$this->assertArrayHasKey('actor', Arr::get($json, 'data.relationships'));
$this->assertEquals(2, Arr::get($json, 'data.relationships.actor.data.id'));
} }
/** /**