HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/heifetz/heifetz-app/models/TelegramMessageQueue.php
<?php

declare(strict_types=1);

namespace Models;

use Core\Models\CoreHelper;
use Core\Models\OldUser;
use Helpers\TelegramHelper;

/**
 * @property int $id
 * @property int $user_id
 * @property int $telegram_id
 * @property int $status
 * @property string $message
 * @property string $created_at
 * @property string $send_after
 * @property string $send_at
 * @property int $external_id
 * @property int $message_type
 * @property int $error_log_id
 * @property int $company_id
 */
class TelegramMessageQueue extends AbstractMessageQueue
{

    static string $tableName = 'telegram_message_queue';

    const MESSAGE_TYPE_DEFAULT = 0;
    const MESSAGE_TYPE_ERROR = 1;

    public static function save(int $telegramId, string $message, ?string $sendAfter = null, ?int $userId = null, ?int $authorId = null, bool $isError = false, ?int $logId = null): int
    {
        if ($authorId > 0) {
            $message = OldUser::getName($authorId) . ': ' . $message;
        }

        $data = [
            'telegram_id' => $telegramId,
            'message' => $message,
            'user_id' => $userId,
            'status' => self::STATUS_NEW,
            'created_at' => date('Y-m-d H:i:s'),
            'send_after' => $sendAfter,
            'message_type' => $isError ? self::MESSAGE_TYPE_ERROR : self::MESSAGE_TYPE_DEFAULT,
            'author_id' => $authorId,
            'error_log_id' => $logId,
            'company_id' => CoreHelper::$companyId,
        ];

        return self::create($data);
    }

    public static function getNotGenerated(): array
    {
        return self::getQuery()->whereIn('status', [self::STATUS_NEW, self::STATUS_IN_PROGRESS])->fetchAll();
    }

    public static function clearErrorMessages(): void
    {
        self::delete(['message_type' => self::MESSAGE_TYPE_ERROR, 'company_id' => CoreHelper::$companyId]);
    }

    public static function removeErrorsFromChats()
    {
        /** @var self[] $errorsQueue */
        $errorsQueue = self::getQuery()->whereIsNotNull('error_log_id')->whereIsNotNull('external_id')->fetchAll();

        foreach ($errorsQueue as $error) {
            TelegramHelper::delete($error->telegram_id, $error->external_id);
            self::delete(['id' => $error->id, 'company_id' => CoreHelper::$companyId]);
        }
        self::clearErrorMessages();
    }

    public static function removeErrorsFromChatsByIds(array $errorLogsIds)
    {
        /** @var self[] $errorsQueue */
        $errorsQueue = self::getQuery()->whereIn('error_log_id', $errorLogsIds)->fetchAll();

        foreach ($errorsQueue as $error) {
            if (empty($error->external_id)) {
                continue;
            }
            TelegramHelper::delete($error->telegram_id, $error->external_id);
            self::delete(['id' => $error->id, 'company_id' => CoreHelper::$companyId]);
        }
    }

}