From dcde6b2567eeeef652c594e0b8e2476785fca394 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Thu, 22 Jun 2023 21:31:30 +0530 Subject: [PATCH] dev: notification endpoints and fix bg worker for saving notifications --- apiserver/plane/api/views/notification.py | 29 +++++++++++++++++++ .../plane/bgtasks/issue_activites_task.py | 9 ++++-- apiserver/plane/db/models/notification.py | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/api/views/notification.py b/apiserver/plane/api/views/notification.py index add3c6994..1ccd43212 100644 --- a/apiserver/plane/api/views/notification.py +++ b/apiserver/plane/api/views/notification.py @@ -1,6 +1,7 @@ # Third party imports from rest_framework import status from rest_framework.response import Response +from sentry_sdk import capture_exception # Module imports from .base import BaseViewSet @@ -18,7 +19,35 @@ class NotificationViewSet(BaseViewSet): .get_queryset() .filter( workspace__slug=self.kwargs.get("slug"), + receiver_id=self.request.user.id, ) .select_related("workspace") ) + def partial_update(self, request, slug, pk): + try: + notification = Notification.objects.get( + workspace__slug=slug, pk=pk, receiver=request.user + ) + # Only read_at and snoozed_till can be updated + notification_data = { + "read_at": request.data.get("read_at", None), + "snoozed_till": request.data.get("snoozed_till", None), + } + serializer = NotificationSerializer(notification, data=notification_data, partial=True) + + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + except Notification.DoesNotExist: + return Response( + {"error": "Notification does not exists"}, + status=status.HTTP_400_BAD_REQUEST, + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) diff --git a/apiserver/plane/bgtasks/issue_activites_task.py b/apiserver/plane/bgtasks/issue_activites_task.py index 9e4f9475f..f14d8ddc8 100644 --- a/apiserver/plane/bgtasks/issue_activites_task.py +++ b/apiserver/plane/bgtasks/issue_activites_task.py @@ -1014,9 +1014,9 @@ def issue_activity( bulk_notifications = [] issue_subscribers = ( - IssueSubscriber.objects.filter(project=project) + IssueSubscriber.objects.filter(project=project, issue_id=issue_id) .exclude(subscriber_id=actor_id) - .values_list("subscriber") + .values_list("subscriber", flat=True) ) issue = Issue.objects.get(project=project, pk=issue_id) @@ -1026,7 +1026,7 @@ def issue_activity( Notification( workspace=project.workspace, sender="in_app:issue_activities", - triggered_by=actor, + triggered_by_id=actor_id, receiver_id=subscriber, entity_identifier=issue_id, entity_name="issue", @@ -1045,6 +1045,9 @@ def issue_activity( ) ) + # Bulk create notifications + Notification.objects.bulk_create(bulk_notifications, batch_size=100) + return except Exception as e: # Print logs if in DEBUG mode diff --git a/apiserver/plane/db/models/notification.py b/apiserver/plane/db/models/notification.py index 10ee1e709..6c4777bcd 100644 --- a/apiserver/plane/db/models/notification.py +++ b/apiserver/plane/db/models/notification.py @@ -33,4 +33,4 @@ class Notification(BaseModel): def __str__(self): """Return name of the notifications""" - return f"{self.receiver.name} <{self.workspace.name}>" + return f"{self.receiver.email} <{self.workspace.name}>"