From 714a74518ed449391b92c47c8d5e50c25604c656 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Thu, 13 Jan 2022 16:41:37 -0500 Subject: [PATCH] Some fixes to tag metadata calculations (#154) * If a discussion doesn't exist, it can't be the last posted discussion This fixes some rare errors where deleting the only post in the last posted discussion of a tag triggers a DB integrity exception. * Decrement tag's discussion count when deleting a post deletes an unhidden discussion. Co-authored-by: Sami Mazouz --- extensions/tags/src/Listener/UpdateTagMetadata.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extensions/tags/src/Listener/UpdateTagMetadata.php b/extensions/tags/src/Listener/UpdateTagMetadata.php index 23066d2cb..87366b86f 100755 --- a/extensions/tags/src/Listener/UpdateTagMetadata.php +++ b/extensions/tags/src/Listener/UpdateTagMetadata.php @@ -100,7 +100,9 @@ class UpdateTagMetadata */ public function whenPostIsDeleted(PostDeleted $event) { - $this->updateTags($event->post->discussion); + $discussion = $event->post->discussion; + $delta = ! $discussion->exists && $discussion->hidden_at === null ? -1 : 0; + $this->updateTags($discussion, $delta); } /** @@ -145,7 +147,7 @@ class UpdateTagMetadata // If this is a new / restored discussion, it isn't private, it isn't null, // and it's more recent than what we have now, set it as last posted discussion. - if ($delta >= 0 && ! $discussion->is_private && $discussion->hidden_at == null && ($discussion->last_posted_at >= $tag->last_posted_at)) { + if ($delta >= 0 && ! $discussion->is_private && $discussion->hidden_at == null && ($discussion->last_posted_at >= $tag->last_posted_at) && $discussion->exists) { $tag->setLastPostedDiscussion($discussion); } elseif ($discussion->id == $tag->last_posted_discussion_id) { // This is to persist refreshLastPost above. It is here instead of there so that