Skip to content

Commit

Permalink
feat: compatibility with v17development/flarum-blog
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Jan 28, 2024
1 parent a856851 commit c03455a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"require-dev": {
"flarum/phpstan": "^1.8",
"flarum/tags": "*"
"flarum/tags": "*",
"v17development/flarum-blog": "^0.7.7"
},
"suggest": {
"blomstra/flarum-redis": "This library allows using Redis as cache, session and for the queue. https://github.com/blomstra/flarum-redis#set-up"
Expand Down
9 changes: 9 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ACPL\FlarumCache\Compatibility\Flarum\Likes\FlarumLikesPurgeMiddleware;
use ACPL\FlarumCache\Compatibility\Flarum\Tags\FlarumTagsPurgeMiddleware;
use ACPL\FlarumCache\Compatibility\FriendsOfFlarum\Masquerade\FofMasqueradePurgeMiddleware;
use ACPL\FlarumCache\Compatibility\v17development\FlarumBlog\FlarumBlogPurgeMiddleware;
use ACPL\FlarumCache\Listener\ClearingCacheListener;
use ACPL\FlarumCache\Middleware\LoginMiddleware;
use ACPL\FlarumCache\Middleware\LogoutMiddleware;
Expand Down Expand Up @@ -83,4 +84,12 @@
->whenExtensionEnabled('fof-masquerade', [
(new Extend\Middleware('api'))->add(FofMasqueradePurgeMiddleware::class),
])
->whenExtensionEnabled('v17development-blog', [
// Using insertBefore enables reading headers set by LSCachePurgeMiddleware, while insertAfter does not.
// This suggests Flarum processes middleware in a reverse order 🤔.
(new Extend\Middleware('api'))->insertBefore(
LSCachePurgeMiddleware::class,
FlarumBlogPurgeMiddleware::class
),
])
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace ACPL\FlarumCache\Compatibility\v17development\FlarumBlog;

use ACPL\FlarumCache\Abstract\PurgeMiddleware;
use ACPL\FlarumCache\LSCacheHeadersEnum;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use V17Development\FlarumBlog\BlogMeta\BlogMeta;

class FlarumBlogPurgeMiddleware extends PurgeMiddleware
{
protected function processPurge(
ServerRequestInterface $request,
RequestHandlerInterface $handler,
ResponseInterface $response
): ResponseInterface {
$isDiscussion = $this->isDiscussion;
$isPost = $this->isPost;

if (! ($isDiscussion || $isPost)) {
return $response;
}

if ($this->currentRouteName === 'discussions.create') {
$body = $request->getParsedBody();
if (Arr::has($body, 'data.attributes.blogMeta')) {
return $this->addPurgeParamsToResponse($response, ['tag=blog.overview']);
}
}

$currentPurgeParams = $response->getHeaderLine(LSCacheHeadersEnum::PURGE);
if (empty($currentPurgeParams)) {
return $response;
}

$newPurgeParams = [];
$currentPurgeParams = explode(',', $currentPurgeParams);

// Blog extension is using default Flarum discussion api routes, so we can just reuse previous middleware to get the blog post id
$discussionParam = Arr::first(
$currentPurgeParams,
fn (string $param) => Str::startsWith($param, ['tag=discussion_', 'tag=discussions_'])
);
if (empty($discussionParam)) {
return $response;
}

if (preg_match('/(\d+)/', $discussionParam, $matches)) {
$discussionId = $matches[1];
$newPurgeParams[] = 'tag=blog_'.$discussionId;

// If the previous response wants to purge the index page and this is a blog post, we need to purge the blog overview page as well
if (
in_array('tag=index', $currentPurgeParams)
&& BlogMeta::where('discussion_id', '=', $discussionId)->first()
) {
$newPurgeParams[] = 'tag=blog.overview';
}
}

return $this->addPurgeParamsToResponse($response, $newPurgeParams);
}
}

0 comments on commit c03455a

Please sign in to comment.