Chat Serializers Documentation
Overview
This document explains the functionality and purpose of the serializers used for the Chat and Message models. The serializers ensure data validation, structure the data for API responses, and handle logic for creating and updating these models in your chat application.
UserSerializer
Purpose
The UserSerializer serializes user-related data and determines whether the user is a vendor or a regular user.
Key Features
- Fields: Includes
id,email,photo, anduser_type. - User Type: A
get_user_typemethod determines if the user is associated with a vendor and returns"vendor"or"user"accordingly.
VendorSerializer
Purpose
The VendorSerializer handles the serialization of vendor-related data, including associated user details and shop images.
Key Features
- Nested User Data: Includes user details using the
UserSerializer. - Shop Image: Dynamically retrieves and constructs the absolute URL for the vendor’s shop image.
MessageinChatSerializer
Purpose
This serializer provides a detailed representation of messages within a chat, including sender and receiver details and associated files.
Key Features
- Fields: Includes
id,chat,sender,receiver,message_type,content,image,audio,video,is_read,created_at, andupdated_at. - File URLs: Dynamically generates absolute URLs for uploaded files (images, audio, videos).
- Nested Sender and Receiver: Uses
UserSerializerto provide detailed information about the sender and receiver.
ChatSerializer
Purpose
The ChatSerializer manages the serialization of chat-related data, including nested vendor, user, and message details.
Key Features
- Fields: Includes
id,vendor,user,messages,created_at, andupdated_at. - Messages: Uses a
get_messagesmethod to serialize all messages in the chat, sorted by creation time. - Validation:
- Ensures that
user_idandvendor_iddo not refer to the same individual. - Validates that both the user and vendor exist in the database.
- Checks authorization for accessing or creating a chat.
- Create Method: Handles the creation of a chat while ensuring uniqueness using the
vendor_idanduser_id.
MessageSerializer
Purpose
The MessageSerializer handles the creation and validation of individual messages within a chat.
Key Features
- Fields: Includes
id,chat,sender,receiver,message_type,content,image,audio,video,is_read,created_at, andupdated_at. - Validation:
- Ensures that the sender is authorized to send messages within the given chat.
- Allows
is_readto be set toTruebut prevents it from being set toFalseexplicitly. - Validates the presence of the
chatfield during creation. - File Validation: Dynamically validates and processes files based on the message type.
Relationships
- Chat and Messages:
- A
Chatcontains multipleMessageinstances, serialized using theMessageinChatSerializer. - Vendor and User:
ChatSerializerensures that both vendor and user information is included.- File Attachments:
MessageinChatSerializerdynamically generates URLs for files like images, audio, and videos.
Summary
These serializers ensure robust and validated communication between the API and the Chat and Message models. They handle data formatting, authorization checks, and nested serialization for an enhanced and secure user experience.