aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Forum/PostingReactions.php
blob: 12caad4a0be372c3aaae4b0dd7f7c7b2490f7629 (plain)
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
<?php
namespace JsonApi\Routes\Forum;

use Forum\Posting;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\JsonApiController;
use SimpleORMapCollection;

class PostingReactions extends JsonApiController
{
    protected $allowedPagingParameters = ['offset', 'limit'];
    protected $allowedIncludePaths = [
        \JsonApi\Schemas\Forum\PostingReaction::REL_POSTING,
        \JsonApi\Schemas\Forum\PostingReaction::REL_USER
    ];

    public function __invoke(Request $request, Response $response, $args)
    {
        $posting = Posting::find($args['posting_id']);
        if (!$posting) {
            throw new RecordNotFoundException();
        }

        $range = get_object_by_range_id($posting->range_id);
        if (!$range) {
            throw new RecordNotFoundException();
        }

        $user = $this->getUser($request);
        if (!Authority::canShowForum($user, $range)) {
            throw new AuthorizationFailedException();
        }

        $reactions = $posting->reactions ?? SimpleORMapCollection::createFromArray([]);

        return $this->getPaginatedContentResponse(
            $reactions->limit(...$this->getOffsetAndLimit()),
            count($reactions)
        );
    }
}