diff --git a/apps/friends/views.py b/apps/friends/views.py index a173df8..aaf225e 100644 --- a/apps/friends/views.py +++ b/apps/friends/views.py @@ -84,7 +84,7 @@ def invites_list_profiles_view(request: HttpRequest) -> HttpResponse: page = request.GET.get("page") if page is None: page = 1 - context = cache.get(f"list_relation_receiver_and_sender_context_{page}") + context = cache.get(f"list_relation_receiver_and_sender_context_{page}_{request.user.id}") if context is None: try: @@ -103,7 +103,7 @@ def invites_list_profiles_view(request: HttpRequest) -> HttpResponse: ) try: cache.set( - f"list_relation_receiver_and_sender_context_{page}", + f"list_relation_receiver_and_sender_context_{page}_{request.user.id}", context, 60 * 60 * 24, ) @@ -144,7 +144,7 @@ def my_friends_invites_profiles_view(request): page = request.GET.get("page") if page is None: page = 1 - context = cache.get(f"my_friends_invites_profiles_view_context_{page}") + context = cache.get(f"my_friends_invites_profiles_view_context_{page}_{request.user.id}") if context is None: @@ -171,7 +171,7 @@ def my_friends_invites_profiles_view(request): context.update(statistic_relationship_receiver(request)) try: cache.set( - f"my_friends_invites_profiles_view_context_{page}", + f"my_friends_invites_profiles_view_context_{page}_{request.user.id}", context, 60 * 60 * 24, ) diff --git a/apps/notifications/views.py b/apps/notifications/views.py index 43e4e36..47b82be 100644 --- a/apps/notifications/views.py +++ b/apps/notifications/views.py @@ -1,15 +1,18 @@ import asyncio +from collections.abc import AsyncGenerator from asgiref.sync import async_to_sync, sync_to_async from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import ( + HttpRequest, HttpResponse, HttpResponseNotFound, HttpResponsePermanentRedirect, HttpResponseRedirect, JsonResponse, + StreamingHttpResponse, ) from django.shortcuts import get_object_or_404, redirect, render @@ -105,3 +108,23 @@ def seen_notification( elif qs.type_notif == "invitation_send": return redirect("friends:invitation_received") return redirect("notifications:notif") + + +async def stream_notifications() -> AsyncGenerator[str, None]: + latest_notif = None + + while True: + current_notif = await Notification.objects.order_by("-id").afirst() + + # If we have a new message yield that + if latest_notif != current_notif: + yield "data: {current_notif.text}\n\n" + latest_notif = current_notif + + await asyncio.sleep(3) + +async def stream_notifications_view(request: HttpRequest) -> StreamingHttpResponse: + return StreamingHttpResponse( + streaming_content=stream_notifications(), + content_type="text/event-stream", + )