2025-06-06 14:28:21 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: MaxKB
|
|
|
|
|
|
@Author:虎虎
|
|
|
|
|
|
@file: chat.py
|
|
|
|
|
|
@date:2025/6/6 11:18
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
from drf_spectacular.utils import extend_schema
|
|
|
|
|
|
from rest_framework.request import Request
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
2025-06-09 08:18:43 +00:00
|
|
|
|
from chat.api.chat_api import ChatAPI
|
|
|
|
|
|
from chat.api.chat_authentication_api import ChatAuthenticationAPI, ChatAuthenticationProfileAPI, ChatOpenAPI
|
|
|
|
|
|
from chat.serializers.chat import OpenChatSerializers, ChatSerializers
|
|
|
|
|
|
from chat.serializers.chat_authentication import AnonymousAuthenticationSerializer, ApplicationProfileSerializer, \
|
|
|
|
|
|
AuthProfileSerializer
|
2025-06-06 14:28:21 +00:00
|
|
|
|
from common.auth import TokenAuth
|
2025-06-09 08:18:43 +00:00
|
|
|
|
from common.constants.permission_constants import ChatAuth
|
2025-06-06 14:28:21 +00:00
|
|
|
|
from common.exception.app_exception import AppAuthenticationFailed
|
|
|
|
|
|
from common.result import result
|
2025-06-17 07:01:59 +00:00
|
|
|
|
from users.api import CaptchaAPI
|
|
|
|
|
|
from users.serializers.login import CaptchaSerializer
|
2025-06-06 14:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-09 08:18:43 +00:00
|
|
|
|
class AnonymousAuthentication(APIView):
|
2025-06-06 14:28:21 +00:00
|
|
|
|
def options(self, request, *args, **kwargs):
|
|
|
|
|
|
return HttpResponse(
|
|
|
|
|
|
headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true",
|
|
|
|
|
|
"Access-Control-Allow-Methods": "POST",
|
|
|
|
|
|
"Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"}, )
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['POST'],
|
2025-06-09 08:18:43 +00:00
|
|
|
|
description=_('Application Anonymous Certification'),
|
|
|
|
|
|
summary=_('Application Anonymous Certification'),
|
|
|
|
|
|
operation_id=_('Application Anonymous Certification'), # type: ignore
|
2025-06-06 14:28:21 +00:00
|
|
|
|
request=ChatAuthenticationAPI.get_request(),
|
|
|
|
|
|
responses=None,
|
|
|
|
|
|
tags=[_('Chat')] # type: ignore
|
|
|
|
|
|
)
|
|
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
return result.success(
|
2025-06-09 08:18:43 +00:00
|
|
|
|
AnonymousAuthenticationSerializer(data={'access_token': request.data.get("access_token")}).auth(
|
2025-06-06 14:28:21 +00:00
|
|
|
|
request),
|
|
|
|
|
|
headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true",
|
|
|
|
|
|
"Access-Control-Allow-Methods": "POST",
|
|
|
|
|
|
"Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationProfile(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['GET'],
|
|
|
|
|
|
description=_("Get application related information"),
|
|
|
|
|
|
summary=_("Get application related information"),
|
|
|
|
|
|
operation_id=_("Get application related information"), # type: ignore
|
|
|
|
|
|
request=None,
|
|
|
|
|
|
responses=None,
|
|
|
|
|
|
tags=[_('Chat')] # type: ignore
|
|
|
|
|
|
)
|
|
|
|
|
|
def get(self, request: Request):
|
2025-06-09 08:18:43 +00:00
|
|
|
|
if isinstance(request.auth, ChatAuth):
|
2025-06-06 14:28:21 +00:00
|
|
|
|
return result.success(ApplicationProfileSerializer(
|
2025-06-09 08:18:43 +00:00
|
|
|
|
data={'application_id': request.auth.application_id}).profile())
|
2025-06-06 14:28:21 +00:00
|
|
|
|
raise AppAuthenticationFailed(401, "身份异常")
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-09 08:18:43 +00:00
|
|
|
|
class AuthProfile(APIView):
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['GET'],
|
|
|
|
|
|
description=_("Get application authentication information"),
|
|
|
|
|
|
summary=_("Get application authentication information"),
|
|
|
|
|
|
operation_id=_("Get application authentication information"), # type: ignore
|
|
|
|
|
|
parameters=ChatAuthenticationProfileAPI.get_parameters(),
|
|
|
|
|
|
responses=None,
|
|
|
|
|
|
tags=[_('Chat')] # type: ignore
|
|
|
|
|
|
)
|
|
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
AuthProfileSerializer(data={'access_token': request.query_params.get("access_token")}).profile())
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-06 14:28:21 +00:00
|
|
|
|
class ChatView(APIView):
|
2025-06-09 08:18:43 +00:00
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['POST'],
|
|
|
|
|
|
description=_("dialogue"),
|
|
|
|
|
|
summary=_("dialogue"),
|
|
|
|
|
|
operation_id=_("dialogue"), # type: ignore
|
|
|
|
|
|
request=ChatAPI.get_request(),
|
|
|
|
|
|
parameters=ChatAPI.get_parameters(),
|
|
|
|
|
|
responses=None,
|
|
|
|
|
|
tags=[_('Chat')] # type: ignore
|
|
|
|
|
|
)
|
|
|
|
|
|
def post(self, request: Request, chat_id: str):
|
|
|
|
|
|
return ChatSerializers(data={'chat_id': chat_id,
|
|
|
|
|
|
'chat_user_id': request.auth.chat_user_id,
|
|
|
|
|
|
'chat_user_type': request.auth.chat_user_type,
|
2025-06-17 03:58:35 +00:00
|
|
|
|
'application_id': request.auth.application_id,
|
|
|
|
|
|
'debug': False}
|
2025-06-09 08:18:43 +00:00
|
|
|
|
).chat(request.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpenView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['GET'],
|
|
|
|
|
|
description=_("Get the session id according to the application id"),
|
|
|
|
|
|
summary=_("Get the session id according to the application id"),
|
|
|
|
|
|
operation_id=_("Get the session id according to the application id"), # type: ignore
|
|
|
|
|
|
parameters=ChatOpenAPI.get_parameters(),
|
|
|
|
|
|
responses=None,
|
|
|
|
|
|
tags=[_('Chat')] # type: ignore
|
|
|
|
|
|
)
|
2025-06-17 03:58:35 +00:00
|
|
|
|
def get(self, request: Request):
|
2025-06-09 08:18:43 +00:00
|
|
|
|
return result.success(OpenChatSerializers(
|
2025-06-17 03:58:35 +00:00
|
|
|
|
data={'application_id': request.auth.application_id,
|
|
|
|
|
|
'chat_user_id': request.auth.chat_user_id, 'chat_user_type': request.auth.chat_user_type,
|
|
|
|
|
|
'debug': False}).open())
|
2025-06-17 07:01:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CaptchaView(APIView):
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-06-23 12:19:32 +00:00
|
|
|
|
summary=_("Get Chat captcha"),
|
|
|
|
|
|
description=_("Get Chat captcha"),
|
|
|
|
|
|
operation_id=_("Get Chat captcha"), # type: ignore
|
2025-06-17 07:01:59 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
|
|
|
|
|
responses=CaptchaAPI.get_response())
|
|
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(CaptchaSerializer().generate())
|