Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post numbers #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
vendor
composer.lock
js/dist
/.idea
42 changes: 28 additions & 14 deletions src/Api/Commands/MergeDiscussionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Discussion\DiscussionRepository;
use Flarum\Foundation\ValidationException;
use Flarum\Post\Post;
use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\UserRepository;
use FoF\MergeDiscussions\Events\DiscussionWasMerged;
use FoF\MergeDiscussions\Models\Redirection;
Expand Down Expand Up @@ -58,14 +59,19 @@ public function __construct(
$this->validator = $validator;
}

public function handle(MergeDiscussion $command)
/**
* @throws ValidationException
* @throws PermissionDeniedException
* @throws \Illuminate\Validation\ValidationException
*/
public function handle(MergeDiscussion $command): Discussion
{
$discussion = $this->discussions->findOrFail($command->discussionId);

$command->actor->assertCan('merge', $discussion);

if ($command->merge && $command->ordering === 'date') {
$this->fixPostsNumber($discussion);
$this->fixPostsNumberForDateSort($discussion);
}

/** @var Collection $discussions */
Expand Down Expand Up @@ -142,7 +148,10 @@ public function handle(MergeDiscussion $command)
return $discussion;
}

private function catchError(Throwable $e, string $type)
/**
* @throws ValidationException
*/
private function catchError(Throwable $e, string $type): void
{
$msg = resolve('translator')->trans("fof-merge-discussions.api.error.{$type}_failed");

Expand All @@ -154,23 +163,28 @@ private function catchError(Throwable $e, string $type)
]);
}

private function fixPostsNumber(Discussion $discussion): void
/**
* @throws ValidationException
*/
private function fixPostsNumberForDateSort(Discussion $discussion): void
{
$posts = $discussion->posts;
if ($posts->count() === $discussion->posts()->max('number')) {
$postMaxNumber = $discussion->posts()->max('number');
if ($discussion->posts->count() === $postMaxNumber) {
return;
}

$number = 0;
$this->setRelationsAndMergeByDate($discussion, new SupportCollection([]), $postMaxNumber);
$this->saveDiscussionPosts($discussion);

$posts->sortBy('created_at')->each(function ($post, $i) use ($discussion, &$number) {
$number++;
$post->number = $number;
$discussion->posts[$i] = $post;
});

$discussion->setRelation('posts', $discussion->posts->sortBy('number'));
$this->setRelationsAndMergeByDate($discussion, new SupportCollection([]));
$this->saveDiscussionPosts($discussion);
}

/**
* @throws ValidationException
*/
private function saveDiscussionPosts(Discussion $discussion): void
{
resolve('db.connection')->transaction(function () use ($discussion) {
try {
$discussion->push();
Expand Down