1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries;
7
8use App\Models\BeatmapDiscussionPost;
9use App\Traits\Memoizes;
10use App\Transformers\BeatmapDiscussionPostTransformer;
11use App\Transformers\BeatmapDiscussionTransformer;
12use App\Transformers\BeatmapsetCompactTransformer;
13use App\Transformers\UserCompactTransformer;
14use Illuminate\Pagination\Paginator;
15
16class BeatmapsetDiscussionPostsBundle extends BeatmapsetDiscussionsBundleBase
17{
18 use Memoizes;
19
20 public function getData()
21 {
22 return $this->getPosts();
23 }
24
25 public function toArray()
26 {
27 return array_merge([
28 'beatmapsets' => json_collection($this->getBeatmapsets(), new BeatmapsetCompactTransformer()),
29 'discussions' => json_collection($this->getDiscussions(), new BeatmapDiscussionTransformer()),
30 'posts' => json_collection($this->getPosts(), new BeatmapDiscussionPostTransformer()),
31 'users' => json_collection($this->getUsers(), new UserCompactTransformer()),
32 ], cursor_for_response($this->getCursor()));
33 }
34
35 private function getBeatmapsets()
36 {
37 return $this->memoize(__FUNCTION__, function () {
38 return $this->getPosts()->pluck('beatmapDiscussion.beatmapset')->uniqueStrict('beatmapset_id')->values();
39 });
40 }
41
42 private function getDiscussions()
43 {
44 return $this->memoize(__FUNCTION__, function () {
45 return $this->getPosts()->pluck('beatmapDiscussion')->uniqueStrict('id')->values();
46 });
47 }
48
49 private function getPosts()
50 {
51 return $this->memoize(__FUNCTION__, function () {
52 ['query' => $query, 'params' => $params] = BeatmapDiscussionPost::search($this->params);
53
54 $posts = $query->with(['user.userGroups', 'beatmapDiscussion.beatmapset'])->limit($params['limit'] + 1)->get();
55
56 $this->paginator = new Paginator(
57 $posts,
58 $params['limit'],
59 $params['page'],
60 [
61 'path' => Paginator::resolveCurrentPath(),
62 'query' => $params,
63 ]
64 );
65
66 return $this->paginator->getCollection();
67 });
68 }
69
70 private function getUsers()
71 {
72 return $this->memoize(__FUNCTION__, function () {
73 $users = $this->getPosts()
74 ->pluck('user')
75 ->uniqueStrict('user_id')
76 ->values();
77
78 // see note in BeatmapDiscussionVotesBundle
79 if (!$this->isModerator) {
80 $users = $users->filter(function ($user) {
81 return !$user->isRestricted();
82 });
83 }
84
85 return $users;
86 });
87 }
88}