-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Purge updated discussion tags only
- Loading branch information
Showing
7 changed files
with
269 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace ACPL\FlarumCache\Abstract; | ||
|
||
use ACPL\FlarumCache\LSCacheHeadersEnum; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Support\Arr; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
abstract class PurgeMiddleware implements MiddlewareInterface | ||
{ | ||
protected SettingsRepositoryInterface $settings; | ||
|
||
protected string $currentRouteName; | ||
protected bool $isDiscussion; | ||
protected bool $isPost; | ||
|
||
public function __construct(SettingsRepositoryInterface $settings) | ||
{ | ||
$this->settings = $settings; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = $handler->handle($request); | ||
|
||
if ( | ||
! in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE']) | ||
|| $response->getStatusCode() >= 400 | ||
) { | ||
return $response; | ||
} | ||
|
||
$this->currentRouteName = $request->getAttribute('routeName'); | ||
$this->isDiscussion = str_starts_with($this->currentRouteName, 'discussions'); | ||
$this->isPost = str_starts_with($this->currentRouteName, 'posts'); | ||
|
||
// If this is just an update of the last read post, there is no point in clearing the public cache | ||
if ($this->isDiscussion && Arr::get( | ||
$request->getParsedBody(), | ||
'data.attributes.lastReadPostNumber') | ||
) { | ||
return $response; | ||
} | ||
|
||
|
||
return $this->processPurge($request, $handler, $response); | ||
} | ||
|
||
abstract protected function processPurge( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
ResponseInterface $response | ||
): ResponseInterface; | ||
|
||
|
||
protected function addPurgeParamsToResponse(ResponseInterface $response, array $newPurgeParams): ResponseInterface | ||
{ | ||
if ($response->hasHeader(LSCacheHeadersEnum::PURGE)) { | ||
$existingPurgeParams = explode(',', $response->getHeaderLine(LSCacheHeadersEnum::PURGE)); | ||
$newPurgeParams = array_unique(array_merge($existingPurgeParams, $newPurgeParams)); | ||
} | ||
|
||
if (count($newPurgeParams) < 1) { | ||
return $response; | ||
} | ||
|
||
if ($this->settings->get('acpl-lscache.serve_stale') && ! array_key_exists('stale', $newPurgeParams)) { | ||
array_unshift($newPurgeParams, 'stale'); | ||
} | ||
|
||
return $response->withHeader(LSCacheHeadersEnum::PURGE, implode(',', $newPurgeParams)); | ||
} | ||
|
||
protected function getRouteParams($request): array | ||
{ | ||
return $request->getAttribute('routeParameters'); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/Compatibility/FlarumTags/FlarumTagsPurgeMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace ACPL\FlarumCache\Compatibility\FlarumTags; | ||
|
||
use ACPL\FlarumCache\Abstract\PurgeMiddleware; | ||
use Flarum\Discussion\Discussion; | ||
use Flarum\Http\UrlGenerator; | ||
use Flarum\Post\Post; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Flarum\Tags\Tag; | ||
use Illuminate\Support\Arr; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class FlarumTagsPurgeMiddleware extends PurgeMiddleware | ||
{ | ||
protected UrlGenerator $url; | ||
|
||
public function __construct(SettingsRepositoryInterface $settings, UrlGenerator $url) | ||
{ | ||
$this->url = $url; | ||
parent::__construct($settings); | ||
} | ||
|
||
protected function processPurge( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
ResponseInterface $response | ||
): ResponseInterface { | ||
$isDiscussion = $this->isDiscussion; | ||
$isPost = $this->isPost; | ||
|
||
if (! $isDiscussion && ! $isPost) { | ||
return $response; | ||
} | ||
|
||
$body = $request->getParsedBody(); | ||
$routeName = $this->currentRouteName; | ||
|
||
// When a post is edited, there is no need to purge tags cache unless the post is being hidden | ||
if ($routeName === 'posts.update' && ! Arr::has($body, 'data.attributes.isHidden')) { | ||
return $response; | ||
} | ||
|
||
$payload = $response->getBody()->getContents(); | ||
$payload = json_decode($payload, true); | ||
|
||
if ($isDiscussion) { | ||
$discussionId = Arr::get($payload, 'data.id'); | ||
} else { | ||
$discussionId = Arr::get($payload, 'data.relationships.discussion.data.id'); | ||
if (! $discussionId) { | ||
$postId = Arr::get($payload, 'data.id'); | ||
$discussionId = Post::find($postId)->discussion_id; | ||
} | ||
} | ||
|
||
$discussion = Discussion::find($discussionId); | ||
if (! $discussion) { | ||
return $response; | ||
} | ||
|
||
/** | ||
* @var Tag[] $tags | ||
* @phpstan-ignore-next-line | ||
*/ | ||
$tags = $discussion->tags; | ||
|
||
if (! $tags) { | ||
return $response; | ||
} | ||
|
||
$purgeParams = ['tags.index', 'tags']; | ||
|
||
foreach ($tags as $tag) { | ||
$purgeParams[] = "tag_$tag->slug"; | ||
} | ||
|
||
return $this->addPurgeParamsToResponse($response, $purgeParams); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace ACPL\FlarumCache\Compatibility\FofMasquerade; | ||
|
||
use ACPL\FlarumCache\Abstract\PurgeMiddleware; | ||
use Flarum\Http\RequestUtil; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
|
||
class Middleware extends PurgeMiddleware | ||
{ | ||
protected function processPurge( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
ResponseInterface $response | ||
): ResponseInterface { | ||
// Purge user profile cache when updating FriendsOfFlarum/masquerade fields | ||
if ($this->currentRouteName === 'masquerade.api.configure.save') { | ||
$user = RequestUtil::getActor($request); | ||
return $this->addPurgeParamsToResponse( | ||
$response, | ||
[ | ||
"tag=user_$user->id", | ||
"tag=users_$user->id", | ||
"tag=user_$user->username", | ||
"tag=users_$user->username", | ||
"tag=masquerade_$user->id", | ||
] | ||
); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.