<commit_before><?php namespace Strimoid\Models;

use Auth;
use Str;
use URL;

/**
 * Notification model.
 *
 * @property string $_id
 * @property string $title Notification title
 * @property string $type Type of notification
 * @property array $targets
 * @property DateTime $created_at
 * @property-read User::class)->select([' $sourceUser 
 * @property-read \ $element 
 * @property mixed $read 
 * @property-read mixed $vote_state 
 * @property-read \Illuminate\Database\Eloquent\Collection|Vote[] $vote 
 * @property-read \Illuminate\Database\Eloquent\Collection|Save[] $usave 
 * @method static \Strimoid\Models\Notification target($param)
 * @method static \Strimoid\Models\BaseModel fromDaysAgo($days)
 */
class Notification extends BaseModel
{
    protected $table = 'notifications';
    protected $visible = [
        'id', 'created_at', 'sourceUser',
        'read', 'title', 'type', 'url',
    ];

    public function sourceUser()
    {
        return $this->belongsTo(User::class)->select(['avatar', 'name']);
    }

    public function targets()
    {
        return $this->belongsToMany(User::class, 'notification_targets')->withPivot('read');
    }

    public function element()
    {
        return $this->morphTo();
    }

    public function setTitle($title)
    {
        $clean = preg_replace('/<span class="spoiler">(.*?)<\/span>/s', '', $title);
        $clean = strip_tags($clean);
        $text = Str::limit($clean, 60);

        $this->title = $text;
    }

    public function getReadAttribute()
    {
        $target = $this->targets->filter(function ($x) {
            return $x->user_id == Auth::id();
        })->first();

        if (! $target) {
            return false;
        }

        return $target->read;
    }

    public function setReadAttribute($value)
    {
        $this->attributes['users.read'] = toBool($value);
    }

    public function getURL()
    {
        $url = null;
        $params = null;

        // Add parameter to mark notification as read
        if (!$this->read) {
            $params .= '?ntf_read='.mid_to_b58($this->getKey());
        }

        try {
            switch ($this->type) {
                case 'entry':
                    $url = URL::route('single_entry', $this->entry_id, false)
                        .$params;
                    break;
                case 'entry_reply':
                    $url = URL::route('single_entry_reply', $this->entry_reply_id, false)
                        .$params.'#'.$this->entry_reply_id;
                    break;
                case 'comment':
                    $url = URL::route('content_comments', $this->content_id, false)
                        .$params.'#'.$this->comment_id;
                    break;
                case 'comment_reply':
                    $url = URL::route('content_comments', $this->content_id, false)
                        .$params.'#'.$this->comment_reply_id;
                    break;
                case 'conversation':
                    $url = URL::route('conversation', $this->conversation_id, false)
                        .$params;
                    break;
                case 'moderator':
                    $url = URL::route('group_contents', $this->group_id, false)
                        .$params;
                    break;
            }
        } catch (Exception $e) {
            // Triggered when element was removed, but notification still exists
        }

        return $url;
    }

    public function getTypeDescription()
    {
        switch ($this->type) {
            case 'content':           return 'Treść';
            case 'related':           return 'Powiązany link';
            case 'entry':             return 'Wpis';
            case 'entry_reply':       return 'Odpowiedź na wpis';
            case 'comment':           return 'Komentarz';
            case 'comment_reply':     return 'Odpowiedź na komentarz';
            case 'conversation':      return 'Konwersacja';
            case 'moderator':         return 'Powiadomienie';
        }
    }

    public function getThumbnailPath()
    {
        return $this->sourceUser->getAvatarPath();
    }

    public function addTarget(User $user)
    {
        $target = new NotificationTarget();
        $target->user()->associate($user);

        $this->targets()->associate($target);<commit_msg>Fix notifications<commit_after><?php namespace Strimoid\Models;

use Auth;
use Str;
use URL;

/**
 * Notification model.
 *
 * @property string $_id
 * @property string $title Notification title
 * @property string $type Type of notification
 * @property array $targets
 * @property DateTime $created_at
 * @property-read User::class)->select([' $sourceUser 
 * @property-read \ $element 
 * @property mixed $read 
 * @property-read mixed $vote_state 
 * @property-read \Illuminate\Database\Eloquent\Collection|Vote[] $vote 
 * @property-read \Illuminate\Database\Eloquent\Collection|Save[] $usave 
 * @method static \Strimoid\Models\Notification target($param)
 * @method static \Strimoid\Models\BaseModel fromDaysAgo($days)
 */
class Notification extends BaseModel
{
    protected $table = 'notifications';
    protected $visible = [
        'id', 'created_at', 'sourceUser',
        'read', 'title', 'type', 'url',
    ];

    public function sourceUser()
    {
        return $this->belongsTo(User::class)->select(['avatar', 'name']);
    }

    public function targets()
    {
        return $this->belongsToMany(User::class, 'notification_targets')->withPivot('read');
    }

    public function element()
    {
        return $this->morphTo();
    }

    public function setTitle($title)
    {
        $clean = preg_replace('/<span class="spoiler">(.*?)<\/span>/s', '', $title);
        $clean = strip_tags($clean);
        $text = Str::limit($clean, 60);

        $this->title = $text;
    }

    public function getReadAttribute()
    {
        $target = $this->targets->filter(function ($x) {
            return $x->user_id == Auth::id();
        })->first();

        if (! $target) {
            return false;
        }

        return $target->read;
    }

    public function setReadAttribute($value)
    {
        $this->attributes['users.read'] = toBool($value);
    }

    public function getURL()
    {
        $url = null;
        $params = null;

        // Add parameter to mark notification as read
        if (!$this->read) {
            $params .= '?ntf_read='.mid_to_b58($this->getKey());
        }

        try {
            switch ($this->type) {
                case 'entry':
                    $url = URL::route('single_entry', $this->entry_id, false)
                        .$params;
                    break;
                case 'entry_reply':
                    $url = URL::route('single_entry_reply', $this->entry_reply_id, false)
                        .$params.'#'.$this->entry_reply_id;
                    break;
                case 'comment':
                    $url = URL::route('content_comments', $this->content_id, false)
                        .$params.'#'.$this->comment_id;
                    break;
                case 'comment_reply':
                    $url = URL::route('content_comments', $this->content_id, false)
                        .$params.'#'.$this->comment_reply_id;
                    break;
                case 'conversation':
                    $url = URL::route('conversation', $this->conversation_id, false)
                        .$params;
                    break;
                case 'moderator':
                    $url = URL::route('group_contents', $this->group_id, false)
                        .$params;
                    break;
            }
        } catch (Exception $e) {
            // Triggered when element was removed, but notification still exists
        }

        return $url;
    }

    public function getTypeDescription()
    {
        switch ($this->type) {
            case 'content':           return 'Treść';
            case 'related':           return 'Powiązany link';
            case 'entry':             return 'Wpis';
            case 'entry_reply':       return 'Odpowiedź na wpis';
            case 'comment':           return 'Komentarz';
            case 'comment_reply':     return 'Odpowiedź na komentarz';
            case 'conversation':      return 'Konwersacja';
            case 'moderator':         return 'Powiadomienie';
        }
    }

    public function getThumbnailPath()
    {
        return $this->sourceUser->getAvatarPath();
    }

    public function addTarget(User $user)
    {
        $target = new NotificationTarget();
        $target->user()->associate($user);

        $this->targets()->save($target);
{'fraction_non_alphanumeric': 0.13739230327398047, 'fraction_numerical': 0.0009190120620333142, 'mean_word_length': 1.8247890979883192, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, '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': '4523951', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2356, 'n_words': 526}