Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized chat checking, if it will break #441

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chats/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand Down
23 changes: 23 additions & 0 deletions users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
Loading