feat: historical conversation (#3358)
This commit is contained in:
parent
fd6b9c00f6
commit
7f9caca5c7
@ -10,7 +10,9 @@ from drf_spectacular.types import OpenApiTypes
|
|||||||
from drf_spectacular.utils import OpenApiParameter
|
from drf_spectacular.utils import OpenApiParameter
|
||||||
|
|
||||||
from chat.serializers.chat import ChatMessageSerializers
|
from chat.serializers.chat import ChatMessageSerializers
|
||||||
|
from chat.serializers.chat_record import HistoryChatRecordModel
|
||||||
from common.mixins.api_mixin import APIMixin
|
from common.mixins.api_mixin import APIMixin
|
||||||
|
from common.result import ResultSerializer, ResultPageSerializer
|
||||||
|
|
||||||
|
|
||||||
class ChatAPI(APIMixin):
|
class ChatAPI(APIMixin):
|
||||||
@ -27,3 +29,33 @@ class ChatAPI(APIMixin):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get_request():
|
def get_request():
|
||||||
return ChatMessageSerializers
|
return ChatMessageSerializers
|
||||||
|
|
||||||
|
|
||||||
|
class ApplicationCreateResponse(ResultSerializer):
|
||||||
|
def get_data(self):
|
||||||
|
return HistoryChatRecordModel(many=True)
|
||||||
|
|
||||||
|
|
||||||
|
class PageApplicationCreateResponse(ResultPageSerializer):
|
||||||
|
def get_data(self):
|
||||||
|
return HistoryChatRecordModel(many=True)
|
||||||
|
|
||||||
|
|
||||||
|
class HistoricalConversationAPI(APIMixin):
|
||||||
|
@staticmethod
|
||||||
|
def get_parameters():
|
||||||
|
return []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_response():
|
||||||
|
return ApplicationCreateResponse
|
||||||
|
|
||||||
|
|
||||||
|
class PageHistoricalConversationAPI(APIMixin):
|
||||||
|
@staticmethod
|
||||||
|
def get_parameters():
|
||||||
|
return []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_response():
|
||||||
|
return PageApplicationCreateResponse
|
||||||
|
|||||||
@ -33,7 +33,7 @@ class AnonymousAuthenticationSerializer(serializers.Serializer):
|
|||||||
try:
|
try:
|
||||||
# 校验token
|
# 校验token
|
||||||
if token is not None:
|
if token is not None:
|
||||||
token_details = signing.loads(token)
|
token_details = signing.loads(token[7:])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
if with_valid:
|
if with_valid:
|
||||||
|
|||||||
@ -13,7 +13,8 @@ from django.db.models import QuerySet
|
|||||||
from django.utils.translation import gettext_lazy as _, gettext
|
from django.utils.translation import gettext_lazy as _, gettext
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from application.models import VoteChoices, ChatRecord
|
from application.models import VoteChoices, ChatRecord, Chat
|
||||||
|
from common.db.search import page_search
|
||||||
from common.exception.app_exception import AppApiException
|
from common.exception.app_exception import AppApiException
|
||||||
from common.utils.lock import try_lock, un_lock
|
from common.utils.lock import try_lock, un_lock
|
||||||
|
|
||||||
@ -23,6 +24,16 @@ class VoteRequest(serializers.Serializer):
|
|||||||
label=_("Bidding Status"))
|
label=_("Bidding Status"))
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryChatRecordModel(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Chat
|
||||||
|
fields = ['id',
|
||||||
|
'application_id',
|
||||||
|
'abstract',
|
||||||
|
'create_time',
|
||||||
|
'update_time']
|
||||||
|
|
||||||
|
|
||||||
class VoteSerializer(serializers.Serializer):
|
class VoteSerializer(serializers.Serializer):
|
||||||
chat_id = serializers.UUIDField(required=True, label=_("Conversation ID"))
|
chat_id = serializers.UUIDField(required=True, label=_("Conversation ID"))
|
||||||
|
|
||||||
@ -63,3 +74,22 @@ class VoteSerializer(serializers.Serializer):
|
|||||||
finally:
|
finally:
|
||||||
un_lock(self.data.get('chat_record_id'))
|
un_lock(self.data.get('chat_record_id'))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class ChatRecordSerializer(serializers.Serializer):
|
||||||
|
application_id = serializers.UUIDField(required=True, label=_('Application ID'))
|
||||||
|
chat_user_id = serializers.UUIDField(required=True, label=_('Chat User ID'))
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
chat_user_id = self.data.get('chat_user_id')
|
||||||
|
application_id = self.data.get("application_id")
|
||||||
|
return QuerySet(Chat).filter(application_id=application_id, chat_user_id=chat_user_id, is_deleted=False)
|
||||||
|
|
||||||
|
def list(self):
|
||||||
|
self.is_valid(raise_exception=True)
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
return [HistoryChatRecordModel(r).data for r in queryset]
|
||||||
|
|
||||||
|
def page(self, current_page, page_size):
|
||||||
|
self.is_valid(raise_exception=True)
|
||||||
|
return page_search(current_page, page_size, self.get_queryset(), lambda r: HistoryChatRecordModel(r).data)
|
||||||
|
|||||||
@ -13,4 +13,8 @@ urlpatterns = [
|
|||||||
path('open', views.OpenView.as_view()),
|
path('open', views.OpenView.as_view()),
|
||||||
path('captcha', views.CaptchaView.as_view(), name='captcha'),
|
path('captcha', views.CaptchaView.as_view(), name='captcha'),
|
||||||
path('vote/chat/<str:chat_id>/chat_record/<str:chat_record_id>', views.VoteView.as_view(), name='vote'),
|
path('vote/chat/<str:chat_id>/chat_record/<str:chat_record_id>', views.VoteView.as_view(), name='vote'),
|
||||||
|
path('historical_conversation', views.HistoricalConversationView.as_view(), name='historical_conversation'),
|
||||||
|
path('historical_conversation/<int:current_page>/<int:page_size>',
|
||||||
|
views.HistoricalConversationView.PageView.as_view(),
|
||||||
|
name='historical_conversation')
|
||||||
]
|
]
|
||||||
|
|||||||
@ -6,13 +6,14 @@
|
|||||||
@date:2025/6/23 10:42
|
@date:2025/6/23 10:42
|
||||||
@desc:
|
@desc:
|
||||||
"""
|
"""
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from drf_spectacular.utils import extend_schema
|
from drf_spectacular.utils import extend_schema
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from django.utils.translation import gettext_lazy as _
|
|
||||||
|
|
||||||
|
from chat.api.chat_api import HistoricalConversationAPI, PageHistoricalConversationAPI
|
||||||
from chat.api.vote_api import VoteAPI
|
from chat.api.vote_api import VoteAPI
|
||||||
from chat.serializers.chat_record import VoteSerializer
|
from chat.serializers.chat_record import VoteSerializer, ChatRecordSerializer
|
||||||
from common import result
|
from common import result
|
||||||
from common.auth import TokenAuth
|
from common.auth import TokenAuth
|
||||||
|
|
||||||
@ -35,3 +36,42 @@ class VoteView(APIView):
|
|||||||
data={'chat_id': chat_id,
|
data={'chat_id': chat_id,
|
||||||
'chat_record_id': chat_record_id
|
'chat_record_id': chat_record_id
|
||||||
}).vote(request.data))
|
}).vote(request.data))
|
||||||
|
|
||||||
|
|
||||||
|
class HistoricalConversationView(APIView):
|
||||||
|
authentication_classes = [TokenAuth]
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
methods=['GET'],
|
||||||
|
description=_("Get historical conversation"),
|
||||||
|
summary=_("Get historical conversation"),
|
||||||
|
operation_id=_("Get historical conversation"), # type: ignore
|
||||||
|
parameters=HistoricalConversationAPI.get_parameters(),
|
||||||
|
responses=HistoricalConversationAPI.get_response(),
|
||||||
|
tags=[_('Chat')] # type: ignore
|
||||||
|
)
|
||||||
|
def get(self, request: Request):
|
||||||
|
return result.success(ChatRecordSerializer(
|
||||||
|
data={
|
||||||
|
'application_id': request.auth.application_id,
|
||||||
|
'chat_user_id': request.auth.chat_user_id,
|
||||||
|
}).list())
|
||||||
|
|
||||||
|
class PageView(APIView):
|
||||||
|
authentication_classes = [TokenAuth]
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
methods=['GET'],
|
||||||
|
description=_("Get historical conversation by page"),
|
||||||
|
summary=_("Get historical conversation by page"),
|
||||||
|
operation_id=_("Get historical conversation by page"), # type: ignore
|
||||||
|
parameters=PageHistoricalConversationAPI.get_parameters(),
|
||||||
|
responses=PageHistoricalConversationAPI.get_response(),
|
||||||
|
tags=[_('Chat')] # type: ignore
|
||||||
|
)
|
||||||
|
def get(self, request: Request, current_page: int, page_size: int):
|
||||||
|
return result.success(ChatRecordSerializer(
|
||||||
|
data={
|
||||||
|
'application_id': request.auth.application_id,
|
||||||
|
'chat_user_id': request.auth.chat_user_id,
|
||||||
|
}).page(current_page, page_size))
|
||||||
|
|||||||
@ -182,6 +182,13 @@ const vote: (
|
|||||||
loading,
|
loading,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
const pageChat: (
|
||||||
|
current_page: number,
|
||||||
|
page_size: number,
|
||||||
|
loading?: Ref<boolean>,
|
||||||
|
) => Promise<Result<boolean>> = (current_page, page_size, loading) => {
|
||||||
|
return get(`/historical_conversation/${current_page}/${page_size}`, undefined, loading)
|
||||||
|
}
|
||||||
export default {
|
export default {
|
||||||
open,
|
open,
|
||||||
chat,
|
chat,
|
||||||
@ -200,4 +207,5 @@ export default {
|
|||||||
getAuthSetting,
|
getAuthSetting,
|
||||||
passwordAuthentication,
|
passwordAuthentication,
|
||||||
vote,
|
vote,
|
||||||
|
pageChat,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -153,6 +153,7 @@
|
|||||||
import { ref, onMounted, nextTick, computed } from 'vue'
|
import { ref, onMounted, nextTick, computed } from 'vue'
|
||||||
import { marked } from 'marked'
|
import { marked } from 'marked'
|
||||||
import { saveAs } from 'file-saver'
|
import { saveAs } from 'file-saver'
|
||||||
|
import chatAPI from '@/api/chat/chat'
|
||||||
import { isAppIcon } from '@/utils/common'
|
import { isAppIcon } from '@/utils/common'
|
||||||
import useStore from '@/stores'
|
import useStore from '@/stores'
|
||||||
import useResize from '@/layout/hooks/useResize'
|
import useResize from '@/layout/hooks/useResize'
|
||||||
@ -278,7 +279,7 @@ function getChatLog(id: string, refresh?: boolean) {
|
|||||||
page_size: 20,
|
page_size: 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
chatLog.asyncGetChatLogClient(id, page, left_loading).then((res: any) => {
|
chatAPI.pageChat(page.current_page, page.page_size, left_loading).then((res: any) => {
|
||||||
chatLogData.value = res.data.records
|
chatLogData.value = res.data.records
|
||||||
if (refresh) {
|
if (refresh) {
|
||||||
currentChatName.value = chatLogData.value?.[0]?.abstract
|
currentChatName.value = chatLogData.value?.[0]?.abstract
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user