|
| 1 | +""" |
| 2 | + @project: MaxKB |
| 3 | + @Author: niu |
| 4 | + @file: application_chat_link.py |
| 5 | + @date: 2026/2/9 10:50 |
| 6 | + @desc: |
| 7 | +""" |
| 8 | +from django.utils.translation import gettext_lazy as _ |
| 9 | +from rest_framework import serializers |
| 10 | + |
| 11 | +from application.models import Chat, ChatShareLink, ShareLinkType, ChatRecord |
| 12 | +from common.exception.app_exception import AppApiException |
| 13 | +from common.utils.chat_link_code import UUIDEncoder |
| 14 | +import uuid_utils.compat as uuid |
| 15 | + |
| 16 | + |
| 17 | +class ShareChatRecordModelSerializer(serializers.ModelSerializer): |
| 18 | + class Meta: |
| 19 | + model = ChatRecord |
| 20 | + fields = ['id', 'problem_text', 'answer_text', 'answer_text_list', 'create_time'] |
| 21 | + |
| 22 | +class ChatRecordShareLinkRequestSerializer(serializers.Serializer): |
| 23 | + chat_record_ids = serializers.ListSerializer( |
| 24 | + child=serializers.UUIDField(), |
| 25 | + required=False, |
| 26 | + allow_empty=False, |
| 27 | + label=_("Chat record IDs") |
| 28 | + ) |
| 29 | + is_current_all = serializers.BooleanField(required=False, default=False) |
| 30 | + |
| 31 | + def validate(self, attrs): |
| 32 | + if not attrs.get('is_current_all') and not attrs.get('chat_record_ids'): |
| 33 | + raise serializers.ValidationError(_('Chat record ids can not be empty')) |
| 34 | + return attrs |
| 35 | + |
| 36 | +class ChatRecordShareLinkSerializer(serializers.Serializer): |
| 37 | + chat_id = serializers.UUIDField(required=True, label=_("Conversation ID")) |
| 38 | + workspace_id = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_("Workspace ID")) |
| 39 | + application_id = serializers.UUIDField(required=True, label=_("Application ID")) |
| 40 | + user_id = serializers.UUIDField(required=False, label=_("User ID")) |
| 41 | + |
| 42 | + def is_valid(self, *, raise_exception=False): |
| 43 | + super().is_valid(raise_exception=True) |
| 44 | + chat_id = self.data.get('chat_id') |
| 45 | + application_id = self.data.get('application_id') |
| 46 | + |
| 47 | + chat_query_set = Chat.objects.filter(id=chat_id, application_id=application_id, is_deleted=False) |
| 48 | + if not chat_query_set.exists(): |
| 49 | + raise AppApiException(500, _('Chat id does not exist')) |
| 50 | + |
| 51 | + def generate_link(self, instance, with_valid=True): |
| 52 | + if with_valid: |
| 53 | + request_serializer = ChatRecordShareLinkRequestSerializer(data=instance) |
| 54 | + request_serializer.is_valid(raise_exception=True) |
| 55 | + self.is_valid(raise_exception=True) |
| 56 | + if not instance.get('is_current_all', False): |
| 57 | + chat_record_ids: list[str] = instance.get('chat_record_ids') |
| 58 | + |
| 59 | + record_count = ChatRecord.objects.filter(id__in=chat_record_ids, chat_id=self.data.get('chat_id')).count() |
| 60 | + if record_count != len(chat_record_ids): |
| 61 | + raise AppApiException(500, _('Invalid chat record ids')) |
| 62 | + chat_id = self.data.get('chat_id') |
| 63 | + application_id = self.data.get('application_id') |
| 64 | + user_id = self.data.get('user_id') |
| 65 | + |
| 66 | + is_current_all = instance.get('is_current_all', False) |
| 67 | + if is_current_all: |
| 68 | + sorted_ids = list( |
| 69 | + ChatRecord.objects.filter(chat_id=chat_id).order_by('create_time').values_list('id',flat=True) |
| 70 | + ) |
| 71 | + else: |
| 72 | + chat_record_ids: list[str] = instance.get('chat_record_ids') |
| 73 | + sorted_ids = list(ChatRecord.objects.filter(id__in=chat_record_ids).order_by('create_time').values_list('id',flat=True)) |
| 74 | + |
| 75 | + existing = ChatShareLink.objects.filter( |
| 76 | + chat_id=chat_id, application_id=application_id, |
| 77 | + share_type=ShareLinkType.PUBLIC, |
| 78 | + user_id=user_id, |
| 79 | + chat_record_ids=sorted_ids |
| 80 | + ).first() |
| 81 | + |
| 82 | + if existing: |
| 83 | + return {'link': UUIDEncoder.encode(existing.id)} |
| 84 | + |
| 85 | + chat_share_link_model = ChatShareLink( |
| 86 | + id=uuid.uuid7(), |
| 87 | + chat_id=chat_id, |
| 88 | + application_id=application_id, |
| 89 | + share_type=ShareLinkType.PUBLIC, |
| 90 | + user_id=user_id, |
| 91 | + chat_record_ids=sorted_ids |
| 92 | + ) |
| 93 | + chat_share_link_model.save() |
| 94 | + |
| 95 | + link = UUIDEncoder.encode(chat_share_link_model.id) |
| 96 | + |
| 97 | + return {'link': link} |
| 98 | + |
| 99 | + |
| 100 | +class ChatShareLinkDetailSerializer(serializers.Serializer): |
| 101 | + link = serializers.CharField(required=True, label=_("Link")) |
| 102 | + |
| 103 | + def is_valid(self, *, raise_exception=False): |
| 104 | + super().is_valid(raise_exception=True) |
| 105 | + |
| 106 | + link = self.data.get('link') |
| 107 | + share_link_id = UUIDEncoder.decode_to_str(link) |
| 108 | + |
| 109 | + share_link_query_set = ChatShareLink.objects.filter(id=share_link_id).first() |
| 110 | + if not share_link_query_set: |
| 111 | + raise AppApiException(500, _('Share link does not exist')) |
| 112 | + if share_link_query_set.chat.is_deleted: |
| 113 | + raise AppApiException(500, _('Chat has been deleted')) |
| 114 | + |
| 115 | + return share_link_query_set |
| 116 | + |
| 117 | + def get_record_list(self): |
| 118 | + share_link_model = self.is_valid(raise_exception=True) |
| 119 | + |
| 120 | + chat_record_model_list = ChatRecord.objects.filter(id__in=share_link_model.chat_record_ids, |
| 121 | + chat_id=share_link_model.chat_id).order_by('create_time') |
| 122 | + |
| 123 | + abstract = Chat.objects.filter( |
| 124 | + id=share_link_model.chat_id |
| 125 | + ).values_list('abstract', flat=True).first() |
| 126 | + chat_record_list = ShareChatRecordModelSerializer(chat_record_model_list, many=True).data |
| 127 | + |
| 128 | + return { |
| 129 | + 'abstract': abstract, |
| 130 | + 'chat_record_list': chat_record_list |
| 131 | + } |
0 commit comments