From 0c9bcba3a6de907795dd67f41ab9ec20d1a045e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sevilla=20Mart=C3=ADn?= <6401250+datitisev@users.noreply.github.com> Date: Wed, 22 Jan 2020 18:01:26 -0500 Subject: [PATCH] Add Content for User page, preload user & throw 404 accordingly (#1901) --- js/src/forum/components/UserPage.js | 7 ++- src/Forum/Content/User.php | 82 +++++++++++++++++++++++++++++ src/Forum/routes.php | 2 +- 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/Forum/Content/User.php diff --git a/js/src/forum/components/UserPage.js b/js/src/forum/components/UserPage.js index 67c27fb01..6598cbe03 100644 --- a/js/src/forum/components/UserPage.js +++ b/js/src/forum/components/UserPage.js @@ -88,8 +88,13 @@ export default class UserPage extends Page { loadUser(username) { const lowercaseUsername = username.toLowerCase(); + // Load the preloaded user object, if any, into the global app store + // We don't use the output of the method because it returns raw JSON + // instead of the parsed models + app.preloadedApiDocument(); + app.store.all('users').some(user => { - if (user.username().toLowerCase() === lowercaseUsername && user.joinTime()) { + if ((user.username().toLowerCase() === lowercaseUsername || user.id() === username) && user.joinTime()) { this.show(user); return true; } diff --git a/src/Forum/Content/User.php b/src/Forum/Content/User.php new file mode 100644 index 000000000..26a6764a6 --- /dev/null +++ b/src/Forum/Content/User.php @@ -0,0 +1,82 @@ +api = $api; + $this->url = $url; + } + + public function __invoke(Document $document, Request $request) + { + $queryParams = $request->getQueryParams(); + $actor = $request->getAttribute('actor'); + $userId = Arr::get($queryParams, 'username'); + + $params = [ + 'id' => $userId, + ]; + + $apiDocument = $this->getApiDocument($actor, $params); + $user = $apiDocument->data->attributes; + + $document->title = $user->displayName; + $document->canonicalUrl = $this->url->to('forum')->route('user', ['username' => $user->username]); + $document->payload['apiDocument'] = $apiDocument; + + return $document; + } + + /** + * Get the result of an API request to show a user. + * + * @param FlarumUser $actor + * @param array $params + * @return object + * @throws ModelNotFoundException + */ + protected function getApiDocument(FlarumUser $actor, array $params) + { + $response = $this->api->send(ShowUserController::class, $actor, $params); + $statusCode = $response->getStatusCode(); + + if ($statusCode === 404) { + throw new ModelNotFoundException; + } + + return json_decode($response->getBody()); + } +} diff --git a/src/Forum/routes.php b/src/Forum/routes.php index 4cccfaa19..61448d511 100644 --- a/src/Forum/routes.php +++ b/src/Forum/routes.php @@ -28,7 +28,7 @@ return function (RouteCollection $map, RouteHandlerFactory $route) { $map->get( '/u/{username}[/{filter:[^/]*}]', 'user', - $route->toForum() + $route->toForum(Content\User::class) ); $map->get(