Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions apps/application/serializers/chat_message_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,21 @@ def get_message(instance):
return instance.get('messages')[-1].get('content')

@staticmethod
def generate_chat(chat_id, application_id, message, client_id):
def generate_chat(chat_id, application_id, message, client_id, asker=None):
if chat_id is None:
chat_id = str(uuid.uuid1())
chat = QuerySet(Chat).filter(id=chat_id).first()
if chat is None:
Chat(id=chat_id, application_id=application_id, abstract=message[0:1024], client_id=client_id).save()
asker_dict = {'user_name': '游客'}
if asker is not None:
if isinstance(asker, str):
asker_dict = {
'user_name': asker
}
elif isinstance(asker, dict):
asker_dict = asker
Chat(id=chat_id, application_id=application_id, abstract=message[0:1024], client_id=client_id,
asker=asker_dict).save()
return chat_id

def chat(self, instance: Dict, with_valid=True):
Expand All @@ -232,7 +241,8 @@ def chat(self, instance: Dict, with_valid=True):
application_id = self.data.get('application_id')
client_id = self.data.get('client_id')
client_type = self.data.get('client_type')
chat_id = self.generate_chat(chat_id, application_id, message, client_id)
chat_id = self.generate_chat(chat_id, application_id, message, client_id,
asker=instance.get('form_data', {}).get("asker"))
return ChatMessageSerializer(
data={
'chat_id': chat_id, 'message': message,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The asker parameter was added to the generate_chat method, so you need to handle it accordingly in all parts of the code where this function is called.

  2. In the constructor (__init__) of the class, self.client_id should be set based on self.data['client_id'] or None.

  3. It's unclear whether ChatMessageSerializer requires additional initialization parameters beyond what it takes from the serializer itself (e.g., data). If so, consider adding them here.

  4. Ensure that the input dictionary self.data does not contain null values for required keys like chat_id, application_id, and message. If they aren't optional, handle their absence gracefully.

  5. Consider using type hints throughout the code to improve readability and maintainability. For example, change the line defining self.data: Dict[str, Any] = {} into self.data: Optional[Dict[str, Union[str, int]]] = {} for better understanding about its expected content.

  6. When handling user input via APIs, remember to validate it before processing to prevent attacks such as SQL injection, cross-site scripting (XSS), and other security vulnerabilities.

  7. Review how QuerySet(Chat) interacts with other parts of your API to ensure proper database queries are executed when retrieving Chat objects.

  8. Since this code handles messages and potentially sensitive information, implement appropriate logging around critical operations within try-except blocks to capture errors cleanly for debugging purposes.

Expand Down