Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: notifications #1515

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apiserver/plane/api/serializers/notification.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Module imports
from .base import BaseSerializer
from .user import UserLiteSerializer
from plane.db.models import Notification

class NotificationSerializer(BaseSerializer):

triggered_by_details = UserLiteSerializer(read_only=True, source="triggered_by")

class Meta:
model = Notification
fields = "__all__"
Expand Down
6 changes: 6 additions & 0 deletions apiserver/plane/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
## End Analytics
# Notification
NotificationViewSet,
UnreadNotificationEndpoint,
## End Notification
)

Expand Down Expand Up @@ -1383,5 +1384,10 @@
),
name="notifications",
),
path(
"workspaces/<str:slug>/users/notifications/unread/",
UnreadNotificationEndpoint.as_view(),
name="unread-notifications",
),
## End Notification
]
2 changes: 1 addition & 1 deletion apiserver/plane/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,4 @@
DefaultAnalyticsEndpoint,
)

from .notification import NotificationViewSet
from .notification import NotificationViewSet, UnreadNotificationEndpoint
52 changes: 48 additions & 4 deletions apiserver/plane/api/views/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sentry_sdk import capture_exception

# Module imports
from .base import BaseViewSet
from .base import BaseViewSet, BaseAPIView
from plane.db.models import Notification, IssueAssignee, IssueSubscriber, Issue
from plane.api.serializers import NotificationSerializer

Expand All @@ -25,7 +25,7 @@ def get_queryset(self):
workspace__slug=self.kwargs.get("slug"),
receiver_id=self.request.user.id,
)
.select_related("workspace")
.select_related("workspace", "project," "triggered_by", "receiver")
)

def list(self, request, slug):
Expand Down Expand Up @@ -123,7 +123,7 @@ def partial_update(self, request, slug, pk):
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)

def mark_read(self, request, slug, pk):
try:
notification = Notification.objects.get(
Expand Down Expand Up @@ -166,7 +166,6 @@ def mark_unread(self, request, slug, pk):
status=status.HTTP_400_BAD_REQUEST,
)


def archive(self, request, slug, pk):
try:
notification = Notification.objects.get(
Expand Down Expand Up @@ -209,3 +208,48 @@ def unarchive(self, request, slug, pk):
status=status.HTTP_400_BAD_REQUEST,
)


class UnreadNotificationEndpoint(BaseAPIView):
def get(self, request, slug):
try:
# Watching Issues Count
watching_notification_count = Notification.objects.filter(
workspace__slug=slug,
receiver_id=request.user.id,
entity_identifier__in=IssueSubscriber.objects.filter(
workspace__slug=slug, subscriber_id=request.user.id
).values_list("issue_id", flat=True),
).count()

# My Issues Count
my_issues_count = Notification.objects.filter(
workspace__slug=slug,
receiver_id=request.user.id,
entity_identifier__in=IssueAssignee.objects.filter(
workspace__slug=slug, assignee_id=request.user.id
).values_list("issue_id", flat=True),
).count()

# Created Issues Count
created_issues_count = Notification.objects.filter(
workspace__slug=slug,
receiver_id=request.user.id,
entity_identifier__in=Issue.objects.filter(
workspace__slug=slug, created_by=request.user
).values_list("pk", flat=True),
).count()

return Response(
{
"watching_notifications": watching_notification_count,
"my_issues": my_issues_count,
"created_issues": created_issues_count,
},
status=status.HTTP_200_OK,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
15 changes: 13 additions & 2 deletions apiserver/plane/bgtasks/issue_activites_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,8 @@ def issue_activity(

issue_subscribers = issue_subscribers + issue_assignees

if issue.created_by_id:
# Add bot filtering
if issue.created_by_id is not None and not issue.created_by.is_bot:
issue_subscribers = issue_subscribers + [issue.created_by_id]

issue = Issue.objects.get(project=project, pk=issue_id)
Expand All @@ -1134,7 +1135,17 @@ def issue_activity(
"state_name": issue.state.name,
"state_group": issue.state.group,
},
"issue_activity": str(issue_activity.id),
"issue_activity": {
"id": str(issue_activity.id),
"verb": str(issue_activity.verb),
"field": str(issue_activity.field),
"actor": str(issue_activity.actor_id),
"new_value": str(issue_activity.new_value),
"old_value": str(issue_activity.old_value),
"issue_comment": str(
issue_activity.issue_comment.comment_stripped if issue_activity.issue_comment is not None else ""
),
},
},
)
)
Expand Down