2024-07-01 18:10:40 +02:00
|
|
|
"""Client serializers for the Meet core app."""
|
2024-07-30 16:48:26 +02:00
|
|
|
|
2025-09-01 10:43:38 +02:00
|
|
|
# pylint: disable=W0223
|
|
|
|
|
|
2024-06-25 00:21:36 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
from rest_framework import serializers
|
2024-06-25 00:21:36 +02:00
|
|
|
from rest_framework.exceptions import PermissionDenied
|
2025-04-22 19:15:05 +02:00
|
|
|
from timezone_field.rest_framework import TimeZoneSerializerField
|
2024-01-09 15:30:36 +01:00
|
|
|
|
2024-06-25 19:07:34 +02:00
|
|
|
from core import models, utils
|
2024-01-09 15:30:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize users."""
|
|
|
|
|
|
2025-04-22 19:15:05 +02:00
|
|
|
timezone = TimeZoneSerializerField()
|
|
|
|
|
|
2024-01-09 15:30:36 +01:00
|
|
|
class Meta:
|
|
|
|
|
model = models.User
|
2025-04-22 19:15:05 +02:00
|
|
|
fields = ["id", "email", "full_name", "short_name", "timezone", "language"]
|
2024-11-13 11:43:14 +01:00
|
|
|
read_only_fields = ["id", "email", "full_name", "short_name"]
|
2024-06-25 00:21:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceAccessSerializerMixin:
|
|
|
|
|
"""
|
|
|
|
|
A serializer mixin to share controlling that the logged-in user submitting a room access object
|
|
|
|
|
is administrator on the targeted room.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-boolean-expressions
|
|
|
|
|
def validate(self, data):
|
|
|
|
|
"""
|
|
|
|
|
Check access rights specific to writing (create/update)
|
|
|
|
|
"""
|
|
|
|
|
request = self.context.get("request", None)
|
|
|
|
|
user = getattr(request, "user", None)
|
|
|
|
|
if (
|
|
|
|
|
# Update
|
|
|
|
|
self.instance
|
|
|
|
|
and (
|
2025-05-27 16:13:42 +02:00
|
|
|
data.get("role") == models.RoleChoices.OWNER
|
2024-06-25 00:21:36 +02:00
|
|
|
and not self.instance.resource.is_owner(user)
|
|
|
|
|
or self.instance.role == models.RoleChoices.OWNER
|
2025-06-30 13:43:03 +02:00
|
|
|
and self.instance.user != user
|
2024-06-25 00:21:36 +02:00
|
|
|
)
|
|
|
|
|
) or (
|
|
|
|
|
# Create
|
|
|
|
|
not self.instance
|
|
|
|
|
and data.get("role") == models.RoleChoices.OWNER
|
|
|
|
|
and not data["resource"].is_owner(user)
|
|
|
|
|
):
|
|
|
|
|
raise PermissionDenied(
|
|
|
|
|
"Only owners of a room can assign other users as owners."
|
|
|
|
|
)
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def validate_resource(self, resource):
|
|
|
|
|
"""The logged-in user must be administrator of the resource."""
|
|
|
|
|
request = self.context.get("request", None)
|
|
|
|
|
user = getattr(request, "user", None)
|
|
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
if not (
|
|
|
|
|
user and user.is_authenticated and resource.is_administrator_or_owner(user)
|
|
|
|
|
):
|
2024-06-25 00:21:36 +02:00
|
|
|
raise PermissionDenied(
|
|
|
|
|
_("You must be administrator or owner of a room to add accesses to it.")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return resource
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceAccessSerializer(
|
|
|
|
|
ResourceAccessSerializerMixin, serializers.ModelSerializer
|
|
|
|
|
):
|
|
|
|
|
"""Serialize Room to User accesses for the API."""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.ResourceAccess
|
|
|
|
|
fields = ["id", "user", "resource", "role"]
|
|
|
|
|
read_only_fields = ["id"]
|
|
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
|
"""Make "user" and "resource" fields readonly but only on update."""
|
|
|
|
|
validated_data.pop("resource", None)
|
|
|
|
|
validated_data.pop("user", None)
|
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NestedResourceAccessSerializer(ResourceAccessSerializer):
|
|
|
|
|
"""Serialize Room accesses for the API with full nested user."""
|
|
|
|
|
|
|
|
|
|
user = UserSerializer(read_only=True)
|
|
|
|
|
|
|
|
|
|
|
2024-11-06 17:00:23 +01:00
|
|
|
class ListRoomSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize Room model for a list API endpoint."""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.Room
|
2025-02-15 14:08:40 +01:00
|
|
|
fields = ["id", "name", "slug", "access_level"]
|
2024-11-06 17:00:23 +01:00
|
|
|
read_only_fields = ["id", "slug"]
|
|
|
|
|
|
|
|
|
|
|
2024-06-25 00:21:36 +02:00
|
|
|
class RoomSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize Room model for the API."""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.Room
|
2025-07-01 17:58:52 +02:00
|
|
|
fields = ["id", "name", "slug", "configuration", "access_level", "pin_code"]
|
|
|
|
|
read_only_fields = ["id", "slug", "pin_code"]
|
2024-06-25 00:21:36 +02:00
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
"""
|
|
|
|
|
Add users only for administrator users.
|
|
|
|
|
Add LiveKit credentials for public instance or related users/groups
|
|
|
|
|
"""
|
|
|
|
|
output = super().to_representation(instance)
|
|
|
|
|
request = self.context.get("request")
|
|
|
|
|
|
|
|
|
|
if not request:
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
role = instance.get_role(request.user)
|
2025-06-23 17:17:02 +02:00
|
|
|
is_admin_or_owner = models.RoleChoices.check_administrator_role(
|
|
|
|
|
role
|
|
|
|
|
) or models.RoleChoices.check_owner_role(role)
|
2024-06-25 00:21:36 +02:00
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
if is_admin_or_owner:
|
2024-06-25 00:21:36 +02:00
|
|
|
access_serializer = NestedResourceAccessSerializer(
|
|
|
|
|
instance.accesses.select_related("resource", "user").all(),
|
|
|
|
|
context=self.context,
|
|
|
|
|
many=True,
|
|
|
|
|
)
|
|
|
|
|
output["accesses"] = access_serializer.data
|
|
|
|
|
|
2025-08-25 19:04:26 +02:00
|
|
|
configuration = output["configuration"]
|
|
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
if not is_admin_or_owner:
|
2024-06-25 00:21:36 +02:00
|
|
|
del output["configuration"]
|
|
|
|
|
|
2025-03-04 10:52:24 +01:00
|
|
|
should_access_room = (
|
|
|
|
|
(
|
|
|
|
|
instance.access_level == models.RoomAccessLevel.TRUSTED
|
|
|
|
|
and request.user.is_authenticated
|
|
|
|
|
)
|
|
|
|
|
or role is not None
|
|
|
|
|
or instance.is_public
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if should_access_room:
|
2025-02-18 17:44:14 +01:00
|
|
|
room_id = f"{instance.id!s}"
|
2024-08-07 16:25:54 +02:00
|
|
|
username = request.query_params.get("username", None)
|
2025-02-18 17:44:14 +01:00
|
|
|
output["livekit"] = utils.generate_livekit_config(
|
2025-08-25 19:04:26 +02:00
|
|
|
room_id=room_id,
|
|
|
|
|
user=request.user,
|
|
|
|
|
username=username,
|
|
|
|
|
configuration=configuration,
|
|
|
|
|
is_admin_or_owner=is_admin_or_owner,
|
2025-02-18 17:44:14 +01:00
|
|
|
)
|
2024-06-25 00:21:36 +02:00
|
|
|
|
2025-06-23 17:17:02 +02:00
|
|
|
output["is_administrable"] = is_admin_or_owner
|
2024-06-25 00:21:36 +02:00
|
|
|
|
|
|
|
|
return output
|
2024-11-06 17:00:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RecordingSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize Recording for the API."""
|
|
|
|
|
|
|
|
|
|
room = ListRoomSerializer(read_only=True)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.Recording
|
2025-04-23 15:36:29 +02:00
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"room",
|
|
|
|
|
"created_at",
|
|
|
|
|
"updated_at",
|
|
|
|
|
"status",
|
|
|
|
|
"mode",
|
|
|
|
|
"key",
|
|
|
|
|
"is_expired",
|
|
|
|
|
"expired_at",
|
|
|
|
|
]
|
2024-11-06 17:00:23 +01:00
|
|
|
read_only_fields = fields
|
2024-11-08 10:32:50 +01:00
|
|
|
|
|
|
|
|
|
2025-09-01 10:43:38 +02:00
|
|
|
class BaseValidationOnlySerializer(serializers.Serializer):
|
|
|
|
|
"""Base serializer for validation-only operations."""
|
|
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
|
"""Not implemented as this is a validation-only serializer."""
|
|
|
|
|
raise NotImplementedError(f"{self.__class__.__name__} is validation-only")
|
|
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
|
"""Not implemented as this is a validation-only serializer."""
|
|
|
|
|
raise NotImplementedError(f"{self.__class__.__name__} is validation-only")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StartRecordingSerializer(BaseValidationOnlySerializer):
|
2024-11-08 10:32:50 +01:00
|
|
|
"""Validate start recording requests."""
|
|
|
|
|
|
|
|
|
|
mode = serializers.ChoiceField(
|
|
|
|
|
choices=models.RecordingModeChoices.choices,
|
|
|
|
|
required=True,
|
|
|
|
|
error_messages={
|
|
|
|
|
"required": "Recording mode is required.",
|
|
|
|
|
"invalid_choice": "Invalid recording mode. Choose between "
|
|
|
|
|
"screen_recording or transcript.",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-18 22:09:02 +01:00
|
|
|
|
2025-09-01 10:43:38 +02:00
|
|
|
class RequestEntrySerializer(BaseValidationOnlySerializer):
|
2025-02-18 22:09:02 +01:00
|
|
|
"""Validate request entry data."""
|
|
|
|
|
|
|
|
|
|
username = serializers.CharField(required=True)
|
|
|
|
|
|
|
|
|
|
|
2025-09-01 10:43:38 +02:00
|
|
|
class ParticipantEntrySerializer(BaseValidationOnlySerializer):
|
2025-02-18 22:09:02 +01:00
|
|
|
"""Validate participant entry decision data."""
|
|
|
|
|
|
2025-08-26 16:17:20 +02:00
|
|
|
participant_id = serializers.UUIDField(required=True)
|
2025-02-18 22:09:02 +01:00
|
|
|
allow_entry = serializers.BooleanField(required=True)
|
|
|
|
|
|
2025-03-28 19:59:39 +01:00
|
|
|
|
2025-09-01 10:43:38 +02:00
|
|
|
class CreationCallbackSerializer(BaseValidationOnlySerializer):
|
2025-03-28 19:59:39 +01:00
|
|
|
"""Validate room creation callback data."""
|
|
|
|
|
|
|
|
|
|
callback_id = serializers.CharField(required=True)
|
|
|
|
|
|
2025-04-15 16:13:18 +02:00
|
|
|
|
|
|
|
|
class RoomInviteSerializer(serializers.Serializer):
|
|
|
|
|
"""Validate room invite creation data."""
|
|
|
|
|
|
|
|
|
|
emails = serializers.ListField(child=serializers.EmailField(), allow_empty=False)
|