-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
Index.php
93 lines (78 loc) · 2.28 KB
/
Index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Forum\Content;
use Flarum\Api\Client;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Frontend\Document;
use Flarum\User\User;
use Illuminate\Contracts\View\Factory;
use Psr\Http\Message\ServerRequestInterface as Request;
class Index
{
/**
* @var Client
*/
protected $api;
/**
* @var Factory
*/
protected $view;
/**
* @param Client $api
* @param Factory $view
*/
public function __construct(Client $api, Factory $view)
{
$this->api = $api;
$this->view = $view;
}
public function __invoke(Document $document, Request $request)
{
$queryParams = $request->getQueryParams();
$sort = array_pull($queryParams, 'sort');
$q = array_pull($queryParams, 'q');
$page = array_pull($queryParams, 'page', 1);
$sortMap = $this->getSortMap();
$params = [
'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '',
'filter' => compact('q'),
'page' => ['offset' => ($page - 1) * 20, 'limit' => 20]
];
$apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params);
$document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page', 'forum'));
$document->payload['apiDocument'] = $apiDocument;
return $document;
}
/**
* Get a map of sort query param values and their API sort params.
*
* @return array
*/
private function getSortMap()
{
return [
'latest' => '-lastPostedAt',
'top' => '-commentCount',
'newest' => '-createdAt',
'oldest' => 'createdAt'
];
}
/**
* Get the result of an API request to list discussions.
*
* @param User $actor
* @param array $params
* @return object
*/
private function getApiDocument(User $actor, array $params)
{
return json_decode($this->api->send(ListDiscussionsController::class, $actor, $params)->getBody());
}
}