<commit_before><?php

namespace WiwatSrt\BestAnswer\Listener;

use Flarum\Event\PostWillBeSaved;
use Illuminate\Contracts\Events\Dispatcher;

class SelectBestAnswers
{
    /**
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events)
    {
        $events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']);
    }

    /**
     * @param PostWillBeSaved $event
     */
    public function whenPostWillBeSaved(PostWillBeSaved $event)
    {
        $post = $event->post;
        $data = $event->data;

        if ($post->exists && isset($data['attributes']['isBestAnswer']) && $post->number !== 1) {
            $isBestAnswer = (bool) $data['attributes']['isBestAnswer'];

            if ($isBestAnswer) {
                // Remove other best answer in same discussion
                $post->where([
                    'discussion_id' => $post->discussion->id,
                    'is_best_answer' => 1
                ])->update(['is_best_answer' => 0]);

                $post->discussion->has_best_answer = true;
                $post->discussion->save();
            } else {
                $post->discussion->has_best_answer = false;
                $post->discussion->save();
            }

            $post->is_best_answer = $isBestAnswer;
            $post->save();
        }
    }
}<commit_msg>update has_best_answer when deleted post<commit_after><?php

namespace WiwatSrt\BestAnswer\Listener;

use Flarum\Event\PostWasDeleted;
use Flarum\Event\PostWillBeSaved;
use Illuminate\Contracts\Events\Dispatcher;

class SelectBestAnswers
{
    /**
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events)
    {
        $events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']);
        $events->listen(PostWasDeleted::class, [$this, 'whenPostWasDeleted']);
    }

    /**
     * @param PostWillBeSaved $event
     */
    public function whenPostWillBeSaved(PostWillBeSaved $event)
    {
        $post = $event->post;
        $data = $event->data;

        if ($post->exists && isset($data['attributes']['isBestAnswer']) && $post->number !== 1) {
            $isBestAnswer = (bool) $data['attributes']['isBestAnswer'];

            if ($isBestAnswer) {
                // Remove other best answer in same discussion
                $post->where([
                    'discussion_id' => $post->discussion->id,
                    'is_best_answer' => 1
                ])->update(['is_best_answer' => 0]);

                $post->discussion->has_best_answer = true;
                $post->discussion->save();
            } else {
                $post->discussion->has_best_answer = false;
                $post->discussion->save();
            }

            $post->is_best_answer = $isBestAnswer;
            $post->save();
        }
    }

    /**
     * @param PostWasDeleted $event
     */
    public function whenPostWasDeleted(PostWasDeleted $event)
    {
        $post = $event->post;
        $data = $event->data;

        if ($post->exists && isset($data['attributes']['isBestAnswer'])) {
            $isBestAnswer = (bool) $data['attributes']['isBestAnswer'];

            if ($isBestAnswer) {
                $post->discussion->has_best_answer = false;
                $post->discussion->save();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.14611461146114613, 'fraction_numerical': 0.0018001800180018, 'mean_word_length': 2.0447488584474884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2472394', 'n_tokens_mistral': 1007, 'n_tokens_neox': 935, 'n_words': 159}