<commit_before><?php

namespace Coyote\Listeners;

use Coyote\Events\PostWasSaved;
use Coyote\Notifications\Post\ChangedNotification;
use Coyote\Notifications\Post\SubmittedNotification;
use Coyote\Notifications\Post\UserMentionedNotification;
use Coyote\Post;
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
use Coyote\Services\Parser\Helpers\Login as LoginHelper;
use Coyote\User;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;

class DispatchPostNotifications implements ShouldQueue
{
    /**
     * @var Dispatcher
     */
    private $dispatcher;

    /**
     * @var UserRepository
     */
    private $user;

    /**
     * @param Dispatcher $dispatcher
     * @param UserRepository $user
     */
    public function __construct(Dispatcher $dispatcher, UserRepository $user)
    {
        $this->dispatcher = $dispatcher;
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  PostWasSaved  $event
     * @return void
     */
    public function handle(PostWasSaved $event)
    {
        $post = $event->post;
        $topic = $event->post->topic;

        if ($event->wasRecentlyCreated) {
            $user = $event->post->user;
            $subscribers = $topic->subscribers()->with('user.notificationSettings')->get()->pluck('user')->exceptUser($user);
            $notification = (new SubmittedNotification($user, $post))->setSender($this->getSender($post));

            $this->dispatcher->send($subscribers, $notification);

            $this->sendUserMentionedNotification($post, $user, $subscribers, $this->getSender($post));
        } else {
            $user = $event->post->editor;
            $subscribers = $post->subscribers()->with('user')->get()->pluck('user')->exceptUser($user);

            $this->dispatcher->send($subscribers, new ChangedNotification($user, $post));

            $this->sendUserMentionedNotification($post, $user, $subscribers, $user->name);
        }
    }

    /**
     * @param Post $post
     * @param User $user
     * @param array $subscribers
     * @param string $senderName
     */
    private function sendUserMentionedNotification(Post $post, User $user, $subscribers, string $senderName): void
    {
        // get id of users that were mentioned in the text<commit_msg>sprawdzanie czy post faktycznie zostal zmodyfikowany (istnieje relacja editor)<commit_after><?php

namespace Coyote\Listeners;

use Coyote\Events\PostWasSaved;
use Coyote\Notifications\Post\ChangedNotification;
use Coyote\Notifications\Post\SubmittedNotification;
use Coyote\Notifications\Post\UserMentionedNotification;
use Coyote\Post;
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
use Coyote\Services\Parser\Helpers\Login as LoginHelper;
use Coyote\User;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;

class DispatchPostNotifications implements ShouldQueue
{
    /**
     * @var Dispatcher
     */
    private $dispatcher;

    /**
     * @var UserRepository
     */
    private $user;

    /**
     * @param Dispatcher $dispatcher
     * @param UserRepository $user
     */
    public function __construct(Dispatcher $dispatcher, UserRepository $user)
    {
        $this->dispatcher = $dispatcher;
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  PostWasSaved  $event
     * @return void
     */
    public function handle(PostWasSaved $event)
    {
        $post = $event->post;
        $topic = $event->post->topic;

        if ($event->wasRecentlyCreated) {
            $user = $event->post->user;
            $subscribers = $topic->subscribers()->with('user.notificationSettings')->get()->pluck('user')->exceptUser($user);
            $notification = (new SubmittedNotification($user, $post))->setSender($this->getSender($post));

            $this->dispatcher->send($subscribers, $notification);

            $this->sendUserMentionedNotification($post, $user, $subscribers, $this->getSender($post));
        } elseif ($event->post->editor) {
            $user = $event->post->editor;
            $subscribers = $post->subscribers()->with('user')->get()->pluck('user')->exceptUser($user);

            $this->dispatcher->send($subscribers, new ChangedNotification($user, $post));

            $this->sendUserMentionedNotification($post, $user, $subscribers, $user->name);
        }
    }

    /**
     * @param Post $post
     * @param User $user
     * @param array $subscribers
     * @param string $senderName
     */
    private function sendUserMentionedNotification(Post $post, User $user, $subscribers, string $senderName): void
    {
        // get id of users that were mentioned in the text
{'fraction_non_alphanumeric': 0.13474434199497065, 'fraction_numerical': 0.0, 'mean_word_length': 3.1075731497418246, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '2223963', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1371, 'n_words': 272}