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

142 - Post Comments Notifications #233

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
26 changes: 26 additions & 0 deletions thenewboston/social/views/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading