Skip to content

Commit

Permalink
fix: listen for deletion, reduce ba count
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 29, 2023
1 parent 6538041 commit d001417
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Listeners/RecalculateBestAnswerCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace FoF\BestAnswer\Listeners;

use Flarum\Discussion\Event\Deleting as DiscussionDeleting;
use Flarum\Post\Event\Deleting as PostDeleting;
use Flarum\Post\Post;
use Flarum\User\User;
use FoF\BestAnswer\Events\BestAnswerSet;
use FoF\BestAnswer\Events\BestAnswerUnset;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -21,6 +25,8 @@ public function subscribe(Dispatcher $events)
{
$events->listen(BestAnswerSet::class, [$this, 'addBestAnswerCount']);
$events->listen(BestAnswerUnset::class, [$this, 'subtractBestAnswerCount']);
$events->listen(PostDeleting::class, [$this, 'handlePostDeletion']);
$events->listen(DiscussionDeleting::class, [$this, 'handleDiscussionDeletion']);
}

public function addBestAnswerCount(BestAnswerSet $event): void
Expand All @@ -42,4 +48,38 @@ public function subtractBestAnswerCount(BestAnswerUnset $event): void
$author->save();
}
}

public function handlePostDeletion(PostDeleting $event): void
{
$post = $event->post;

if ($post->discussion->best_answer_post_id === $post->id) {
$post->discussion->best_answer_post_id = null;
$post->discussion->best_answer_user_id = null;
$post->discussion->best_answer_set_at = null;
$post->discussion->save();

$author = $post->user;

if ($author) {
$author->best_answer_count--;
$author->save();
}
}
}

public function handleDiscussionDeletion(DiscussionDeleting $event): void
{
$discussion = $event->discussion;

if ($discussion->best_answer_post_id) {
$post = Post::find($discussion->best_answer_post_id);
$author = $post->user;

if ($author) {
$author->best_answer_count--;
$author->save();
}
}
}
}
40 changes: 40 additions & 0 deletions tests/integration/api/BestAnswerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
Expand Down Expand Up @@ -161,6 +162,37 @@ public function unsetting_best_answer_decreases_author_best_answer_count()
$this->assertEquals(0, $answerAuthor->best_answer_count);
}

/**
* @test
*/
public function deleting_the_best_answer_post_in_a_discussion_reduces_author_best_answer_count()
{
$response = $this->send(
$this->request(
'DELETE',
'/api/posts/6',
[
'authenticatedAs' => '1',
'json' => [],
],
)
);

$this->assertEquals(204, $response->getStatusCode());

$discussion = Discussion::find(3);

$this->assertNull($discussion->best_answer_post_id);

$post = Post::find(6);

$this->assertNull($post);

$answerAuthor = User::find(3);

$this->assertEquals(0, $answerAuthor->best_answer_count);
}

/**
* @test
*/
Expand All @@ -179,6 +211,14 @@ public function deleting_a_discussion_with_a_best_answer_reduces_author_best_ans

$this->assertEquals(204, $response->getStatusCode());

$discussion = Discussion::find(3);

$this->assertNull($discussion);

$post = Post::find(6);

$this->assertNull($post);

$answerAuthor = User::find(3);

$this->assertEquals(0, $answerAuthor->best_answer_count);
Expand Down

0 comments on commit d001417

Please sign in to comment.