Skip to content

Commit

Permalink
fix: use user Deleting event instead of Deleted to access user posts
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Sep 14, 2024
1 parent 7c47230 commit e095732
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/Listener/UserEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@

namespace ACPL\FlarumLSCache\Listener;

use Flarum\User\Event\{AvatarChanged, Deleted, GroupsChanged, Renamed};
use Flarum\User\Event\{AvatarChanged, Deleting, GroupsChanged, Renamed};
use Illuminate\Contracts\Events\Dispatcher;

class UserEventSubscriber extends AbstractCachePurgeSubscriber
{
public function subscribe(Dispatcher $events): void
{
$shared = [AvatarChanged::class, Deleted::class, GroupsChanged::class, Renamed::class];
$shared = [AvatarChanged::class, Deleting::class, GroupsChanged::class, Renamed::class];
foreach ($shared as $event) {
$this->addPurgeListener($events, $event, [$this, 'handleUserWithPosts']);
}
}

/** Purge discussions where user has posted. */
public function handleUserWithPosts(AvatarChanged|GroupsChanged|Renamed $event): void
public function handleUserWithPosts(AvatarChanged|Deleting|GroupsChanged|Renamed $event): void
{
$this->purger->addPurgeTags([
"user_{$event->user->id}",
"user_{$event->user->username}",
'posts',
'discussions',
...array_map(fn ($id) => "discussion_$id", $discussions),
// TODO: If user has a lot of discussions chunk it and push to the queue job
...array_map(
fn ($id) => "discussion_$id",
$event->user->posts()->pluck('discussion_id')->toArray(),
),
]);

$discussionCount = $event->user->posts()->getRelation('discussion')->distinct()->count();
if ($discussionCount < 50) {
/** @phpstan-ignore-next-line Call to an undefined method Illuminate\Database\Eloquent\Relations\Relation::pluck(). */
$discussions = $event->user->posts()->getRelation('discussion')->pluck('id')->toArray();
$this->purger->addPurgeTags([

]);
}
}
}

0 comments on commit e095732

Please sign in to comment.