Renames PK/reference fields across chat models, services, serializers, views, urls, websocket routing, and events (id->uuid, user_id->user_uuid, chat_id->chat_uuid, sender_id->sender_uuid, user_1_id/user_2_id-> user_1_uuid/user_2_uuid) for consistency with the uuid-named PK convention already used in core/users/gooyal_oauth2. Mattermost's own opaque identifiers (post_id, channel_id, mattermost_user_id) are left unchanged since they are not UUIDs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import json
|
|
from uuid import UUID
|
|
|
|
from django.conf import settings
|
|
from django_redis import get_redis_connection
|
|
from drf_spectacular.utils import extend_schema
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from apps.chat.serializers.messages import MessageSerializer
|
|
from apps.chat.serializers.read_state import MarkReadSerializer, ReadStateSerializer
|
|
from apps.chat.services.read_state import ReadStateService
|
|
|
|
|
|
class ReadStateView(APIView):
|
|
authentication_classes = []
|
|
permission_classes = []
|
|
|
|
@extend_schema(
|
|
request=MarkReadSerializer,
|
|
responses={200: ReadStateSerializer},
|
|
)
|
|
def post(self, request, chat_uuid):
|
|
serializer = MarkReadSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
data = serializer.validated_data
|
|
|
|
read_state = ReadStateService().mark_read(
|
|
conversation_uuid=UUID(str(chat_uuid)),
|
|
user_uuid=data["user_uuid"],
|
|
post_id=data["post_id"],
|
|
)
|
|
|
|
return Response(
|
|
ReadStateSerializer(
|
|
{
|
|
"conversation_uuid": read_state.conversation_id,
|
|
"user_uuid": read_state.user_uuid,
|
|
"has_unread": False,
|
|
}
|
|
).data
|
|
)
|
|
|
|
|
|
class ChatEventsView(APIView):
|
|
authentication_classes = []
|
|
permission_classes = []
|
|
|
|
@extend_schema(
|
|
responses={200: MessageSerializer(many=True)},
|
|
description=(
|
|
"Long-poll endpoint. Blocks up to CHAT_LONG_POLL_TIMEOUT_SECONDS "
|
|
"waiting for a new message event. Returns immediately when an event "
|
|
"arrives; returns an empty list on timeout."
|
|
),
|
|
)
|
|
def get(self, request, chat_uuid):
|
|
timeout = getattr(settings, "CHAT_LONG_POLL_TIMEOUT_SECONDS", 25)
|
|
|
|
redis = get_redis_connection("default")
|
|
key = f"chat_events:{chat_uuid}"
|
|
|
|
result = redis.blpop(key, timeout=timeout)
|
|
|
|
if result is None:
|
|
return Response([])
|
|
|
|
_, raw = result
|
|
event = json.loads(raw)
|
|
return Response([event])
|