feat: added list and unsubscribe function in issue subscriber

This commit is contained in:
NarayanBavisetti 2023-06-23 01:15:35 +05:30
parent dcde6b2567
commit be96ec47ba
3 changed files with 53 additions and 0 deletions

View File

@ -96,6 +96,7 @@ class ProjectMemberSerializer(BaseSerializer):
workspace = WorkSpaceSerializer(read_only=True) workspace = WorkSpaceSerializer(read_only=True)
project = ProjectSerializer(read_only=True) project = ProjectSerializer(read_only=True)
member = UserLiteSerializer(read_only=True) member = UserLiteSerializer(read_only=True)
is_subscribed = serializers.BooleanField(read_only=True)
class Meta: class Meta:
model = ProjectMember model = ProjectMember

View File

@ -824,6 +824,7 @@ urlpatterns = [
{ {
"get": "subscription_status", "get": "subscription_status",
"post": "subscribe", "post": "subscribe",
"delete": "unsubscribe",
} }
), ),
name="project-issue-subscribers", name="project-issue-subscribers",

View File

@ -15,6 +15,7 @@ from django.db.models import (
Value, Value,
CharField, CharField,
When, When,
Exists,
) )
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -43,6 +44,7 @@ from plane.api.serializers import (
IssueLiteSerializer, IssueLiteSerializer,
IssueAttachmentSerializer, IssueAttachmentSerializer,
IssueSubscriberSerializer, IssueSubscriberSerializer,
ProjectMemberSerializer,
) )
from plane.api.permissions import ( from plane.api.permissions import (
ProjectEntityPermission, ProjectEntityPermission,
@ -60,6 +62,7 @@ from plane.db.models import (
IssueAttachment, IssueAttachment,
State, State,
IssueSubscriber, IssueSubscriber,
ProjectMember,
) )
from plane.bgtasks.issue_activites_task import issue_activity from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results from plane.utils.grouper import group_results
@ -868,6 +871,30 @@ class IssueSubscriberViewSet(BaseViewSet):
ProjectEntityPermission, 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): def perform_create(self, serializer):
serializer.save( serializer.save(
project_id=self.kwargs.get("project_id"), project_id=self.kwargs.get("project_id"),
@ -913,6 +940,30 @@ class IssueSubscriberViewSet(BaseViewSet):
status=status.HTTP_400_BAD_REQUEST, 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): def subscription_status(self, request, slug, project_id, issue_id):
try: try:
issue_subscriber = IssueSubscriber.objects.filter( issue_subscriber = IssueSubscriber.objects.filter(