dev: notification endpoints and fix bg worker for saving notifications

This commit is contained in:
pablohashescobar 2023-06-22 21:31:30 +05:30
parent 8260e2f897
commit dcde6b2567
3 changed files with 36 additions and 4 deletions

View File

@ -1,6 +1,7 @@
# Third party imports # Third party imports
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from sentry_sdk import capture_exception
# Module imports # Module imports
from .base import BaseViewSet from .base import BaseViewSet
@ -18,7 +19,35 @@ class NotificationViewSet(BaseViewSet):
.get_queryset() .get_queryset()
.filter( .filter(
workspace__slug=self.kwargs.get("slug"), workspace__slug=self.kwargs.get("slug"),
receiver_id=self.request.user.id,
) )
.select_related("workspace") .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,
)

View File

@ -1014,9 +1014,9 @@ def issue_activity(
bulk_notifications = [] bulk_notifications = []
issue_subscribers = ( issue_subscribers = (
IssueSubscriber.objects.filter(project=project) IssueSubscriber.objects.filter(project=project, issue_id=issue_id)
.exclude(subscriber_id=actor_id) .exclude(subscriber_id=actor_id)
.values_list("subscriber") .values_list("subscriber", flat=True)
) )
issue = Issue.objects.get(project=project, pk=issue_id) issue = Issue.objects.get(project=project, pk=issue_id)
@ -1026,7 +1026,7 @@ def issue_activity(
Notification( Notification(
workspace=project.workspace, workspace=project.workspace,
sender="in_app:issue_activities", sender="in_app:issue_activities",
triggered_by=actor, triggered_by_id=actor_id,
receiver_id=subscriber, receiver_id=subscriber,
entity_identifier=issue_id, entity_identifier=issue_id,
entity_name="issue", entity_name="issue",
@ -1045,6 +1045,9 @@ def issue_activity(
) )
) )
# Bulk create notifications
Notification.objects.bulk_create(bulk_notifications, batch_size=100)
return return
except Exception as e: except Exception as e:
# Print logs if in DEBUG mode # Print logs if in DEBUG mode

View File

@ -33,4 +33,4 @@ class Notification(BaseModel):
def __str__(self): def __str__(self):
"""Return name of the notifications""" """Return name of the notifications"""
return f"{self.receiver.name} <{self.workspace.name}>" return f"{self.receiver.email} <{self.workspace.name}>"