diff --git a/framework/core/src/Core/Handlers/Events/DiscussionMetadataUpdater.php b/framework/core/src/Core/Handlers/Events/DiscussionMetadataUpdater.php index d8a83d739..b7b0c7312 100755 --- a/framework/core/src/Core/Handlers/Events/DiscussionMetadataUpdater.php +++ b/framework/core/src/Core/Handlers/Events/DiscussionMetadataUpdater.php @@ -54,12 +54,14 @@ class DiscussionMetadataUpdater { $discussion = $post->discussion; - $discussion->refreshCommentsCount(); + if ($discussion->exists) { + $discussion->refreshCommentsCount(); - if ($discussion->last_post_id == $post->id) { - $discussion->refreshLastPost(); + if ($discussion->last_post_id == $post->id) { + $discussion->refreshLastPost(); + } + + $discussion->save(); } - - $discussion->save(); } } diff --git a/framework/core/src/Core/Handlers/Events/UserActivitySyncer.php b/framework/core/src/Core/Handlers/Events/UserActivitySyncer.php index 369222b90..b2e3d4d42 100755 --- a/framework/core/src/Core/Handlers/Events/UserActivitySyncer.php +++ b/framework/core/src/Core/Handlers/Events/UserActivitySyncer.php @@ -71,6 +71,6 @@ class UserActivitySyncer protected function postedActivity(Post $post) { - return $post->number === 1 ? new StartedDiscussionActivity($post) : new PostedActivity($post); + return $post->number == 1 ? new StartedDiscussionActivity($post) : new PostedActivity($post); } } diff --git a/framework/core/src/Core/Models/Discussion.php b/framework/core/src/Core/Models/Discussion.php index 7718644ec..3728d0d6b 100755 --- a/framework/core/src/Core/Models/Discussion.php +++ b/framework/core/src/Core/Models/Discussion.php @@ -6,6 +6,7 @@ use Flarum\Core\Support\VisibleScope; use Flarum\Core\Events\DiscussionWasDeleted; use Flarum\Core\Events\DiscussionWasStarted; use Flarum\Core\Events\DiscussionWasRenamed; +use Flarum\Core\Events\PostWasDeleted; use Flarum\Core\Models\User; class Discussion extends Model @@ -79,7 +80,16 @@ class Discussion extends Model static::deleted(function ($discussion) { $discussion->raise(new DiscussionWasDeleted($discussion)); - $discussion->posts()->allTypes()->delete(); + $posts = $discussion->posts()->allTypes(); + + foreach ($posts->get() as $post) { + $post->setRelation('discussion', $discussion); + + $discussion->raise(new PostWasDeleted($post)); + } + + $posts->delete(); + $discussion->readers()->detach(); }); }