Skip to content

Commit

Permalink
Apply Review
Browse files Browse the repository at this point in the history
  • Loading branch information
TriangleYJ authored and retroinspect committed Feb 6, 2024
1 parent 96df878 commit 4f0831a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
32 changes: 8 additions & 24 deletions apps/user/views/fcm.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
from django.db.models.functions import Now
from django.utils import timezone
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.user.models import FCMToken, FCMTopic
from ara.firebase import fcm_subscrible, fcm_unsubscrible

from rest_framework.permissions import IsAuthenticated
import typing

class FCMTokenView(APIView):
def patch(self, request, mode):
token = request.data["token"]
if mode == "delete":
FCMToken.objects.filter(token=token).delete()
pass
elif mode == "update":
if not request.user.is_authenticated:
return Response(status=status.HTTP_401_UNAUTHORIZED)
token = FCMToken(token=token, user=request.user, last_activated_at=Now())
token = FCMToken(token=token, user=request.user, last_activated_at=timezone.now())
token.save()
return Response(status=status.HTTP_200_OK)


class FCMTopicView(APIView):
def get(self, request):
# TODO: More better way for authentication guard?
if not request.user.is_authenticated:
return Response(status=status.HTTP_401_UNAUTHORIZED)
permission_classes = [IsAuthenticated]

# user_topics = tmp_topic_storage.get(str(request.user.id))
def get(self, request):
user_topics = (
FCMTopic.objects.filter(user=request.user)
.values_list("topic", flat=True)
Expand All @@ -36,24 +33,11 @@ def get(self, request):
return Response(user_topics)

def patch(self, request):
if not request.user.is_authenticated:
return Response(status=status.HTTP_401_UNAUTHORIZED)

topic_put_list: list[str] = request.data.get("put")
topic_delete_list: list[str] = request.data.get("delete")
# print(topic_put_list, topic_delete_list)
topic_put_list: typing.List[str] = request.data.get("put")
topic_delete_list: typing.List[str] = request.data.get("delete")
# TODO: santize user topic list to available topics
user_id = str(request.user.id)

# tmp_topic_storage = {
# '1': set(['board_13', 'board_17', 'portal_popular', 'article_8148']),
# }
# if tmp_topic_storage.get(user_id) == None:
# tmp_topic_storage[user_id] = set()
# user_topics = tmp_topic_storage[user_id]
# user_topics.update(topic_put_list)
# user_topics.difference_update(topic_delete_list)

user_tokens = list(
FCMToken.objects.filter(user=request.user)
.values_list("token", flat=True)
Expand Down
1 change: 0 additions & 1 deletion ara/firebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def fcm_notify_user(user, title, body, open_url):
fcm_simple(title, body, open_url, token=i.token)
except:
FCMToken.objects.filter(token=i.token).delete()
pass


def fcm_simple(title="Title", body="Body", open_url="/", **kwargs):
Expand Down

0 comments on commit 4f0831a

Please sign in to comment.