|
5 | 5 | use App\DTO\User\AuthorExpGrade; |
6 | 6 | use App\DTO\User\ForumModerator; |
7 | 7 | use App\DTO\User\User; |
| 8 | +use App\Entity\LatestReplier; |
8 | 9 | use App\Entity\Post\Post; |
9 | 10 | use App\Entity\Post\Thread; |
10 | 11 | use App\PostsQuery\ParamsValidator; |
@@ -65,26 +66,41 @@ public function query(Request $request): array |
65 | 66 | $latestRepliers = $this->latestReplierRepository->getLatestRepliersWithoutNameWhenHasUid( |
66 | 67 | $this->query->postsTree->threads->map(fn(Thread $thread) => $thread->getLatestReplierId()), |
67 | 68 | ); |
68 | | - $uids = collect([$this->query->postsTree->threads, $this->query->postsTree->replies, $this->query->postsTree->subReplies]) |
69 | | - ->flatMap(static fn(Collection $posts) => |
70 | | - $posts->map(fn(Post $post) => $post->getAuthorUid())) |
71 | | - ->concat($latestRepliers->pluck('uid') |
72 | | - ->filter(static fn(?int $uid) => $uid !== null)) |
| 69 | + $posts = collect([ |
| 70 | + $this->query->postsTree->threads, |
| 71 | + $this->query->postsTree->replies, |
| 72 | + $this->query->postsTree->subReplies |
| 73 | + ])->flatten(); |
| 74 | + $latestRepliersUidKeyById = $latestRepliers |
| 75 | + ->mapWithKeys(fn(array|LatestReplier $latestReplier) => [ |
| 76 | + is_array($latestReplier) ? $latestReplier['id'] : $latestReplier->getId() => |
| 77 | + is_array($latestReplier) ? $latestReplier['uid'] : $latestReplier->getUid() |
| 78 | + ]) |
| 79 | + ->filter(static fn(?int $uid) => $uid !== null); |
| 80 | + $uids = $posts |
| 81 | + ->map(fn(Post $post) => $post->getAuthorUid()) |
| 82 | + ->concat($latestRepliersUidKeyById) |
73 | 83 | ->unique(); |
74 | 84 | $users = collect($this->userRepository->getUsers($uids)) |
75 | | - ->map(fn(\App\Entity\User $entity) => User::fromEntity($entity)); |
| 85 | + ->mapWithKeys(fn(\App\Entity\User $entity) => [$entity->getUid() => User::fromEntity($entity)]); |
76 | 86 | $this->stopwatch->stop('queryUsers'); |
77 | 87 |
|
78 | 88 | $this->stopwatch->start('queryUserRelated'); |
79 | | - $fid = $this->query->queryResult->fid; |
80 | | - $authorExpGrades = collect($this->authorExpGradeRepository->getLatestOfUsers($fid, $uids)) |
| 89 | + $authorsUidKeyByFid = $posts |
| 90 | + ->map(fn(Post $post) => ['fid' => $post->getFid(), 'authorUid' => $post->getAuthorUid()]) |
| 91 | + ->groupBy(fn(array $fidAndAuthorId) => $fidAndAuthorId['fid']) |
| 92 | + ->map(fn(Collection $fidAndAuthorsUid) => $fidAndAuthorsUid->pluck('authorUid')); |
| 93 | + $authorExpGrades = collect($this->authorExpGradeRepository->getLatestOfUsers($authorsUidKeyByFid)) |
81 | 94 | ->keyBy(fn(AuthorExpGrade $authorExpGrade) => $authorExpGrade->uid); |
82 | | - $users->each(fn(User $user) => $user->setCurrentAuthorExpGrade($authorExpGrades[$user->getUid()] ?? null)); |
83 | | - |
84 | | - $forumModerators = collect($this->forumModeratorRepository |
85 | | - ->getLatestOfUsers($fid, $users->map(fn(User $user) => $user->getPortrait()))) |
86 | | - ->keyBy(fn(ForumModerator $forumModerator) => $forumModerator->portrait); |
87 | | - $users->each(fn(User $user) => $user->setCurrentForumModerator($forumModerators->get($user->getPortrait()))); |
| 95 | + $forumModerators = collect($this->forumModeratorRepository->getLatestOfUsers($authorsUidKeyByFid |
| 96 | + ->map(fn(Collection $authorsUid) => $authorsUid |
| 97 | + ->map(fn(int $authorUid) => $users->get($authorUid)?->getPortrait()) |
| 98 | + ->filter(fn(?string $portrait) => $portrait !== null)) |
| 99 | + ))->keyBy(fn(ForumModerator $forumModerator) => $forumModerator->portrait); |
| 100 | + $users = $users->each(fn(User $user) => $user->setForumSpecific([ |
| 101 | + 'authorExpGrades' => $authorExpGrades->get($user->getUid()), |
| 102 | + 'forumModerators' => $forumModerators->get($user->getPortrait()) |
| 103 | + ])); |
88 | 104 | $this->stopwatch->stop('queryUserRelated'); |
89 | 105 |
|
90 | 106 | return [ |
|
0 commit comments