diff --git a/chats/serializers.py b/chats/serializers.py index 9c6200b0..75d7df13 100644 --- a/chats/serializers.py +++ b/chats/serializers.py @@ -7,7 +7,7 @@ ProjectChatMessage, ) from files.serializers import UserFileSerializer -from users.serializers import UserListSerializer, UserDetailSerializer +from users.serializers import UserListSerializer, UserDetailSerializer, UserChatSerializer class DirectChatListSerializer(serializers.ModelSerializer): @@ -128,7 +128,8 @@ class Meta: class DirectChatMessageListSerializer(serializers.ModelSerializer): - author = UserDetailSerializer() + # author = UserDetailSerializer() + author = UserChatSerializer() reply_to = DirectChatMessageSerializer(allow_null=True) files = serializers.SerializerMethodField() diff --git a/users/serializers.py b/users/serializers.py index c7caf56b..a255571c 100644 --- a/users/serializers.py +++ b/users/serializers.py @@ -349,6 +349,29 @@ def update(self, instance, validated_data): return instance +class UserChatSerializer(serializers.ModelSerializer[CustomUser]): + is_online = serializers.SerializerMethodField() + + def get_is_online(self, user: CustomUser): + request = self.context.get("request") + if request and request.user.is_authenticated and request.user.id == user.id: + return True + cache_key = get_user_online_cache_key(user) + return cache.get(cache_key, False) + + class Meta: + model = CustomUser + fields = [ + "id", + "first_name", + "last_name", + "patronymic", + "avatar", + "is_active", + "is_online", + ] + + class UserListSerializer( serializers.ModelSerializer[CustomUser], SkillsWriteSerializerMixin ):