From be96ec47ba1160c618d4a10b15555ab30040371b Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Fri, 23 Jun 2023 01:15:35 +0530 Subject: [PATCH] feat: added list and unsubscribe function in issue subscriber --- apiserver/plane/api/serializers/project.py | 1 + apiserver/plane/api/urls.py | 1 + apiserver/plane/api/views/issue.py | 51 ++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/apiserver/plane/api/serializers/project.py b/apiserver/plane/api/serializers/project.py index 18ee19e7b..cbea7171a 100644 --- a/apiserver/plane/api/serializers/project.py +++ b/apiserver/plane/api/serializers/project.py @@ -96,6 +96,7 @@ class ProjectMemberSerializer(BaseSerializer): workspace = WorkSpaceSerializer(read_only=True) project = ProjectSerializer(read_only=True) member = UserLiteSerializer(read_only=True) + is_subscribed = serializers.BooleanField(read_only=True) class Meta: model = ProjectMember diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index 0e084faea..a75a878de 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -824,6 +824,7 @@ urlpatterns = [ { "get": "subscription_status", "post": "subscribe", + "delete": "unsubscribe", } ), name="project-issue-subscribers", diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index 81f7889b5..935377d46 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -15,6 +15,7 @@ from django.db.models import ( Value, CharField, When, + Exists, ) from django.core.serializers.json import DjangoJSONEncoder from django.utils.decorators import method_decorator @@ -43,6 +44,7 @@ from plane.api.serializers import ( IssueLiteSerializer, IssueAttachmentSerializer, IssueSubscriberSerializer, + ProjectMemberSerializer, ) from plane.api.permissions import ( ProjectEntityPermission, @@ -60,6 +62,7 @@ from plane.db.models import ( IssueAttachment, State, IssueSubscriber, + ProjectMember, ) from plane.bgtasks.issue_activites_task import issue_activity from plane.utils.grouper import group_results @@ -868,6 +871,30 @@ class IssueSubscriberViewSet(BaseViewSet): ProjectEntityPermission, ] + def list(self, request, slug, project_id, issue_id): + try: + members = ProjectMember.objects.filter( + workspace__slug=slug, project_id=project_id + ) + members = members.annotate( + is_subscribed=Exists( + IssueSubscriber.objects.filter( + workspace__slug=slug, + project_id=project_id, + issue_id=issue_id, + subscriber=OuterRef("member"), + ) + ) + ) + serializer = ProjectMemberSerializer(members, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + except Exception as e: + capture_exception(e) + return Response( + {"error": e}, + status=status.HTTP_400_BAD_REQUEST, + ) + def perform_create(self, serializer): serializer.save( project_id=self.kwargs.get("project_id"), @@ -913,6 +940,30 @@ class IssueSubscriberViewSet(BaseViewSet): status=status.HTTP_400_BAD_REQUEST, ) + def unsubscribe(self, request, slug, project_id, issue_id): + try: + issue_subscriber = IssueSubscriber.objects.get( + project=project_id, + subscriber=request.user, + workspace__slug=slug, + issue=issue_id, + ) + issue_subscriber.delete() + return Response( + status=status.HTTP_204_NO_CONTENT, + ) + except IssueSubscriber.DoesNotExist: + return Response( + {"error": "User subscribed to this issue"}, + 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, + ) + def subscription_status(self, request, slug, project_id, issue_id): try: issue_subscriber = IssueSubscriber.objects.filter(