From 0d513e5830f56d05198e4c63c83101a7af9105b5 Mon Sep 17 00:00:00 2001 From: Muhammad Abdurrehman Date: Wed, 4 Sep 2024 16:40:30 +0500 Subject: [PATCH] feat: post comment notification to owner --- thenewboston/social/views/comment.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/thenewboston/social/views/comment.py b/thenewboston/social/views/comment.py index 98510f2..ac5a39d 100644 --- a/thenewboston/social/views/comment.py +++ b/thenewboston/social/views/comment.py @@ -2,7 +2,12 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response +from thenewboston.general.enums import MessageType from thenewboston.general.permissions import IsObjectOwnerOrReadOnly +from thenewboston.notifications.consumers.notification import NotificationConsumer +from thenewboston.notifications.models.notification import Notification +from thenewboston.notifications.serializers.notification import NotificationReadSerializer +from thenewboston.users.serializers.user import UserReadSerializer from ..models import Comment from ..serializers.comment import CommentReadSerializer, CommentUpdateSerializer, CommentWriteSerializer @@ -18,8 +23,29 @@ def create(self, request, *args, **kwargs): comment = serializer.save() read_serializer = CommentReadSerializer(comment, context={'request': request}) + if comment.post.owner != comment.owner: + self.notify_post_owner(comment=comment) + return Response(read_serializer.data, status=status.HTTP_201_CREATED) + def notify_post_owner(self, comment): + post = comment.post + notification = Notification.objects.create( + owner=post.owner, + payload={ + 'post_id': post.id, + 'commenter': UserReadSerializer(comment.owner).data, + 'comment': comment.content, + 'notification_type': 'POST_COMMENT', + } + ) + + notification_data = NotificationReadSerializer(notification).data + + NotificationConsumer.stream_notification( + message_type=MessageType.CREATE_NOTIFICATION, notification_data=notification_data + ) + def get_serializer_class(self): if self.action == 'create': return CommentWriteSerializer