Skip to content

Commit

Permalink
Merge pull request #73 from Macktireh/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Macktireh authored Oct 1, 2023
2 parents 1947245 + 58deb64 commit ebb0a76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions apps/friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
)
Expand Down Expand Up @@ -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:

Expand All @@ -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,
)
Expand Down
23 changes: 23 additions & 0 deletions apps/notifications/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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",
)

0 comments on commit ebb0a76

Please sign in to comment.