<commit_before><?php namespace App\Limits;

use App\Activity;
use Ivacuum\Generic\Contracts\Limit;
use Ivacuum\Generic\Events\LimitExceeded;

class CommentsTodayLimit implements Limit
{
    public function __construct()
    {
        if (auth()->guest()) {
            throw new \Exception('Для гостя лимиты не предусмотрены');
        }
    }

    public function floodControl(): bool
    {
        $user_id = auth()->id();
        $interval = config('cfg.limits.comment.flood_interval');

        if ($interval <= 0) {
            return false;
        }

        $created_at = Activity::where('user_id', $user_id)
            ->where('type', 'Comment.created')
            ->orderBy('id', 'desc')
            ->first(['created_at'])
            ->created_at;

        $diff = now()->diffInSeconds($created_at);

        if ($diff < $interval) {
            event(new LimitExceeded('comment.flood_interval', $user_id));

            return true;
        }

        return false;
    }

    public function ipExceeded(): bool
    {
        $ip = request()->ip();
        $limit = config('cfg.limits.comment.ip');

        $count = Activity::where('type', 'Comment.created')
            ->where('ip', $ip)
            ->where('created_at', '>', now()->startOfDay())
            ->count();

        if ($count >= $limit) {
            event(new LimitExceeded('comment.ip', $ip));

            return true;
        }

        return false;
    }

    public function userExceeded(): bool
    {<commit_msg>Исправление ошибки<commit_after><?php namespace App\Limits;

use App\Activity;
use Ivacuum\Generic\Contracts\Limit;
use Ivacuum\Generic\Events\LimitExceeded;

class CommentsTodayLimit implements Limit
{
    public function __construct()
    {
        if (auth()->guest()) {
            throw new \Exception('Для гостя лимиты не предусмотрены');
        }
    }

    public function floodControl(): bool
    {
        $user_id = auth()->id();
        $interval = config('cfg.limits.comment.flood_interval');

        if ($interval <= 0) {
            return false;
        }

        $last = Activity::where('user_id', $user_id)
            ->where('type', 'Comment.created')
            ->orderBy('id', 'desc')
            ->first(['created_at']);

        if (is_null($last)) {
            return false;
        }

        $diff = now()->diffInSeconds($last->created_at);

        if ($diff < $interval) {
            event(new LimitExceeded('comment.flood_interval', $user_id));

            return true;
        }

        return false;
    }

    public function ipExceeded(): bool
    {
        $ip = request()->ip();
        $limit = config('cfg.limits.comment.ip');

        $count = Activity::where('type', 'Comment.created')
            ->where('ip', $ip)
            ->where('created_at', '>', now()->startOfDay())
            ->count();

        if ($count >= $limit) {
            event(new LimitExceeded('comment.ip', $ip));

            return true;
        }

        return false;
    }

    public function userExceeded(): bool
    {
{'fraction_non_alphanumeric': 0.15263675073698002, 'fraction_numerical': 0.0006550933508024893, 'mean_word_length': 2.0, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, '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': '7638316', 'n_tokens_mistral': 917, 'n_tokens_neox': 876, 'n_words': 182}