package com.webchat.controller.client;

import com.webchat.common.exception.BusinessException;
import com.webchat.common.helper.SessionHelper;
import com.webchat.common.helper.SseEmitterHelper;
import com.webchat.config.annotation.ValidateLogin;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2024/10/29 23:54
 * @description
 */
@RestController
@RequestMapping("/api/robot/chat")
public class RobotChatController {

    /**
     * 获取SSE链接对象
     *
     * @param request
     * @return
     */
    @ValidateLogin
    @GetMapping(path = "/stream", produces = "text/event-stream")
    public SseEmitter stream(HttpServletRequest request) {
        String biz = request.getParameter("biz");
        String userId = SessionHelper.getCurrentUserId();
        if (StringUtils.isBlank(userId)) {
            throw new BusinessException("对话Stream流长链接获取异常，未登录！");
        }
        return SseEmitterHelper.get(biz, userId);
    }

}
