<?php

use think\Db;
use addons\fastim\library\Common;
use addons\fastim\library\controller\Base;

/**
 * im基础类
 */
class ImBase extends Base
{
    /**
     * 无需登录的方法
     * @var array
     */
    protected $noNeedLogin = ['login'];

    /**
     * 心跳
     */
    public function ping()
    {
        $this->server->push($this->frame->fd, json_encode([
            'event' => 'pong'
        ]));
    }

    public function equipmentInspection($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['type'], $data, true)) {
            return false;
        }

        if ($data['type'] == 'check') {

            $recipientIdFds = $this->swooleCommon->getFdByUid($this->userInfo['id']);
            if (!$recipientIdFds) {
                return false;
            }
            foreach ($recipientIdFds as $key => $value) {
                if ($this->server->isEstablished($value)) {
                    $this->server->push($value, json_encode([
                        'event' => 'equipment-inspection',
                        'data'  => [
                            'time' => $data['time'],
                            'fd'   => $value
                        ]
                    ]));
                }
            }
        } elseif ($data['type'] == 'close') {
            $this->swooleCommon->unbindUid($data['fd'], $this->userInfo['id']);
        }
    }

    /**
     * 登录
     */
    public function login($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['identity', 'tokens', 'platform'], $data, true)) {
            return false;
        }

        $tourist = false;
        if (isset($data['tokens']['fastim_token']) && ($data['identity'] == 'admin' || $data['identity'] == 'fauser')) {
            // 管理员或用户
            if ($data['identity'] == 'fauser') {
                $userInfo = Common::checkFaUser($data['tokens']['fastim_token']);
                if ($userInfo) {
//                    $this->userInfo = Common::checkWxIMUser(false,$userInfo);
                    $this->userInfo = Common::checkIMUser(false, $userInfo);
                }
            } else {
                $userInfo = Common::checkAdmin($data['tokens']['fastim_token']);
                if ($userInfo) {
//                    $this->userInfo = Common::checkWxIMUser(false,$userInfo);
                    $this->userInfo = Common::checkIMUser(false, $userInfo);
                }
            }
        } elseif (isset($data['tokens']['im_tourists_token']) && $data['identity'] == 'imuser') {
            // imuser
//            $this->userInfo = Common::checkWxIMUser($data['tokens']['im_tourists_token']);
            $this->userInfo = Common::checkIMUser($data['tokens']['im_tourists_token']);
            $tourist        = true;

            if (isset($data['tokens']['auth_token']) && $this->userInfo) {
                $tourist = Common::checkAuthToken($data['tokens']['auth_token']) ? false : true;
            }
        } else {
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => '登录失败，请重试！',
                'close' => true
            ]));
            $this->server->close($this->frame->fd);
            return false;
        }

        if ($this->userInfo) {

            $this->swooleCommon->bindUid($this->frame->fd, $this->userInfo['id']);
            $this->swooleCommon->bindFd($this->frame->fd, $this->userInfo['id'], $tourist, $data['platform']);

            if ($this->userInfo['status'] === 0) {

                $loginStatus = Common::imUserConfig($this->userInfo['id'], 'login_status');
                $loginStatus++;

                $this->userInfo['status'] = $loginStatus;
                Db::name('fastim_user')->where('id', $this->userInfo['id'])->update([
                    'status' => $loginStatus
                ]);

                $this->swooleCommon->radioStatusMessage($this->userInfo['id'], ($loginStatus == 3 ? 0 : $loginStatus));
            }

            // "我的客服"功能
            $openCsr = Db::name('fastim_config')->where('name', 'open_csr')->value('value');
            if ($openCsr && $this->userInfo['type'] != 'csr') {
                // 为用户添加我的客服会话
                $imSession         = Common::imSession('service', $this->userInfo['id'], 3);
                $welcomeNewNserMsg = Db::name('fastim_config')->where('name', 'welcome_new_user_msg')->value('value');
                if ($welcomeNewNserMsg && isset($imSession['create']) && $imSession['create']) {
                    // 发送欢迎消息给用户
                    $sessionInfo = Common::sessionInfo($imSession['id'], $this->userInfo['id']);
                    // 消息入库
                    $messageData = Common::insertMessage([
                        'type'         => 'default',
                        'session_id'   => $sessionInfo['id'],
                        'sender_id'    => 0,
                        'recipient_id' => $this->userInfo['id'],
                        'message'      => $welcomeNewNserMsg
                    ]);
                }
            }

            $this->userInfo['unreadMessage'] = Common::getUnreadMessage($this->userInfo['id'], $data['initialize_time'] ?? false);
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'login_success',
                'data'  => $this->userInfo
            ]));
            return $this->userInfo;
        } else {
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => '用户身份识别失败，请重新登录！',
                'close' => true
            ]));
            $this->server->close($this->frame->fd);
            return false;
        }
    }

    /**
     * 用户修改状态
     */
    public function changeStatus($data)
    {
        $imUserStatus = Common::imUserStatus($data['status']);

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'change_status',
            'data'  => [
                'status_chinese' => $imUserStatus['chinese'],
                'status'         => $data['status'],
                'uid'            => $this->userInfo['id']
            ]
        ]));

        if ($data['status'] == 0) {
            // 用户主动离线
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => '',
                'close' => true
            ]));
            $this->server->close($this->frame->fd);
        } else {

            $this->swooleCommon->radioStatusMessage($this->userInfo['id'], ($data['status'] == 3 ? 0 : $data['status']));

            Db::name('fastim_user')->where('id', $this->userInfo['id'])->update([
                'status' => $data['status']
            ]);
        }
    }

    /**
     * 待办
     */
    public function loadTODO($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['method'], $data)) {
            return;
        }

        if ($data['method'] == 'save') {
            if (!$this->swooleCommon->requiredParamCheck(['add_val'], $data, false, ['add_val' => '请输入正确的待办内容~'])) {
                return;
            }
            if (isset($data['id']) && $data['id']) {
                Db::name('fastim_todo')->where('id', $data['id'])->where('user_id', $this->userInfo['id'])->update([
                    'title' => $data['add_val']
                ]);
            } else {
                Db::name('fastim_todo')->insert([
                    'user_id'    => $this->userInfo['id'],
                    'title'      => $data['add_val'],
                    'status'     => '0',
                    'createtime' => time()
                ]);
            }
        } elseif ($data['method'] == 'update') {
            if (!$this->swooleCommon->requiredParamCheck(['id'], $data)) {
                return;
            }
            Db::name('fastim_todo')->where('id', $data['id'])->where('user_id', $this->userInfo['id'])->update([
                'status' => $data['status'] ? '1' : '0'
            ]);
            if ($data['status']) {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => '待办事项已完成~',
                        'type' => 'success'
                    ]
                ]));
            }
        } elseif ($data['method'] == 'get-all-count') {
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'load_to_do',
                'data'  => [
                    'count' => Db::name('fastim_todo')
                        ->where('user_id', $this->userInfo['id'])
                        ->where('status', '0')
                        ->where('deletetime', null)
                        ->count(),
                    'data'  => $data
                ]
            ]));
            return;
        }

        if (!isset($data['page']) || $data['page'] == 1) {
            $min          = 0;
            $data['page'] = 1;
        } else {
            $min = ($data['page'] - 1) * $this->pageCount;
        }

        // 未完成的-加载全部
        $incomplete = Db::name('fastim_todo')
            ->where('user_id', $this->userInfo['id'])
            ->where('status', '0')
            ->where('deletetime', null)
            ->order('createtime desc')
            ->select();

        // 完成了的
        $completed = Db::name('fastim_todo')
            ->where('user_id', $this->userInfo['id'])
            ->where('status', '1')
            ->where('deletetime', null)
            ->order('createtime desc')
            ->limit($min, $this->pageCount)
            ->select();

        if ($data['page'] == 1) {
            $data['incomplete'] = count($incomplete);
            $data['completed']  = Db::name('fastim_todo')
                ->where('user_id', $this->userInfo['id'])
                ->where('status', '1')
                ->where('deletetime', null)
                ->order('createtime desc')
                ->count('id');
        }

        $data['nextpage'] = (count($completed) < $this->pageCount) ? false : true;
        $this->server->push($this->frame->fd, json_encode([
            'event' => 'load_to_do',
            'data'  => [
                'incomplete' => $incomplete,
                'completed'  => $completed,
                'data'       => $data
            ]
        ]));
    }

    /**
     * 获得设置数据
     */
    public function config($data)
    {
        // 已屏蔽的联系人
        $shieldUsers = Db::name('fastim_user_shield')
            ->alias('s')
            ->field('s.*,f.remark,fu.nickname as fu_nickname,a.nickname as a_nickname,u.nickname,u.id as uid,se.id as session_id')
            ->join('fastim_session se', '(se.user_one=s.user_id AND se.user_two=s.shield_id) OR (se.user_one=s.shield_id AND se.user_two=s.user_id)')
            ->join('fastim_user u', 's.shield_id=u.id')
            ->join('user fu', 'u.user_id=fu.id', 'LEFT')
            ->join('admin a', 'u.admin_id=a.id', 'LEFT')
            ->join('fastim_friendship f', 's.user_id=f.user_id AND s.shield_id=f.friend_id', 'LEFT')
            ->where('s.user_id', $this->userInfo['id'])
            ->select();
        $nicknameKey = ['nickname', 'fu_nickname', 'a_nickname', 'remark'];
        foreach ($shieldUsers as $key => &$value) {
            $value['nickname'] = Common::trueAttr(Common::nicknameSort([
                $value['remark'],
                $value['nickname'],
                $value['fu_nickname'],
                $value['a_nickname']
            ], $value['uid']));
        }

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'config',
            'data'  => [
                'shield_users' => $shieldUsers
            ]
        ]));
    }

    /**
     * 更新用户配置
     */
    public function updateUserConfig($data)
    {
        $imDbConfig = Db::name('fastim_config')->where('user_settings', 1)->column('name,value');
        $userConfig = Db::name('fastim_user_config')->where('user_id', $this->userInfo['id'])->column('name,value');
        foreach ($data as $key => $value) {
            $value = trim($value, ',');
            // 是否为有效配置
            if (array_key_exists($key, $imDbConfig)) {
                // 是否已设置过
                if (array_key_exists($key, $userConfig)) {
                    $setUp = true;
                } else {
                    $setUp = false;
                }

                // 设置为默认值
                if ($value == $imDbConfig[$key]) {
                    if ($setUp) {
                        Db::name('fastim_user_config')
                            ->where('user_id', $this->userInfo['id'])
                            ->where('name', $key)
                            ->delete();
                    }
                    continue;
                }

                if ($setUp) {
                    Db::name('fastim_user_config')
                        ->where('user_id', $this->userInfo['id'])
                        ->where('name', $key)
                        ->update([
                            'value' => $value
                        ]);
                } else {
                    $insert = [
                        'user_id' => $this->userInfo['id'],
                        'name'    => $key,
                        'value'   => $value
                    ];
                    Db::name('fastim_user_config')->insert($insert);
                }
            }
        }

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'show_msg',
            'data'  => [
                'msg'  => '设置已保存,刷新生效~',
                'type' => 'success'
            ]
        ]));
    }

    /**
     * 屏蔽/解除屏蔽一个会话
     */
    public function shieldSession($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['session_id', 'method'], $data)) {
            return;
        }

        $sessionInfo = Common::sessionInfo($data['session_id'], $this->userInfo['id']);

        $exist = Db::name('fastim_user_shield')
            ->where('user_id', $this->userInfo['id'])
            ->where('shield_id', $sessionInfo['sessionUser']['id'])
            ->value('id');

        if ($data['method'] == 'shield' && !$exist) {
            $shield = [
                'user_id'   => $this->userInfo['id'],
                'shield_id' => $sessionInfo['sessionUser']['id']
            ];
            Db::name('fastim_user_shield')->insert($shield);
            if ($sessionInfo['deleteuser']) {
                Db::name('fastim_session')->where('id', $sessionInfo['id'])->delete();
            } else {
                Db::name('fastim_session')->where('id', $sessionInfo['id'])->update([
                    'deleteuser' => $this->userInfo['id']
                ]);
            }
        } elseif ($data['method'] == 'relieve' && $exist) {
            Db::name('fastim_user_shield')->where('id', $exist)->delete();
            if ($sessionInfo['deleteuser'] == $this->userInfo['id']) {
                Db::name('fastim_session')->where('id', $sessionInfo['id'])->update([
                    'deleteuser' => 0
                ]);
            }
        }

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'shield_session',
            'data'  => [
                'session_id' => $data['session_id'],
                'method'     => $data['method']
            ]
        ]));
    }

    /**
     * 一个用户的全部快捷回复
     */
    public function userAllFastReply()
    {
        $fastReply = Db::name('fastim_fast_reply')->field('id,title,content')->where(function ($query) {
            $query->where('user_id', $this->userInfo['id'])->whereOr('user_id', 0);
        })->where('status', '1')->where('deletetime', null)->order('user_id desc,createtime desc')->select();

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'user_all_fast_reply',
            'data'  => $fastReply
        ]));
    }

    /**
     * 用户快捷回复
     */
    public function fastReply($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['method'], $data)) {
            return;
        }

        $isUniApp = false;
        if (isset($data['form']) && $data['form'] == 'uni-app') {
            $isUniApp = true;
        }

        if ($data['method'] == 'del') {
            if (!$this->swooleCommon->requiredParamCheck(['id'], $data)) {
                return;
            }

            Db::name('fastim_fast_reply')->where('id', $data['id'])->update([
                'deletetime' => time()
            ]);

            $data['page'] = 1;

            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => [
                    'msg'  => '快捷回复已删除~',
                    'type' => 'success'
                ]
            ]));

            if ($isUniApp) {
                $this->userAllFastReply();
            }
        } elseif ($data['method'] == 'edit') {
            $info = Db::name('fastim_fast_reply')->where('id', $data['id'])->where('deletetime', null)->find();

            $this->server->push($this->frame->fd, json_encode([
                'event' => 'fast_reply',
                'data'  => [
                    'data' => $data,
                    'info' => $info
                ]
            ]));
            return;
        } elseif ($data['method'] == 'update') {
            $updateData     = $data['data'];
            $data['method'] = 'get';
            unset($data['data']);

            if (!$this->swooleCommon->requiredParamCheck(['title', 'content'], $updateData, false, [
                'title'   => '请输入标题~',
                'content' => '请输入回复内容~'
            ])) {
                return;
            }

            if ($updateData['id'] > 0) {
                // 更新
                Db::name('fastim_fast_reply')
                    ->where('id', $updateData['id'])
                    ->where('user_id', $this->userInfo['id'])
                    ->update($updateData);
                $msg = '快捷回复已更新~';
            } else {
                // 添加
                unset($updateData['id']);
                $updateData['user_id']    = $this->userInfo['id'];
                $updateData['createtime'] = time();
                Db::name('fastim_fast_reply')->insert($updateData);
                $msg = '快捷回复添加成功~';
            }

            if ($isUniApp) {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'fast_reply',
                    'data'  => [
                        'data' => [
                            'method' => 'opt-done',
                            'msg'    => $msg
                        ]
                    ]
                ]));

                $this->userAllFastReply();
                return;
            } else {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => $msg,
                        'type' => 'success'
                    ]
                ]));
            }
        }

        if (!isset($data['page']) || $data['page'] == 1) {
            $min = 0;
        } else {
            $min = ($data['page'] - 1) * $this->pageCount;
        }

        $fastReplyList = Db::name('fastim_fast_reply')
            ->where('user_id', $this->userInfo['id'])
            ->where('deletetime', null)
            ->limit($min, $this->pageCount)
            ->select();

        $data['nextpage'] = (count($fastReplyList) < $this->pageCount) ? false : true;
        $this->server->push($this->frame->fd, json_encode([
            'event' => 'fast_reply',
            'data'  => [
                'data'            => $data,
                'fast_reply_list' => $fastReplyList
            ]
        ]));
    }

    /**
     * 举报和问题反馈提交
     */
    public function report($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['method'], $data)) {
            return;
        }

        if ($data['type'] == 'user' || $data['type'] == 'group') {
            if (!$this->swooleCommon->requiredParamCheck(['session_id'], $data, false, ['session_id' => '会话找不到啦~'])) {
                return;
            }
            $sessionInfo = Common::sessionInfo($data['session_id'], $this->userInfo['id']);
        }

        if ($data['method'] == 'post') {

            $title = ($data['type'] == 'user' || $data['type'] == 'group') ? '举报' : '反馈';
            if (!$this->swooleCommon->requiredParamCheck(['describe'], $data, false, [
                'describe' => '请输入' . $title . '详情~'
            ])) {
                return;
            }

            // 数据入库
            $report = [
                'type'        => $data['type'],
                'session_id'  => $sessionInfo['id'] ?? 0,
                'report_id'   => $this->userInfo['id'],
                'user_id'     => $sessionInfo['sessionUser']['id'] ?? 0,
                'describe'    => $data['describe'],
                'mobile'      => $data['mobile'] ?? '',
                'reportimage' => isset($data['reportimage']) ? trim($data['reportimage'], ',') : '',
                'createtime'  => time()
            ];
            if (Db::name('fastim_report')->insert($report)) {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => $title . '提交成功!',
                        'type' => 'success'
                    ]
                ]));
            } else {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => $title . '提交失败,请重试!',
                        'type' => 'error'
                    ]
                ]));
            }
            return;
        }

        $this->server->push($this->frame->fd, json_encode([
            'event' => ($data['type'] == 'user' || $data['type'] == 'group') ? 'report' : 'feedback',
            'data'  => $sessionInfo
        ]));
    }

    /**
     * 获取分组
     * @return void
     */
    public function getGroups($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['type'], $data)) {
            return;
        }

        // 获取用户的好友和群聊分组
        if ($data['type'] == 'contacts' || $data['type'] == 'pick-contacts') {
            $res = [
                'friends' => Common::getGroups($this->userInfo['id'], 0),
                'group'   => Common::getGroups($this->userInfo['id'], 1)
            ];
        } elseif ($data['type'] == 'collection') {
            // 获取用户的收藏分组
            $res = [
                'collection' => Common::getGroups($this->userInfo['id'], 2)
            ];
        }

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'get_groups',
            'data'  => [
                'res'  => $res,
                'data' => $data
            ]
        ]));
    }

    /**
     * 好友和群聊分组管理
     */
    public function updateGroup($data)
    {
        if (!$this->swooleCommon->requiredParamCheck(['action'], $data)) {
            return false;
        }

        if ($data['action'] != 'add-new-group') {
            $group = Db::name('fastim_group')
                ->where('id', $data['id'])
                ->where('user_id', $this->userInfo['id'])
                ->find();
            if (!$group) {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => '要操作的分组找不到啦~'
                ]));
                return false;
            }

            // 重新加载前台的分组需要的参数
            if ($group['type'] == 0 || $group['type'] == 1) {
                $reloadGroupData = [
                    'type' => 'contacts'
                ];
            } elseif ($group['type'] == 2) {
                $reloadGroupData = [
                    'type' => 'collection'
                ];
            }
        }

        if ($data['action'] == 'add-new-group') {
            if (!$this->swooleCommon->requiredParamCheck(['type', 'name'], $data, false, ['name' => '请输入正确的分组名称~'])) {
                return false;
            }

            switch ($data['type']) {
                case 'friends':
                    $data['type'] = 0;
                    break;
                case 'group':
                    $data['type'] = 1;
                    break;
                case 'collection':
                    $data['type'] = 2;
                    break;
            }

            $weigh  = Db::name('fastim_group')
                ->where('user_id', $this->userInfo['id'])
                ->where('type', $data['type'])
                ->max('id');
            $weigh  = $weigh ? ($weigh + 1) : 1;
            $insert = [
                'type'    => $data['type'],
                'user_id' => $this->userInfo['id'],
                'name'    => $data['name'],
                'weigh'   => $weigh
            ];
            if (Db::name('fastim_group')->insert($insert)) {
                if ($data['type'] == 0 || $data['type'] == 1) {
                    $reloadGroupData = [
                        'type' => 'contacts'
                    ];
                } else {
                    $reloadGroupData = [
                        'type' => 'collection'
                    ];
                }
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => '添加分组成功~',
                        'type' => 'success'
                    ]
                ]));
            } else {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => '添加失败,请重试!'
                ]));
                return false;
            }
        } elseif ($data['action'] == 'update-group-name') {
            if (!$this->swooleCommon->requiredParamCheck([
                'id',
                'newname'
            ], $data, false, ['newname' => '请输入正确的分组名称~'])) {
                return false;
            }

            if (Db::name('fastim_group')
                ->where('id', $data['id'])
                ->where('user_id', $this->userInfo['id'])
                ->update(['name' => $data['newname']])) {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => [
                        'msg'  => '保存成功~',
                        'type' => 'success'
                    ]
                ]));
            } else {
                $this->server->push($this->frame->fd, json_encode([
                    'event' => 'show_msg',
                    'data'  => '未做任何修改~'
                ]));
                return false;
            }
        } elseif ($data['action'] == 'del' && Db::name('fastim_group')
                ->where('id', $data['id'])
                ->where('user_id', $this->userInfo['id'])
                ->delete()) {
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => [
                    'msg'  => '分组已删除~',
                    'type' => 'success'
                ]
            ]));
        } elseif ($data['action'] == 'moveup') {
            $up = Db::name('fastim_group')
                ->where('user_id', $this->userInfo['id'])
                ->where('type', $group['type'])
                ->where('weigh', '<', $group['weigh'])
                ->order('weigh desc')
                ->find();

            Db::name('fastim_group')->update([
                'id'    => $up['id'],
                'weigh' => $group['weigh']
            ]);
            Db::name('fastim_group')->update([
                'id'    => $group['id'],
                'weigh' => $up['weigh']
            ]);
        } elseif ($data['action'] == 'movedown') {
            $down = Db::name('fastim_group')
                ->where('user_id', $this->userInfo['id'])
                ->where('type', $group['type'])
                ->where('weigh', '>', $group['weigh'])
                ->order('weigh asc')
                ->find();
            Db::name('fastim_group')->update([
                'id'    => $down['id'],
                'weigh' => $group['weigh']
            ]);
            Db::name('fastim_group')->update([
                'id'    => $group['id'],
                'weigh' => $down['weigh']
            ]);
        }

        return $this->getGroups($reloadGroupData);
    }

    /**
     * 更新云存储 multipart
     */
    public function getUploadMultipart($data)
    {
        // 获取上传配置
        $upload = \app\common\model\Config::upload();
        \think\Hook::listen("upload_config_init", $upload);

        $this->server->push($this->frame->fd, json_encode([
            'event' => 'upload_multipart',
            'data'  => [
                'multipart' => isset($upload['multipart']) ? $upload['multipart'] : []
            ]
        ]));
    }
}