Skip to content

Commit

Permalink
Update first post for new discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed Sep 27, 2021
1 parent 02156bf commit 27ff239
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$faker->addProvider(new FlarumInternetProvider($faker));
$faker->addProvider(new FlarumUniqueProvider($faker));

$userIds = [];
$newUserIds = [];
$bulkUserIncrement = 1;

$output->info("Seeding {$config->userCount} users");
Expand All @@ -59,7 +59,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$user->joined_at = $config->nextDate();
$user->save();

$userIds[] = $user->id;
$newUserIds[] = $user->id;
$bulkUserIncrement++;

$output->progressAdvance();
Expand All @@ -72,7 +72,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$userQuery = User::query()->inRandomOrder();

if ($config->userCount > 0) {
$userQuery->whereIn('id', $userIds);
$userQuery->whereIn('id', $newUserIds);
}

if (is_array($config->providedUserIds)) {
Expand All @@ -90,7 +90,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$output->info("Will use $count users for discussion and post seed");
}

$discussionIds = [];
$newDiscussionIds = [];
$discussionIdsToRefresh = [];
$userIdsToRefresh = [];
$tagIdsToRefresh = [];
Expand Down Expand Up @@ -139,7 +139,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
}
}

$discussionIds[] = $discussion->id;
$newDiscussionIds[] = $discussion->id;
$discussionIdsToRefresh[] = $discussion->id;

if (!in_array($author->id, $userIdsToRefresh)) {
Expand All @@ -163,7 +163,7 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$discussionQuery = Discussion::query()->inRandomOrder();

if ($config->discussionCount > 0) {
$discussionQuery->whereIn('id', $discussionIds);
$discussionQuery->whereIn('id', $newDiscussionIds);
}

if (is_array($config->providedDiscussionIds)) {
Expand Down Expand Up @@ -216,7 +216,13 @@ public function seed(SeedConfiguration $config, OutputStyle $output)
$output->info('Updating meta of ' . count($discussionIdsToRefresh) . ' discussions');
$output->progressStart(count($discussionIdsToRefresh));

Discussion::query()->whereIn('id', $discussionIdsToRefresh)->each(function (Discussion $discussion) use ($output) {
Discussion::query()->whereIn('id', $discussionIdsToRefresh)->each(function (Discussion $discussion) use ($newDiscussionIds, $output) {
// Not all discussions need their first post refreshed
// This IF will save some precious time when seeding large number of replies only
if (in_array($discussion->id, $newDiscussionIds)) {
$discussion->setFirstPost($discussion->comments()->oldest()->first());
}

$discussion->refreshLastPost();
$discussion->refreshCommentCount();
$discussion->refreshParticipantCount();
Expand Down

0 comments on commit 27ff239

Please sign in to comment.