Skip to content

Commit

Permalink
Merge pull request #42 from bertt6/message
Browse files Browse the repository at this point in the history
messages pagination and rate limit
  • Loading branch information
muratkaanmesum authored Apr 21, 2024
2 parents 59bbd94 + 57ac81a commit 35b12dd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
16 changes: 15 additions & 1 deletion API/API/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
),
'DEFAULT_THROTTLE_CLASSES': [

'rest_framework.throttling.AnonRateThrottle',

'rest_framework.throttling.UserRateThrottle'

],
'DEFAULT_THROTTLE_RATES': {

'anon': '50/min',

'user': '100/min'

}
}


Expand Down
2 changes: 0 additions & 2 deletions API/Apps/Chat/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class Meta:
fields = "__all__"

class RoomSerializer(serializers.ModelSerializer):
messages = MessageSerializer(read_only=True, many=True)

class Meta:
model = Room
fields = '__all__'
3 changes: 2 additions & 1 deletion API/Apps/Chat/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# chat/urls.py
from django.urls import path
from .views import start_chat
from .views import start_chat, get_messages

urlpatterns = [
path('start-chat', start_chat, name='start-chat'),
path('get-messages/<uuid:room_id>', get_messages, name='get-messages'),
]
21 changes: 18 additions & 3 deletions API/Apps/Chat/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from django.contrib.auth.models import User
from django.db.models import Q
from rest_framework.decorators import api_view, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from Apps.Chat.api.serializers import RoomSerializer
from Apps.Chat.models import Room
from Apps.Chat.api.serializers import RoomSerializer, MessageSerializer
from Apps.Chat.models import Room, Message
from Apps.Profile.models import Profile


Expand All @@ -25,4 +26,18 @@ def start_chat(request):
except Room.DoesNotExist:
room = Room.objects.create(first_user=request.user.profile, second_user=second_user)
room_serializer = RoomSerializer(room)
return Response({"success": True, "room": room_serializer.data}, status=200)
return Response({"success": True, "room": room_serializer.data}, status=200)


@api_view(["GET"])
@permission_classes([IsAuthenticated])
def get_messages(request, room_id):
try:
messages = Message.objects.filter(room=room_id)
paginator = PageNumberPagination()
paginator.page_size = 10
paginated_data = paginator.paginate_queryset(messages, request)
serializer = MessageSerializer(paginated_data, many=True)
return paginator.get_paginated_response({"success": True, "messages": serializer.data})
except Message.DoesNotExist:
return Response({"error": "Messages not found"}, status=404)

0 comments on commit 35b12dd

Please sign in to comment.