aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Forum/DiscussionPostings.php
blob: 0360aadebe891ad5cf0eb1a9933c8034aeb9c720 (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
45
46
47
48
49
50
51
52
<?php
namespace JsonApi\Routes\Forum;

use Forum\Discussion;
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 Forum\PostingRead;

class DiscussionPostings extends JsonApiController
{
    protected $allowedPagingParameters = ['offset', 'limit'];
    protected $allowedIncludePaths = [
        \JsonApi\Schemas\Forum\Posting::REL_DISCUSSION,
        \JsonApi\Schemas\Forum\Posting::REL_POSTING,
        \JsonApi\Schemas\Forum\Posting::REL_OPENGRAPH_URLS,
        \JsonApi\Schemas\Forum\Posting::REL_AUTHOR,
        \JsonApi\Schemas\Forum\Posting::REL_REACTIONS,
        \JsonApi\Schemas\Forum\Posting::REL_REACTIONS_USER
    ];

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

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

        $user = \User::findCurrent();
        if (!Authority::canShowForum($user, $range)) {
            throw new AuthorizationFailedException();
        }

        $postings = $discussion->postings ?? \SimpleORMapCollection::createFromArray([]);

        if ($user) {
            PostingRead::updateUserReadPoint($user->user_id, $discussion->discussion_id, count($postings));
        }

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