Skip to content

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, and user_type.
  • User Type: A get_user_type method 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, and updated_at.
  • File URLs: Dynamically generates absolute URLs for uploaded files (images, audio, videos).
  • Nested Sender and Receiver: Uses UserSerializer to 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, and updated_at.
  • Messages: Uses a get_messages method to serialize all messages in the chat, sorted by creation time.
  • Validation:
  • Ensures that user_id and vendor_id do 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_id and user_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, and updated_at.
  • Validation:
  • Ensures that the sender is authorized to send messages within the given chat.
  • Allows is_read to be set to True but prevents it from being set to False explicitly.
  • Validates the presence of the chat field during creation.
  • File Validation: Dynamically validates and processes files based on the message type.

Relationships

  1. Chat and Messages:
  2. A Chat contains multiple Message instances, serialized using the MessageinChatSerializer.
  3. Vendor and User:
  4. ChatSerializer ensures that both vendor and user information is included.
  5. File Attachments:
  6. MessageinChatSerializer dynamically 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.