Skip to content

Commit ff4978c

Browse files
authored
Performance Improvements for the new combined front page (#1697)
1 parent 616fc42 commit ff4978c

18 files changed

+579
-20
lines changed

src/Controller/Api/Content/ContentRetrieveApi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\DTO\ContentResponseDto;
1010
use App\Entity\Entry;
1111
use App\Entity\Post;
12+
use App\Entity\User;
1213
use App\PageView\ContentPageView;
1314
use App\Repository\ContentRepository;
1415
use App\Repository\Criteria;
@@ -254,6 +255,10 @@ private function generateResponse(
254255
$this->handleLanguageCriteria($criteria);
255256
$criteria->content = Criteria::CONTENT_THREADS;
256257
$criteria->perPage = $perPage;
258+
$user = $security->getUser();
259+
if ($user instanceof User) {
260+
$criteria->fetchCachedItems($contentRepository, $user);
261+
}
257262

258263
switch ($collectionType) {
259264
case 'sub':

src/Controller/Api/Entry/DomainEntriesRetrieveApi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\DTO\EntryResponseDto;
99
use App\Entity\Domain;
1010
use App\Entity\Entry;
11+
use App\Entity\User;
1112
use App\Factory\EntryFactory;
1213
use App\PageView\EntryPageView;
1314
use App\Repository\ContentRepository;
@@ -145,6 +146,10 @@ public function __invoke(
145146
$criteria->sortOption = $sort ?? Criteria::SORT_HOT;
146147
$criteria->time = $criteria->resolveTime($time ?? Criteria::TIME_ALL);
147148
$this->handleLanguageCriteria($criteria);
149+
$user = $security->getUser();
150+
if ($user instanceof User) {
151+
$criteria->fetchCachedItems($repository, $user);
152+
}
148153

149154
$criteria->perPage = self::constrainPerPage($perPage ?? ContentRepository::PER_PAGE);
150155

src/Controller/Api/Entry/EntriesRetrieveApi.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Controller\Traits\PrivateContentTrait;
88
use App\DTO\EntryResponseDto;
99
use App\Entity\Entry;
10+
use App\Entity\User;
1011
use App\Event\Entry\EntryHasBeenSeenEvent;
1112
use App\Factory\EntryFactory;
1213
use App\PageView\EntryPageView;
@@ -201,6 +202,11 @@ public function collection(
201202
$this->handleLanguageCriteria($criteria);
202203
$criteria->content = Criteria::CONTENT_THREADS;
203204

205+
$user = $security->getUser();
206+
if ($user instanceof User) {
207+
$criteria->fetchCachedItems($repository, $user);
208+
}
209+
204210
$entries = $repository->findByCriteria($criteria);
205211

206212
$dtos = [];
@@ -317,6 +323,11 @@ public function subscribed(
317323
$criteria->subscribed = true;
318324
$criteria->content = Criteria::CONTENT_THREADS;
319325

326+
$user = $security->getUser();
327+
if ($user instanceof User) {
328+
$criteria->fetchCachedItems($repository, $user);
329+
}
330+
320331
$entries = $repository->findByCriteria($criteria);
321332

322333
$dtos = [];
@@ -433,6 +444,11 @@ public function moderated(
433444
$criteria->moderated = true;
434445
$criteria->content = Criteria::CONTENT_THREADS;
435446

447+
$user = $security->getUser();
448+
if ($user instanceof User) {
449+
$criteria->fetchCachedItems($repository, $user);
450+
}
451+
436452
$entries = $repository->findByCriteria($criteria);
437453

438454
$dtos = [];
@@ -548,6 +564,11 @@ public function favourited(
548564

549565
$criteria->favourite = true;
550566

567+
$user = $security->getUser();
568+
if ($user instanceof User) {
569+
$criteria->fetchCachedItems($repository, $user);
570+
}
571+
551572
$entries = $repository->findByCriteria($criteria);
552573
$criteria->content = Criteria::CONTENT_THREADS;
553574

src/Controller/Api/Entry/MagazineEntriesRetrieveApi.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\DTO\EntryResponseDto;
99
use App\Entity\Entry;
1010
use App\Entity\Magazine;
11+
use App\Entity\User;
1112
use App\Factory\EntryFactory;
1213
use App\PageView\EntryPageView;
1314
use App\Repository\ContentRepository;
@@ -152,6 +153,11 @@ public function __invoke(
152153

153154
$criteria->magazine = $magazine;
154155

156+
$user = $security->getUser();
157+
if ($user instanceof User) {
158+
$criteria->fetchCachedItems($repository, $user);
159+
}
160+
155161
$entries = $repository->findByCriteria($criteria);
156162

157163
$dtos = [];

src/Controller/Api/Post/PostsRetrieveApi.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\DTO\PostResponseDto;
99
use App\Entity\Magazine;
1010
use App\Entity\Post;
11+
use App\Entity\User;
1112
use App\Event\Post\PostHasBeenSeenEvent;
1213
use App\Factory\PostFactory;
1314
use App\PageView\PostPageView;
@@ -329,6 +330,11 @@ public function subscribed(
329330

330331
$this->handleLanguageCriteria($criteria);
331332

333+
$user = $security->getUser();
334+
if ($user instanceof User) {
335+
$criteria->fetchCachedItems($repository, $user);
336+
}
337+
332338
$posts = $repository->findByCriteria($criteria);
333339

334340
$dtos = [];
@@ -447,6 +453,11 @@ public function moderated(
447453
$criteria->moderated = true;
448454
$criteria->setContent(Criteria::CONTENT_MICROBLOG);
449455

456+
$user = $security->getUser();
457+
if ($user instanceof User) {
458+
$criteria->fetchCachedItems($repository, $user);
459+
}
460+
450461
$posts = $repository->findByCriteria($criteria);
451462

452463
$dtos = [];
@@ -562,6 +573,11 @@ public function favourited(
562573

563574
$this->logger->debug(var_export($criteria, true));
564575

576+
$user = $security->getUser();
577+
if ($user instanceof User) {
578+
$criteria->fetchCachedItems($repository, $user);
579+
}
580+
565581
$posts = $repository->findByCriteria($criteria);
566582

567583
$dtos = [];
@@ -706,6 +722,11 @@ public function byMagazine(
706722
$criteria->magazine = $magazine;
707723
$criteria->setContent(Criteria::CONTENT_MICROBLOG);
708724

725+
$user = $security->getUser();
726+
if ($user instanceof User) {
727+
$criteria->fetchCachedItems($repository, $user);
728+
}
729+
709730
$posts = $repository->findByCriteria($criteria);
710731

711732
$dtos = [];

src/Controller/Domain/DomainFrontController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controller\Domain;
66

77
use App\Controller\AbstractController;
8+
use App\Entity\User;
89
use App\PageView\EntryPageView;
910
use App\Repository\ContentRepository;
1011
use App\Repository\DomainRepository;
@@ -42,6 +43,12 @@ public function __invoke(
4243
->setDomain($name);
4344
$resolvedSort = $criteria->resolveSort($sortBy);
4445
$criteria->sortOption = $resolvedSort;
46+
47+
$user = $security->getUser();
48+
if ($user instanceof User) {
49+
$criteria->fetchCachedItems($this->contentRepository, $user);
50+
}
51+
4552
$listing = $this->contentRepository->findByCriteria($criteria);
4653

4754
if ($request->isXmlHttpRequest()) {

src/Controller/Entry/EntryFrontController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function front(
5454

5555
$this->setUserPreferences($user, $criteria);
5656

57+
if (null !== $user) {
58+
$criteria->fetchCachedItems($this->contentRepository, $user);
59+
}
60+
5761
$entities = $this->contentRepository->findByCriteria($criteria);
5862
$templatePath = 'content/';
5963
$dataKey = 'results';
@@ -119,6 +123,9 @@ public function magazine(
119123
$this->handleSubscription($subscription, $criteria);
120124

121125
$this->setUserPreferences($user, $criteria);
126+
if (null !== $user) {
127+
$criteria->fetchCachedItems($this->contentRepository, $user);
128+
}
122129

123130
return $this->renderResponse(
124131
$request,

src/EventSubscriber/ActivityPub/MagazineFollowSubscriber.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
use App\Event\Magazine\MagazineSubscribedEvent;
88
use App\Message\ActivityPub\Outbox\FollowMessage;
9+
use App\Repository\ContentRepository;
910
use JetBrains\PhpStorm\ArrayShape;
1011
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1112
use Symfony\Component\Messenger\MessageBusInterface;
1213

1314
class MagazineFollowSubscriber implements EventSubscriberInterface
1415
{
15-
public function __construct(private readonly MessageBusInterface $bus)
16-
{
16+
public function __construct(
17+
private readonly MessageBusInterface $bus,
18+
private readonly ContentRepository $contentRepository,
19+
) {
1720
}
1821

1922
#[ArrayShape([MagazineSubscribedEvent::class => 'string'])]
@@ -26,6 +29,8 @@ public static function getSubscribedEvents(): array
2629

2730
public function onMagazineFollow(MagazineSubscribedEvent $event): void
2831
{
32+
$this->contentRepository->clearCachedUserSubscribedMagazines($event->user);
33+
2934
if ($event->magazine->apId && !$event->user->apId) {
3035
$this->bus->dispatch(
3136
new FollowMessage($event->user->getId(), $event->magazine->getId(), $event->unfollow, true)

src/EventSubscriber/ActivityPub/MagazineModeratorAddedRemovedSubscriber.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Event\Magazine\MagazineModeratorRemovedEvent;
1212
use App\Message\ActivityPub\Outbox\AddMessage;
1313
use App\Message\ActivityPub\Outbox\RemoveMessage;
14+
use App\Repository\ContentRepository;
1415
use Doctrine\ORM\EntityManagerInterface;
1516
use Psr\Cache\InvalidArgumentException;
1617
use Psr\Log\LoggerInterface;
@@ -25,11 +26,14 @@ public function __construct(
2526
private readonly CacheInterface $cache,
2627
private readonly LoggerInterface $logger,
2728
private readonly EntityManagerInterface $entityManager,
29+
private readonly ContentRepository $contentRepository,
2830
) {
2931
}
3032

3133
public function onModeratorAdded(MagazineModeratorAddedEvent $event): void
3234
{
35+
$this->contentRepository->clearCachedUserModeratedMagazines($event->user);
36+
3337
// if the magazine is local then we have authority over it, otherwise the addedBy user has to be a local user
3438
if (!$event->magazine->apId or (null !== $event->addedBy and !$event->addedBy->apId)) {
3539
$this->bus->dispatch(new AddMessage($event->addedBy->getId(), $event->magazine->getId(), $event->user->getId()));
@@ -42,6 +46,8 @@ public function onModeratorAdded(MagazineModeratorAddedEvent $event): void
4246

4347
public function onModeratorRemoved(MagazineModeratorRemovedEvent $event): void
4448
{
49+
$this->contentRepository->clearCachedUserModeratedMagazines($event->user);
50+
4551
// if the magazine is local then we have authority over it, otherwise the removedBy user has to be a local user
4652
if (!$event->magazine->apId or (null !== $event->removedBy and !$event->removedBy->apId)) {
4753
$this->bus->dispatch(new RemoveMessage($event->removedBy->getId(), $event->magazine->getId(), $event->user->getId()));

src/EventSubscriber/ActivityPub/UserFollowSubscriber.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Event\User\UserFollowEvent;
88
use App\Message\ActivityPub\Outbox\FollowMessage;
9+
use App\Repository\ContentRepository;
910
use JetBrains\PhpStorm\ArrayShape;
1011
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1112
use Symfony\Component\Messenger\MessageBusInterface;
@@ -16,6 +17,7 @@ class UserFollowSubscriber implements EventSubscriberInterface
1617
public function __construct(
1718
private readonly MessageBusInterface $bus,
1819
private readonly CacheInterface $cache,
20+
private readonly ContentRepository $contentRepository,
1921
) {
2022
}
2123

@@ -29,6 +31,8 @@ public static function getSubscribedEvents(): array
2931

3032
public function onUserFollow(UserFollowEvent $event): void
3133
{
34+
$this->contentRepository->clearCachedUserFollows($event->follower);
35+
3236
if (!$event->follower->apId && $event->following->apId) {
3337
$this->bus->dispatch(
3438
new FollowMessage($event->follower->getId(), $event->following->getId(), $event->unfollow)

0 commit comments

Comments
 (0)